| 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 | } from 'vitest'; |
| 18 | |
| 19 | import { DATABASE } |
| 20 | import { RepositoryPathIndexService } |
| 21 | |
| 22 | const CONNECTION = process |
| 23 | const REF = ' |
| 24 | const MIGRATIONS = path |
| 25 | import.meta.dirname, |
| 26 | '../../../../../../packages/db/drizzle', |
| 27 | ); |
| 28 | |
| 29 | // Needs a throwaway Postgres; the suite is skipped without one. |
| 30 | describe.skipIf(!CONNECTION |
| 31 | let db: |
| 32 | let pool: |
| 33 | let service: |
| 34 | let root: |
| 35 | let gitDir: |
| 36 | let repositoryId: |
| 37 | |
| 38 | function git(... |
| 39 | const env = |
| 40 | delete env.GIT_DIR; |
| 41 | return execFileSync('git |
| 42 | cwd: root, |
| 43 | encoding: 'utf8', |
| 44 | env, |
| 45 | }).trim(); |
| 46 | } |
| 47 | |
| 48 | function commit(file |
| 49 | mkdirSync(path.dirname |
| 50 | writeFileSync(path.join |
| 51 | git('add', |
| 52 | git('commit', |
| 53 | return git('rev-parse |
| 54 | } |
| 55 | |
| 56 | const sync = |
| 57 | service.sync({ |
| 58 | |
| 59 | async function rowsByPath |
| 60 | const rows = |
| 61 | .select() |
| 62 | .from(schema.repositoryPathCommit |
| 63 | .where(eq(schema |
| 64 | return new Map |
| 65 | } |
| 66 | |
| 67 | beforeAll(async () |
| 68 | ({ db, pool |
| 69 | await migrate(db |
| 70 | |
| 71 | const module: |
| 72 | providers: [ |
| 73 | RepositoryPathIndexService, |
| 74 | { provide: DATABASE, |
| 75 | ], |
| 76 | }).compile(); |
| 77 | service = module. |
| 78 | |
| 79 | await db |
| 80 | .insert(schema.user |
| 81 | .values({ |
| 82 | id: 'user_path_index_spec', |
| 83 | name: 'Test', |
| 84 | email: 'path-index@example.com', |
| 85 | }) |
| 86 | .onConflictDoNothing(); |
| 87 | }); |
| 88 | |
| 89 | afterAll(async () |
| 90 | await db |
| 91 | .delete(schema.user |
| 92 | .where(eq(schema |
| 93 | await pool.end |
| 94 | }); |
| 95 | |
| 96 | beforeEach(async () |
| 97 | root = mkdtempSync( |
| 98 | gitDir = path. |
| 99 | execFileSync('git', |
| 100 | git('config', |
| 101 | git('config', |
| 102 | |
| 103 | const [repository] |
| 104 | .insert(schema.repository |
| 105 | .values({ |
| 106 | name: 'spec', |
| 107 | slug: `spec-${Date |
| 108 | ownerId: 'user_path_index_spec', |
| 109 | }) |
| 110 | .returning(); |
| 111 | repositoryId = repository. |
| 112 | }); |
| 113 | |
| 114 | afterEach(async () |
| 115 | rmSync(root, { |
| 116 | // cascades to both index tables |
| 117 | await db |
| 118 | .delete(schema.repository |
| 119 | .where(eq(schema |
| 120 | }); |
| 121 | |
| 122 | it('returns null and writes nothing when the ref does not exist', |
| 123 | expect(await sync |
| 124 | expect(await rowsByPath |
| 125 | }); |
| 126 | |
| 127 | it('records the newest commit for every path and its ancestors', |
| 128 | commit('README.md', |
| 129 | const addX = |
| 130 | |
| 131 | expect(await sync |
| 132 | |
| 133 | const rows = |
| 134 | expect(rows.get |
| 135 | commitSha: addX, |
| 136 | subject: 'add x', |
| 137 | }); |
| 138 | // ancestors inherit the newest commit beneath them |
| 139 | expect(rows.get |
| 140 | expect(rows.get |
| 141 | expect(rows.get |
| 142 | expect(rows.get |
| 143 | }); |
| 144 | |
| 145 | it('stores the committer timestamp', |
| 146 | commit('README.md', |
| 147 | await sync(); |
| 148 | |
| 149 | const seconds = |
| 150 | expect((await rowsByPath |
| 151 | new Date(seconds |
| 152 | ); |
| 153 | }); |
| 154 | |
| 155 | it('tops up incrementally and leaves untouched paths alone', |
| 156 | const first = |
| 157 | await sync(); |
| 158 | |
| 159 | const second = |
| 160 | expect(await sync |
| 161 | |
| 162 | const rows = |
| 163 | expect(rows.get |
| 164 | expect(rows.get |
| 165 | // the root row follows the tip |
| 166 | expect(rows.get |
| 167 | }); |
| 168 | |
| 169 | it('is a no-op when the ref has not moved', |
| 170 | commit('README.md', |
| 171 | await sync(); |
| 172 | const before = |
| 173 | .select() |
| 174 | .from(schema.repositoryRefIndex |
| 175 | .where(eq(schema |
| 176 | |
| 177 | await sync(); |
| 178 | const after = |
| 179 | .select() |
| 180 | .from(schema.repositoryRefIndex |
| 181 | .where(eq(schema |
| 182 | |
| 183 | expect(after).toEqual |
| 184 | }); |
| 185 | |
| 186 | it('rebuilds from scratch after a force push drops commits', |
| 187 | const first = |
| 188 | commit('gone.txt', |
| 189 | await sync(); |
| 190 | expect((await rowsByPath |
| 191 | |
| 192 | git('reset', |
| 193 | const replacement = |
| 194 | |
| 195 | expect(await sync |
| 196 | |
| 197 | const rows = |
| 198 | // a rebuild clears the ref's rows, so the discarded path is gone |
| 199 | expect(rows.has |
| 200 | expect(rows.get |
| 201 | }); |
| 202 | |
| 203 | it('attributes a merged file to the commit that wrote it', |
| 204 | commit('README.md', |
| 205 | git('checkout', |
| 206 | const onBranch = |
| 207 | git('checkout', |
| 208 | git('merge', |
| 209 | |
| 210 | await sync(); |
| 211 | |
| 212 | expect((await rowsByPath |
| 213 | commitSha: onBranch, |
| 214 | subject: 'add z on branch', |
| 215 | }); |
| 216 | }); |
| 217 | |
| 218 | it('looks up only the requested paths', |
| 219 | commit('README.md', |
| 220 | const addY = |
| 221 | await sync(); |
| 222 | |
| 223 | const found = |
| 224 | repositoryId, |
| 225 | ref: REF, |
| 226 | paths: ['src/y.ts', |
| 227 | }); |
| 228 | |
| 229 | expect([...found. |
| 230 | expect(found.get |
| 231 | }); |
| 232 | |
| 233 | it('keeps a second ref independent', |
| 234 | commit('README.md', |
| 235 | await sync(); |
| 236 | |
| 237 | const rows = |
| 238 | .select() |
| 239 | .from(schema.repositoryPathCommit |
| 240 | .where( |
| 241 | and( |
| 242 | eq(schema.repositoryPathCommit |
| 243 | eq(schema.repositoryPathCommit |
| 244 | ), |
| 245 | ); |
| 246 | expect(rows).toEqual |
| 247 | }); |
| 248 | }); |