droboticflow/tests/browser/review-continuation.mjs

141 lines
7.2 KiB
JavaScript

// Real browser + authenticated API. Explicitly selected isolated task IDs only.
import assert from "node:assert/strict";
import fs from "node:fs/promises";
import { chromium } from "playwright";
const dir = process.env.REVIEW_FIXTURES || "local-data/verification/review-focused-20260922";
const base = process.env.REVIEW_TEST_URL || "http://review-session.test:5179";
const host = new URL(base).hostname;
const fixtures = JSON.parse(await fs.readFile(`${dir}/fixture-index.json`));
const ids = JSON.parse(
process.env.REVIEW_CONTINUATION_ITEMS ||
'{"quality":"human-item-review-focused-20260922-qc-06","acceptance":"human-item-review-focused-20260922-acceptance-05"}',
);
const browser = await chromium.launch({
executablePath: process.env.BROWSER_EXECUTABLE || "/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge",
headless: true,
args: host.endsWith(".test") ? [`--host-resolver-rules=MAP ${host} 127.0.0.1`, "--no-proxy-server"] : [],
});
const reports = [];
try {
for (const mode of ["quality", "acceptance"]) {
const auth = JSON.parse(await fs.readFile(`${dir}/fixture-${mode === "quality" ? "qc" : "acceptance"}-auth.json`));
auth.cookies = auth.cookies.map((c) => ({ ...c, domain: host }));
const context = await browser.newContext({ storageState: auth, viewport: { width: 1413, height: 615 } });
const page = await context.newPage();
page.setDefaultTimeout(25000);
const errors = [],
calls = [],
operations = [];
page.on("pageerror", (e) => errors.push(e.message));
page.on("request", (r) => {
if (r.method() === "POST" && r.url().endsWith("/review-continuation")) calls.push(r.postDataJSON());
if (r.method() === "POST" && /\/(reviews|decisions)$/.test(r.url())) operations.push(r.postDataJSON());
});
const task = fixtures[mode].find((i) => i.humanItemId === ids[mode]);
assert.ok(task, "explicit isolated task exists");
await page.goto(
`${base}/plugins/annotation-qa/workflows/all/${mode}?projectId=${fixtures.projectId}&${mode === "quality" ? `taskId=${task.taskId}` : `acceptanceId=${task.humanItemId}`}&reviewFocus=1`,
);
await page
.getByRole("textbox", { name: "审查问题说明", exact: true })
.waitFor()
.catch(async (e) => {
console.log("PAGE", mode, page.url(), await page.locator("body").innerText());
await page.screenshot({ path: `${dir}/continuation-failure-${mode}.png` });
throw e;
});
await page
.getByRole("status")
.filter({ hasText: /本页可编辑审查意见|本页只读/ })
.waitFor();
if (await page.getByRole("button", { name: "在本页接管审查", exact: true }).count())
await page.getByRole("button", { name: "在本页接管审查", exact: true }).click();
await page.getByText("本页可编辑审查意见", { exact: true }).waitFor();
assert.equal(calls.filter((c) => c.action === "start").length, 0, "entering never starts automatically");
await page.getByRole("button", { name: "开始/继续审查", exact: true }).click();
await page.getByText("连续审查中", { exact: true }).waitFor();
assert.equal(calls.filter((c) => c.action === "start").length, 1);
const approve = page.getByRole("button", { name: mode === "quality" ? "审核通过" : "验收通过", exact: true });
if (!(await approve.count())) {
console.log("BUTTONS", mode, await page.getByRole("button").allTextContents());
throw Error("approval button missing");
}
await approve.click();
const dialog = page.getByRole("dialog", { name: "确认审查决定", exact: true });
await dialog.waitFor();
await page.keyboard.press("Escape");
await dialog.waitFor({ state: "hidden" });
assert.equal(operations.length, 0, "cancel does not decide or advance");
assert.equal(calls.filter((c) => c.action === "next").length, 0);
await approve.click();
await dialog.waitFor();
await page.keyboard.press("Enter");
await page.waitForFunction(
({ id, mode, taskId }) => {
const p = new URL(location.href).searchParams;
return mode === "quality" ? p.get("taskId") !== taskId : p.get("acceptanceId") !== id;
},
{ id: task.humanItemId, mode, taskId: task.taskId },
);
await page.getByRole("textbox", { name: "审查问题说明", exact: true }).waitFor();
await page.getByText("本页可编辑审查意见", { exact: true }).waitFor();
await page.waitForFunction(() => [...document.querySelectorAll("video")].some((v) => v.readyState >= 2));
assert.equal(operations.length, 1, "formal decision exactly once");
const next = calls.find((c) => c.action === "next");
assert.equal(next.itemId, task.humanItemId);
assert.equal(next.operationId, operations[0].operationId);
const receipt = await page.evaluate(
async ({ id, op }) => {
const r = await fetch(`/api/human-items/${id}/review-operations/${op}`);
return r.json();
},
{ id: task.humanItemId, op: operations[0].operationId },
);
assert.equal(receipt.status, "completed");
await fs.writeFile(
`${dir}/review-continuation-${mode}-decision.json`,
JSON.stringify({ receipt, calls, operations, nextURL: page.url() }, null, 2),
);
await page.getByRole("button", { name: "暂停连续审查", exact: true }).click();
await page.getByRole("button", { name: "开始/继续审查", exact: true }).waitFor();
const starts = calls.filter((c) => c.action === "start").length;
await page.reload();
await page.getByRole("button", { name: "开始/继续审查", exact: true }).waitFor();
await page.getByRole("textbox", { name: "审查问题说明", exact: true }).waitFor();
assert.equal(calls.filter((c) => c.action === "start").length, starts, "refresh does not resume");
await page
.getByRole("status")
.filter({ hasText: /本页可编辑审查意见|本页只读/ })
.waitFor();
if (await page.getByRole("button", { name: "在本页接管审查", exact: true }).count())
await page.getByRole("button", { name: "在本页接管审查", exact: true }).click();
await page.getByText("本页可编辑审查意见", { exact: true }).waitFor();
await page.getByRole("button", { name: "开始/继续审查", exact: true }).click();
await page.getByRole("button", { name: "暂停连续审查", exact: true }).waitFor();
await page.setViewportSize({ width: 1280, height: 600 });
await page.getByRole("dialog", { name: "工作区已暂停", exact: true }).waitFor();
await page.setViewportSize({ width: 1413, height: 615 });
await page.getByRole("button", { name: "继续当前工作", exact: true }).click();
await page.getByRole("button", { name: "开始/继续审查", exact: true }).waitFor();
assert.deepEqual(errors, []);
reports.push({
mode,
receipt: receipt.status,
operationId: receipt.operationId,
next: page.url(),
calls,
refreshPaused: true,
resizePaused: true,
pageErrors: errors,
});
await context.close();
}
await fs.writeFile(`${dir}/review-continuation-browser.json`, JSON.stringify(reports, null, 2));
console.log(
"PASS real review continuation",
reports.map((r) => r.mode),
);
} finally {
await browser.close();
}