forked from longtao.wu/droboticflow
176 lines
7.9 KiB
JavaScript
176 lines
7.9 KiB
JavaScript
// Actual HTTP application/API with transport failure injection only.
|
|
import { chromium } from "playwright";
|
|
import fs from "node:fs/promises";
|
|
import assert from "node:assert/strict";
|
|
const dir = process.env.REVIEW_DRAFT_FIXTURES || "local-data/verification/review-focused-20260922";
|
|
const base = process.env.REVIEW_DRAFT_URL || "http://review-session.test:5178";
|
|
const mode = process.env.REVIEW_DRAFT_MODE || "quality";
|
|
const hostname = new URL(base).hostname;
|
|
const fixtures = JSON.parse(await fs.readFile(`${dir}/fixture-index.json`, "utf8"));
|
|
const item = fixtures[mode][0];
|
|
const state = JSON.parse(
|
|
await fs.readFile(`${dir}/fixture-${mode === "quality" ? "qc" : "acceptance"}-auth.json`, "utf8"),
|
|
);
|
|
state.cookies = state.cookies.map((cookie) => ({ ...cookie, domain: hostname }));
|
|
const browser = await chromium.launch({
|
|
executablePath: process.env.BROWSER_EXECUTABLE || "/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge",
|
|
headless: true,
|
|
args: [`--host-resolver-rules=MAP ${hostname} 127.0.0.1`, "--no-proxy-server"],
|
|
});
|
|
try {
|
|
const context = await browser.newContext({ storageState: state, viewport: { width: 1600, height: 1000 } });
|
|
const page = await context.newPage();
|
|
page.setDefaultTimeout(20000);
|
|
const errors = [];
|
|
page.on("pageerror", (e) => errors.push(e.stack || e.message));
|
|
const url = `${base}/plugins/annotation-qa/workflows/all/${mode}?projectId=${fixtures.projectId}${item.taskId ? `&taskId=${item.taskId}` : ""}`;
|
|
const open = async (p) => {
|
|
await p.goto(url);
|
|
await p.getByRole("button", { name: /^我的任务/ }).click();
|
|
if (mode === "acceptance")
|
|
await p
|
|
.getByTestId("acceptance-task-card")
|
|
.first()
|
|
.getByRole("button", { name: /^预览 / })
|
|
.click();
|
|
await p
|
|
.getByRole("status")
|
|
.filter({ hasText: /本页可编辑审查意见|本页只读/ })
|
|
.waitFor();
|
|
const take = p.getByRole("button", { name: "在本页接管审查", exact: true });
|
|
if (await take.count()) await take.click();
|
|
await p.getByText("本页可编辑审查意见", { exact: true }).waitFor();
|
|
await p.getByRole("button", { name: "保存批注", exact: true }).waitFor();
|
|
await p.getByRole("button", { name: "保存批注", exact: true }).isEnabled();
|
|
};
|
|
await open(page);
|
|
const panel = page.getByRole("region", { name: "审查问题草稿" });
|
|
const input = page.getByRole("textbox", { name: "审查问题说明", exact: true });
|
|
const read = () =>
|
|
page.evaluate(async (id) => {
|
|
const r = await fetch(`/api/human-items/${id}/review-draft`);
|
|
if (!r.ok) throw Error(`readback ${r.status}`);
|
|
return r.json();
|
|
}, item.humanItemId);
|
|
// New contract (spec 已确认行为 2/4): entry starts with F, scope via radiogroup, Enter reaches text.
|
|
const startEntry = async (p, scopeName) => {
|
|
const sc = p.getByRole("region", { name: "审查问题草稿" }).getByRole("radiogroup", { name: "问题范围" });
|
|
// F is a workbench shortcut: focus a non-activating workbench control so the keydown reaches it.
|
|
await p
|
|
.getByTestId("capture-annotation-workbench")
|
|
.first()
|
|
.getByRole("button", { name: "上一帧", exact: true })
|
|
.focus();
|
|
// F fixes the actual ready frame; wait for decodable media first.
|
|
await p.waitForFunction(() => {
|
|
const videos = [...document.querySelectorAll("video")].filter((v) => v.getBoundingClientRect().width > 0);
|
|
return videos.length > 0 && videos.every((v) => v.readyState >= 2 && !v.seeking);
|
|
});
|
|
await p.keyboard.press("f");
|
|
await sc.waitFor();
|
|
// Clicking a radio confirms that scope directly; Enter on the default radio confirms 动作.
|
|
if (scopeName) await sc.getByRole("radio", { name: scopeName, exact: true }).click();
|
|
else await sc.getByRole("radio", { name: "动作", exact: true }).press("Enter");
|
|
await p.waitForFunction(() => document.activeElement?.getAttribute("aria-label") === "审查问题说明");
|
|
};
|
|
await startEntry(page, "整体");
|
|
const original = `迟到响应原保存 ${Date.now()}`;
|
|
const newer = `接管页面的新保存 ${Date.now()}`;
|
|
let release;
|
|
let captured;
|
|
const held = new Promise((resolve) => {
|
|
captured = resolve;
|
|
});
|
|
const gate = new Promise((resolve) => {
|
|
release = resolve;
|
|
});
|
|
let holdReadback = false;
|
|
let didHold = false;
|
|
await page.route("**/review-draft", async (route) => {
|
|
if (route.request().method() === "PUT") {
|
|
holdReadback = true;
|
|
return route.continue();
|
|
}
|
|
if (holdReadback && !didHold && route.request().method() === "GET") {
|
|
didHold = true;
|
|
const response = await route.fetch({ url: route.request().url().replace(hostname, "127.0.0.1") });
|
|
captured();
|
|
await gate;
|
|
return route.fulfill({ response });
|
|
}
|
|
return route.continue();
|
|
});
|
|
let puts = 0;
|
|
page.on("request", (r) => {
|
|
if (r.method() === "PUT" && r.url().endsWith("/review-draft")) puts++;
|
|
});
|
|
await input.fill(original);
|
|
await panel.getByRole("button", { name: "保存批注", exact: true }).click();
|
|
await held;
|
|
// Writes are serial (spec 已确认行为 9): while the save is unresolved no second PUT may leave.
|
|
assert.equal(puts, 1, "exactly one write in flight");
|
|
// The original PUT has completed server-side, but its readback is held in transit.
|
|
const other = await context.newPage();
|
|
await open(other);
|
|
const otherPanel = other.getByRole("region", { name: "审查问题草稿" });
|
|
const otherInput = other.getByRole("textbox", { name: "审查问题说明", exact: true });
|
|
await startEntry(other, "整体");
|
|
await otherInput.fill(newer);
|
|
await otherPanel.getByRole("button", { name: "保存批注", exact: true }).click();
|
|
await otherPanel
|
|
.getByRole("status")
|
|
.filter({ hasText: /^已保存$/ })
|
|
.waitFor();
|
|
await page
|
|
.getByRole("status")
|
|
.filter({ hasText: /失去编辑权/ })
|
|
.waitFor();
|
|
assert.equal(await input.inputValue(), original, "lost page keeps local text");
|
|
await page.getByRole("button", { name: "在本页接管审查", exact: true }).click();
|
|
await page.getByText("本页可编辑审查意见", { exact: true }).waitFor();
|
|
// Takeover retains the unconfirmed editor; readonly is only the transient reload window.
|
|
assert.equal(await input.inputValue(), original, "takeover keeps the unconfirmed text");
|
|
await input.fill("接回编辑权后的新文字,不可被迟到回读覆盖");
|
|
const delayedResponse = page.waitForResponse(
|
|
(response) => response.url().endsWith("/review-draft") && response.request().method() === "GET",
|
|
);
|
|
release();
|
|
await delayedResponse;
|
|
await page.evaluate(() => new Promise((resolve) => requestAnimationFrame(() => requestAnimationFrame(resolve))));
|
|
assert.equal(await input.inputValue(), "接回编辑权后的新文字,不可被迟到回读覆盖");
|
|
assert.ok((await read()).draft.problems.some((p) => p.text === newer));
|
|
await panel.getByRole("button", { name: "保存批注", exact: true }).click();
|
|
await panel
|
|
.getByRole("alert")
|
|
.filter({ hasText: /之前的文字已保存|问题列表已更新/ })
|
|
.waitFor();
|
|
assert.ok(
|
|
(await read()).draft.problems.some((p) => p.text === newer),
|
|
"reconciliation does not overwrite newer draft",
|
|
);
|
|
await panel.getByRole("button", { name: "保存批注", exact: true }).click();
|
|
await panel
|
|
.getByRole("status")
|
|
.filter({ hasText: /^已保存$/ })
|
|
.waitFor();
|
|
assert.ok((await read()).draft.problems.some((p) => p.text === "接回编辑权后的新文字,不可被迟到回读覆盖"));
|
|
assert.deepEqual(errors, []);
|
|
await fs.writeFile(
|
|
`${dir}/review-draft-latency-browser.json`,
|
|
JSON.stringify(
|
|
{
|
|
ordinaryHTTP: true,
|
|
delayedReadbackFenced: true,
|
|
generationTakeoverRetainsText: true,
|
|
unknownSaveReconciledBeforeNewSave: true,
|
|
},
|
|
null,
|
|
2,
|
|
),
|
|
);
|
|
console.log("Draft delayed readback, takeover fencing, and unknown-save reconciliation passed");
|
|
await context.close();
|
|
} finally {
|
|
await browser.close();
|
|
}
|