| 1 | import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | import { Test, TestingModule } from '@nestjs/testing'; |
| 3 | import { bufferBody } from '../lib/git/protocol/git-request-body.js'; |
| 4 | import { execFileSync } from 'node:child_process'; |
| 5 | import { mkdtempSync, rmSync } from 'node:fs'; |
| 6 | import { tmpdir } from 'node:os'; |
| 7 | import path from 'node:path'; |
| 8 | import { PassThrough } from 'node:stream'; |
| 9 | |
| 10 | import { PackProcessService } from '../services/git/pack-process/pack-process.service.js'; |
| 11 | import { RefAdvertisementService } from '../services/git/ref-advertisement/ref-advertisement.service.js'; |
| 12 | import { RepositoryMaterializerService } from '../services/git/materializer/repository-materializer.service.js'; |
| 13 | import { PushTransactionService } from '../services/git/wal/push-transaction.service.js'; |
| 14 | import { RepositoryStorageService } from '../services/git/repository-storage/repository-storage.service.js'; |
| 15 | import { RepositoryContributionService } from '../services/git/contributions/repository-contribution.service.js'; |
| 16 | import { CodeSearchService } from '../services/git/code-search/code-search.service.js'; |
| 17 | import { IssueReferencesService } from '../services/issues/issue-references.service.js'; |
| 18 | import { UnsupportedGitServiceError } from './git.errors.js'; |
| 19 | import { DATABASE } from '../database/database.module.js'; |
| 20 | import { GitService } from './git.service.js'; |
| 21 | |
| 22 | describe('GitService', () => { |
| 23 | let service: GitService; |
| 24 | const storage = { |
| 25 | getRepoPath: vi.fn().mockResolvedValue('/repos/ghost.git'), |
| 26 | }; |
| 27 | const refAdvertisement = { |
| 28 | advertise: vi.fn().mockReturnValue(new PassThrough()), |
| 29 | }; |
| 30 | const packProcess = { |
| 31 | streamUploadPack: vi.fn().mockReturnValue(new PassThrough()), |
| 32 | streamReceivePack: vi.fn().mockReturnValue(new PassThrough()), |
| 33 | }; |
| 34 | const materializer = { materialize: vi.fn().mockResolvedValue(undefined) }; |
| 35 | const pushTransaction = { |
| 36 | commitPush: vi |
| 37 | .fn() |
| 38 | .mockResolvedValue({ seq: 1, ulid: '01ARZ3NDEKTSV4RRFFQ69G5FAV' }), |
| 39 | }; |
| 40 | const contributions = { |
| 41 | sync: vi.fn().mockResolvedValue('55ff3318cbb1ad74a1e1a1e6f4bd91f4b5a9c0d2'), |
| 42 | }; |
| 43 | |
| 44 | const codeSearch = { indexInBackground: vi.fn() }; |
| 45 | const references = { closeFromCommits: vi.fn().mockResolvedValue(undefined) }; |
| 46 | const published = vi.fn().mockResolvedValue(undefined); |
| 47 | const db = { insert: () => ({ values: published }) }; |
| 48 | |
| 49 | beforeEach(async () => { |
| 50 | vi.clearAllMocks(); |
| 51 | const module: TestingModule = await Test.createTestingModule({ |
| 52 | providers: [ |
| 53 | GitService, |
| 54 | { provide: RepositoryStorageService, useValue: storage }, |
| 55 | { provide: RefAdvertisementService, useValue: refAdvertisement }, |
| 56 | { provide: PackProcessService, useValue: packProcess }, |
| 57 | { provide: PushTransactionService, useValue: pushTransaction }, |
| 58 | { provide: RepositoryMaterializerService, useValue: materializer }, |
| 59 | { provide: RepositoryContributionService, useValue: contributions }, |
| 60 | { provide: CodeSearchService, useValue: codeSearch }, |
| 61 | { provide: IssueReferencesService, useValue: references }, |
| 62 | { provide: DATABASE, useValue: db }, |
| 63 | ], |
| 64 | }).compile(); |
| 65 | |
| 66 | service = module.get<GitService>(GitService); |
| 67 | }); |
| 68 | |
| 69 | it('should be defined', () => { |
| 70 | expect(service).toBeDefined(); |
| 71 | }); |
| 72 | |
| 73 | it('materializes the cache before advertising refs', async () => { |
| 74 | const { headers } = await service.advertiseRefs({ |
| 75 | repositoryId: 'repo_ghost', |
| 76 | defaultBranch: null, |
| 77 | service: 'git-upload-pack', |
| 78 | }); |
| 79 | |
| 80 | expect(headers['Content-Type']).toBe( |
| 81 | 'application/x-git-upload-pack-advertisement', |
| 82 | ); |
| 83 | expect(materializer.materialize).toHaveBeenCalledWith( |
| 84 | 'repo_ghost', |
| 85 | '/repos/ghost.git', |
| 86 | null, |
| 87 | ); |
| 88 | expect(refAdvertisement.advertise).toHaveBeenCalledWith({ |
| 89 | repoDirectory: '/repos/ghost.git', |
| 90 | service: 'git-upload-pack', |
| 91 | }); |
| 92 | }); |
| 93 | |
| 94 | it('rejects the dumb protocol', async () => { |
| 95 | await expect( |
| 96 | service.advertiseRefs({ |
| 97 | repositoryId: 'repo_ghost', |
| 98 | defaultBranch: null, |
| 99 | service: '', |
| 100 | }), |
| 101 | ).rejects.toBeInstanceOf(UnsupportedGitServiceError); |
| 102 | }); |
| 103 | |
| 104 | it('reads the command section before doing any slower work', async () => { |
| 105 | materializer.materialize.mockImplementationOnce(async () => { |
| 106 | expect(pushTransaction.commitPush).not.toHaveBeenCalled(); |
| 107 | }); |
| 108 | |
| 109 | await service.receivePack({ |
| 110 | repositoryId: 'repo_ghost', |
| 111 | defaultBranch: null, |
| 112 | isPublic: true, |
| 113 | body: bufferBody(receivePackBody()), |
| 114 | }); |
| 115 | |
| 116 | expect(materializer.materialize).toHaveBeenCalled(); |
| 117 | }); |
| 118 | |
| 119 | it('answers the pre-push probe without touching the log', async () => { |
| 120 | const { headers } = await service.receivePack({ |
| 121 | repositoryId: 'repo_ghost', |
| 122 | defaultBranch: null, |
| 123 | isPublic: true, |
| 124 | body: bufferBody(Buffer.from('0000')), |
| 125 | }); |
| 126 | |
| 127 | expect(headers['Content-Type']).toBe( |
| 128 | 'application/x-git-receive-pack-result', |
| 129 | ); |
| 130 | expect(pushTransaction.commitPush).not.toHaveBeenCalled(); |
| 131 | expect(packProcess.streamReceivePack).not.toHaveBeenCalled(); |
| 132 | }); |
| 133 | |
| 134 | it('commits to the log before touching the local repository', async () => { |
| 135 | const { headers } = await service.receivePack({ |
| 136 | repositoryId: 'repo_ghost', |
| 137 | defaultBranch: null, |
| 138 | isPublic: true, |
| 139 | body: bufferBody(receivePackBody()), |
| 140 | }); |
| 141 | |
| 142 | expect(headers['Content-Type']).toBe( |
| 143 | 'application/x-git-receive-pack-result', |
| 144 | ); |
| 145 | expect(pushTransaction.commitPush).toHaveBeenCalledWith( |
| 146 | expect.objectContaining({ repoId: 'repo_ghost' }), |
| 147 | ); |
| 148 | |
| 149 | const [{ transitions }] = pushTransaction.commitPush.mock.calls[0]; |
| 150 | expect(transitions[0].ref).toBe('refs/heads/main'); |
| 151 | |
| 152 | const materializeOrder = |
| 153 | materializer.materialize.mock.invocationCallOrder[0]; |
| 154 | const commitOrder = pushTransaction.commitPush.mock.invocationCallOrder[0]; |
| 155 | const spawnOrder = |
| 156 | packProcess.streamReceivePack.mock.invocationCallOrder[0]; |
| 157 | expect(materializeOrder).toBeLessThan(commitOrder); |
| 158 | expect(commitOrder).toBeLessThan(spawnOrder); |
| 159 | }); |
| 160 | |
| 161 | it('indexes contributions once the pushed pack finishes streaming', async () => { |
| 162 | const { body } = await service.receivePack({ |
| 163 | repositoryId: 'repo_ghost', |
| 164 | defaultBranch: null, |
| 165 | isPublic: true, |
| 166 | body: bufferBody(receivePackBody()), |
| 167 | }); |
| 168 | |
| 169 | expect(contributions.sync).not.toHaveBeenCalled(); |
| 170 | |
| 171 | body.emit('close'); |
| 172 | await new Promise((resolve) => setImmediate(resolve)); |
| 173 | |
| 174 | expect(contributions.sync).toHaveBeenCalledWith({ |
| 175 | repositoryId: 'repo_ghost', |
| 176 | repoDirectory: '/repos/ghost.git', |
| 177 | }); |
| 178 | expect(codeSearch.indexInBackground).toHaveBeenCalledWith({ |
| 179 | repositoryId: 'repo_ghost', |
| 180 | isPublic: true, |
| 181 | repoDirectory: '/repos/ghost.git', |
| 182 | }); |
| 183 | // creating a branch closes nothing |
| 184 | expect(references.closeFromCommits).not.toHaveBeenCalled(); |
| 185 | }); |
| 186 | |
| 187 | describe('closing referenced issues', () => { |
| 188 | let directory: string; |
| 189 | const git = (...args: string[]) => |
| 190 | execFileSync('git', ['-C', directory, ...args], { |
| 191 | encoding: 'utf8', |
| 192 | env: { |
| 193 | ...process.env, |
| 194 | GIT_AUTHOR_NAME: 'a', |
| 195 | GIT_AUTHOR_EMAIL: 'a@example.com', |
| 196 | GIT_COMMITTER_NAME: 'a', |
| 197 | GIT_COMMITTER_EMAIL: 'a@example.com', |
| 198 | }, |
| 199 | }).trim(); |
| 200 | |
| 201 | beforeEach(() => { |
| 202 | directory = mkdtempSync(path.join(tmpdir(), 'ghost-close-')); |
| 203 | git('init', '-q', '-b', 'main'); |
| 204 | git('commit', '-q', '--allow-empty', '-m', 'first'); |
| 205 | git('commit', '-q', '--allow-empty', '-m', 'second', '-m', 'Fixes #1'); |
| 206 | storage.getRepoPath.mockResolvedValueOnce(path.join(directory, '.git')); |
| 207 | }); |
| 208 | |
| 209 | afterEach(() => rmSync(directory, { recursive: true, force: true })); |
| 210 | |
| 211 | async function push(ref: string) { |
| 212 | const { body } = await service.receivePack({ |
| 213 | repositoryId: 'repo_ghost', |
| 214 | defaultBranch: null, |
| 215 | isPublic: true, |
| 216 | body: bufferBody( |
| 217 | receivePackBody( |
| 218 | git('rev-parse', 'HEAD~1'), |
| 219 | git('rev-parse', 'HEAD'), |
| 220 | ref, |
| 221 | ), |
| 222 | ), |
| 223 | pushedBy: 'user_pusher', |
| 224 | }); |
| 225 | body.emit('close'); |
| 226 | await vi.waitFor(() => expect(contributions.sync).toHaveBeenCalled()); |
| 227 | } |
| 228 | |
| 229 | it('hands the commits a default-branch push added to the reference index', async () => { |
| 230 | await push('refs/heads/main'); |
| 231 | |
| 232 | const sha = git('rev-parse', 'HEAD'); |
| 233 | await vi.waitFor(() => |
| 234 | expect(references.closeFromCommits).toHaveBeenCalledWith({ |
| 235 | repository: { id: 'repo_ghost' }, |
| 236 | actorId: 'user_pusher', |
| 237 | commits: [expect.objectContaining({ sha, body: 'Fixes #1' })], |
| 238 | }), |
| 239 | ); |
| 240 | }); |
| 241 | |
| 242 | it('publishes a push event with the commits the push added', async () => { |
| 243 | await push('refs/heads/feature'); |
| 244 | |
| 245 | const [before, after] = [ |
| 246 | git('rev-parse', 'HEAD~1'), |
| 247 | git('rev-parse', 'HEAD'), |
| 248 | ]; |
| 249 | await vi.waitFor(() => |
| 250 | expect(published).toHaveBeenCalledWith({ |
| 251 | type: 'push', |
| 252 | repositoryId: 'repo_ghost', |
| 253 | actorId: 'user_pusher', |
| 254 | payload: { |
| 255 | ref: 'refs/heads/feature', |
| 256 | before, |
| 257 | after, |
| 258 | commits: [ |
| 259 | expect.objectContaining({ |
| 260 | sha: after, |
| 261 | subject: 'second', |
| 262 | body: 'Fixes #1', |
| 263 | authorName: 'a', |
| 264 | }), |
| 265 | ], |
| 266 | }, |
| 267 | }), |
| 268 | ); |
| 269 | }); |
| 270 | |
| 271 | it('ignores pushes to other branches', async () => { |
| 272 | await push('refs/heads/feature'); |
| 273 | // nothing to wait for when nothing happens, so give the hook time to have run |
| 274 | await new Promise((resolve) => setTimeout(resolve, 50)); |
| 275 | |
| 276 | expect(references.closeFromCommits).not.toHaveBeenCalled(); |
| 277 | }); |
| 278 | }); |
| 279 | }); |
| 280 | |
| 281 | function receivePackBody( |
| 282 | old = '0'.repeat(40), |
| 283 | next = '55ff3318cbb1ad74a1e1a1e6f4bd91f4b5a9c0d2', |
| 284 | ref = 'refs/heads/main', |
| 285 | ) { |
| 286 | const command = `${old} ${next} ${ref}\0report-status\n`; |
| 287 | const length = (Buffer.byteLength(command) + 4).toString(16).padStart(4, '0'); |
| 288 | return Buffer.concat([ |
| 289 | Buffer.from(length + command + '0000', 'utf8'), |
| 290 | Buffer.from('PACKDATA'), |
| 291 | ]); |
| 292 | } |