Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions app/src/routes/umap/[id]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,23 @@ const UmapDetail: VoidComponent = () => {
const [busy, setBusy] = createSignal(false);
const [regeneratingRegions, setRegeneratingRegions] = createSignal(false);

const detail = createAsync(async (): Promise<UmapDetailData | null> => {
if (!params.id) return null;
const detail = createAsync<UmapDetailData | null>(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 });
Expand Down
6 changes: 1 addition & 5 deletions app/src/services/umap.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,7 @@ export const fetchUmapPoints = query(
async (runId: string): Promise<UmapPoint[]> => {
"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 },
Expand Down
40 changes: 39 additions & 1 deletion app/tests/e2e/helpers/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -380,6 +382,7 @@ export const seedCanvasRegionFixture = async (
params: {},
regionsJson,
regionsUpdatedAt: new Date(),
createdAt,
points: { create: points },
},
});
Expand All @@ -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();
}
};
53 changes: 53 additions & 0 deletions app/tests/e2e/umap-run-refresh.spec.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});
Loading