forked from longtao.wu/droboticflow
342 lines
15 KiB
JavaScript
342 lines
15 KiB
JavaScript
// Local API + real Edge acceptance. A non-loopback hostname is genuinely insecure;
|
|
// no browser security override or fake lock success is used for the HTTP path.
|
|
// Requires a create-once pr-review-http fixture project with empty annotation tasks.
|
|
import assert from "node:assert/strict";
|
|
import { readFileSync, mkdirSync } from "node:fs";
|
|
import { chromium } from "playwright";
|
|
import { createServer } from "vite";
|
|
|
|
const prefix = process.env.REVIEW_FIXTURE_PREFIX || "pr-review-http";
|
|
assert.match(prefix, /^pr-review-http(?:-[a-z0-9]+)?$/);
|
|
const project = `project-${prefix}`;
|
|
const owned = `annotation-task-${prefix}-annotate-primary`;
|
|
const other = `annotation-task-${prefix}-annotate-delayed`;
|
|
const output = "local-data/verification/pr-review-20260918";
|
|
mkdirSync(output, { recursive: true });
|
|
const credentials = JSON.parse(
|
|
readFileSync("local-data/verification/annotation-workspace-layout/local-auth.json", "utf8"),
|
|
);
|
|
const server = await createServer({
|
|
logLevel: "silent",
|
|
// Freeze file watching during acceptance, including docs written by other agents.
|
|
server: {
|
|
host: "127.0.0.1",
|
|
port: 5174,
|
|
strictPort: true,
|
|
hmr: false,
|
|
watch: { ignored: ["**/*"] },
|
|
allowedHosts: ["annotation-http.test"],
|
|
},
|
|
});
|
|
await server.listen();
|
|
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", "--host-resolver-rules=MAP annotation-http.test 127.0.0.1"],
|
|
});
|
|
const http = "http://annotation-http.test:5174";
|
|
const secure = "http://127.0.0.1:5174";
|
|
const path = (mode, task = "") =>
|
|
`/plugins/annotation-qa/workflows/all/${mode}?projectId=${project}${task ? `&taskId=${task}` : ""}`;
|
|
const button = (page, name) => page.getByRole("button", { name, exact: true });
|
|
const claims = [];
|
|
const errors = [];
|
|
let page;
|
|
async function login(context, base) {
|
|
const tab = await context.newPage();
|
|
tab.setDefaultTimeout(20000);
|
|
tab.on("pageerror", (e) => errors.push(e.message));
|
|
tab.on("request", (r) => {
|
|
if (r.url().endsWith("/random-claims") && r.method() === "POST") claims.push(r.postDataJSON());
|
|
});
|
|
await tab.goto(base);
|
|
assert.equal(
|
|
await tab.evaluate(
|
|
async (data) =>
|
|
(
|
|
await fetch("/api/auth/login", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify(data),
|
|
})
|
|
).status,
|
|
credentials,
|
|
),
|
|
200,
|
|
);
|
|
return tab;
|
|
}
|
|
const read = async (page, task) =>
|
|
page.evaluate(async (id) => {
|
|
const r = await fetch(`/api/human-items/${id.replace("annotation-task-", "human-item-")}`);
|
|
if (!r.ok) throw new Error(`readback ${r.status}`);
|
|
return (await r.json()).item;
|
|
}, task);
|
|
const bench = (page) => page.getByTestId("capture-annotation-workbench");
|
|
const post = (page, suffix) =>
|
|
page.waitForResponse((r) => r.url().endsWith(suffix) && r.request().method() === "POST", { timeout: 60000 });
|
|
const media = (page) =>
|
|
page.waitForFunction(
|
|
() =>
|
|
document.querySelectorAll("video").length === 2 &&
|
|
[...document.querySelectorAll("video")].every((v) => v.readyState >= 2),
|
|
null,
|
|
{ timeout: 60000 },
|
|
);
|
|
const boundaries = (item) =>
|
|
item.subject.payload.annotationWorkspace.sliceSegments.map(
|
|
({ id, start, end, startTimestampUs, endTimestampUs }) => ({ id, start, end, startTimestampUs, endTimestampUs }),
|
|
);
|
|
try {
|
|
const context = await browser.newContext({ viewport: { width: 1440, height: 900 } });
|
|
page = await login(context, http);
|
|
assert.deepEqual(await page.evaluate(() => ({ secure: isSecureContext, locks: typeof navigator.locks })), {
|
|
secure: false,
|
|
locks: "undefined",
|
|
});
|
|
// A slow project read must not erase the bookmarked identity in the fallback link.
|
|
let resumeProjects;
|
|
const projectGate = new Promise((resolve) => {
|
|
resumeProjects = resolve;
|
|
});
|
|
await page.route(/\/(?:claim-projects|projects)(?:\?.*)?$/, async (route) => {
|
|
await projectGate;
|
|
await route.continue();
|
|
});
|
|
await page.goto(http + path("work", owned));
|
|
const earlyFallback = page.getByRole("link", { name: "返回手动工作台", exact: true });
|
|
await earlyFallback.waitFor();
|
|
const earlyHref = await earlyFallback.getAttribute("href");
|
|
resumeProjects();
|
|
assert.equal(earlyHref, path("annotate", owned));
|
|
await earlyFallback.click();
|
|
console.log("PASS slow project loading preserves bookmarked project/task in immediate manual return");
|
|
await page.goto(http + path("annotate", owned));
|
|
await bench(page).waitFor();
|
|
await page.getByText("当前连接使用手动模式;独立工作页和连续领取需安全连接。", { exact: true }).waitFor();
|
|
assert.ok(await button(page, "开始工作").isDisabled());
|
|
const initial = await read(page, owned);
|
|
assert.equal(
|
|
(initial.subject.payload.annotationWorkspace.segments || []).length,
|
|
0,
|
|
"requires empty descriptions before discard",
|
|
);
|
|
await media(page);
|
|
await button(page, "舍弃片段").click();
|
|
await page.getByRole("textbox", { name: "舍弃原因", exact: true }).fill("HTTP零描述回归");
|
|
let response = post(page, "/slice-dispositions");
|
|
await button(page, "确认舍弃").click();
|
|
const discardedResponse = await response;
|
|
assert.ok(discardedResponse.ok());
|
|
const discarded = await read(page, owned);
|
|
assert.equal(discarded.version, initial.version + 1);
|
|
assert.deepEqual(boundaries(discarded), boundaries(initial));
|
|
assert.equal(discarded.assignedUserId, initial.assignedUserId);
|
|
assert.ok(
|
|
discarded.subject.payload.annotationWorkspace.sliceSegments.some(
|
|
(s) => s.deliveryDisposition === "discard" && s.discardReason === "HTTP零描述回归",
|
|
),
|
|
);
|
|
await page.reload();
|
|
await bench(page).waitFor();
|
|
await button(page, "恢复为保留").click();
|
|
response = post(page, "/slice-dispositions");
|
|
await button(page, "确认恢复").click();
|
|
assert.ok((await response).ok());
|
|
await page.reload();
|
|
await bench(page).waitFor();
|
|
const restored = await read(page, owned);
|
|
assert.equal(restored.version, initial.version + 2);
|
|
assert.equal((restored.subject.payload.annotationWorkspace.segments || []).length, 0);
|
|
assert.deepEqual(boundaries(restored), boundaries(initial));
|
|
assert.equal(restored.assignedUserId, initial.assignedUserId);
|
|
assert.ok(
|
|
restored.subject.payload.annotationWorkspace.sliceSegments.every((s) => s.deliveryDisposition !== "discard"),
|
|
);
|
|
console.log("PASS empty descriptions → discard → refresh → restore → refresh; boundaries/ownership preserved");
|
|
|
|
await page.goto(http + path("work", owned));
|
|
const fallback = page.getByRole("link", { name: "返回手动工作台", exact: true });
|
|
await fallback.waitFor();
|
|
assert.equal(await bench(page).count(), 0);
|
|
assert.equal(await fallback.getAttribute("href"), path("annotate", owned));
|
|
await fallback.click();
|
|
await bench(page).waitFor();
|
|
assert.equal((await read(page, owned)).version, restored.version);
|
|
assert.equal(claims.length, 0);
|
|
console.log("PASS direct HTTP /work returns explicitly to same project/task without release or claim");
|
|
|
|
// Unknown response: let the real server allocate, lose only its response, retry.
|
|
let allocation;
|
|
await page.route(
|
|
"**/random-claims",
|
|
async (route) => {
|
|
const r = await route.fetch({
|
|
url: route.request().url().replace(http, secure),
|
|
headers: { ...(await route.request().allHeaders()), host: "annotation-http.test:5174" },
|
|
});
|
|
assert.ok(r.ok());
|
|
allocation = await r.json();
|
|
await route.abort("failed");
|
|
},
|
|
{ times: 1 },
|
|
);
|
|
await button(page, "随机领取任务").click();
|
|
await page
|
|
.getByTestId("project-random-claim")
|
|
.getByRole("status")
|
|
.filter({ hasText: /Failed to fetch|领取结果暂未确认/ })
|
|
.waitFor();
|
|
assert.equal(claims.length, 1);
|
|
assert.ok(allocation.task.id.startsWith(`annotation-task-${prefix}-pool-`));
|
|
await button(page, "随机领取任务").click();
|
|
await page.waitForURL((u) => u.searchParams.get("taskId") === allocation.task.id);
|
|
await bench(page).waitFor();
|
|
assert.equal(claims.length, 2);
|
|
assert.equal(claims[0].requestId, claims[1].requestId);
|
|
assert.equal((await read(page, allocation.task.id)).assignedUserId, initial.assignedUserId);
|
|
console.log("PASS real HTTP manual claim and unknown response replay use the same requestId and one allocation");
|
|
|
|
// The original manual workbench can save and submit with the real API.
|
|
await media(page);
|
|
await button(page, "新增动作描述").click();
|
|
await bench(page).focus();
|
|
for (let n = 0; n < 6; n++) {
|
|
await page.keyboard.press("ArrowRight");
|
|
await page.waitForTimeout(100);
|
|
}
|
|
await button(page, "确认结束点").click();
|
|
await page.getByRole("textbox", { name: "动作描述", exact: true }).fill("HTTP手动标注保存与送检");
|
|
response = post(page, "/annotation-confirmations");
|
|
await button(page, "确认保存描述").click();
|
|
assert.ok((await response).ok());
|
|
const saved = await read(page, allocation.task.id);
|
|
assert.ok(saved.subject.payload.annotationWorkspace.segments.some((s) => s.description === "HTTP手动标注保存与送检"));
|
|
await page.reload();
|
|
await bench(page).waitFor();
|
|
await button(page, "完成任务并提交质检").click();
|
|
response = post(page, "/combined-submissions");
|
|
await button(page, "确认送检").click();
|
|
assert.ok((await response).ok());
|
|
let completed;
|
|
for (let n = 0; n < 180; n++) {
|
|
completed = await read(page, allocation.task.id);
|
|
if (completed.currentStageId === "quality_review") break;
|
|
await page.waitForTimeout(1000);
|
|
}
|
|
assert.equal(completed.currentStageId, "quality_review");
|
|
assert.equal(claims.length, 2, "HTTP manual completion must not auto-claim");
|
|
console.log("PASS HTTP manual save → reload/My Tasks → submit → authoritative quality_review without automatic next");
|
|
|
|
// Hold an actual allocation response while the user switches task.
|
|
await page.goto(http + path("annotate", owned));
|
|
await bench(page).waitFor();
|
|
let deliver, started;
|
|
const held = new Promise((resolve) => {
|
|
started = resolve;
|
|
});
|
|
const release = new Promise((resolve) => {
|
|
deliver = resolve;
|
|
});
|
|
let lateTask;
|
|
await page.route(
|
|
"**/random-claims",
|
|
async (route) => {
|
|
const r = await route.fetch({
|
|
url: route.request().url().replace(http, secure),
|
|
headers: { ...(await route.request().allHeaders()), host: "annotation-http.test:5174" },
|
|
});
|
|
lateTask = (await r.json()).task.id;
|
|
started();
|
|
await release;
|
|
await route.fulfill({ response: r });
|
|
},
|
|
{ times: 1 },
|
|
);
|
|
await button(page, "随机领取任务").click();
|
|
await held;
|
|
await page.locator(`article[data-task-id="${other}"]`).getByTestId("annotation-task-card").click();
|
|
await page.waitForURL((u) => u.searchParams.get("taskId") === other);
|
|
deliver();
|
|
await page.waitForResponse((r) => r.url().endsWith("/random-claims"));
|
|
await bench(page).waitFor();
|
|
assert.equal(new URL(page.url()).searchParams.get("taskId"), other);
|
|
assert.equal((await read(page, lateTask)).assignedUserId, initial.assignedUserId);
|
|
console.log("PASS late HTTP allocation retains server ownership without replacing the selected task");
|
|
await context.close();
|
|
|
|
// A real secure-context lock in another tab must block manual fallback.
|
|
const locksContext = await browser.newContext({ viewport: { width: 1440, height: 900 } });
|
|
page = await login(locksContext, secure);
|
|
assert.equal(await page.evaluate(() => isSecureContext && !!navigator.locks), true);
|
|
await page.goto(secure + path("annotate", owned));
|
|
await bench(page).waitFor();
|
|
await button(page, "开始工作").click();
|
|
await page.waitForURL(/\/work\?/);
|
|
await bench(page).waitFor();
|
|
await button(page, "完成当前任务后停止").click();
|
|
const second = await locksContext.newPage();
|
|
await second.goto(secure + path("annotate", other));
|
|
await bench(second).waitFor();
|
|
await second.getByText("此账号在本项目已有活动工作页。请切换回该页面继续,避免同时编辑。", { exact: true }).waitFor();
|
|
assert.ok(await button(second, "随机领取任务").isDisabled());
|
|
assert.ok(await button(second, "开始工作").isDisabled());
|
|
// The query may race another owner; acquire must still fail on the actual lock.
|
|
const racing = await locksContext.newPage();
|
|
await racing.addInitScript(() => {
|
|
navigator.locks.query = async () => ({ held: [], pending: [] });
|
|
});
|
|
let racingClaims = 0;
|
|
racing.on("request", (r) => {
|
|
if (r.method() === "POST" && r.url().endsWith("/random-claims")) racingClaims++;
|
|
});
|
|
await racing.goto(secure + path("annotate", other));
|
|
await bench(racing).waitFor();
|
|
await button(racing, "随机领取任务").click();
|
|
await racing.getByText("此账号在本项目已有活动工作页。请切换回该页面继续,避免同时编辑。", { exact: true }).waitFor();
|
|
assert.equal(racingClaims, 0);
|
|
await racing.close();
|
|
console.log("PASS actual lock-acquisition conflict blocks allocation even when query misses the owner");
|
|
await page.close();
|
|
await button(second, "开始工作").waitFor();
|
|
await second.waitForFunction(() =>
|
|
[...document.querySelectorAll("button")].some((b) => b.textContent === "开始工作" && !b.disabled),
|
|
);
|
|
await button(second, "开始工作").click();
|
|
await second.waitForURL(/\/work\?/);
|
|
await bench(second).waitFor();
|
|
console.log("PASS real secure Web Locks two-tab exclusion and owner-close recovery");
|
|
await locksContext.close();
|
|
|
|
// Fault injection only for the browser API error path, never used to pass HTTP.
|
|
const failedContext = await browser.newContext();
|
|
await failedContext.addInitScript(() => {
|
|
navigator.locks.request = () => Promise.reject(new DOMException("Injected coordination failure", "AbortError"));
|
|
});
|
|
page = await login(failedContext, secure);
|
|
await page.goto(secure + path("annotate", owned));
|
|
await bench(page).waitFor();
|
|
const beforeError = claims.length;
|
|
await button(page, "随机领取任务").click();
|
|
await page.getByText("页面协调失败,尚未领取新任务。请刷新后重试;不会绕过页面锁。", { exact: true }).waitFor();
|
|
assert.equal(claims.length, beforeError);
|
|
assert.equal(
|
|
await page.getByText("当前连接使用手动模式;独立工作页和连续领取需安全连接。", { exact: true }).count(),
|
|
0,
|
|
);
|
|
console.log("PASS supported-but-failed lock coordination does not fall back or allocate");
|
|
assert.deepEqual(errors, []);
|
|
await page.screenshot({ path: `${output}/http-manual-final.png` });
|
|
console.log("PASS all HTTP manual/secure coordination acceptance");
|
|
} catch (error) {
|
|
if (page && !page.isClosed()) {
|
|
await page.screenshot({ path: `${output}/http-manual-error.png` });
|
|
console.error((await page.locator("body").innerText()).slice(-9000));
|
|
}
|
|
throw error;
|
|
} finally {
|
|
await browser.close();
|
|
await server.close();
|
|
}
|