droboticflow/tests/browser/annotation-source-validation-replica.mjs

168 lines
8.1 KiB
JavaScript

import { browserExecutableOptions } from "../../scripts/lib/browser-executable.mjs";
import assert from "node:assert/strict";
import fs from "node:fs";
import { createServer } from "vite";
import { chromium } from "playwright";
const api = process.env.SOURCE_VALIDATION_API;
assert.match(api, /^http:\/\/127\.0\.0\.1:\d+$/);
const output = "local-data/test-results/source-validation-browser";
fs.mkdirSync(output, { recursive: true });
const vite = await createServer({
cacheDir: "local-data/test-results/vite-annotation-source-validation-replica",
server: {
host: "127.0.0.1",
port: 5208,
strictPort: true,
hmr: false,
watch: { ignored: ["**/*"] },
proxy: { "/api": { target: api }, "/__fixture": { target: api } },
},
});
let browser, page;
const report = { pageErrors: [], writes: [], cases: [] };
try {
await vite.listen();
browser = await chromium.launch({
...browserExecutableOptions(),
headless: true,
args: ["--no-proxy-server"],
});
const context = await browser.newContext({ viewport: { width: 1600, height: 1000 } });
page = await context.newPage();
page.setDefaultTimeout(20000);
page.on("pageerror", (error) => report.pageErrors.push(error.message));
await page.route("**/*", (route) => {
const request = route.request(),
url = new URL(request.url());
if (url.hostname !== "127.0.0.1") return route.abort();
if (url.pathname.includes("/worker-api/")) return route.fulfill({ json: { status: "ok" } });
if (!["GET", "HEAD"].includes(request.method())) report.writes.push(url.pathname);
return route.continue();
});
const base = "http://127.0.0.1:5208";
const path = "/plugins/annotation-qa/workflows/all/annotate?projectId=draft-project&taskId=draft-task";
await page.goto(base + path);
const notice = page.getByTestId("frame-timeline-status");
await notice.getByText(/418451.*gyr/).waitFor();
assert.match(await notice.innerText(), /数据提供方.*管理员重新校验/);
assert.equal(await notice.getAttribute("role"), "alert");
assert.equal(await notice.getByRole("button", { name: "重新校验", exact: true }).count(), 0);
await notice.getByRole("button", { name: "刷新状态", exact: true }).click();
await notice.getByText(/418451.*gyr/).waitFor();
assert.equal(report.writes.filter((path) => path.endsWith("source-validation-retries")).length, 0);
report.cases.push(
"annotator sees exact source fault and repair responsibility; refresh does not enqueue revalidation",
);
await page.screenshot({ path: output + "/annotator-source-failed.png", fullPage: true });
await page.goto(base + "/plugins/annotation-qa/workflows/all/work?projectId=draft-project&taskId=draft-task");
await page
.getByRole("combobox", { name: "个人任务切换", exact: true })
.getByRole("option", { name: /源数据异常/ })
.waitFor({ state: "attached" });
await page
.getByRole("region", { name: "恢复工作会话" })
.getByRole("button", { name: /capture-custom/ })
.click();
await notice.getByText(/418451.*gyr/).waitFor();
await page.screenshot({ path: output + "/owned-source-failed.png", fullPage: true });
report.cases.push("owned task remains selectable for inspection and its buffer option shows source failure");
await context.addCookies([{ name: "source-fixture-role", value: "admin", url: base }]);
await page.goto(base + path + "&pool=all");
await page.getByTestId("human-task-pool-all").click();
const retry = notice.getByRole("button", { name: "重新校验", exact: true });
await retry.waitFor();
page.once("dialog", (dialog) => dialog.dismiss());
await retry.click();
assert.equal(report.writes.filter((path) => path.endsWith("source-validation-retries")).length, 0);
page.once("dialog", (dialog) => dialog.accept());
await retry.click();
await notice.getByText("源数据校验中", { exact: true }).waitFor();
assert.equal(await notice.getAttribute("role"), "status");
assert.equal(await notice.getByRole("button", { name: "重新校验", exact: true }).count(), 0);
const start = await context.request.post(base + "/__fixture/validate");
assert.equal(start.status(), 202);
let validation;
for (let attempt = 0; attempt < 100; attempt++) {
const response = await context.request.get(base + "/api/plugins/annotation-qa/tasks/draft-task?view=status");
assert.equal(response.status(), 200);
validation = (await response.json()).sourceValidation;
if (validation?.status === "valid") break;
await new Promise((resolve) => setTimeout(resolve, 100));
}
assert.equal(validation?.status, "valid", "actual source validation completed");
const readyFrame = await context.request.get(
base + "/api/plugins/annotation-qa/tasks/draft-task/frame-timeline?camera=left&encoding=compact",
);
assert.equal(readyFrame.status(), 200, "validated source exposes its frame timeline");
await notice.getByRole("button", { name: "刷新状态", exact: true }).click();
await notice.waitFor({ state: "detached" });
await page.reload();
await page.getByTestId("human-task-pool-all").click();
await page.getByTestId("capture-annotation-workbench").waitFor();
await page.waitForFunction(() => !document.querySelector('[data-testid="frame-timeline-status"]'));
assert.equal(report.writes.filter((path) => path.endsWith("source-validation-retries")).length, 1);
assert.equal(
report.writes.some((path) => /drafts|combined-submissions|slice-confirmations/.test(path)),
false,
);
assert.deepEqual(report.pageErrors, []);
report.cases.push(
"admin confirmation, queued status, actual source validation, refresh and reload restore workspace without annotation writes",
);
await page.screenshot({ path: output + "/source-recovered.png", fullPage: true });
let generationRecovered = false;
let generationReads = 0;
await page.route("**/api/plugins/annotation-qa/tasks/draft-task?*", async (route) => {
const url = new URL(route.request().url());
if (url.searchParams.get("view") !== "status") return route.continue();
generationReads++;
const response = await route.fetch();
const data = await response.json();
await route.fulfill({ response, json: { ...data, sliceStatus: generationRecovered ? "completed" : "failed" } });
});
await page.route("**/api/plugins/annotation-qa/tasks/draft-task", async (route) => {
const response = await route.fetch();
const data = await response.json();
data.humanItem.subject.payload.sliceWorkflow = {
status: generationRecovered ? "completed" : "failed",
phase: "annotate",
error: "合成生成失败",
};
data.editorState = { ...data.editorState, phase: "annotate", canConfirmDescriptions: generationRecovered };
await route.fulfill({ response, json: data });
});
await page.reload();
await page.getByTestId("human-task-pool-all").click();
const generationFault = page
.getByTestId("capture-annotation-workbench")
.getByText("切片生成失败:合成生成失败", { exact: true });
await generationFault.waitFor();
await page.screenshot({ path: output + "/generation-failed.png", fullPage: true });
generationRecovered = true;
await generationFault.waitFor({ state: "detached", timeout: 20000 });
assert.ok(generationReads > 0);
assert.equal(await page.getByText("切片生成失败:合成生成失败", { exact: true }).count(), 0);
assert.equal(report.writes.filter((path) => path.endsWith("source-validation-retries")).length, 1);
assert.equal(
report.writes.some((path) => /drafts|slice-confirmations|combined-submissions/.test(path)),
false,
);
assert.deepEqual(report.pageErrors, []);
await page.screenshot({ path: output + "/generation-recovered.png", fullPage: true });
report.cases.push(
"synthetic failed generation automatically observes external completion without regeneration or annotation writes",
);
console.log(JSON.stringify(report));
} catch (error) {
if (page) {
await page.screenshot({ path: output + "/failure.png", fullPage: true }).catch(() => {});
console.error((await page.locator("body").innerText()).slice(-2800));
}
throw error;
} finally {
fs.writeFileSync(output + "/report.json", JSON.stringify(report, null, 2));
await browser?.close();
await vite.close();
}