| 1 | import { Test, TestingModule } from '@nestjs/testing'; |
| 2 | import { execFileSync } |
| 3 | import { mkdtempSync, rmSync |
| 4 | import { tmpdir } |
| 5 | import path from 'node:path |
| 6 | import { afterEach, beforeEach |
| 7 | |
| 8 | import { PushTransactionService } |
| 9 | import { WalStoreService } |
| 10 | import { |
| 11 | ZERO_OID, |
| 12 | type RefTransition, |
| 13 | } from '../../../lib/git/wal/wal.types.js'; |
| 14 | import { bufferBody } |
| 15 | import { RepositoryMaterializerService } |
| 16 | import { InMemoryWalStore } |
| 17 | |
| 18 | const REPO_ID = ' |
| 19 | |
| 20 | function git(cwd: |
| 21 | const env = |
| 22 | delete env.GIT_DIR; |
| 23 | return execFileSync('git |
| 24 | } |
| 25 | |
| 26 | describe('RepositoryMaterializerService', () |
| 27 | let root: |
| 28 | let source: |
| 29 | let cache: |
| 30 | let store: |
| 31 | let materializer: |
| 32 | let pushes: |
| 33 | |
| 34 | beforeEach(async () |
| 35 | root = mkdtempSync( |
| 36 | source = path. |
| 37 | cache = path. |
| 38 | |
| 39 | execFileSync('git', |
| 40 | git(source, ' |
| 41 | git(source, ' |
| 42 | execFileSync('git', |
| 43 | |
| 44 | store = new |
| 45 | const module: |
| 46 | providers: [ |
| 47 | RepositoryMaterializerService, |
| 48 | PushTransactionService, |
| 49 | { provide: WalStoreService, |
| 50 | ], |
| 51 | }).compile(); |
| 52 | |
| 53 | materializer = module. |
| 54 | pushes = module. |
| 55 | }); |
| 56 | |
| 57 | afterEach(() => |
| 58 | |
| 59 | /** Commits a file in the source repo and pushes it through the log. */ |
| 60 | async function commitAndLog |
| 61 | const before = |
| 62 | writeFileSync(path.join |
| 63 | git(source, ' |
| 64 | git(source, ' |
| 65 | const after = |
| 66 | |
| 67 | const transition: |
| 68 | ref: 'refs/heads/main', |
| 69 | oldOid: before ? |
| 70 | newOid: Buffer.from |
| 71 | }; |
| 72 | |
| 73 | await pushes.commitPush |
| 74 | repoId: REPO_ID, |
| 75 | transitions: [transition], |
| 76 | body: bufferBody(packSince |
| 77 | packOffset: 0, |
| 78 | }); |
| 79 | |
| 80 | return after; |
| 81 | } |
| 82 | |
| 83 | function headOid() { |
| 84 | try { |
| 85 | return git(source |
| 86 | } catch { |
| 87 | return ''; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | /** The objects reachable from main but not from `exclude` - a thin pack. */ |
| 92 | function packSince(exclude |
| 93 | const revs = |
| 94 | ? `refs/heads/main\n^${exclude |
| 95 | : 'refs/heads/main\n'; |
| 96 | return execFileSync( |
| 97 | 'git', |
| 98 | ['pack-objects', '--stdout' |
| 99 | { |
| 100 | cwd: source, |
| 101 | input: revs, |
| 102 | maxBuffer: 1 << 28, |
| 103 | }, |
| 104 | ); |
| 105 | } |
| 106 | |
| 107 | it('leaves a cache untouched when the log is empty', |
| 108 | const index = |
| 109 | |
| 110 | expect(index.seq |
| 111 | expect(git(cache |
| 112 | }); |
| 113 | |
| 114 | it('reconstructs a repository from the log alone', |
| 115 | const oid = |
| 116 | |
| 117 | await materializer.materialize |
| 118 | |
| 119 | expect(git(cache |
| 120 | expect(git(cache |
| 121 | }); |
| 122 | |
| 123 | it('points HEAD at a branch that exists so clones are not empty', |
| 124 | await commitAndLog('README.md |
| 125 | await materializer.materialize |
| 126 | |
| 127 | expect(git(cache |
| 128 | |
| 129 | const clone = |
| 130 | execFileSync('git', |
| 131 | expect(git(clone |
| 132 | }); |
| 133 | |
| 134 | it('points HEAD at the chosen default branch, even on a current cache', |
| 135 | const oid = |
| 136 | await pushes.commitPush |
| 137 | repoId: REPO_ID, |
| 138 | transitions: [ |
| 139 | { |
| 140 | ref: 'refs/heads/dev', |
| 141 | oldOid: ZERO_OID, |
| 142 | newOid: Buffer.from |
| 143 | }, |
| 144 | ], |
| 145 | body: bufferBody(packSince |
| 146 | packOffset: 0, |
| 147 | }); |
| 148 | |
| 149 | await materializer.materialize |
| 150 | expect(git(cache |
| 151 | |
| 152 | await materializer.materialize |
| 153 | expect(git(cache |
| 154 | }); |
| 155 | |
| 156 | it('ignores a chosen default branch the log does not carry', |
| 157 | await commitAndLog('README.md |
| 158 | await materializer.materialize |
| 159 | |
| 160 | expect(git(cache |
| 161 | }); |
| 162 | |
| 163 | it('replays thin packs in sequence order across several pushes', |
| 164 | await commitAndLog('a.txt |
| 165 | await commitAndLog('b.txt |
| 166 | const third = |
| 167 | |
| 168 | await materializer.materialize |
| 169 | |
| 170 | expect(git(cache |
| 171 | expect(git(cache |
| 172 | }); |
| 173 | |
| 174 | it('replays only what the cache is missing', |
| 175 | await commitAndLog('a.txt |
| 176 | await materializer.materialize |
| 177 | |
| 178 | const second = |
| 179 | const index = |
| 180 | |
| 181 | expect(index.seq |
| 182 | expect(git(cache |
| 183 | }); |
| 184 | |
| 185 | it('is a no-op once the cache has caught up', |
| 186 | await commitAndLog('a.txt |
| 187 | await materializer.materialize |
| 188 | const before = |
| 189 | |
| 190 | const index = |
| 191 | |
| 192 | expect(index.seq |
| 193 | expect(git(cache |
| 194 | }); |
| 195 | |
| 196 | it('deletes a cached ref the log no longer carries', |
| 197 | const oid = |
| 198 | await materializer.materialize |
| 199 | |
| 200 | git(cache, ' |
| 201 | await pushes.commitPush |
| 202 | repoId: REPO_ID, |
| 203 | transitions: [ |
| 204 | { |
| 205 | ref: 'refs/heads/main', |
| 206 | oldOid: Buffer.from |
| 207 | newOid: Buffer.from |
| 208 | }, |
| 209 | ], |
| 210 | body: bufferBody(Buffer |
| 211 | packOffset: 0, |
| 212 | }); |
| 213 | |
| 214 | await materializer.materialize |
| 215 | |
| 216 | expect(git(cache |
| 217 | 'refs/heads/main', |
| 218 | ); |
| 219 | }); |
| 220 | |
| 221 | it('collapses concurrent materializations of the same repository', |
| 222 | await commitAndLog('a.txt |
| 223 | |
| 224 | const [first, |
| 225 | materializer.materialize(REPO_ID |
| 226 | materializer.materialize(REPO_ID |
| 227 | ]); |
| 228 | |
| 229 | expect(first).toBe |
| 230 | }); |
| 231 | }); |