droboticflow/tests/browser/slice-phase-replica.mjs

191 lines
8.6 KiB
JavaScript

import fs from "node:fs";
import assert from "node:assert/strict";
import { createRequire } from "node:module";
const source = process.cwd();
const require = createRequire(source + "/package.json"),
{ createServer } = require("vite"),
{ chromium } = require("playwright");
const api = process.env.REWORK_BOUNDARY_API,
root = new URL("../../local-data/verification/slice-phase/", import.meta.url),
report = { cases: [], errors: [], keys: [], writes: [] };
assert.match(api, /^http:\/\/127\.0\.0\.1:\d+$/);
fs.mkdirSync(root, { recursive: true });
const server = await createServer({
root: source,
configFile: "tests/browser/vite.platform-feedback.config.ts",
server: { host: "127.0.0.1", port: 5204, strictPort: true, proxy: { "/api": { target: api, changeOrigin: true } } },
});
let browser, page;
try {
await server.listen();
browser = await chromium.launch({
executablePath: "/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge",
headless: true,
args: ["--no-proxy-server"],
});
page = await browser.newPage({ viewport: { width: 1600, height: 1100 } });
page.setDefaultTimeout(10000);
page.on("pageerror", (e) => report.errors.push(e.message));
page.on("request", (r) => {
if (!["GET", "HEAD"].includes(r.method())) report.writes.push(new URL(r.url()).pathname);
});
await page.route("**/*", (r) => {
const u = new URL(r.request().url());
if (u.hostname !== "127.0.0.1") return r.abort();
if (u.pathname.includes("/worker-api/")) return r.fulfill({ json: { status: "ok" } });
return r.continue();
});
for (const status of ["empty", "pending", "draft"]) {
await fetch(api + "/__boundary/reset?status=" + status);
await page.goto(
"http://127.0.0.1:5204/plugins/annotation-qa/workflows/all/work?projectId=draft-project&taskId=draft-task",
);
await page.getByRole("button", { name: "继续工作", exact: true }).click();
const bench = page.getByTestId("capture-annotation-workbench"),
del = bench.getByRole("button", { name: "删除切片", exact: true });
await del.first().waitFor();
assert.equal(await del.count(), 1);
await page.evaluate(() => {
window.keyProbe = [];
document.addEventListener("keydown", (e) =>
setTimeout(
() =>
window.keyProbe.push({
key: e.key,
code: e.code,
isComposing: e.isComposing,
defaultPrevented: e.defaultPrevented,
}),
0,
),
);
});
const slider = bench.locator("input[type=range]").first(),
box = await slider.boundingBox();
await page.mouse.click(box.x + box.width * 0.15, box.y + box.height / 2);
await page.waitForTimeout(300);
await bench.focus();
await page.keyboard.press("c");
await page.waitForTimeout(500);
const result = {
status,
afterC: await del.count(),
visibleConflictLists: await page.getByTestId("slice-edit-conflict-list").count(),
receivedKeys: await page.evaluate(() => window.keyProbe),
text: await bench.innerText(),
};
report.cases.push({ ...result, text: undefined });
if (status === "empty") assert.equal(result.afterC, 2, "no actions: C must split");
else {
assert.equal(result.visibleConflictLists, 1, "pending actions must show a visible conflict");
await bench.focus();
await page.keyboard.press("Enter");
await del.nth(1).waitFor();
assert.equal(await del.count(), 2);
await page.getByRole("button", { name: "撤销", exact: true }).click();
await page.waitForTimeout(500);
assert.equal(await del.count(), 1);
await page.waitForTimeout(1100);
await page.getByRole("button", { name: "恢复", exact: true }).click();
await del.nth(1).waitFor();
assert.equal(await del.count(), 2);
await page.reload();
await page.getByRole("button", { name: "继续工作", exact: true }).click();
await del.nth(1).waitFor();
assert.equal(await del.count(), 2);
}
}
const taskURL =
"http://127.0.0.1:5204/plugins/annotation-qa/workflows/all/work?projectId=draft-project&taskId=draft-task";
const state = async () => (await (await fetch(api + "/__boundary/state")).json()).subject.payload.annotationWorkspace;
const load = async (shape) => {
await fetch(api + "/__boundary/reset?status=pending&shape=" + shape);
await page.goto(taskURL);
await page.getByRole("button", { name: "继续工作", exact: true }).click();
await page.getByTestId("capture-annotation-workbench").waitFor();
};
const bench = page.getByTestId("capture-annotation-workbench");
const labels = () => bench.getByRole("combobox").first().locator("option").allTextContents();
const settle = async () => {
const list = page.getByTestId("slice-edit-conflict-list");
if (await list.count()) await list.getByRole("button", { name: /Enter 应用/ }).click();
await page.waitForTimeout(500);
};
await load("two");
await bench.getByRole("button", { name: "拖动切片边界", exact: true }).first().waitFor();
const before = await labels();
const handle = await bench.getByRole("button", { name: "拖动切片边界", exact: true }).first().boundingBox();
await page.mouse.move(handle.x + handle.width / 2, handle.y + handle.height / 2);
await page.mouse.down();
await page.mouse.move(handle.x + handle.width / 2 + 100, handle.y + handle.height / 2, { steps: 8 });
await page.mouse.up();
await page.waitForTimeout(200);
await settle();
const after = await labels();
assert.notDeepEqual(after, before, "pending actions: drag must apply after confirmation");
await page.getByRole("button", { name: "撤销", exact: true }).click();
await page.waitForTimeout(500);
assert.deepEqual(await labels(), before);
await page.waitForTimeout(1100);
await page.getByRole("button", { name: "恢复", exact: true }).click();
await page.waitForTimeout(500);
assert.deepEqual(await labels(), after);
assert.equal((await state()).segments.length >= 2, true);
report.cases.push({ scenario: "pending-drag-undo-redo" });
await page.waitForTimeout(1100);
await bench.getByRole("button", { name: "删除切片", exact: true }).first().click();
await page.waitForTimeout(200);
await settle();
assert.equal(await bench.getByRole("button", { name: "删除切片", exact: true }).count(), 1);
await page.getByRole("button", { name: "撤销", exact: true }).click();
await page.waitForTimeout(500);
assert.deepEqual(await labels(), after);
await page.waitForTimeout(1100);
await page.getByRole("button", { name: "恢复", exact: true }).click();
await page.waitForTimeout(500);
assert.equal(await bench.getByRole("button", { name: "删除切片", exact: true }).count(), 1);
report.cases.push({ scenario: "pending-delete-undo-redo" });
await load("rework");
const rail = page.getByTestId("rework-opinion-rail");
await rail.waitFor();
assert.equal(
await rail.locator('[data-rework-state="red"]').count(),
2,
"derived slice status must stay red on load",
);
const block = bench.locator("[data-annotation-block]").first();
await block.click();
const editor = bench.getByRole("textbox", { name: "动作描述", exact: true });
await editor.waitFor();
await editor.fill("更正描述");
await page.waitForTimeout(300);
assert.equal(
await rail.locator('[data-rework-state="gray"]').count(),
2,
"unsaved editor draft updates opinions immediately",
);
await editor.fill("原描述");
await page.waitForTimeout(300);
assert.equal(await rail.locator('[data-rework-state="red"]').count(), 2, "reverted editor draft restores red");
await bench.getByRole("combobox").first().selectOption("discard");
await page.getByTestId("slice-disposition-panel").waitFor();
assert.equal(await rail.count(), 1);
assert.equal(await page.getByTestId("discard-review-sidebar").count(), 1);
assert.equal(await page.getByTestId("annotation-description-editor").count(), 0);
const side = await page.getByTestId("discard-review-sidebar").boundingBox();
const video = await page.getByTestId("capture-video-panel").boundingBox();
assert.ok(side.x >= video.x + video.width - 2, "discard opinions remain in right sidebar");
report.cases.push({ scenario: "live-draft-status-and-discard-evidence" });
assert.deepEqual(report.errors, []);
console.log(JSON.stringify(report));
} catch (e) {
report.failure = e.message;
if (page) fs.writeFileSync(new URL("failure.txt", root), await page.locator("body").innerText());
throw e;
} finally {
fs.writeFileSync(new URL("c-page-report.json", root), JSON.stringify(report, null, 2));
await browser?.close();
await server.close();
}