| 1 | import { describe, it, expect, beforeEach, vi } from 'vitest'; |
| 2 | import { Test, TestingModule |
| 3 | import { bufferBody } |
| 4 | import { PassThrough } |
| 5 | |
| 6 | import { PackProcessService } |
| 7 | import { RefAdvertisementService } |
| 8 | import { RepositoryMaterializerService } |
| 9 | import { PushTransactionService } |
| 10 | import { RepositoryStorageService } |
| 11 | import { RepositoryContributionService } |
| 12 | import { UnsupportedGitServiceError } |
| 13 | import { GitService } |
| 14 | |
| 15 | describe('GitService', () |
| 16 | let service: |
| 17 | const storage = |
| 18 | getRepoPath: vi.fn |
| 19 | }; |
| 20 | const refAdvertisement = |
| 21 | advertise: vi.fn |
| 22 | }; |
| 23 | const packProcess = |
| 24 | streamUploadPack: vi.fn |
| 25 | streamReceivePack: vi.fn |
| 26 | }; |
| 27 | const materializer = |
| 28 | const pushTransaction = |
| 29 | commitPush: vi |
| 30 | .fn() |
| 31 | .mockResolvedValue({ seq: |
| 32 | }; |
| 33 | const contributions = |
| 34 | sync: vi.fn |
| 35 | }; |
| 36 | |
| 37 | beforeEach(async () |
| 38 | vi.clearAllMocks(); |
| 39 | const module: |
| 40 | providers: [ |
| 41 | GitService, |
| 42 | { provide: RepositoryStorageService, |
| 43 | { provide: RefAdvertisementService, |
| 44 | { provide: PackProcessService, |
| 45 | { provide: PushTransactionService, |
| 46 | { provide: RepositoryMaterializerService, |
| 47 | { provide: RepositoryContributionService, |
| 48 | ], |
| 49 | }).compile(); |
| 50 | |
| 51 | service = module. |
| 52 | }); |
| 53 | |
| 54 | it('should be defined', |
| 55 | expect(service).toBeDefined |
| 56 | }); |
| 57 | |
| 58 | it('materializes the cache before advertising refs', |
| 59 | const { headers |
| 60 | repositoryId: 'repo_ghost', |
| 61 | service: 'git-upload-pack', |
| 62 | }); |
| 63 | |
| 64 | expect(headers['Content-Type |
| 65 | 'application/x-git-upload-pack-advertisement', |
| 66 | ); |
| 67 | expect(materializer.materialize |
| 68 | 'repo_ghost', |
| 69 | '/repos/ghost.git', |
| 70 | ); |
| 71 | expect(refAdvertisement.advertise |
| 72 | repoDirectory: '/repos/ghost.git', |
| 73 | service: 'git-upload-pack', |
| 74 | }); |
| 75 | }); |
| 76 | |
| 77 | it('rejects the dumb protocol', |
| 78 | await expect( |
| 79 | service.advertiseRefs({ |
| 80 | repositoryId: 'repo_ghost', |
| 81 | service: '', |
| 82 | }), |
| 83 | ).rejects.toBeInstanceOf( |
| 84 | }); |
| 85 | |
| 86 | it('reads the command section before doing any slower work', |
| 87 | materializer.materialize.mockImplementationOnce |
| 88 | expect(pushTransaction.commitPush |
| 89 | }); |
| 90 | |
| 91 | await service.receivePack |
| 92 | repositoryId: 'repo_ghost', |
| 93 | body: bufferBody(receivePackBody |
| 94 | }); |
| 95 | |
| 96 | expect(materializer.materialize |
| 97 | }); |
| 98 | |
| 99 | it('answers the pre-push probe without touching the log', |
| 100 | const { headers |
| 101 | repositoryId: 'repo_ghost', |
| 102 | body: bufferBody(Buffer |
| 103 | }); |
| 104 | |
| 105 | expect(headers['Content-Type |
| 106 | 'application/x-git-receive-pack-result', |
| 107 | ); |
| 108 | expect(pushTransaction.commitPush |
| 109 | expect(packProcess.streamReceivePack |
| 110 | }); |
| 111 | |
| 112 | it('commits to the log before touching the local repository', |
| 113 | const { headers |
| 114 | repositoryId: 'repo_ghost', |
| 115 | body: bufferBody(receivePackBody |
| 116 | }); |
| 117 | |
| 118 | expect(headers['Content-Type |
| 119 | 'application/x-git-receive-pack-result', |
| 120 | ); |
| 121 | expect(pushTransaction.commitPush |
| 122 | expect.objectContaining({ |
| 123 | ); |
| 124 | |
| 125 | const [{ transitions |
| 126 | expect(transitions[0] |
| 127 | |
| 128 | const materializeOrder = |
| 129 | materializer.materialize.mock |
| 130 | const commitOrder = |
| 131 | const spawnOrder = |
| 132 | packProcess.streamReceivePack.mock |
| 133 | expect(materializeOrder).toBeLessThan |
| 134 | expect(commitOrder).toBeLessThan |
| 135 | }); |
| 136 | |
| 137 | it('indexes contributions once the pushed pack finishes streaming', |
| 138 | const { body |
| 139 | repositoryId: 'repo_ghost', |
| 140 | body: bufferBody(receivePackBody |
| 141 | }); |
| 142 | |
| 143 | expect(contributions.sync |
| 144 | |
| 145 | body.emit('close |
| 146 | await new Promise |
| 147 | |
| 148 | expect(contributions.sync |
| 149 | repositoryId: 'repo_ghost', |
| 150 | repoDirectory: '/repos/ghost.git', |
| 151 | }); |
| 152 | }); |
| 153 | }); |
| 154 | |
| 155 | function receivePackBody() { |
| 156 | const zero = |
| 157 | const next = |
| 158 | const command = |
| 159 | const length = |
| 160 | return Buffer.concat |
| 161 | Buffer.from(length |
| 162 | Buffer.from('PACKDATA |
| 163 | ]); |
| 164 | } |