forked from longtao.wu/droboticflow
107 lines
5.3 KiB
JavaScript
107 lines
5.3 KiB
JavaScript
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({
|
|
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({
|
|
executablePath: process.env.BROWSER_EXECUTABLE || "/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge",
|
|
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 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 });
|
|
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();
|
|
}
|