diff --git a/apps/api/src/sports/service.test.ts b/apps/api/src/sports/service.test.ts index df9dfc5..170df98 100644 --- a/apps/api/src/sports/service.test.ts +++ b/apps/api/src/sports/service.test.ts @@ -24,6 +24,12 @@ const p57p03 = () => { code: "P2039" }, ); +const p57p03SportsPlayer = () => + Object.assign( + new Error("Invalid `prisma.sportsPlayer.findMany()` invocation:\n\nDatabase error. Code: `57P03`. Message: `the database system is shutting down`"), + { code: "P2039" }, + ); + const p57p01 = () => Object.assign( new Error("Invalid `prisma.$queryRaw()` invocation:\n\nRaw query failed. Code: `57P01`. Message: `terminating connection due to administrator command`"), @@ -144,6 +150,49 @@ describe("syncEventSquads downgrades a 57P03 database-shutting-down error (BIT-3 }); }); +describe("syncEventSquads downgrades a 57P03 from personForIdentity's sportsPlayer.findMany (BIT-32474)", () => { + it("logs at info and returns zeros when personForIdentity hits 57P03 on sportsPlayer.findMany", async () => { + // The error loop reported 57P03 escaping at personForIdentity → sportsPlayer.findMany, one call deeper than + // the BIT-32470 test's person.findFirst. The same syncEventSquads catch (isPrismaServerUnreachable, which + // matches 57P03 since BIT-32470) covers this path: the error propagates through linkSquadPerson (no catch) to + // the syncEventSquads wrapper. This test proves that coverage. + const fpaTeam = { + id: "t-fpa", + provider: "FPA", + externalId: "fpa-1", + name: "Team FPA", + abbreviation: "FPA", + logoUrl: null, + }; + let findManyCalls = 0; + const db = { + event: { findUnique: async () => makeEvent({ homeTeam: fpaTeam, awayTeam: null }) }, + sportsPlayer: { + findFirst: async () => ({ season: "2026" }), + // First call: squad() reads the team's players. Second call: personForIdentity looks up the same human + // by birthDate/fullName (no externalId on the player, so the FPA licence lookup is skipped) and hits 57P03. + findMany: async () => { + findManyCalls++; + if (findManyCalls === 1) + return [{ id: "pl-1", name: "Player 1", externalId: null, birthDate: "2000-01-01", fullName: "Player One", number: "1", photoUrl: null, season: "2026" }]; + throw p57p03SportsPlayer(); + }, + findUnique: async () => null, + }, + person: { + findUnique: async () => null, + findFirst: async () => null, + }, + }; + const service = new SportsService(makeDeps(db)); + const out = await service.syncEventSquads("evt-1"); + expect(out).toEqual({ created: 0, updated: 0, headshots: 0 }); + const log = (service as unknown as { deps: { log: { calls: LogCall[] } } }).deps.log; + expect(log.calls.some((c) => c.level === "info" && /database unreachable/.test(c.msg))).toBe(true); + expect(log.calls.some((c) => c.level === "warn")).toBe(false); + }); +}); + describe("linkStoredSquads downgrades a 57P01 admin-terminated connection (BIT-32471)", () => { it("logs at info and returns 0 when the raw query is terminated mid-flight (57P01)", async () => { const db = {