| 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 } |
| 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('tops up without recounting after a new push', |
| 153 | commitAs('alice@example.com', |
| 154 | await sync(); |
| 155 | |
| 156 | commitAs('alice@example.com', |
| 157 | await sync(); |
| 158 | |
| 159 | const rows = |
| 160 | expect(rows.get |
| 161 | expect(rows.get |
| 162 | expect(rows.size |
| 163 | }); |
| 164 | |
| 165 | it('rebuilds from scratch after a force push', |
| 166 | commitAs('alice@example.com', |
| 167 | commitAs('alice@example.com', |
| 168 | await sync(); |
| 169 | |
| 170 | git('reset', |
| 171 | await sync(); |
| 172 | |
| 173 | const rows = |
| 174 | expect(rows.get |
| 175 | expect(rows.get |
| 176 | expect(rows.size |
| 177 | }); |
| 178 | |
| 179 | it('matches emails case-insensitively', |
| 180 | commitAs('Alice@Example.COM', |
| 181 | await sync(); |
| 182 | |
| 183 | const rows = |
| 184 | expect(rows.get |
| 185 | }); |
| 186 | |
| 187 | it('links rows to the account matching the author email', |
| 188 | await db.insert |
| 189 | id: 'user_contribution_alice', |
| 190 | name: 'Alice', |
| 191 | email: 'alice-link@example.com', |
| 192 | }); |
| 193 | |
| 194 | try { |
| 195 | commitAs( |
| 196 | 'alice-link@example.com', |
| 197 | 'Alice', |
| 198 | '2026-05-01T12:00:00Z', |
| 199 | 'one', |
| 200 | ); |
| 201 | commitAs( |
| 202 | 'stranger@example.com', |
| 203 | 'Stranger', |
| 204 | '2026-05-02T12:00:00Z', |
| 205 | 'two', |
| 206 | ); |
| 207 | await sync(); |
| 208 | |
| 209 | const rows = |
| 210 | expect(rows.get |
| 211 | 'user_contribution_alice', |
| 212 | ); |
| 213 | expect(rows.get |
| 214 | } finally { |
| 215 | await db |
| 216 | .delete(schema.user |
| 217 | .where(eq(schema |
| 218 | } |
| 219 | }); |
| 220 | |
| 221 | it('links rows when the account registers after indexing', |
| 222 | commitAs('late@example.com', |
| 223 | await sync(); |
| 224 | |
| 225 | let rows = |
| 226 | expect(rows.get |
| 227 | |
| 228 | await db.insert |
| 229 | id: 'user_contribution_late', |
| 230 | name: 'Late', |
| 231 | email: 'late@example.com', |
| 232 | }); |
| 233 | |
| 234 | try { |
| 235 | // No new commits: the sync is a no-op for the walk but still relinks. |
| 236 | await sync(); |
| 237 | |
| 238 | rows = await |
| 239 | expect(rows.get |
| 240 | 'user_contribution_late', |
| 241 | ); |
| 242 | } finally { |
| 243 | await db |
| 244 | .delete(schema.user |
| 245 | .where(eq(schema |
| 246 | } |
| 247 | }); |
| 248 | |
| 249 | it("links commits pushed from an account's extra address", |
| 250 | await db.insert |
| 251 | id: 'user_contribution_mai', |
| 252 | name: 'Mai', |
| 253 | email: 'mai@example.com', |
| 254 | }); |
| 255 | await db.insert |
| 256 | { |
| 257 | id: 'uem_spec_verified', |
| 258 | userId: 'user_contribution_mai', |
| 259 | email: 'mai-work@example.com', |
| 260 | verified: true, |
| 261 | }, |
| 262 | { |
| 263 | id: 'uem_spec_unverified', |
| 264 | userId: 'user_contribution_mai', |
| 265 | email: 'mai-claimed@example.com', |
| 266 | verified: false, |
| 267 | }, |
| 268 | ]); |
| 269 | |
| 270 | try { |
| 271 | commitAs('mai-work@example.com', |
| 272 | commitAs('mai-claimed@example.com', |
| 273 | await sync(); |
| 274 | |
| 275 | const rows = |
| 276 | expect(rows.get |
| 277 | 'user_contribution_mai', |
| 278 | ); |
| 279 | // An unverified address is a claim, not proof: it attributes to nobody. |
| 280 | expect( |
| 281 | rows.get('mai-claimed@example.com 2026-06-11 |
| 282 | ).toBeNull(); |
| 283 | } finally { |
| 284 | await db |
| 285 | .delete(schema.user |
| 286 | .where(eq(schema |
| 287 | } |
| 288 | }); |
| 289 | |
| 290 | it('relinks an address verified after indexing', |
| 291 | commitAs('later@example.com', |
| 292 | await sync(); |
| 293 | |
| 294 | await db.insert |
| 295 | id: 'user_contribution_nils', |
| 296 | name: 'Nils', |
| 297 | email: 'nils@example.com', |
| 298 | }); |
| 299 | await db.insert |
| 300 | id: 'uem_spec_later', |
| 301 | userId: 'user_contribution_nils', |
| 302 | email: 'later@example.com', |
| 303 | verified: true, |
| 304 | }); |
| 305 | |
| 306 | try { |
| 307 | await sync(); |
| 308 | |
| 309 | const rows = |
| 310 | expect(rows.get |
| 311 | 'user_contribution_nils', |
| 312 | ); |
| 313 | } finally { |
| 314 | await db |
| 315 | .delete(schema.user |
| 316 | .where(eq(schema |
| 317 | } |
| 318 | }); |
| 319 | |
| 320 | describe('listContributors', |
| 321 | async function seedRow |
| 322 | email: string, |
| 323 | name: string, |
| 324 | day: string, |
| 325 | commits: number, |
| 326 | authorId: string |
| 327 | ) { |
| 328 | await db.insert |
| 329 | repositoryId, |
| 330 | authorEmail: email, |
| 331 | authorName: name, |
| 332 | day, |
| 333 | authorId, |
| 334 | commits, |
| 335 | }); |
| 336 | } |
| 337 | |
| 338 | it('ranks by commits with the linked account preferred', |
| 339 | await db.insert |
| 340 | id: 'user_contribution_zoe', |
| 341 | name: 'Zoe Account', |
| 342 | email: 'zoe@example.com', |
| 343 | username: 'zoe', |
| 344 | }); |
| 345 | |
| 346 | try { |
| 347 | await seedRow( |
| 348 | 'zoe@example.com', |
| 349 | 'Zoe Git', |
| 350 | '2026-07-01', |
| 351 | 2, |
| 352 | 'user_contribution_zoe', |
| 353 | ); |
| 354 | await seedRow( |
| 355 | 'zoe@example.com', |
| 356 | 'Zoe Git', |
| 357 | '2026-07-03', |
| 358 | 3, |
| 359 | 'user_contribution_zoe', |
| 360 | ); |
| 361 | await seedRow('stranger@example.com |
| 362 | |
| 363 | const page = |
| 364 | |
| 365 | expect(page.totalCommits |
| 366 | expect(page.totalContributors |
| 367 | expect(page.contributors |
| 368 | 'stranger@example.com', |
| 369 | 'zoe@example.com', |
| 370 | ]); |
| 371 | |
| 372 | const [stranger, |
| 373 | expect(stranger.username |
| 374 | expect(stranger.authorName |
| 375 | expect(zoe.username |
| 376 | expect(zoe.authorName |
| 377 | expect(zoe.commits |
| 378 | expect(zoe.lastCommittedAt |
| 379 | '2026-07-03T00:00:00.000Z', |
| 380 | ); |
| 381 | } finally { |
| 382 | await db |
| 383 | .delete(schema.user |
| 384 | .where(eq(schema |
| 385 | } |
| 386 | }); |
| 387 | |
| 388 | it('resolves the account over the foreign key', |
| 389 | await db.insert |
| 390 | id: 'user_contribution_yara', |
| 391 | name: 'Yara Account', |
| 392 | email: 'other@example.com', |
| 393 | username: 'yara', |
| 394 | image: 'https://example.com/yara.png', |
| 395 | }); |
| 396 | |
| 397 | try { |
| 398 | await seedRow( |
| 399 | 'yara-git@example.com', |
| 400 | 'Yara Git', |
| 401 | '2026-08-01', |
| 402 | 4, |
| 403 | 'user_contribution_yara', |
| 404 | ); |
| 405 | |
| 406 | const page = |
| 407 | const [yara] |
| 408 | |
| 409 | // The git email matches no account email: only the FK links her. |
| 410 | expect(yara.authorEmail |
| 411 | expect(yara.username |
| 412 | expect(yara.authorName |
| 413 | expect(yara.image |
| 414 | expect(yara.commits |
| 415 | } finally { |
| 416 | await db |
| 417 | .delete(schema.user |
| 418 | .where(eq(schema |
| 419 | } |
| 420 | }); |
| 421 | |
| 422 | it("folds an account's addresses into one contributor", |
| 423 | await db.insert |
| 424 | id: 'user_contribution_wren', |
| 425 | name: 'Wren Account', |
| 426 | email: 'wren@example.com', |
| 427 | username: 'wren', |
| 428 | }); |
| 429 | |
| 430 | try { |
| 431 | // Same person, two addresses, both linked to the account. |
| 432 | await seedRow( |
| 433 | 'wren@example.com', |
| 434 | 'Wren Git', |
| 435 | '2026-10-01', |
| 436 | 2, |
| 437 | 'user_contribution_wren', |
| 438 | ); |
| 439 | await seedRow( |
| 440 | 'wren-work@example.com', |
| 441 | 'Wren Work', |
| 442 | '2026-10-05', |
| 443 | 3, |
| 444 | 'user_contribution_wren', |
| 445 | ); |
| 446 | |
| 447 | const page = |
| 448 | |
| 449 | expect(page.contributors |
| 450 | expect(page.totalContributors |
| 451 | expect(page.contributors |
| 452 | expect(page.contributors |
| 453 | // The newest address names the row. |
| 454 | expect(page.contributors |
| 455 | } finally { |
| 456 | await db |
| 457 | .delete(schema.user |
| 458 | .where(eq(schema |
| 459 | } |
| 460 | }); |
| 461 | |
| 462 | it('honours the limit', |
| 463 | await seedRow('a@example.com |
| 464 | await seedRow('b@example.com |
| 465 | |
| 466 | const page = |
| 467 | repositoryId, |
| 468 | limit: 1, |
| 469 | }); |
| 470 | |
| 471 | expect(page.contributors |
| 472 | expect(page.contributors |
| 473 | expect(page.totalContributors |
| 474 | expect(page.totalCommits |
| 475 | }); |
| 476 | }); |
| 477 | }); |