import { closeBrowserFixture } from "../../scripts/lib/browser-test-lifecycle.mjs"; import { browserExecutableOptions } from "../../scripts/lib/browser-executable.mjs"; import fs from "node:fs"; import assert from "node:assert/strict"; import { createRequire } from "node:module"; const require = createRequire(process.cwd() + "/package.json"); const { createServer } = require("vite"), { chromium } = require("playwright"); const api = process.env.REWORK_BOUNDARY_API; assert.match(api, /^http:\/\/127\.0\.0\.1:\d+$/); const runId = process.env.VERIFICATION_RUN_ID || `${Date.now()}-${process.pid}`; assert.match(runId, /^[a-zA-Z0-9_-]+$/); const scenario = process.env.INITIAL_SLICE_CONFIRM_SCENARIO; assert.match(scenario, /^[a-z0-9-]+$/); const root = new URL(`../../local-data/verification/initial-slice-confirmation/${runId}/${scenario}/`, import.meta.url); fs.mkdirSync(root, { recursive: true }); const report = { cases: [], errors: [], requests: [] }; const vite = await createServer({ root: process.cwd(), configFile: "tests/browser/vite.platform-feedback.config.ts", server: { host: "127.0.0.1", port: 5204, strictPort: true, proxy: { "/api": { target: api, changeOrigin: true } } }, }); let browser, owner, activeContext, activePage; try { await vite.listen(); owner = await chromium.launchServer({ ...browserExecutableOptions(), headless: true, args: ["--no-proxy-server"], }); browser = await chromium.connect(owner.wsEndpoint()); // A separate context for each case prevents autosaving one scenario into another. for (const status of [process.env.INITIAL_SLICE_CONFIRM_SCENARIO]) { await fetch(api + "/__boundary/reset?status=" + status); const context = await browser.newContext({ viewport: { width: 1600, height: 1100 } }); activeContext = context; await context.tracing.start({ screenshots: true, snapshots: true, sources: false }); const page = (activePage = await context.newPage()); page.setDefaultTimeout(20000); page.on("pageerror", (e) => report.errors.push(e.message)); page.on("response", async (r) => { if (r.url().endsWith("/drafts") || r.url().endsWith("/slice-confirmations")) report.requests.push({ path: new URL(r.url()).pathname, status: r.status() }); }); const saveFailureStatus = status === "save-dictionary" ? 504 : status === "save-throttle" ? 429 : status === "save-timeout" ? 408 : 503; // The dictionary scenario fails a real prerequisite store read in the Go // handler; the other scenarios inject transport/status failures here. let failNextSave = status.startsWith("save-") && status !== "save-dictionary"; await page.route("**/*", (r) => { const u = new URL(r.request().url()); if (u.hostname !== "127.0.0.1") return r.abort(); if (failNextSave && u.pathname.endsWith("/drafts") && r.request().method() === "PUT") { failNextSave = false; return r.fulfill({ status: saveFailureStatus, json: { error: "synthetic save unavailable" } }); } if (u.pathname.includes("/worker-api/")) return r.fulfill({ json: { status: "ok" } }); return r.continue(); }); await page.goto( "http://127.0.0.1:5204/plugins/annotation-qa/workflows/all/work?projectId=draft-project&taskId=draft-task", ); await page.getByRole("button", { name: "继续工作", exact: true }).click(); const bench = page.getByTestId("capture-annotation-workbench"); if (status === "malformed") { await page .getByText(/数据格式错误/) .first() .waitFor(); assert.equal(await bench.count(), 0, "unreadable saved content must never open a blank editable workbench"); const unchanged = await (await fetch(api + "/__boundary/state")).json(); assert.equal(unchanged.subject.payload.annotationWorkspace.segments.description, "保留异常旧内容"); assert.equal(report.requests.length, 0, "unreadable content must not autosave or generate"); await page.screenshot({ path: new URL(status + "-blocked.png", root).pathname }); report.cases.push({ status, preserved: true }); assert.deepEqual(report.errors, []); await context.close(); continue; } await bench.waitFor(); if (status === "failed") await page.locator("details.workspace-status-details > summary").click(); if (status !== "pending") { await bench.getByRole("button", { name: "从切片字典选择", exact: true }).click(); await page.getByRole("button", { name: "重新选择切片", exact: true }).click(); } await page.screenshot({ path: new URL(status + "-before.png", root).pathname }); if (status === "failed") { await page.locator("details.workspace-status-details > summary").click(); const retry = page .locator("details.workspace-status-details") .getByRole("button", { name: "重试生成", exact: true }); assert.equal( await retry.isEnabled(), true, "edited failed generation must be saveable through its recovery action", ); await retry.click(); // A changed saved snapshot needs a separate, explicit confirmation. await Promise.all([ page.waitForResponse((r) => r.url().endsWith("/slice-confirmations") && r.status() === 202), page .locator("details.workspace-status-details") .getByRole("button", { name: "按当前保存内容重新确认生成", exact: true }) .click(), ]); } else { await page .getByRole("button", { name: status === "legacy" ? "恢复生成并保留标注" : "确认全部切片", exact: true }) .click(); if (status.startsWith("save-")) { await Promise.all([ page.waitForResponse((r) => r.url().endsWith("/drafts") && r.status() === saveFailureStatus), page.getByRole("button", { name: "确认生成", exact: true }).click(), ]); await page.getByText("当前编辑尚未保存,请核对保存状态后重试;已有内容仍保留。", { exact: true }).waitFor(); const unchanged = await (await fetch(api + "/__boundary/state")).json(); assert.equal(unchanged.subject.payload.sliceWorkflow.status, "editing"); assert.equal(unchanged.subject.payload.annotationWorkspace.sliceSegments[0].name, "测试切片"); assert.deepEqual( unchanged.subject.payload.annotationWorkspace.segments.map((a) => a.id), ["pending-first"], ); assert.equal( report.requests.filter((r) => r.path.endsWith("/slice-confirmations")).length, 0, "failed saving must never dispatch generation", ); await page.locator("details.workspace-status-details > summary").click(); await page.getByRole("button", { name: "确认全部切片", exact: true }).click(); } await Promise.all([ page.waitForResponse((r) => r.url().endsWith("/slice-confirmations") && r.status() === 202), page.getByRole("button", { name: "确认生成", exact: true }).click(), ]); } let item; for (let i = 0; i < 150; i++) { item = await (await fetch(api + "/__boundary/state")).json(); if (["completed", "failed"].includes(item.subject.payload.sliceWorkflow?.status)) break; await new Promise((r) => setTimeout(r, 200)); } assert.equal( item.subject.payload.sliceWorkflow?.status, "completed", JSON.stringify({ workflow: item.subject.payload.sliceWorkflow, requests: report.requests, text: await page.locator("body").innerText(), }), ); const workspace = item.subject.payload.annotationWorkspace; if (status === "deleted-reset") { assert.ok( workspace.segments.every((action) => action.id !== "pending-first"), "old user's actions must not return", ); assert.ok( workspace.sliceSegments.every((slice) => slice.id !== "first"), "old user's cuts must not return", ); assert.ok(workspace.segments.length > 0, "new annotation remains editable"); } else { assert.deepEqual( workspace.segments.map((a) => a.id), ["pending-first"], ); } assert.equal(workspace.sliceSegments[0].name, status !== "pending" ? "重新选择切片" : "测试切片"); await page.reload(); await page.getByRole("button", { name: "继续工作", exact: true }).click(); await bench.waitFor(); await page.screenshot({ path: new URL(status + "-after.png", root).pathname }); report.cases.push({ status, retainedAction: workspace.segments[0].id, sliceName: workspace.sliceSegments[0].name }); assert.deepEqual(report.errors, []); await context.close(); } assert.deepEqual(report.errors, []); } catch (error) { report.failure = { name: error.name, message: error.message, stack: error.stack }; await activePage?.screenshot({ path: new URL("failure.png", root).pathname, fullPage: true }).catch(() => {}); await activeContext?.tracing.stop({ path: new URL("failure-trace.zip", root).pathname }).catch(() => {}); throw error; } finally { fs.writeFileSync( new URL(`report-${process.env.INITIAL_SLICE_CONFIRM_SCENARIO}.json`, root), JSON.stringify(report, null, 2), ); await closeBrowserFixture({ browser, owner, vite }); } console.log(JSON.stringify(report));