droboticflow/tests/browser/review-automatic-entry.mjs
2026-09-28 19:44:17 -07:00

430 lines
21 KiB
JavaScript

// Real browser + authenticated API against the isolated local stack (ticket 03:
// automatic review entry, spec rule 10). Every scenario drives the real UI and
// the real API; POST /review-continuation action="start" is hard-counted and
// must fire exactly once per explicit claim/continue entry, after the write
// lease and the exact task context are ready.
//
// Two clearly labeled exceptions use Playwright route control only:
// [delayed-response] holds a real GET detail response briefly (timing edge).
// [mocked-edge] answers POST /review-continuation with a controlled empty
// "waiting" snapshot, because a real empty queue would make the server
// auto-claim and consume unrelated tasks. The UI code path stays real.
//
// Fixtures (see fixture-index.json): quality[1] is owned by the qc user and
// opened directly; quality[3] must be unclaimed for the claim scenarios;
// acceptance[1] must be unclaimed. If a previous run consumed them, restore
// ONLY those documents with local-data/pi-review-flow/reset-ticket03-fixtures.js.
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://127.0.0.1:5193";
const host = new URL(base).hostname;
const fixtures = JSON.parse(await fs.readFile(`${dir}/fixture-index.json`, "utf8"));
const selected = JSON.parse(
process.env.REVIEW_ENTRY_ITEMS || '{"qualityOwned":1,"qualityClaim":3,"acceptanceClaim":1}',
);
const qualityOwned = fixtures.quality[selected.qualityOwned];
const qualityClaim = fixtures.quality[selected.qualityClaim];
const acceptanceClaim = fixtures.acceptance[selected.acceptanceClaim];
assert.ok(qualityOwned?.taskId && qualityClaim?.taskId && acceptanceClaim?.humanItemId, "fixture index incomplete");
const browser = await chromium.launch({
executablePath: process.env.BROWSER_EXECUTABLE || "/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge",
headless: true,
// The local machine has a system proxy that hangs loopback navigation.
args: ["--no-proxy-server"],
});
const reports = [];
let diagnosticContext;
const qualityPath = (taskId) =>
`${base}/plugins/annotation-qa/workflows/all/quality?projectId=${fixtures.projectId}${taskId ? `&taskId=${taskId}` : ""}`;
const acceptancePath = () => `${base}/plugins/annotation-qa/workflows/all/acceptance?projectId=${fixtures.projectId}`;
const RUNNING = "暂停连续审查";
const PAUSED = "开始/继续审查";
const BIG = { width: 1500, height: 900 };
const SMALL = { width: 1200, height: 600 };
async function openContext(mode) {
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: BIG });
const page = await context.newPage();
page.setDefaultTimeout(30000);
const calls = [];
const errors = [];
page.on("pageerror", (e) => errors.push(e.message));
page.on("request", (r) => {
if (r.method() === "POST" && r.url().includes("/review-continuation")) {
try {
calls.push(r.postDataJSON());
} catch {
/* keepalive bodies are still JSON; ignore parse races */
}
}
});
// Diagnostics trail for first-failure artifacts (statuses only, never bodies/cookies).
const http = [];
page.on("response", (r) => {
const u = r.url();
if (/\/claim|review-session|review-continuation|\/api\/plugins\/annotation-qa\/tasks\//.test(u))
http.push(`${r.request().method()} ${r.status()} ${u.slice(u.indexOf("/api/"))}`);
});
diagnosticContext = { page, http };
return { context, page, calls, errors, http };
}
/** Hard failure capture: screenshot + visible text + URL + request statuses.
* Artifacts go to the ignored local fixtures dir, never into the repo. */
async function captureFailure(page, http, cause) {
if (!page) return;
const stamp = `${dir}/review-automatic-entry-failure`;
try {
await page.screenshot({ path: `${stamp}.png` });
const bodyText = await page.evaluate(() => document.body.innerText.slice(0, 3000));
await fs.writeFile(
`${stamp}.json`,
JSON.stringify({ url: page.url(), error: String((cause && cause.message) || cause), bodyText, http }, null, 2),
);
} catch {
/* the page may already be gone; the original error matters */
}
}
const starts = (calls) => calls.filter((c) => c.action === "start").length;
async function waitForSession(page, timeout = 30000) {
await page
.getByRole("status")
.filter({ hasText: /本页可编辑审查意见|本页只读/ })
.first()
.waitFor({ timeout });
}
async function ensureEditable(page) {
await waitForSession(page);
const takeover = page.getByRole("button", { name: "在本页接管审查", exact: true });
if (
await takeover
.waitFor({ timeout: 4000 })
.then(() => true)
.catch(() => false)
)
await takeover.click();
await page.getByText("本页可编辑审查意见", { exact: true }).waitFor();
}
const waitForPaused = (page) => page.getByRole("button", { name: PAUSED, exact: true }).first().waitFor();
const waitForRunning = (page) => page.getByRole("button", { name: RUNNING, exact: true }).first().waitFor();
try {
// ================= Quality role =================
{
const { context, page, calls, errors } = await openContext("quality");
// Q1: direct URL restore of an owned task (focused) never auto-starts.
await page.goto(qualityPath(qualityOwned.taskId) + "&reviewFocus=1");
await ensureEditable(page);
await waitForPaused(page);
assert.equal(starts(calls), 0, "direct URL restore must not auto-start");
reports.push("quality direct URL stays paused");
// Q2: layout toggle focused -> ordinary -> focused never starts and never
// pauses a (not yet running) queue.
await page
.getByRole("button", { name: /^(返回任务池|返回我的任务)$/ })
.first()
.click();
await waitForPaused(page); // ordinary session-bar toggle
await page.getByRole("button", { name: "进入专注审查", exact: true }).click();
await waitForPaused(page);
assert.equal(starts(calls), 0, "layout toggles must not auto-start");
reports.push("quality layout toggle stays paused");
// Q3: explicit claim while the ordinary window is ALREADY undersized must
// not fire on resize-back; only a manual continue may start the queue.
await page.goto(qualityPath());
await page.setViewportSize(SMALL);
await page.getByTestId("human-task-pool-available").click();
const claimCard = page.locator(`[data-task-id="${qualityClaim.taskId}"]`);
const claimAction = claimCard.getByTestId("annotation-task-action");
await claimAction.waitFor();
assert.equal((await claimAction.textContent()).trim(), "领取", "claim fixture must be unclaimed");
await claimAction.click();
await claimCard.waitFor({ state: "hidden" });
assert.equal(starts(calls), 0, "claim alone preserves the pool without starting review");
await page.getByTestId("human-task-pool-mine").click();
await claimCard.getByTestId("annotation-task-action").click();
await ensureEditable(page);
await page.waitForTimeout(1500);
assert.equal(starts(calls), 0, "claim while undersized must not auto-start");
await page.setViewportSize(BIG);
await page.waitForTimeout(1500);
assert.equal(starts(calls), 0, "resize-back must stay paused until a manual continue");
await waitForPaused(page);
reports.push("quality undersized entry latched until manual continue");
// Q3b: manual continue is a real start; then the queue stays put.
await page.getByTestId("review-continuation-toggle").click();
await waitForRunning(page);
assert.equal(starts(calls), 1, "manual continue starts exactly once");
assert.equal(
calls.filter((c) => c.action === "start")[0].itemId,
qualityClaim.humanItemId,
"start binds the claimed task",
);
await page.waitForTimeout(2500);
assert.equal(starts(calls), 1, "no repeated starts from renew/context updates");
// Q4: pause, refresh, passive pool preview: refresh and lease recovery
// never arm; a plain card preview is not an entry.
await page.getByTestId("review-continuation-toggle").click();
await waitForPaused(page);
await page.reload();
await page.getByTestId("human-task-pool-mine").click();
await page.locator(`[data-task-id="${qualityClaim.taskId}"]`).getByTestId("annotation-task-card").click();
await ensureEditable(page);
await waitForPaused(page);
assert.equal(starts(calls), 1, "refresh + passive preview + lease recovery never auto-start");
reports.push("quality refresh/preview stay paused");
// Q5: two explicit 继续处理 entries in one page lifetime each start once.
const resumeCard = page.locator(`[data-task-id="${qualityClaim.taskId}"]`).getByTestId("annotation-task-action");
await resumeCard.waitFor();
assert.equal((await resumeCard.textContent()).trim(), "继续处理", "owned fixture must offer 继续处理");
await resumeCard.click();
await waitForRunning(page);
assert.equal(starts(calls), 2, "first explicit continue starts once");
await page.getByTestId("review-continuation-toggle").click();
await waitForPaused(page);
await page.locator(`[data-task-id="${qualityClaim.taskId}"]`).getByTestId("annotation-task-action").click();
await waitForRunning(page);
assert.equal(starts(calls), 3, "second explicit continue starts once more (per-entry intent)");
reports.push("quality per-entry auto-start twice in one page lifetime");
// Q6: shrinking an active ordinary queue pauses it; enlarging keeps it
// paused until a manual continue.
await page.getByTestId("review-continuation-toggle").click();
await waitForPaused(page);
await page.getByTestId("review-continuation-toggle").click();
await waitForRunning(page);
assert.equal(starts(calls), 4);
await page.setViewportSize(SMALL);
await waitForPaused(page);
await page.getByRole("status").filter({ hasText: "窗口空间不足" }).first().waitFor();
assert.equal(starts(calls), 4, "undersized pause sends no start");
assert.equal(
await page.getByTestId("review-continuation-toggle").isDisabled(),
true,
"manual resume disabled while undersized",
);
await page.setViewportSize(BIG);
await page.waitForTimeout(1500);
assert.equal(starts(calls), 4, "no automatic resume after enlarging");
await page.getByTestId("review-continuation-toggle").click();
await waitForRunning(page);
assert.equal(starts(calls), 5, "manual resume after undersized pause");
reports.push("quality ordinary shrink pauses, enlarge keeps paused, manual resume");
// Q7: layout toggle into focused never pauses; focused shrink pauses;
// enlarge keeps paused; the explicit 返回任务池 return pauses.
await page.getByRole("button", { name: "进入专注审查", exact: true }).click();
await waitForRunning(page); // focused heading still shows the running queue
await page.setViewportSize(SMALL);
await waitForPaused(page);
await page.setViewportSize(BIG);
await page.waitForTimeout(1200);
assert.equal(starts(calls), 5, "focused shrink/enlarge never auto-resumes");
await page
.getByText(/^(返回任务池|返回我的任务)$/, { exact: true })
.first()
.click();
await waitForPaused(page);
assert.equal(starts(calls), 5, "explicit return-to-pool pauses without new starts");
reports.push("quality focused shrink pauses; layout toggle and return keep rule");
// Q8: stale intent. [delayed-response] Holding the owned task's real GET
// detail response creates the window where its explicit-entry intent is
// armed but not ready; an unrelated preview selection must drop it, and a
// later passive preview of that task must not fire it.
// Route lifecycle: the page re-requests this detail several times (and can
// re-request it late), so every handler invocation is tracked; after the
// gate opens we wait for all held responses to settle BEFORE unroute.
// Unrouting while a handler is still holding would dispose its route and
// turn the pending route.continue() into an unhandled "already handled"
// rejection. Late arrivals after the release pass straight through.
let releaseDetail = () => {};
let released = false;
const gate = new Promise((resolve) => (releaseDetail = resolve));
const inFlight = new Set();
const routeErrors = [];
const delayedPath = `/api/plugins/annotation-qa/tasks/${qualityOwned.taskId}`;
const delayRoute = (route) => {
const done = (async () => {
const url = new URL(route.request().url());
if (!released && url.pathname === delayedPath) await gate;
await route.continue();
})();
inFlight.add(done);
done.then(
() => inFlight.delete(done),
(e) => {
routeErrors.push(String(e && e.message ? e.message : e));
inFlight.delete(done);
},
);
return done;
};
await page.route(`**${delayedPath}`, delayRoute);
await page.locator(`[data-task-id="${qualityOwned.taskId}"]`).getByTestId("annotation-task-action").click();
await page.locator(`[data-task-id="${qualityClaim.taskId}"]`).getByTestId("annotation-task-card").click();
await ensureEditable(page);
released = true;
releaseDetail();
await Promise.all([...inFlight]);
await page.unroute(`**${delayedPath}`, delayRoute);
assert.deepEqual(routeErrors, [], "delayed detail passthrough must not fail");
await page.waitForTimeout(1500);
assert.equal(starts(calls), 5, "unrelated preview drops the pending entry intent");
await page.locator(`[data-task-id="${qualityOwned.taskId}"]`).getByTestId("annotation-task-card").click();
await ensureEditable(page);
await page.waitForTimeout(1500);
assert.equal(starts(calls), 5, "passive preview of the previously armed task never fires the stale intent");
reports.push("quality stale intent dropped by unrelated preview");
// Q9: [mocked-edge] waiting queue with no editable item keeps the compact
// ordinary controls reachable. A real empty queue would make the server
// auto-claim unrelated tasks, so only the transport answer is controlled;
// the page runs its real code path. The return-to-pool and the paused
// fallback bar up to this point are entirely real API behavior.
const activeWorkbench = page.getByTestId("capture-annotation-workbench");
await activeWorkbench.getByRole("button", { name: /^(返回我的任务|返回任务池)$/ }).click();
// Returning clears the route immediately, while the detail/session cleanup
// follows the next React commit. Wait for both observable states rather
// than asserting during that transition.
await page.waitForFunction(() => !new URL(window.location.href).searchParams.has("taskId"), undefined, {
timeout: 10000,
});
await waitForPaused(page);
await page.getByTestId("review-continuation-toggle").waitFor();
let mockGeneration = 100;
const waitingSnapshot = () => ({
generation: ++mockGeneration,
status: "active",
item: null,
expiresAt: new Date(Date.now() + 45000).toISOString(),
bound: false,
pending: false,
});
const mockRoute = async (route) => {
const body = route.request().postDataJSON();
const snapshot =
body.action === "pause" ? { ...waitingSnapshot(), status: "paused", expiresAt: "" } : waitingSnapshot();
await route.fulfill({ status: 200, contentType: "application/json", body: JSON.stringify(snapshot) });
};
await page.route("**/review-continuation", mockRoute);
await page.getByTestId("review-continuation-toggle").click();
await waitForRunning(page); // pause must be exposed while waiting
await page.getByRole("status").filter({ hasText: "当前无可审查任务" }).first().waitFor();
await page.getByTestId("review-continuation-toggle").click();
await waitForPaused(page);
await page.getByTestId("review-continuation-toggle").waitFor(); // manual resume stays reachable
await page.unroute("**/review-continuation", mockRoute);
reports.push("quality waiting queue exposes pause/resume without an editable item [mocked-edge]");
assert.deepEqual(errors, []);
await context.close();
}
// ================= Acceptance role =================
{
const { context, page, calls, errors } = await openContext("acceptance");
const card = page.locator(`[data-item-id="${acceptanceClaim.humanItemId}"]`);
// A1: browsing the available pool never auto-starts.
await page.goto(acceptancePath());
await page.getByTestId("human-task-pool-available").click();
await page.getByTestId("acceptance-task-card").first().waitFor();
await page.waitForTimeout(2000);
assert.equal(starts(calls), 0, "pool browsing never auto-starts");
// A2: the card header preview is passive and never arms the queue.
await card.getByRole("button", { name: /^预览 / }).click();
await page.waitForTimeout(1500);
assert.equal(starts(calls), 0, "header preview never auto-starts");
reports.push("acceptance pool browsing and header preview stay paused");
await page.goto(acceptancePath());
await page.getByTestId("human-task-pool-available").click();
await card.waitFor();
// A3: explicit claim auto-starts exactly once for the claimed item.
const claimButton = card.getByRole("button", { name: "领取任务", exact: true });
await claimButton.waitFor();
await claimButton.click();
await card.waitFor({ state: "hidden" });
assert.equal(starts(calls), 0, "acceptance claim alone does not enter review");
await page.getByTestId("human-task-pool-mine").click();
await card.getByRole("button", { name: "继续处理", exact: true }).click();
await ensureEditable(page);
await waitForRunning(page);
assert.equal(starts(calls), 1, "acceptance continue after claim auto-starts exactly once");
const acceptanceId = new URL(page.url()).searchParams.get("acceptanceId");
assert.equal(acceptanceId, acceptanceClaim.humanItemId, "continue opens the claimed item");
assert.equal(calls.filter((c) => c.action === "start")[0].itemId, acceptanceId, "start binds the claimed item");
await page.waitForTimeout(2000);
assert.equal(starts(calls), 1, "no repeated acceptance starts");
reports.push("acceptance claim auto-starts once");
// A4: pause + refresh stays paused (URL restore of an owned task is not an
// entry).
await page.getByTestId("review-continuation-toggle").click();
await waitForPaused(page);
await page.reload();
await ensureEditable(page);
await waitForPaused(page);
assert.equal(starts(calls), 1, "acceptance refresh never resumes");
reports.push("acceptance refresh stays paused");
// A5: explicit footer 继续处理 (separated from header preview) starts once.
await page.getByTestId("human-task-pool-mine").click();
const continueButton = card.getByRole("button", { name: "继续处理", exact: true });
await continueButton.waitFor();
await continueButton.click();
await waitForRunning(page);
assert.equal(starts(calls), 2, "acceptance continue auto-starts once");
reports.push("acceptance footer continue auto-starts once");
// A6: explicit continue while ALREADY undersized stays latched after
// enlarging, until a manual continue.
await page.getByTestId("review-continuation-toggle").click();
await waitForPaused(page);
await page.setViewportSize(SMALL);
await card.getByRole("button", { name: "继续处理", exact: true }).click();
await page.waitForTimeout(1500);
assert.equal(starts(calls), 2, "continue while undersized must not auto-start");
await page.setViewportSize(BIG);
await page.waitForTimeout(1500);
assert.equal(starts(calls), 2, "acceptance resize-back stays paused until manual continue");
await page.getByTestId("review-continuation-toggle").click();
await waitForRunning(page);
assert.equal(starts(calls), 3, "manual continue after undersized latch");
await page.getByTestId("review-continuation-toggle").click();
await waitForPaused(page);
reports.push("acceptance undersized continue latched until manual continue");
assert.deepEqual(errors, []);
await context.close();
}
await fs.writeFile(`${dir}/review-automatic-entry.json`, JSON.stringify(reports, null, 2));
console.log("PASS review automatic entry", JSON.stringify(reports, null, 1));
} catch (cause) {
await captureFailure(diagnosticContext?.page, diagnosticContext?.http, cause);
throw cause;
} finally {
await browser.close();
}