droboticflow/tests/browser/annotation-workspace-full-workflow.mjs

222 lines
10 KiB
JavaScript

// One-shot local fixtures: never reset completed tasks to rerun this acceptance.
import assert from "node:assert/strict";
import { chromium } from "playwright";
const base = "http://127.0.0.1:5173";
const prefix = process.env.WORKFLOW_PREFIX || "workspace-full-workflow";
assert.match(prefix, /^workspace-full-workflow[0-9]*(?:-[a-z0-9]+)?$/);
const browser = await chromium.launch({
executablePath: "/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge",
headless: true,
args: ["--no-proxy-server"],
});
const context = await browser.newContext({
storageState: "local-data/verification/annotation-workspace-layout/auth.json",
viewport: { width: 1280, height: 640 },
});
const page = await context.newPage();
page.setDefaultTimeout(20000);
const button = (name) => page.getByRole("button", { name, exact: true });
const writes = [];
page.on("request", (r) => {
if (r.method() === "POST") writes.push({ url: r.url(), body: r.postDataJSON() });
});
const startIndex = Number(process.env.WORKFLOW_START_INDEX || 0);
assert.ok(startIndex === 0 || startIndex === 1);
const claims = () => writes.filter((r) => r.url.endsWith("/random-claims"));
const read = async (task) => {
const r = await context.request.get(`${base}/api/human-items/${task.replace("annotation-task-", "human-item-")}`);
assert.ok(r.ok());
return (await r.json()).item;
};
const media = () =>
page.waitForFunction(
() =>
document.querySelectorAll("video").length === 2 &&
[...document.querySelectorAll("video")].every((v) => v.readyState >= 2),
null,
{ timeout: 60000 },
);
const post = (suffix) =>
page.waitForResponse((r) => r.url().endsWith(suffix) && r.request().method() === "POST", { timeout: 60000 });
const noScroll = async () =>
assert.ok(
await page.evaluate(
() => document.documentElement.scrollWidth <= innerWidth && document.documentElement.scrollHeight <= innerHeight,
),
);
try {
const primary = `annotation-task-${prefix}-${startIndex ? process.env.WORKFLOW_OWNED_TASK : "slice-primary"}`;
assert.match(primary, new RegExp(`^annotation-task-${prefix}-(slice-primary|pool-0[12])$`));
const initial = await read(primary);
assert.equal(initial.version, startIndex ? 2 : 1, "requires untouched owned task; never reset consumed evidence");
if (startIndex) {
await page.goto(`${base}/plugins/annotation-qa/workflows/all/work?projectId=project-${prefix}&taskId=${primary}`);
await page.getByRole("button").filter({ hasText: primary }).click();
await button("继续工作").click();
await button("开始连续工作").click();
} else {
await page.goto(
`${base}/plugins/annotation-qa/workflows/all/annotate?projectId=project-${prefix}&taskId=${primary}`,
);
await page.getByTestId("capture-annotation-workbench").waitFor();
assert.ok(await page.locator('header[aria-label="平台状态与主题"]').isVisible());
await button("开始工作").click();
await page.waitForURL(/\/work\?/);
}
await page.getByTestId("capture-annotation-workbench").waitFor();
assert.equal(new URL(page.url()).searchParams.get("taskId"), primary);
assert.equal(context.pages().length, 1);
assert.equal(claims().length, 0);
console.log("PASS Start/explicit recovery same-tab owned-first without random claim");
for (let index = startIndex; index < 3; index++) {
await button("确认全部切片").waitFor();
await button("删除切片").first().waitFor();
await media();
const task = new URL(page.url()).searchParams.get("taskId");
assert.match(task, new RegExp(`^annotation-task-${prefix}-(slice-primary|pool-0[12])$`));
const before = await read(task);
assert.equal(before.subject.payload.sliceWorkflow.phase, "slice");
const bench = page.getByTestId("capture-annotation-workbench");
await bench.focus();
const cuts = await button("删除切片").count();
for (let n = 0; n < 6; n++) {
await page.keyboard.press("ArrowRight");
await page.waitForTimeout(100);
}
await page.waitForFunction(
() => document.querySelector("video").currentTime > 0 && !document.querySelector("video").seeking,
);
await page.keyboard.press("c");
assert.equal(await button("删除切片").count(), cuts + 1, "split must visibly exist before generation");
// Rename every actual slice using the real project dictionary before generation.
await bench.focus();
for (let n = 0; n < 4; n++) await page.keyboard.press("Control+ArrowLeft");
do {
await button("从切片字典选择").click();
const dictionary = page.getByRole("dialog", { name: "选择切片名称", exact: true });
await dictionary.getByRole("textbox").fill(process.env.WORKFLOW_LABEL || "整体验收切片 001");
await dictionary
.getByRole("button", { name: process.env.WORKFLOW_LABEL || "整体验收切片 001", exact: true })
.click();
if (await button("下一片").isDisabled()) break;
await button("下一片").click();
} while (true);
await noScroll();
await button("确认全部切片").click();
let response = post("/slice-confirmations");
await button("确认生成").click();
const accepted = await response;
assert.ok(accepted.ok());
assert.equal((await accepted.json()).sliceWorkflow.status, "processing");
await button("新增动作描述").waitFor({ timeout: 180000 });
const generated = await read(task);
const sw = generated.subject.payload.sliceWorkflow;
assert.equal(sw.status, "completed");
assert.equal(sw.phase, "annotate");
assert.equal(sw.materialization.status, "completed");
assert.equal(sw.materialization.coverage.gapMicroseconds, 0);
assert.equal(sw.materialization.coverage.overlapMicroseconds, 0);
assert.equal(generated.subject.payload.annotationWorkspace.sliceSegments.length, 4);
await media();
await button("新增动作描述").click();
await bench.focus();
for (let n = 0; n < 6; n++) {
await page.keyboard.press("ArrowRight");
await page.waitForTimeout(100);
}
await button("确认结束点").click();
const input = page.getByRole("textbox", { name: "动作描述", exact: true });
const text = `整体验收已保存动作 ${index + 1}`;
await input.fill(text);
await button("切回原布局").click();
assert.equal(await input.inputValue(), text);
await button("进入专注视图").click();
assert.equal(await input.inputValue(), text);
response = post("/annotation-confirmations");
await button("确认保存描述").click();
assert.ok((await response).ok());
const saved = await read(task);
assert.equal(saved.version, generated.version + 1);
assert.ok(saved.subject.payload.annotationWorkspace.segments.some((s) => s.description === text));
console.log(
"PASS real generation and saved API readback",
JSON.stringify({ task, before: before.version, generated: generated.version, saved: saved.version }),
);
if (index === 1) {
await button("完成当前任务后停止").click();
await button("切回原布局").click();
await button("进入专注视图").click();
}
const count = claims().length;
await button("完成任务并提交质检").click();
await page.getByRole("dialog", { name: "确认提交质检", exact: true }).waitFor();
response = post("/combined-submissions");
await button("确认送检").click();
assert.ok((await response).ok());
let completed;
for (let i = 0; i < 180; i++) {
completed = await read(task);
if (completed.currentStageId === "quality_review") break;
await page.waitForTimeout(1000);
}
assert.equal(completed.currentStageId, "quality_review");
assert.ok(!completed.pendingCombinedSubmissionId);
assert.ok(completed.currentRevision > 0);
console.log(
"PASS real slice/generate/describe/save/sendQA",
JSON.stringify({
task,
before: before.version,
generated: generated.version,
saved: saved.version,
completed: completed.version,
stage: completed.currentStageId,
}),
);
if (index === 1) {
await page.waitForTimeout(3500);
assert.equal(claims().length, count, "stop after current suppresses next claim");
assert.equal(new URL(page.url()).searchParams.get("taskId"), task);
await button("恢复连续工作").click();
console.log("PASS stop after current and explicit resume");
}
if (index < 2) {
await page.waitForURL((u) => u.searchParams.get("taskId") !== task, { timeout: 60000 });
await page.getByTestId("capture-annotation-workbench").waitFor();
assert.equal(claims().length, count + 1);
} else {
await page
.getByText(/本项目暂无您可领取的标注任务/)
.first()
.waitFor({ timeout: 60000 });
assert.equal(claims().length, count + 1);
await page.waitForTimeout(2500);
assert.equal(claims().length, count + 1, "empty pool does not loop");
console.log("PASS authoritative empty pool stops automatic continuation");
}
}
assert.equal(writes.filter((w) => w.url.endsWith("/slice-confirmations")).length, 3 - startIndex);
assert.equal(writes.filter((w) => w.url.endsWith("/annotation-confirmations")).length, 3 - startIndex);
assert.equal(writes.filter((w) => w.url.endsWith("/combined-submissions")).length, 3 - startIndex);
assert.equal(claims().length, 3 - startIndex);
assert.equal(new Set(claims().map((w) => w.body.requestId)).size, 3 - startIndex);
console.log(
"PASS exact request totals",
JSON.stringify({
generation: writes.filter((w) => w.url.endsWith("/slice-confirmations")).length,
save: writes.filter((w) => w.url.endsWith("/annotation-confirmations")).length,
handoff: 3 - startIndex,
successfulClaims: 2 - startIndex,
emptyClaims: 1,
}),
);
await page.screenshot({ path: "local-data/verification/annotation-workspace-layout/task08-full-workflow.png" });
} catch (e) {
await page.screenshot({ path: "local-data/verification/annotation-workspace-layout/task08-workflow-error.png" });
console.log(await page.locator("body").innerText());
throw e;
} finally {
await context.close();
await browser.close();
}