// Actual application, synthetic HTTP only. Never forwards API calls to a server. import assert from "node:assert/strict"; import fs from "node:fs/promises"; import { chromium } from "playwright"; import { createServer } from "vite"; const out = "local-data/test-results/annotation-buffer-summary-browser"; await fs.mkdir(out, { recursive: true }); const server = await createServer({ server: { host: "127.0.0.1", port: 0 } }); let browser, page; const report = { cases: [], errors: [], writes: [], polls: [], detailReads: 0 }; try { await server.listen(); browser = await chromium.launch({ executablePath: process.env.BROWSER_PATH || "/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge", headless: true, args: ["--no-proxy-server"], }); page = await browser.newPage({ viewport: { width: 1600, height: 1000 } }); page.setDefaultTimeout(15000); page.on("pageerror", (e) => report.errors.push(e.message)); let legacy = false, failed = false; const user = { id: "annotator", name: "合成标注员", role: "annotator", status: "active", organizationId: "org" }; const item = { id: "item", projectId: "project", organizationId: "org", workflowId: "workflow", pluginId: "annotation-qa", legacyTaskId: "task", assignedUserId: user.id, currentStageId: "annotation_combined", status: "in_progress", version: 1, reviewRound: 0, processingProfile: "raw_human", subject: { payload: { sliceWorkflow: { status: "completed", phase: "annotate" }, captureDirectory: "合成采集" } }, display: { captureDirectories: ["合成采集"] }, }; const summary = { id: item.id, projectId: item.projectId, legacyTaskId: item.legacyTaskId, assignedUserId: item.assignedUserId, currentStageId: item.currentStageId, status: item.status, version: 1, reviewRound: 0, sliceStatus: "completed", slicePhase: "annotate", display: item.display, }; const task = { id: "task", organizationId: "org", projectId: "project", workflowId: "workflow", templateId: "template", templateVersion: 6, status: "pending_annotation", humanStageId: "annotation_combined", processingProfile: "raw_human", continuousActions: true, currentRevision: 1, sourceVideo: "fixture.mp4", clipStart: 0, clipEnd: 10, assignedUserId: user.id, }; await page.route("**/*", async (route) => { const req = route.request(), u = new URL(req.url()), p = u.pathname; if (u.hostname !== "127.0.0.1" && u.hostname !== "localhost") return route.abort(); if (!p.startsWith("/api/") && !p.startsWith("/worker-api/")) return route.continue(); if (!["GET", "HEAD"].includes(req.method())) report.writes.push(p); const json = (data) => route.fulfill({ json: data }); if (p === "/api/auth/me") return json({ user, organization: { id: "org", name: "合成组织" } }); if (p === "/api/plugins") return json({ items: [{ enabled: true, manifest: { id: "annotation-qa", name: "标注", version: "1" } }] }); if (p.includes("health") || p.includes("ready")) return json({ status: "ok", ready: true }); if (p === "/api/projects") return json({ items: [{ id: "project", organizationId: "org", name: "合成项目", status: "active" }] }); if (p.endsWith("/members")) return json({ items: [{ id: "member", projectId: "project", userId: user.id, supplierId: "supplier", roles: ["annotator"] }], }); if (p.endsWith("/workflows")) return json({ items: [{ id: "workflow", name: "合成流程", nodes: [], edges: [] }] }); if (p.endsWith("/templates")) return json({ items: [{ id: "template", projectId: "project", version: 6, labels: [] }] }); if (p.endsWith("/work-buffer")) { report.polls.push(Object.fromEntries(u.searchParams)); if (failed) return route.fulfill({ status: 503, json: { error: "synthetic offline" } }); return json({ enabled: true, capacity: 2, occupied: 1, processing: {}, ...(legacy ? { items: [item] } : { summaries: [summary] }), }); } if (p === "/api/human-items/item") { report.detailReads++; return json({ item }); } if (p === "/api/human-items") return json({ items: u.searchParams.get("pool") === "mine" ? [item] : [] }); if (p.endsWith("/sidebar")) return json({ items: [item], tasks: [task], hasMore: false }); if (p.endsWith("/tasks")) return json({ items: [task] }); if (p.endsWith("/frame-timeline")) return json({ rangeStartSecond: 0, rangeEndSecond: 10, boundariesSeconds: Array.from({ length: 11 }, (_, i) => i), boundaryContract: "hardware-frame-v1", }); if (p.includes("/tasks/task")) return json({ task, humanItem: item, revisions: [], sliceRevisions: [], reviews: [], continuousActions: true }); return json({ items: [], labelIds: [], sliceLabelIds: [] }); }); await page.goto(server.resolvedUrls.local[0] + "plugins/annotation-qa/workflows/all/work?projectId=project"); await page.waitForFunction(() => document.querySelector('[aria-label="个人任务切换"]')); assert.match(await page.getByRole("combobox", { name: "个人任务切换" }).innerText(), /合成采集/); const before = report.polls.length, reads = report.detailReads; await page.waitForTimeout(6500); assert.ok(report.polls.length >= before + 2, "actual three-second polling continues"); assert.equal(report.detailReads, reads, "stable polls do not hydrate full records again"); assert.ok(report.polls.every((p) => p.view === "summary")); report.cases.push("actual page uses summary polling; stable polls do not reload full item"); legacy = true; await page.waitForTimeout(3500); assert.match(await page.getByRole("combobox", { name: "个人任务切换" }).innerText(), /合成采集/); report.cases.push("actual page accepts legacy full buffer responses"); failed = true; await page.waitForTimeout(3500); assert.match(await page.getByRole("combobox", { name: "个人任务切换" }).innerText(), /合成采集/); failed = false; legacy = false; await page.waitForTimeout(3500); assert.match(await page.getByRole("combobox", { name: "个人任务切换" }).innerText(), /合成采集/); report.cases.push("failed poll retains task switcher and reconnect resumes"); await page.getByRole("combobox", { name: "个人任务切换" }).selectOption("task"); await page.waitForURL(/taskId=task/); await page.waitForFunction(() => document.querySelector('[aria-label="个人任务切换"]')?.value === "task"); await page.waitForTimeout(3500); assert.equal(await page.getByRole("combobox", { name: "个人任务切换" }).inputValue(), "task"); report.cases.push("manual task selection survives subsequent summary poll"); await page.getByTestId("annotation-description-block").click(); const draft = page.getByRole("textbox", { name: "动作描述", exact: true }); await draft.fill("尚未保存的合成草稿"); const pollCount = report.polls.length; await page.waitForTimeout(6500); assert.ok(report.polls.length >= pollCount + 2); assert.equal(await draft.inputValue(), "尚未保存的合成草稿"); report.cases.push("unsaved action description survives two real summary polls"); await fs.writeFile(out + "/page.txt", await page.locator("body").innerText()); assert.deepEqual(report.errors, []); assert.ok( !report.writes.some((p) => p.includes("random-claims") || p.includes("submissions")), "manual polling must not allocate or submit", ); await page.screenshot({ path: out + "/passed.png" }); report.passed = true; console.log(JSON.stringify(report, null, 2)); } catch (e) { report.passed = false; report.error = String(e); await fs.writeFile( out + "/failure.txt", await page ?.locator("body") .innerText() .catch(() => ""), ); await page?.screenshot({ path: out + "/failure.png" }).catch(() => {}); console.error(e); throw e; } finally { await fs.writeFile(out + "/result.json", JSON.stringify(report, null, 2)); await browser?.close(); await server.close(); }