droboticflow/tests/browser/annotation-usability.mjs

146 lines
6.8 KiB
JavaScript

// Run only against an explicitly reserved company/local fixture and public APIs.
import assert from "node:assert/strict";
import fs from "node:fs";
import { chromium } from "playwright";
const base = process.env.ANNOTATION_BASE_URL;
const task = process.env.ANNOTATION_TASK_ID;
const project = process.env.ANNOTATION_PROJECT_ID;
const out = process.env.ANNOTATION_EVIDENCE_DIR;
assert.ok(base && task && project && out && process.env.ANNOTATION_STORAGE_STATE);
assert.match(project, /continuous-actions|acceptance/);
assert.equal(process.env.ANNOTATION_ALLOW_FIXTURE_WRITES, "true");
fs.mkdirSync(out, { recursive: true });
const browser = await chromium.launch({
executablePath:
process.env.PLAYWRIGHT_EXECUTABLE_PATH || "/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge",
headless: true,
args: ["--no-proxy-server"],
});
const context = await browser.newContext({
storageState: process.env.ANNOTATION_STORAGE_STATE,
viewport: { width: 1413, height: 615 },
});
const page = await context.newPage();
page.setDefaultTimeout(30000);
const errors = [];
page.on("pageerror", (e) => errors.push(e.message));
const checks = [];
const endpoint = `${base}/api/plugins/annotation-qa/tasks/${task}`;
const read = async () => {
const r = await context.request.get(endpoint);
assert.ok(r.ok());
return r.json();
};
const workspace = (d) => d.humanItem.subject.payload.annotationWorkspace;
const original = await read();
fs.writeFileSync(`${out}/task-before.json`, JSON.stringify(original), { mode: 0o600 });
const bench = page.getByTestId("capture-annotation-workbench");
const saved = () =>
page.waitForResponse((r) => r.url().endsWith(`/${task}/annotation-confirmations`) && r.request().method() === "POST");
const focus = async () => {
await bench.focus();
await page.waitForFunction(
() => document.activeElement === document.querySelector('[data-testid="capture-annotation-workbench"]'),
);
};
try {
await page.goto(`${base}/plugins/annotation-qa/workflows/all/annotate?projectId=${project}&taskId=${task}`);
await bench.waitFor();
assert.equal(new URL(page.url()).searchParams.get("taskId"), task);
assert.equal(await bench.getAttribute("data-stage"), "annotate");
await page.waitForFunction(() => [...document.querySelectorAll("video")].some((v) => v.readyState >= 2), null, {
timeout: 90000,
});
assert.equal(await page.getByRole("button", { name: "舍弃片段", exact: true }).count(), 0);
for (const label of ["标注说明", "边界说明"]) assert.equal(await page.getByText(label, { exact: true }).count(), 0);
const first = page.getByRole("listbox", { name: "动作区间", exact: true }).getByRole("option").first();
for (let attempt = 0; attempt < 20; attempt++) {
await first.click();
if (await page.getByRole("textbox", { name: "动作描述", exact: true }).count()) break;
await page.waitForTimeout(500);
}
const description = page.getByRole("textbox", { name: "动作描述", exact: true });
const draft = "未保存文字:检查 G 撤销恢复";
await description.fill(draft);
await description.press("g");
assert.equal(await description.inputValue(), draft + "g");
await description.fill(draft);
await focus();
await page.keyboard.press("Control+x");
assert.equal(await description.inputValue(), draft);
assert.equal(await page.getByRole("button", { name: "标为无效动作", exact: true }).count(), 1);
let pending = saved();
await page.keyboard.press("g");
assert.ok((await pending).ok());
let after = workspace(await read());
assert.equal(after.segments[0].isInvalid, true);
assert.equal(after.segments[0].labelId || "", "");
await focus();
pending = saved();
await page.keyboard.press("Control+z");
assert.ok((await pending).ok());
await page.waitForTimeout(200);
assert.equal(await description.inputValue(), draft);
for (const k of ["segments", "sliceSegments", "operations", "statuses"])
assert.deepEqual(workspace(await read())[k], workspace(original)[k]);
checks.push("G replaces unsaved text, API persists invalid, one undo restores saved workspace and unsaved editor");
// An actual failed HTTP request must preserve the editor, not claim success.
await page.route("**/annotation-confirmations", (route) =>
route.fulfill({
status: 422,
contentType: "application/json",
body: JSON.stringify({ error: "测试保存失败", code: "validation_error" }),
}),
);
await focus();
pending = saved();
await page.keyboard.press("g");
assert.equal((await pending).status(), 422);
await page.waitForTimeout(400);
assert.equal(await description.inputValue(), draft);
await page.unroute("**/annotation-confirmations");
checks.push("failed save preserves text");
await page.reload();
await bench.waitFor();
await page.getByRole("button", { name: "开始工作", exact: true }).click();
await page.waitForURL(/\/work\?/);
// Do not edit whichever task the scheduler chose. Verify target before more writes.
assert.equal(new URL(page.url()).searchParams.get("taskId"), task);
const pause = page.getByRole("button", { name: "暂停连续工作", exact: true });
if (await pause.count()) await pause.click();
await bench.waitFor();
for (const size of [
{ width: 1413, height: 615 },
{ width: 1920, height: 1080 },
]) {
await page.setViewportSize(size);
await page.getByRole("listbox", { name: "动作区间", exact: true }).getByRole("option").first().click();
for (const camera of ["放大左相机", "放大右相机", "恢复双画面"]) {
await page.getByRole("button", { name: camera, exact: true }).click();
const box = await page.getByRole("button", { name: "完成任务并提交质检", exact: true }).boundingBox();
assert.ok(box && box.y >= 0 && box.y + box.height <= size.height + 1);
const title = page.locator(".capture-annotation-heading h3");
const tb = await title.boundingBox();
const times = await page.locator(".capture-annotation-heading > div > span").boundingBox();
assert.ok(tb && times && tb.y + tb.height <= times.y + 1, "title must not overlap time");
}
}
checks.push("both viewport sizes and all camera views keep title separate and submission visible");
await focus();
await page.keyboard.press("?");
const help = page.getByRole("dialog");
assert.match(await help.innerText(), /G/);
assert.doesNotMatch(await help.innerText(), /Ctrl \+ X/);
await page.keyboard.press("Escape");
await page.screenshot({ path: `${out}/focused.png` });
assert.deepEqual(errors, []);
fs.writeFileSync(`${out}/result.json`, JSON.stringify({ pass: true, checks, errors }, null, 2));
console.log("PASS", checks);
} catch (e) {
await page.screenshot({ path: `${out}/failure.png` });
fs.writeFileSync(`${out}/failure-text.txt`, await page.locator("body").innerText());
throw e;
} finally {
await browser.close();
}