import { afterAll, beforeEach, describe, expect, it } from "vitest"; import { createDb } from "../src/db.js"; import { numberContext, numbersIn } from "../src/faces/numbers.js"; import { resetDb, seedUser } from "./helpers.js"; /** Shirt numbers hold within a season and competition: another season, a cup or the national team may differ. */ describe("shirt numbers by season and competition", () => { const db = createDb(process.env.DATABASE_URL_TEST ?? ""); afterAll(() => db.$disconnect()); beforeEach(() => resetDb(db)); async function setup() { const { org, user } = await seedUser(db, "owner@fpa.pt", { org: "fpa", role: "OWNER" }); const orgId = org!.id; const team = await db.sportsTeam.create({ data: { provider: "FPA", externalId: "t1", sport: "andebol", name: "Sporting CP", slug: "sporting-cp" } }); const other = await db.sportsTeam.create({ data: { provider: "FPA", externalId: "t2", sport: "andebol", name: "ABC de Braga", slug: "abc" } }); // Ana wore 23 five seasons ago and wears 7 now; the person was made from today's squad entry. await db.sportsPlayer.create({ data: { teamId: team.id, season: "20212022", externalId: "p1", name: "Ana Seabra", number: "23" } }); const now = await db.sportsPlayer.create({ data: { teamId: team.id, season: "20262027", externalId: "p1", name: "Ana Seabra", number: "7" } }); const ana = await db.person.create({ data: { organizationId: orgId, name: "Ana Seabra", club: "Sporting CP", number: "7", sportsPlayerId: now.id } }); // Rui has no squad entry: only the number typed on him. const rui = await db.person.create({ data: { organizationId: orgId, name: "Rui Silva", club: "Sporting CP", number: "14" } }); const event = (data: { home: string; away: string; startsAt: string; competition?: string; linked?: boolean }) => db.event.create({ data: { organizationId: orgId, home: data.home, away: data.away, competition: data.competition ?? null, startsAt: new Date(data.startsAt), ingestFrom: new Date(data.startsAt), ingestUntil: new Date(data.startsAt), iptcTemplate: {}, createdById: user.id, ...(data.linked && { homeTeamId: team.id, awayTeamId: other.id }), }, }); let n = 0; /** Ana confirmed in a game, her shirt clearly read as `number` in three photos. */ const seen = async (eventId: string, number: string) => { const group = await db.faceGroup.create({ data: { organizationId: orgId, eventId, centroid: [1, 0], model: "auraface-v1", size: 3, personId: ana.id } }); for (let i = 0; i < 3; i++) { n++; const photo = await db.photo.create({ data: { organizationId: orgId, eventId, source: "UPLOAD", originalName: `P_${n}.jpg`, sha256: String(n).padStart(64, "0"), byteSize: 1, storageKey: `k${n}`, status: "READY" } }); await db.face.create({ data: { organizationId: orgId, photoId: photo.id, groupId: group.id, x0: 0.4, y0: 0.2, x1: 0.5, y1: 0.35, score: 0.9, embedding: [1, 0], model: "auraface-v1", jerseyNumber: number, jerseyScore: 0.95 } }); } }; return { ana, rui, event, seen }; } const today = new Date("2026-10-15T12:00:00Z"); const numbers = async (eventId: string, ids: string[]) => { const ctx = (await numberContext(db, eventId, today))!; return Object.fromEntries(await numbersIn(db, ctx, ids)); }; it("reads no numbers at an event that is not a game (a gala, a session): faces only", async () => { const { event } = await setup(); const gala = await event({ home: "Gala do Andebol 2026", away: "", startsAt: "2026-07-10T20:00:00Z" }); expect(await numberContext(db, gala.id, today)).toBeNull(); }); it("takes the squad of the game's season, not today's", async () => { const { ana, rui, event } = await setup(); const league = await event({ home: "Sporting CP", away: "ABC de Braga", startsAt: "2026-10-04T18:00:00Z", competition: "Andebol 1", linked: true }); const old = await event({ home: "Sporting CP", away: "ABC de Braga", startsAt: "2021-11-20T18:00:00Z", competition: "Andebol 1", linked: true }); expect(await numbers(league.id, [ana.id, rui.id])).toEqual({ [ana.id]: "7", [rui.id]: "14" }); // Five seasons ago: her number then, and Rui's typed number (no season of its own) says nothing. expect(await numbers(old.id, [ana.id, rui.id])).toEqual({ [ana.id]: "23" }); }); it("never gives a club number to a national team's shirt, and learns the one worn there", async () => { const { ana, rui, event, seen } = await setup(); const game = await event({ home: "Portugal", away: "França", startsAt: "2026-10-10T18:00:00Z" }); expect(await numbers(game.id, [ana.id, rui.id])).toEqual({}); // Confirmed in another national game this season, wearing 13: that is her number for Portugal. const before = await event({ home: "Portugal", away: "Espanha", startsAt: "2026-10-08T18:00:00Z" }); await seen(before.id, "13"); expect(await numbers(game.id, [ana.id])).toEqual({ [ana.id]: "13" }); // Her club games are not affected. const league = await event({ home: "Sporting CP", away: "ABC de Braga", startsAt: "2026-10-04T18:00:00Z", competition: "Andebol 1", linked: true }); expect(await numbers(league.id, [ana.id])).toEqual({ [ana.id]: "7" }); }); it("uses the number worn in a competition with its own numbering", async () => { const { ana, event, seen } = await setup(); const cup1 = await event({ home: "Sporting CP", away: "ABC de Braga", startsAt: "2026-10-01T18:00:00Z", competition: "Taça EHF" }); const cup2 = await event({ home: "ABC de Braga", away: "Sporting CP", startsAt: "2026-10-12T18:00:00Z", competition: "Taça EHF" }); // Before anything is known of that cup: the season's squad. expect(await numbers(cup2.id, [ana.id])).toEqual({ [ana.id]: "7" }); await seen(cup1.id, "70"); expect(await numbers(cup2.id, [ana.id])).toEqual({ [ana.id]: "70" }); }); });