| 1 | import { Test, type TestingModule } from '@nestjs/testing'; |
| 2 | import { createDatabase, schema |
| 3 | import { and, eq |
| 4 | import { migrate } |
| 5 | import { execFileSync } |
| 6 | import { mkdirSync, mkdtempSync |
| 7 | import { tmpdir } |
| 8 | import path from 'node:path |
| 9 | import { |
| 10 | afterAll, |
| 11 | afterEach, |
| 12 | beforeAll, |
| 13 | beforeEach, |
| 14 | describe, |
| 15 | expect, |
| 16 | it, |
| 17 | vi, |
| 18 | } from 'vitest'; |
| 19 | |
| 20 | import { DATABASE } |
| 21 | import { RepositoryPathIndexService } |
| 22 | |
| 23 | const CONNECTION = process |
| 24 | const REF = ' |
| 25 | const MIGRATIONS = path |
| 26 | import.meta.dirname, |
| 27 | '../../../../../../packages/db/drizzle', |
| 28 | ); |
| 29 | |
| 30 | // Needs a throwaway Postgres; the suite is skipped without one. |
| 31 | describe.skipIf(!CONNECTION |
| 32 | let db: |
| 33 | let pool: |
| 34 | let service: |
| 35 | let root: |
| 36 | let gitDir: |
| 37 | let repositoryId: |
| 38 | |
| 39 | function git(... |
| 40 | const env = |
| 41 | delete env.GIT_DIR; |
| 42 | return execFileSync('git |
| 43 | cwd: root, |
| 44 | encoding: 'utf8', |
| 45 | env, |
| 46 | }).trim(); |
| 47 | } |
| 48 | |
| 49 | function commit(file |
| 50 | mkdirSync(path.dirname |
| 51 | writeFileSync(path.join |
| 52 | git('add', |
| 53 | git('commit', |
| 54 | return git('rev-parse |
| 55 | } |
| 56 | |
| 57 | const sync = |
| 58 | service.sync({ |
| 59 | |
| 60 | async function rowsByPath |
| 61 | const rows = |
| 62 | .select() |
| 63 | .from(schema.repositoryPathCommit |
| 64 | .where(eq(schema |
| 65 | return new Map |
| 66 | } |
| 67 | |
| 68 | beforeAll(async () |
| 69 | ({ db, pool |
| 70 | await migrate(db |
| 71 | |
| 72 | const module: |
| 73 | providers: [ |
| 74 | RepositoryPathIndexService, |
| 75 | { provide: DATABASE, |
| 76 | ], |
| 77 | }).compile(); |
| 78 | service = module. |
| 79 | |
| 80 | await db |
| 81 | .insert(schema.user |
| 82 | .values({ |
| 83 | id: 'user_path_index_spec', |
| 84 | name: 'Test', |
| 85 | email: 'path-index@example.com', |
| 86 | }) |
| 87 | .onConflictDoNothing(); |
| 88 | }); |
| 89 | |
| 90 | afterAll(async () |
| 91 | await db |
| 92 | .delete(schema.user |
| 93 | .where(eq(schema |
| 94 | await pool.end |
| 95 | }); |
| 96 | |
| 97 | beforeEach(async () |
| 98 | root = mkdtempSync( |
| 99 | gitDir = path. |
| 100 | execFileSync('git', |
| 101 | git('config', |
| 102 | git('config', |
| 103 | |
| 104 | const [repository] |
| 105 | .insert(schema.repository |
| 106 | .values({ |
| 107 | name: 'spec', |
| 108 | slug: `spec-${Date |
| 109 | ownerId: 'user_path_index_spec', |
| 110 | }) |
| 111 | .returning(); |
| 112 | repositoryId = repository. |
| 113 | }); |
| 114 | |
| 115 | afterEach(async () |
| 116 | rmSync(root, { |
| 117 | // cascades to both index tables |
| 118 | await db |
| 119 | .delete(schema.repository |
| 120 | .where(eq(schema |
| 121 | }); |
| 122 | |
| 123 | it('returns null and writes nothing when the ref does not exist', |
| 124 | expect(await sync |
| 125 | expect(await rowsByPath |
| 126 | }); |
| 127 | |
| 128 | it('records the newest commit for every path and its ancestors', |
| 129 | commit('README.md', |
| 130 | const addX = |
| 131 | |
| 132 | expect(await sync |
| 133 | |
| 134 | const rows = |
| 135 | expect(rows.get |
| 136 | commitSha: addX, |
| 137 | subject: 'add x', |
| 138 | }); |
| 139 | // ancestors inherit the newest commit beneath them |
| 140 | expect(rows.get |
| 141 | expect(rows.get |
| 142 | expect(rows.get |
| 143 | expect(rows.get |
| 144 | }); |
| 145 | |
| 146 | it('stores the committer timestamp', |
| 147 | commit('README.md', |
| 148 | await sync(); |
| 149 | |
| 150 | const seconds = |
| 151 | expect((await rowsByPath |
| 152 | new Date(seconds |
| 153 | ); |
| 154 | }); |
| 155 | |
| 156 | it('tops up incrementally and leaves untouched paths alone', |
| 157 | const first = |
| 158 | await sync(); |
| 159 | |
| 160 | const second = |
| 161 | expect(await sync |
| 162 | |
| 163 | const rows = |
| 164 | expect(rows.get |
| 165 | expect(rows.get |
| 166 | // the root row follows the tip |
| 167 | expect(rows.get |
| 168 | }); |
| 169 | |
| 170 | it('is a no-op when the ref has not moved', |
| 171 | commit('README.md', |
| 172 | await sync(); |
| 173 | const before = |
| 174 | .select() |
| 175 | .from(schema.repositoryRefIndex |
| 176 | .where(eq(schema |
| 177 | |
| 178 | await sync(); |
| 179 | const after = |
| 180 | .select() |
| 181 | .from(schema.repositoryRefIndex |
| 182 | .where(eq(schema |
| 183 | |
| 184 | expect(after).toEqual |
| 185 | }); |
| 186 | |
| 187 | it('rebuilds from scratch after a force push drops commits', |
| 188 | const first = |
| 189 | commit('gone.txt', |
| 190 | await sync(); |
| 191 | expect((await rowsByPath |
| 192 | |
| 193 | git('reset', |
| 194 | const replacement = |
| 195 | |
| 196 | expect(await sync |
| 197 | |
| 198 | const rows = |
| 199 | // a rebuild clears the ref's rows, so the discarded path is gone |
| 200 | expect(rows.has |
| 201 | expect(rows.get |
| 202 | }); |
| 203 | |
| 204 | it('attributes a merged file to the commit that wrote it', |
| 205 | commit('README.md', |
| 206 | git('checkout', |
| 207 | const onBranch = |
| 208 | git('checkout', |
| 209 | git('merge', |
| 210 | |
| 211 | await sync(); |
| 212 | |
| 213 | expect((await rowsByPath |
| 214 | commitSha: onBranch, |
| 215 | subject: 'add z on branch', |
| 216 | }); |
| 217 | }); |
| 218 | |
| 219 | it('looks up only the requested paths', |
| 220 | commit('README.md', |
| 221 | const addY = |
| 222 | await sync(); |
| 223 | |
| 224 | const found = |
| 225 | repositoryId, |
| 226 | ref: REF, |
| 227 | paths: ['src/y.ts', |
| 228 | }); |
| 229 | |
| 230 | expect([...found. |
| 231 | expect(found.get |
| 232 | }); |
| 233 | |
| 234 | it('keeps a second ref independent', |
| 235 | commit('README.md', |
| 236 | await sync(); |
| 237 | |
| 238 | const rows = |
| 239 | .select() |
| 240 | .from(schema.repositoryPathCommit |
| 241 | .where( |
| 242 | and( |
| 243 | eq(schema.repositoryPathCommit |
| 244 | eq(schema.repositoryPathCommit |
| 245 | ), |
| 246 | ); |
| 247 | expect(rows).toEqual |
| 248 | }); |
| 249 | |
| 250 | describe('ensureIndexed', |
| 251 | const ensureIndexed = |
| 252 | service.ensureIndexed({ |
| 253 | |
| 254 | it('is false and builds nothing when the ref does not exist', |
| 255 | expect(await ensureIndexed |
| 256 | expect(await rowsByPath |
| 257 | }); |
| 258 | |
| 259 | it('builds a first index in the background, then answers true', |
| 260 | const first = |
| 261 | |
| 262 | expect(await ensureIndexed |
| 263 | // joins the walk the call above left running |
| 264 | await sync(); |
| 265 | |
| 266 | expect(await ensureIndexed |
| 267 | expect((await rowsByPath |
| 268 | }); |
| 269 | |
| 270 | it('waits for a fast-forward top-up', |
| 271 | commit('README.md', |
| 272 | await sync(); |
| 273 | const next = |
| 274 | |
| 275 | expect(await ensureIndexed |
| 276 | expect((await rowsByPath |
| 277 | }); |
| 278 | |
| 279 | it('is false when the top-up it joined stopped short of the tip', |
| 280 | const first = |
| 281 | await sync(); |
| 282 | commit('src/x.ts', |
| 283 | // what a walk started before the commit above hands back |
| 284 | vi.spyOn(service |
| 285 | |
| 286 | expect(await ensureIndexed |
| 287 | }); |
| 288 | |
| 289 | it('rebuilds in the background after a force push', |
| 290 | const first = |
| 291 | commit('gone.txt', |
| 292 | await sync(); |
| 293 | git('reset', |
| 294 | const replacement = |
| 295 | |
| 296 | expect(await ensureIndexed |
| 297 | await sync(); |
| 298 | |
| 299 | expect(await ensureIndexed |
| 300 | expect((await rowsByPath |
| 301 | }); |
| 302 | }); |
| 303 | }); |