| 1 | import { execFileSync } from 'node:child_process'; |
| 2 | import { mkdirSync, mkdtempSync |
| 3 | import { tmpdir } |
| 4 | import path from 'node:path |
| 5 | import { createDatabase, type |
| 6 | import { Test, type |
| 7 | import { eq, sql |
| 8 | import { migrate } |
| 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 { RepositoryContributionService } |
| 21 | |
| 22 | const CONNECTION = process |
| 23 | const MIGRATIONS = path |
| 24 | import.meta.dirname, |
| 25 | '../../../../../../packages/db/drizzle', |
| 26 | ); |
| 27 | |
| 28 | // Needs a throwaway Postgres; the suite is skipped without one. |
| 29 | describe.skipIf(!CONNECTION |
| 30 | let db: |
| 31 | let pool: |
| 32 | let service: |
| 33 | let root: |
| 34 | let gitDir: |
| 35 | let repositoryId: |
| 36 | let fileSeq = |
| 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 commitAs( |
| 49 | email: string, |
| 50 | name: string, |
| 51 | date: string, |
| 52 | message: string, |
| 53 | ) { |
| 54 | fileSeq += 1; |
| 55 | const file = |
| 56 | mkdirSync(path.dirname |
| 57 | writeFileSync(path.join |
| 58 | git('add', |
| 59 | execFileSync('git', |
| 60 | cwd: root, |
| 61 | encoding: 'utf8', |
| 62 | env: { |
| 63 | ...process.env, |
| 64 | GIT_AUTHOR_NAME: name, |
| 65 | GIT_AUTHOR_EMAIL: email, |
| 66 | GIT_COMMITTER_NAME: name, |
| 67 | GIT_COMMITTER_EMAIL: email, |
| 68 | GIT_AUTHOR_DATE: date, |
| 69 | GIT_COMMITTER_DATE: date, |
| 70 | }, |
| 71 | }); |
| 72 | return git('rev-parse |
| 73 | } |
| 74 | |
| 75 | const sync = |
| 76 | |
| 77 | async function dayCounts |
| 78 | const rows = |
| 79 | .select() |
| 80 | .from(schema.repositoryContribution |
| 81 | .where(eq(schema |
| 82 | return new Map |
| 83 | } |
| 84 | |
| 85 | beforeAll(async () |
| 86 | ({ db, pool |
| 87 | await migrate(db |
| 88 | |
| 89 | const module: |
| 90 | providers: [ |
| 91 | RepositoryContributionService, |
| 92 | { provide: DATABASE, |
| 93 | ], |
| 94 | }).compile(); |
| 95 | service = module. |
| 96 | |
| 97 | await db |
| 98 | .insert(schema.user |
| 99 | .values({ |
| 100 | id: 'user_contribution_spec', |
| 101 | name: 'Test', |
| 102 | email: 'contribution-index@example.com', |
| 103 | }) |
| 104 | .onConflictDoNothing(); |
| 105 | }); |
| 106 | |
| 107 | afterAll(async () |
| 108 | await db |
| 109 | .delete(schema.user |
| 110 | .where(eq(schema |
| 111 | await pool.end |
| 112 | }); |
| 113 | |
| 114 | beforeEach(async () |
| 115 | root = mkdtempSync( |
| 116 | gitDir = path. |
| 117 | execFileSync('git', |
| 118 | fileSeq = 0; |
| 119 | |
| 120 | const [repository] |
| 121 | .insert(schema.repository |
| 122 | .values({ |
| 123 | name: 'spec', |
| 124 | slug: `contrib-spec-${Date |
| 125 | ownerId: 'user_contribution_spec', |
| 126 | }) |
| 127 | .returning(); |
| 128 | repositoryId = repository. |
| 129 | }); |
| 130 | |
| 131 | afterEach(async () |
| 132 | rmSync(root, { |
| 133 | await db |
| 134 | .delete(schema.repository |
| 135 | .where(eq(schema |
| 136 | }); |
| 137 | |
| 138 | it('buckets commits per author and day', |
| 139 | commitAs('alice@example.com', |
| 140 | commitAs('alice@example.com', |
| 141 | commitAs('bob@example.com', |
| 142 | |
| 143 | await sync(); |
| 144 | |
| 145 | const rows = |
| 146 | expect(rows.get |
| 147 | expect(rows.get |
| 148 | expect(rows.get |
| 149 | expect(rows.size |
| 150 | }); |
| 151 | |
| 152 | it('credits each co-author once per commit', |
| 153 | commitAs( |
| 154 | 'alice@example.com', |
| 155 | 'Alice', |
| 156 | '2026-01-10T12:00:00Z', |
| 157 | 'pair\n\nCo-authored-by: Bob <Bob@Example.com>\nco-authored-by: Carol <carol@example.com>\nCo-authored-by: Alice <alice@example.com> |
| 158 | ); |
| 159 | commitAs('bob@example.com', |
| 160 | |
| 161 | await sync(); |
| 162 | |
| 163 | const rows = |
| 164 | expect(rows.get |
| 165 | expect(rows.get |
| 166 | expect(rows.get |
| 167 | expect(rows.size |
| 168 | |
| 169 | const page = |
| 170 | expect(page.totalCommits |
| 171 | expect(page.totalContributors |
| 172 | }); |
| 173 | |
| 174 | it('keeps the commit count across a top-up and a rebuild', |
| 175 | commitAs('alice@example.com', |
| 176 | commitAs( |
| 177 | 'alice@example.com', |
| 178 | 'Alice', |
| 179 | '2026-02-11T12:00:00Z', |
| 180 | 'two\n\nCo-authored-by: Bob <bob@example.com>', |
| 181 | ); |
| 182 | await sync(); |
| 183 | expect( |
| 184 | (await service.listContributors |
| 185 | ).toBe(2); |
| 186 | |
| 187 | commitAs('bob@example.com', |
| 188 | await sync(); |
| 189 | expect( |
| 190 | (await service.listContributors |
| 191 | ).toBe(3); |
| 192 | |
| 193 | git('reset', |
| 194 | await sync(); |
| 195 | expect( |
| 196 | (await service.listContributors |
| 197 | ).toBe(1); |
| 198 | }); |
| 199 | |
| 200 | it('tops up without recounting after a new push', |
| 201 | commitAs('alice@example.com', |
| 202 | await sync(); |
| 203 | |
| 204 | commitAs('alice@example.com', |
| 205 | await sync(); |
| 206 | |
| 207 | const rows = |
| 208 | expect(rows.get |
| 209 | expect(rows.get |
| 210 | expect(rows.size |
| 211 | }); |
| 212 | |
| 213 | it('rebuilds from scratch after a force push', |
| 214 | commitAs('alice@example.com', |
| 215 | commitAs('alice@example.com', |
| 216 | await sync(); |
| 217 | |
| 218 | git('reset', |
| 219 | await sync(); |
| 220 | |
| 221 | const rows = |
| 222 | expect(rows.get |
| 223 | expect(rows.get |
| 224 | expect(rows.size |
| 225 | }); |
| 226 | |
| 227 | it('matches emails case-insensitively', |
| 228 | commitAs('Alice@Example.COM', |
| 229 | await sync(); |
| 230 | |
| 231 | const rows = |
| 232 | expect(rows.get |
| 233 | }); |
| 234 | |
| 235 | it('links rows to the account matching the author email', |
| 236 | await db.insert |
| 237 | id: 'user_contribution_alice', |
| 238 | name: 'Alice', |
| 239 | email: 'alice-link@example.com', |
| 240 | }); |
| 241 | |
| 242 | try { |
| 243 | commitAs( |
| 244 | 'alice-link@example.com', |
| 245 | 'Alice', |
| 246 | '2026-05-01T12:00:00Z', |
| 247 | 'one', |
| 248 | ); |
| 249 | commitAs( |
| 250 | 'stranger@example.com', |
| 251 | 'Stranger', |
| 252 | '2026-05-02T12:00:00Z', |
| 253 | 'two', |
| 254 | ); |
| 255 | await sync(); |
| 256 | |
| 257 | const rows = |
| 258 | expect(rows.get |
| 259 | 'user_contribution_alice', |
| 260 | ); |
| 261 | expect(rows.get |
| 262 | } finally { |
| 263 | await db |
| 264 | .delete(schema.user |
| 265 | .where(eq(schema |
| 266 | } |
| 267 | }); |
| 268 | |
| 269 | it('links rows when the account registers after indexing', |
| 270 | commitAs('late@example.com', |
| 271 | await sync(); |
| 272 | |
| 273 | let rows = |
| 274 | expect(rows.get |
| 275 | |
| 276 | await db.insert |
| 277 | id: 'user_contribution_late', |
| 278 | name: 'Late', |
| 279 | email: 'late@example.com', |
| 280 | }); |
| 281 | |
| 282 | try { |
| 283 | // No new commits: the sync is a no-op for the walk but still relinks. |
| 284 | await sync(); |
| 285 | |
| 286 | rows = await |
| 287 | expect(rows.get |
| 288 | 'user_contribution_late', |
| 289 | ); |
| 290 | } finally { |
| 291 | await db |
| 292 | .delete(schema.user |
| 293 | .where(eq(schema |
| 294 | } |
| 295 | }); |
| 296 | |
| 297 | it("links commits pushed from an account's extra address", |
| 298 | await db.insert |
| 299 | id: 'user_contribution_mai', |
| 300 | name: 'Mai', |
| 301 | email: 'mai@example.com', |
| 302 | }); |
| 303 | await db.insert |
| 304 | { |
| 305 | id: 'uem_spec_verified', |
| 306 | userId: 'user_contribution_mai', |
| 307 | email: 'mai-work@example.com', |
| 308 | verified: true, |
| 309 | }, |
| 310 | { |
| 311 | id: 'uem_spec_unverified', |
| 312 | userId: 'user_contribution_mai', |
| 313 | email: 'mai-claimed@example.com', |
| 314 | verified: false, |
| 315 | }, |
| 316 | ]); |
| 317 | |
| 318 | try { |
| 319 | commitAs('mai-work@example.com', |
| 320 | commitAs('mai-claimed@example.com', |
| 321 | await sync(); |
| 322 | |
| 323 | const rows = |
| 324 | expect(rows.get |
| 325 | 'user_contribution_mai', |
| 326 | ); |
| 327 | // An unverified address is a claim, not proof: it attributes to nobody. |
| 328 | expect( |
| 329 | rows.get('mai-claimed@example.com 2026-06-11 |
| 330 | ).toBeNull(); |
| 331 | } finally { |
| 332 | await db |
| 333 | .delete(schema.user |
| 334 | .where(eq(schema |
| 335 | } |
| 336 | }); |
| 337 | |
| 338 | it('relinks an address verified after indexing', |
| 339 | commitAs('later@example.com', |
| 340 | await sync(); |
| 341 | |
| 342 | await db.insert |
| 343 | id: 'user_contribution_nils', |
| 344 | name: 'Nils', |
| 345 | email: 'nils@example.com', |
| 346 | }); |
| 347 | await db.insert |
| 348 | id: 'uem_spec_later', |
| 349 | userId: 'user_contribution_nils', |
| 350 | email: 'later@example.com', |
| 351 | verified: true, |
| 352 | }); |
| 353 | |
| 354 | try { |
| 355 | await sync(); |
| 356 | |
| 357 | const rows = |
| 358 | expect(rows.get |
| 359 | 'user_contribution_nils', |
| 360 | ); |
| 361 | } finally { |
| 362 | await db |
| 363 | .delete(schema.user |
| 364 | .where(eq(schema |
| 365 | } |
| 366 | }); |
| 367 | |
| 368 | describe('listContributors', |
| 369 | async function seedRow |
| 370 | email: string, |
| 371 | name: string, |
| 372 | day: string, |
| 373 | commits: number, |
| 374 | authorId: string |
| 375 | ) { |
| 376 | await db.insert |
| 377 | repositoryId, |
| 378 | authorEmail: email, |
| 379 | authorName: name, |
| 380 | day, |
| 381 | authorId, |
| 382 | commits, |
| 383 | }); |
| 384 | // The total is the index's own count, as a sync would have stored it. |
| 385 | await db |
| 386 | .insert(schema.repositoryContributionIndex |
| 387 | .values({ |
| 388 | repositoryId, |
| 389 | indexedCommitSha: 'seed', |
| 390 | commitCount: commits, |
| 391 | }) |
| 392 | .onConflictDoUpdate({ |
| 393 | target: [schema.repositoryContributionIndex |
| 394 | set: { |
| 395 | commitCount: sql`${schema |
| 396 | }, |
| 397 | }); |
| 398 | } |
| 399 | |
| 400 | it('ranks by commits with the linked account preferred', |
| 401 | await db.insert |
| 402 | id: 'user_contribution_zoe', |
| 403 | name: 'Zoe Account', |
| 404 | email: 'zoe@example.com', |
| 405 | username: 'zoe', |
| 406 | }); |
| 407 | |
| 408 | try { |
| 409 | await seedRow( |
| 410 | 'zoe@example.com', |
| 411 | 'Zoe Git', |
| 412 | '2026-07-01', |
| 413 | 2, |
| 414 | 'user_contribution_zoe', |
| 415 | ); |
| 416 | await seedRow( |
| 417 | 'zoe@example.com', |
| 418 | 'Zoe Git', |
| 419 | '2026-07-03', |
| 420 | 3, |
| 421 | 'user_contribution_zoe', |
| 422 | ); |
| 423 | await seedRow('stranger@example.com |
| 424 | |
| 425 | const page = |
| 426 | |
| 427 | expect(page.totalCommits |
| 428 | expect(page.totalContributors |
| 429 | expect(page.contributors |
| 430 | 'stranger@example.com', |
| 431 | 'zoe@example.com', |
| 432 | ]); |
| 433 | |
| 434 | const [stranger, |
| 435 | expect(stranger.username |
| 436 | expect(stranger.authorName |
| 437 | expect(zoe.username |
| 438 | expect(zoe.authorName |
| 439 | expect(zoe.commits |
| 440 | expect(zoe.lastCommittedAt |
| 441 | '2026-07-03T00:00:00.000Z', |
| 442 | ); |
| 443 | } finally { |
| 444 | await db |
| 445 | .delete(schema.user |
| 446 | .where(eq(schema |
| 447 | } |
| 448 | }); |
| 449 | |
| 450 | it('resolves the account over the foreign key', |
| 451 | await db.insert |
| 452 | id: 'user_contribution_yara', |
| 453 | name: 'Yara Account', |
| 454 | email: 'other@example.com', |
| 455 | username: 'yara', |
| 456 | image: 'https://example.com/yara.png', |
| 457 | }); |
| 458 | |
| 459 | try { |
| 460 | await seedRow( |
| 461 | 'yara-git@example.com', |
| 462 | 'Yara Git', |
| 463 | '2026-08-01', |
| 464 | 4, |
| 465 | 'user_contribution_yara', |
| 466 | ); |
| 467 | |
| 468 | const page = |
| 469 | const [yara] |
| 470 | |
| 471 | // The git email matches no account email: only the FK links her. |
| 472 | expect(yara.authorEmail |
| 473 | expect(yara.username |
| 474 | expect(yara.authorName |
| 475 | expect(yara.image |
| 476 | expect(yara.commits |
| 477 | } finally { |
| 478 | await db |
| 479 | .delete(schema.user |
| 480 | .where(eq(schema |
| 481 | } |
| 482 | }); |
| 483 | |
| 484 | it("folds an account's addresses into one contributor", |
| 485 | await db.insert |
| 486 | id: 'user_contribution_wren', |
| 487 | name: 'Wren Account', |
| 488 | email: 'wren@example.com', |
| 489 | username: 'wren', |
| 490 | }); |
| 491 | |
| 492 | try { |
| 493 | // Same person, two addresses, both linked to the account. |
| 494 | await seedRow( |
| 495 | 'wren@example.com', |
| 496 | 'Wren Git', |
| 497 | '2026-10-01', |
| 498 | 2, |
| 499 | 'user_contribution_wren', |
| 500 | ); |
| 501 | await seedRow( |
| 502 | 'wren-work@example.com', |
| 503 | 'Wren Work', |
| 504 | '2026-10-05', |
| 505 | 3, |
| 506 | 'user_contribution_wren', |
| 507 | ); |
| 508 | |
| 509 | const page = |
| 510 | |
| 511 | expect(page.contributors |
| 512 | expect(page.totalContributors |
| 513 | expect(page.contributors |
| 514 | expect(page.contributors |
| 515 | // The newest address names the row. |
| 516 | expect(page.contributors |
| 517 | } finally { |
| 518 | await db |
| 519 | .delete(schema.user |
| 520 | .where(eq(schema |
| 521 | } |
| 522 | }); |
| 523 | |
| 524 | it('honours the limit', |
| 525 | await seedRow('a@example.com |
| 526 | await seedRow('b@example.com |
| 527 | |
| 528 | const page = |
| 529 | repositoryId, |
| 530 | limit: 1, |
| 531 | }); |
| 532 | |
| 533 | expect(page.contributors |
| 534 | expect(page.contributors |
| 535 | expect(page.totalContributors |
| 536 | expect(page.totalCommits |
| 537 | }); |
| 538 | }); |
| 539 | }); |