diff --git a/app/src/routes/umap/[id]/index.tsx b/app/src/routes/umap/[id]/index.tsx index 4a4a523c..1eb0c6f7 100644 --- a/app/src/routes/umap/[id]/index.tsx +++ b/app/src/routes/umap/[id]/index.tsx @@ -31,15 +31,23 @@ const UmapDetail: VoidComponent = () => { const [busy, setBusy] = createSignal(false); const [regeneratingRegions, setRegeneratingRegions] = createSignal(false); - const detail = createAsync(async (): Promise => { - if (!params.id) return null; + const detail = createAsync(async (prev) => { + const id = params.id; + if (!id) return null; - const run = await fetchUmapRun(params.id); - if (!run) return null; + // Start both reads before the first await. A query called after it isn't + // tracked, so revalidating it would not rerun this. + const runRead = fetchUmapRun(id); + const pointsRead = fetchUmapPoints(id).catch((error) => { + console.warn("[UmapDetail] points read failed", error); + // A failed reread keeps this run's last points. + return prev?.meta.id === id ? prev.points : []; + }); - const points = await fetchUmapPoints(params.id).catch(() => []); + const run = await runRead; + if (!run) return null; - return { meta: run, points }; + return { meta: run, points: await pointsRead }; }); const { setCanvasContainerEl, setCanvasEl } = useUmapCanvas({ detail }); diff --git a/app/src/services/umap.service.ts b/app/src/services/umap.service.ts index 5425169b..cc4b7ce8 100644 --- a/app/src/services/umap.service.ts +++ b/app/src/services/umap.service.ts @@ -48,11 +48,7 @@ export const fetchUmapPoints = query( async (runId: string): Promise => { "use server"; if (!runId) throw new Error("Missing runId"); - const run = await prisma.umapRun.findUnique({ - where: { id: runId }, - select: { id: true, dims: true }, - }); - if (!run) throw new Error("Run not found"); + // An unknown run has no points. The run page reads both at once. const pts = await prisma.umapPoint.findMany({ where: { runId, doc: { archivedAt: null } }, select: { docId: true, x: true, y: true, z: true }, diff --git a/app/tests/e2e/helpers/fixtures.ts b/app/tests/e2e/helpers/fixtures.ts index 45d51034..6a65bb2d 100644 --- a/app/tests/e2e/helpers/fixtures.ts +++ b/app/tests/e2e/helpers/fixtures.ts @@ -295,11 +295,13 @@ export const seedRevisionFixture = async ( * one embedding run, one UMAP run with a single region, and a point per note. * No app endpoint creates these without OpenAI and the Python UMAP step, so * this writes directly, and only to the run manifest's synthetic database. - * The canvas reads the newest UMAP run, so callers must not run in parallel. + * The canvas reads the newest UMAP run, so callers must not run in parallel, + * unless they date their run into the past with `createdAt`. */ export const seedCanvasRegionFixture = async ( regionTitle: string, notes: Array<{ id: string; title: string }>, + { createdAt }: { createdAt?: Date } = {}, ) => { const datasourceUrl = seedDatasourceUrl(); @@ -380,6 +382,7 @@ export const seedCanvasRegionFixture = async ( params: {}, regionsJson, regionsUpdatedAt: new Date(), + createdAt, points: { create: points }, }, }); @@ -388,3 +391,38 @@ export const seedCanvasRegionFixture = async ( await prisma.$disconnect(); } }; + +/** + * Drops one note's point from a seeded projection, so a reread of the run's + * points returns one fewer. It writes only to the run manifest's synthetic + * database. + */ +export const removeUmapPointFixture = async ( + embeddingRunId: string, + docId: string, +) => { + const { PrismaClient } = await import("@prisma/client"); + const prisma = new PrismaClient({ datasourceUrl: seedDatasourceUrl() }); + try { + await prisma.umapPoint.deleteMany({ + where: { docId, run: { embeddingRunId } }, + }); + } finally { + await prisma.$disconnect(); + } +}; + +/** The id of the UMAP run a seeded embedding run holds. */ +export const umapRunIdFixture = async (embeddingRunId: string) => { + const { PrismaClient } = await import("@prisma/client"); + const prisma = new PrismaClient({ datasourceUrl: seedDatasourceUrl() }); + try { + const run = await prisma.umapRun.findFirstOrThrow({ + where: { embeddingRunId }, + select: { id: true }, + }); + return run.id; + } finally { + await prisma.$disconnect(); + } +}; diff --git a/app/tests/e2e/umap-run-refresh.spec.ts b/app/tests/e2e/umap-run-refresh.spec.ts new file mode 100644 index 00000000..3fd7ce66 --- /dev/null +++ b/app/tests/e2e/umap-run-refresh.spec.ts @@ -0,0 +1,53 @@ +import { expect, test } from "@playwright/test"; +import { + canSeedAuditDatabase, + createNoteFixture, + removeUmapPointFixture, + seedCanvasRegionFixture, + umapRunIdFixture, + uniqueSuffix, +} from "./helpers/fixtures"; +import { waitForAppReady } from "./helpers/readiness"; + +// "Refresh points" sent no request: the run page read its points after an +// await, so revalidating them reran nothing. +test.describe("UMAP run page", () => { + test.skip( + !canSeedAuditDatabase(), + "needs AUDIT_RUN_MANIFEST and AUDIT_ENV_FILE to seed a UMAP run", + ); + + test("Refresh points shows the run's current points", async ({ page }) => { + const suffix = uniqueSuffix(); + const notes: Array<{ id: string; title: string }> = []; + for (const name of ["first", "second"]) { + const title = `UMAP refresh ${name} ${suffix}`; + const id = await createNoteFixture(page, { title, markdown: title }); + notes.push({ id, title }); + } + // Dated into the past so the canvas specs' run stays the newest. + const embeddingRunId = await seedCanvasRegionFixture( + `UMAP refresh ${suffix}`, + notes, + { createdAt: new Date(0) }, + ); + await page.goto(`/umap/${await umapRunIdFixture(embeddingRunId)}`); + await waitForAppReady(page); + await expect(page.getByText("2 points", { exact: true })).toBeVisible(); + + const refresh = page.getByRole("button", { name: "Refresh points" }); + await removeUmapPointFixture(embeddingRunId, notes[0].id); + + await page.route("**/_server*", (route) => route.abort()); + const failed = page.waitForEvent("console", (message) => + message.text().includes("[UmapDetail] points read failed"), + ); + await refresh.click(); + await failed; + await expect(page.getByText("2 points", { exact: true })).toBeVisible(); + + await page.unroute("**/_server*"); + await refresh.click(); + await expect(page.getByText("1 points", { exact: true })).toBeVisible(); + }); +});