| 1 | import { execFileSync } from 'node:child_process'; |
| 2 | import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'; |
| 3 | import { tmpdir } from 'node:os'; |
| 4 | import path from 'node:path'; |
| 5 | import { createDatabase, type Database, type Pool, schema } from '@ghost/db'; |
| 6 | import { Test, type TestingModule } from '@nestjs/testing'; |
| 7 | import { eq } from 'drizzle-orm'; |
| 8 | import { migrate } from 'drizzle-orm/node-postgres/migrator'; |
| 9 | import { |
| 10 | afterAll, |
| 11 | afterEach, |
| 12 | beforeAll, |
| 13 | beforeEach, |
| 14 | describe, |
| 15 | expect, |
| 16 | it, |
| 17 | } from 'vitest'; |
| 18 | |
| 19 | import { DATABASE } from '../../../database/database.module.js'; |
| 20 | import { RepositoryContributionService } from './repository-contribution.service.js'; |
| 21 | |
| 22 | const CONNECTION = process.env.TEST_DATABASE_URL; |
| 23 | const MIGRATIONS = path.resolve( |
| 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)('RepositoryContributionService', () => { |
| 30 | let db: Database; |
| 31 | let pool: Pool; |
| 32 | let service: RepositoryContributionService; |
| 33 | let root: string; |
| 34 | let gitDir: string; |
| 35 | let repositoryId: string; |
| 36 | let fileSeq = 0; |
| 37 | |
| 38 | function git(...args: string[]) { |
| 39 | const env = { ...process.env }; |
| 40 | delete env.GIT_DIR; |
| 41 | return execFileSync('git', args, { |
| 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 = `file-${fileSeq}.txt`; |
| 56 | mkdirSync(path.dirname(path.join(root, file)), { recursive: true }); |
| 57 | writeFileSync(path.join(root, file), message); |
| 58 | git('add', file); |
| 59 | execFileSync('git', ['commit', '-m', message], { |
| 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', 'HEAD'); |
| 73 | } |
| 74 | |
| 75 | const sync = () => service.sync({ repositoryId, repoDirectory: gitDir }); |
| 76 | |
| 77 | async function dayCounts() { |
| 78 | const rows = await db |
| 79 | .select() |
| 80 | .from(schema.repositoryContribution) |
| 81 | .where(eq(schema.repositoryContribution.repositoryId, repositoryId)); |
| 82 | return new Map(rows.map((row) => [`${row.authorEmail} ${row.day}`, row])); |
| 83 | } |
| 84 | |
| 85 | beforeAll(async () => { |
| 86 | ({ db, pool } = createDatabase({ connectionString: CONNECTION })); |
| 87 | await migrate(db, { migrationsFolder: MIGRATIONS }); |
| 88 | |
| 89 | const module: TestingModule = await Test.createTestingModule({ |
| 90 | providers: [ |
| 91 | RepositoryContributionService, |
| 92 | { provide: DATABASE, useValue: db }, |
| 93 | ], |
| 94 | }).compile(); |
| 95 | service = module.get(RepositoryContributionService); |
| 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.user.id, 'user_contribution_spec')); |
| 111 | await pool.end(); |
| 112 | }); |
| 113 | |
| 114 | beforeEach(async () => { |
| 115 | root = mkdtempSync(path.join(tmpdir(), 'ghost-contrib-')); |
| 116 | gitDir = path.join(root, '.git'); |
| 117 | execFileSync('git', ['init', '-q', '-b', 'main', root]); |
| 118 | fileSeq = 0; |
| 119 | |
| 120 | const [repository] = await db |
| 121 | .insert(schema.repository) |
| 122 | .values({ |
| 123 | name: 'spec', |
| 124 | slug: `contrib-spec-${Date.now()}-${Math.floor(Math.random() * 1e6)}`, |
| 125 | ownerId: 'user_contribution_spec', |
| 126 | }) |
| 127 | .returning(); |
| 128 | repositoryId = repository.id; |
| 129 | }); |
| 130 | |
| 131 | afterEach(async () => { |
| 132 | rmSync(root, { recursive: true, force: true }); |
| 133 | await db |
| 134 | .delete(schema.repository) |
| 135 | .where(eq(schema.repository.id, repositoryId)); |
| 136 | }); |
| 137 | |
| 138 | it('buckets commits per author and day', async () => { |
| 139 | commitAs('alice@example.com', 'Alice', '2026-01-05T12:00:00Z', 'one'); |
| 140 | commitAs('alice@example.com', 'Alice', '2026-01-05T13:00:00Z', 'two'); |
| 141 | commitAs('bob@example.com', 'Bob', '2026-01-06T12:00:00Z', 'three'); |
| 142 | |
| 143 | await sync(); |
| 144 | |
| 145 | const rows = await dayCounts(); |
| 146 | expect(rows.get('alice@example.com 2026-01-05')?.commits).toBe(2); |
| 147 | expect(rows.get('bob@example.com 2026-01-06')?.commits).toBe(1); |
| 148 | expect(rows.get('bob@example.com 2026-01-06')?.authorName).toBe('Bob'); |
| 149 | expect(rows.size).toBe(2); |
| 150 | }); |
| 151 | |
| 152 | it('tops up without recounting after a new push', async () => { |
| 153 | commitAs('alice@example.com', 'Alice', '2026-02-01T12:00:00Z', 'one'); |
| 154 | await sync(); |
| 155 | |
| 156 | commitAs('alice@example.com', 'Alice', '2026-02-02T12:00:00Z', 'two'); |
| 157 | await sync(); |
| 158 | |
| 159 | const rows = await dayCounts(); |
| 160 | expect(rows.get('alice@example.com 2026-02-01')?.commits).toBe(1); |
| 161 | expect(rows.get('alice@example.com 2026-02-02')?.commits).toBe(1); |
| 162 | expect(rows.size).toBe(2); |
| 163 | }); |
| 164 | |
| 165 | it('rebuilds from scratch after a force push', async () => { |
| 166 | commitAs('alice@example.com', 'Alice', '2026-03-01T12:00:00Z', 'one'); |
| 167 | commitAs('alice@example.com', 'Alice', '2026-03-02T12:00:00Z', 'two'); |
| 168 | await sync(); |
| 169 | |
| 170 | git('reset', '--hard', 'HEAD~1'); |
| 171 | await sync(); |
| 172 | |
| 173 | const rows = await dayCounts(); |
| 174 | expect(rows.get('alice@example.com 2026-03-01')?.commits).toBe(1); |
| 175 | expect(rows.get('alice@example.com 2026-03-02')).toBeUndefined(); |
| 176 | expect(rows.size).toBe(1); |
| 177 | }); |
| 178 | |
| 179 | it('matches emails case-insensitively', async () => { |
| 180 | commitAs('Alice@Example.COM', 'Alice', '2026-04-01T12:00:00Z', 'one'); |
| 181 | await sync(); |
| 182 | |
| 183 | const rows = await dayCounts(); |
| 184 | expect(rows.get('alice@example.com 2026-04-01')?.commits).toBe(1); |
| 185 | }); |
| 186 | |
| 187 | it('links rows to the account matching the author email', async () => { |
| 188 | await db.insert(schema.user).values({ |
| 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 = await dayCounts(); |
| 210 | expect(rows.get('alice-link@example.com 2026-05-01')?.authorId).toBe( |
| 211 | 'user_contribution_alice', |
| 212 | ); |
| 213 | expect(rows.get('stranger@example.com 2026-05-02')?.authorId).toBeNull(); |
| 214 | } finally { |
| 215 | await db |
| 216 | .delete(schema.user) |
| 217 | .where(eq(schema.user.id, 'user_contribution_alice')); |
| 218 | } |
| 219 | }); |
| 220 | |
| 221 | it('links rows when the account registers after indexing', async () => { |
| 222 | commitAs('late@example.com', 'Late', '2026-06-01T12:00:00Z', 'one'); |
| 223 | await sync(); |
| 224 | |
| 225 | let rows = await dayCounts(); |
| 226 | expect(rows.get('late@example.com 2026-06-01')?.authorId).toBeNull(); |
| 227 | |
| 228 | await db.insert(schema.user).values({ |
| 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 dayCounts(); |
| 239 | expect(rows.get('late@example.com 2026-06-01')?.authorId).toBe( |
| 240 | 'user_contribution_late', |
| 241 | ); |
| 242 | } finally { |
| 243 | await db |
| 244 | .delete(schema.user) |
| 245 | .where(eq(schema.user.id, 'user_contribution_late')); |
| 246 | } |
| 247 | }); |
| 248 | |
| 249 | it("links commits pushed from an account's extra address", async () => { |
| 250 | await db.insert(schema.user).values({ |
| 251 | id: 'user_contribution_mai', |
| 252 | name: 'Mai', |
| 253 | email: 'mai@example.com', |
| 254 | }); |
| 255 | await db.insert(schema.userEmail).values([ |
| 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', 'Mai', '2026-06-10T12:00:00Z', 'one'); |
| 272 | commitAs('mai-claimed@example.com', 'Mai', '2026-06-11T12:00:00Z', 'two'); |
| 273 | await sync(); |
| 274 | |
| 275 | const rows = await dayCounts(); |
| 276 | expect(rows.get('mai-work@example.com 2026-06-10')?.authorId).toBe( |
| 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')?.authorId, |
| 282 | ).toBeNull(); |
| 283 | } finally { |
| 284 | await db |
| 285 | .delete(schema.user) |
| 286 | .where(eq(schema.user.id, 'user_contribution_mai')); |
| 287 | } |
| 288 | }); |
| 289 | |
| 290 | it('relinks an address verified after indexing', async () => { |
| 291 | commitAs('later@example.com', 'Later', '2026-06-20T12:00:00Z', 'one'); |
| 292 | await sync(); |
| 293 | |
| 294 | await db.insert(schema.user).values({ |
| 295 | id: 'user_contribution_nils', |
| 296 | name: 'Nils', |
| 297 | email: 'nils@example.com', |
| 298 | }); |
| 299 | await db.insert(schema.userEmail).values({ |
| 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 = await dayCounts(); |
| 310 | expect(rows.get('later@example.com 2026-06-20')?.authorId).toBe( |
| 311 | 'user_contribution_nils', |
| 312 | ); |
| 313 | } finally { |
| 314 | await db |
| 315 | .delete(schema.user) |
| 316 | .where(eq(schema.user.id, 'user_contribution_nils')); |
| 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 | null = null, |
| 327 | ) { |
| 328 | await db.insert(schema.repositoryContribution).values({ |
| 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', async () => { |
| 339 | await db.insert(schema.user).values({ |
| 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', 'Stranger', '2026-07-02', 10); |
| 362 | |
| 363 | const page = await service.listContributors({ repositoryId }); |
| 364 | |
| 365 | expect(page.totalCommits).toBe(15); |
| 366 | expect(page.totalContributors).toBe(2); |
| 367 | expect(page.contributors.map((c) => c.authorEmail)).toEqual([ |
| 368 | 'stranger@example.com', |
| 369 | 'zoe@example.com', |
| 370 | ]); |
| 371 | |
| 372 | const [stranger, zoe] = page.contributors; |
| 373 | expect(stranger.username).toBeNull(); |
| 374 | expect(stranger.authorName).toBe('Stranger'); |
| 375 | expect(zoe.username).toBe('zoe'); |
| 376 | expect(zoe.authorName).toBe('Zoe Account'); |
| 377 | expect(zoe.commits).toBe(5); |
| 378 | expect(zoe.lastCommittedAt.toISOString()).toBe( |
| 379 | '2026-07-03T00:00:00.000Z', |
| 380 | ); |
| 381 | } finally { |
| 382 | await db |
| 383 | .delete(schema.user) |
| 384 | .where(eq(schema.user.id, 'user_contribution_zoe')); |
| 385 | } |
| 386 | }); |
| 387 | |
| 388 | it('resolves the account over the foreign key', async () => { |
| 389 | await db.insert(schema.user).values({ |
| 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 = await service.listContributors({ repositoryId }); |
| 407 | const [yara] = page.contributors; |
| 408 | |
| 409 | // The git email matches no account email: only the FK links her. |
| 410 | expect(yara.authorEmail).toBe('yara-git@example.com'); |
| 411 | expect(yara.username).toBe('yara'); |
| 412 | expect(yara.authorName).toBe('Yara Account'); |
| 413 | expect(yara.image).toBe('https://example.com/yara.png'); |
| 414 | expect(yara.commits).toBe(4); |
| 415 | } finally { |
| 416 | await db |
| 417 | .delete(schema.user) |
| 418 | .where(eq(schema.user.id, 'user_contribution_yara')); |
| 419 | } |
| 420 | }); |
| 421 | |
| 422 | it("folds an account's addresses into one contributor", async () => { |
| 423 | await db.insert(schema.user).values({ |
| 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 = await service.listContributors({ repositoryId }); |
| 448 | |
| 449 | expect(page.contributors).toHaveLength(1); |
| 450 | expect(page.totalContributors).toBe(1); |
| 451 | expect(page.contributors[0].commits).toBe(5); |
| 452 | expect(page.contributors[0].username).toBe('wren'); |
| 453 | // The newest address names the row. |
| 454 | expect(page.contributors[0].authorEmail).toBe('wren-work@example.com'); |
| 455 | } finally { |
| 456 | await db |
| 457 | .delete(schema.user) |
| 458 | .where(eq(schema.user.id, 'user_contribution_wren')); |
| 459 | } |
| 460 | }); |
| 461 | |
| 462 | it('honours the limit', async () => { |
| 463 | await seedRow('a@example.com', 'A', '2026-09-01', 1); |
| 464 | await seedRow('b@example.com', 'B', '2026-09-01', 2); |
| 465 | |
| 466 | const page = await service.listContributors({ |
| 467 | repositoryId, |
| 468 | limit: 1, |
| 469 | }); |
| 470 | |
| 471 | expect(page.contributors).toHaveLength(1); |
| 472 | expect(page.contributors[0].authorEmail).toBe('b@example.com'); |
| 473 | expect(page.totalContributors).toBe(2); |
| 474 | expect(page.totalCommits).toBe(3); |
| 475 | }); |
| 476 | }); |
| 477 | }); |