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