diff --git a/apps/api/src/faces/service.ts b/apps/api/src/faces/service.ts index c4cfa52..bd53085 100644 --- a/apps/api/src/faces/service.ts +++ b/apps/api/src/faces/service.ts @@ -757,25 +757,51 @@ export class FaceService { update: { ...(who.club !== undefined && { club: who.club }), ...(who.number !== undefined && { number: who.number }) }, }); } - if (person.model && person.model !== group.model) throw new FaceRejected("model_mismatch"); + // A group from another embedding model is still named. recomputePerson keeps the centroid on the + // person's own model, so the vectors are not mixed and Confirmar is not refused. await db.faceGroup.update({ where: { id: groupId }, data: { personId: person.id, suggestedPersonId: null, suggestionScore: null, ignoredAt: null, confirmedById: userId } }); person = await this.recomputePerson(person.id); const photoIds = (await db.face.findMany({ where: { groupId }, select: { photoId: true } })).map((f) => f.photoId); await this.nameConfirmedPhotos([...new Set(photoIds)]); - await this.refreshSuggestions(group.eventId); - // The same person suggested in other games: their cards are read again with this face included, so confirming - // one pulls the rest together instead of leaving a separate guess in every event. + // Same-event cards of this person collapse before the answer. Rescoring every other open face (this match + // and up to 25 others) used to run inside the request, so Confirmar sat there until the proxy gave up. + await this.joinSamePerson(group.eventId); const others = await db.faceGroup.findMany({ where: { suggestedPersonId: person.id, personId: null, ignoredAt: null, eventId: { not: group.eventId } }, select: { eventId: true }, distinct: ["eventId"], take: 25, }); - for (const other of others) await this.refreshSuggestions(other.eventId); + await this.scheduleRefresh([group.eventId, ...others.map((other) => other.eventId)]); this.deps.broadcaster.library(group.organizationId, { type: "faces.updated", eventId: group.eventId }); return person; } + /** + * Rescore open faces after the name is saved. The click must not wait on it: a match with hundreds of + * unnamed faces was holding the request until the proxy gave up, and the button looked dead. + * Tests share one database, so there the refresh finishes before the response (a late write would land in the next test). + */ + private async scheduleRefresh(eventIds: string[]): Promise { + const ids = [...new Set(eventIds)]; + const inline = process.env.NODE_ENV === "test"; + const run = async () => { + for (const eventId of ids) { + try { + await this.refreshSuggestions(eventId); + } catch (err) { + if (inline) throw err; + this.deps.log.warn({ eventId, err: err instanceof Error ? err.message : String(err) }, "faces: suggestion refresh failed"); + } + } + }; + if (inline) { + await run(); + return; + } + void run(); + } + /** A name typed as a code (\v7\) becomes the name in the organization's code list. */ private async expandName(organizationId: string, typed: string): Promise { if (!typed.includes("\\")) return typed.trim(); @@ -788,7 +814,9 @@ export class FaceService { const { db } = this.deps; const person = await db.person.findUniqueOrThrow({ where: { id: personId } }); const groups = await db.faceGroup.findMany({ where: { personId } }); - const model = groups[0]?.model ?? person.model ?? null; + // The person's model wins. A group confirmed from another model is named, but must not take over the + // centroid: faceGroup.findMany has no order, so the first row is not a stable choice. + const model = person.model ?? groups[0]?.model ?? null; if (person.recognitionOffAt) return db.person.update({ where: { id: personId }, data: { centroid: [], faceCount: groups.reduce((n, g) => n + g.size, 0) } }); const parts = groups.filter((g) => g.model === model).map((g) => ({ mean: g.centroid, count: g.size })); if (person.referenceEmbedding.length && person.model === model) parts.push({ mean: person.referenceEmbedding, count: 3 }); diff --git a/apps/app/src/i18n.ts b/apps/app/src/i18n.ts index f0fa558..42f52f7 100644 --- a/apps/app/src/i18n.ts +++ b/apps/app/src/i18n.ts @@ -204,6 +204,9 @@ export const t = { enable: "Ligar", disable: "Desligar", enabled: "Ligado", + confirm: "Confirmar", + confirming: "A confirmar…", + confirmFailed: "Não confirmei. Tenta outra vez.", }, realEstate: { nav: "Imobiliário", diff --git a/apps/app/src/pages/FacesQueuePage.test.tsx b/apps/app/src/pages/FacesQueuePage.test.tsx new file mode 100644 index 0000000..21851a8 --- /dev/null +++ b/apps/app/src/pages/FacesQueuePage.test.tsx @@ -0,0 +1,72 @@ +import { render, screen, waitFor } from "@testing-library/react"; +import userEvent from "@testing-library/user-event"; +import { MemoryRouter } from "react-router"; +import { beforeEach, describe, expect, it, vi } from "vitest"; +import { FacesQueuePage } from "./FacesQueuePage.js"; + +const staged = vi.hoisted(() => ({ + request: vi.fn(), +})); + +vi.mock("../auth.js", () => ({ + useAuth: () => ({ + organization: { id: "org-1" }, + api: { + request: staged.request, + absolute: (path: string) => path, + }, + }), +})); + +const ricardo = { + id: "g1", + eventId: "e1", + size: 4, + suggestion: { id: "p1", name: "Ricardo Candeias", club: "SPORTING CP", number: "C", score: 0.9 }, + event: { title: "SPORTING CP x SL BENFICA", startsAt: "2026-10-01T20:00:00.000Z" }, + samples: [], +}; + +describe("faces queue", () => { + beforeEach(() => { + staged.request.mockReset(); + }); + + it("shows that Confirmar is working, then takes the card away", async () => { + let release: () => void = () => undefined; + const pending = new Promise((resolve) => { + release = resolve; + }); + staged.request.mockImplementation((method: string) => { + if (method === "GET") return Promise.resolve({ groups: [ricardo] }); + return pending.then(() => ({ person: { id: "p1", name: "Ricardo Candeias" } })); + }); + render( + + + , + ); + const button = await screen.findByRole("button", { name: "Confirmar" }); + await userEvent.click(button); + expect((await screen.findByRole("button", { name: "A confirmar…" })).hasAttribute("disabled")).toBe(true); + release(); + await waitFor(() => expect(screen.queryByText("Ricardo Candeias")).toBeNull()); + expect(staged.request).toHaveBeenCalledWith("POST", "/v1/face-groups/g1/confirm", { personId: "p1" }); + }); + + it("says so when Confirmar fails, and leaves the card", async () => { + staged.request.mockImplementation((method: string) => { + if (method === "GET") return Promise.resolve({ groups: [ricardo] }); + return Promise.reject(new Error("conflict")); + }); + render( + + + , + ); + await userEvent.click(await screen.findByRole("button", { name: "Confirmar" })); + expect(await screen.findByText("Não confirmei. Tenta outra vez.")).toBeTruthy(); + expect(screen.getByText("Ricardo Candeias")).toBeTruthy(); + expect(screen.getByRole("button", { name: "Confirmar" }).hasAttribute("disabled")).toBe(false); + }); +}); diff --git a/apps/app/src/pages/FacesQueuePage.tsx b/apps/app/src/pages/FacesQueuePage.tsx index 5d75cfc..6558a67 100644 --- a/apps/app/src/pages/FacesQueuePage.tsx +++ b/apps/app/src/pages/FacesQueuePage.tsx @@ -32,6 +32,7 @@ export function FacesQueuePage() { const orgId = organization?.id ?? ""; const [rows, setRows] = useState(null); const [busy, setBusy] = useState(null); + const [failed, setFailed] = useState(null); const [open, setOpen] = useState(null); const [at, setAt] = useState(0); @@ -54,29 +55,33 @@ export function FacesQueuePage() { const personId = pile[0]?.suggestion?.id; if (!personId) return; setBusy(personId); + setFailed(null); try { for (const row of pile) await api.request("POST", `/v1/face-groups/${row.id}/confirm`, { personId }); setRows((cur) => cur?.filter((r) => !pile.some((p) => p.id === r.id)) ?? null); + } catch { + setFailed(personId); + await load().catch(() => undefined); } finally { setBusy(null); } }; return ( -
+

As caras por confirmar, juntas por pessoa. As camisolas ensinam-se em {t.numbersNav}.

-
+
{rows === null &&

A carregar…

} {rows?.length === 0 &&

Nada por confirmar.

} {[...piles.values()].map((pile) => { const who = pile[0]!.suggestion; const faces = pile.flatMap((r) => r.samples); return ( -
-
- + {failed === who?.id &&

{t.faces.confirmFailed}

} ); })}