droboticflow/tests/browser/project-dictionary-reuse.mjs

134 lines
6.7 KiB
JavaScript

import { chromium } from "playwright";
import assert from "node:assert/strict";
import fs from "node:fs/promises";
const base = process.env.DICTIONARY_TEST_URL || "http://127.0.0.1:5178",
out = process.env.DICTIONARY_TEST_OUTPUT || "verification/dictionary-reuse";
await fs.mkdir(out, { recursive: true });
const browser = await chromium.launch({
...(process.env.BROWSER_EXECUTABLE ? { executablePath: process.env.BROWSER_EXECUTABLE } : { channel: "chrome" }),
headless: true,
});
try {
for (const kind of ["slice", "action", "discard_reason"]) {
const page = await browser.newPage({ viewport: { width: 1280, height: 900 } });
const errors = [];
page.on("pageerror", (e) => errors.push(e.message));
page.on("dialog", (d) => d.accept());
let creates = 0,
failSelection = false;
let enabled = ["disabled"];
const labels = [
{ id: "existing", kind, name: "表面清洁", description: "表面清洁", status: "active", version: 3 },
{ id: "disabled", kind, name: "停用词条", status: "inactive", version: 1 },
];
await page.route("**/api/**", async (route) => {
const u = new URL(route.request().url()),
p = u.pathname,
m = route.request().method();
if (!p.startsWith("/api/")) return route.continue();
const body = m === "GET" ? {} : route.request().postDataJSON();
if (p === "/api/annotation-labels" && m === "GET")
return route.fulfill({
json: { items: labels.filter((x) => x.kind === (u.searchParams.get("kind") || "action")) },
});
if (p === "/api/annotation-labels" && m === "POST") {
creates++;
const entry = { ...body, id: "new-" + creates, version: 1 };
labels.push(entry);
return route.fulfill({ status: 201, json: entry });
}
if (p.startsWith("/api/projects/")) {
if (m === "PUT") {
if (failSelection) return route.fulfill({ status: 400, json: { error: "项目配置保存失败,请重试" } });
enabled = body.labelIds;
}
return route.fulfill({
json: { labelIds: enabled, items: labels.filter((x) => enabled.includes(x.id) && x.status === "active") },
});
}
return route.fulfill({ status: 404, json: { error: "unexpected API" } });
});
await page.route("**/__dictionary_reuse", (r) =>
r.fulfill({
contentType: "text/html; charset=utf-8",
body: `<!doctype html><html><head><meta charset="utf-8"><title>项目字典复用验证</title></head><body><div id="root"></div><script type="module">
import F from '/@react-refresh';F.injectIntoGlobalHook(window);window.$RefreshReg$=()=>{};window.$RefreshSig$=()=>x=>x;window.__vite_plugin_react_preamble_installed__=true;await import('/src/index.css');const R=await import('/node_modules/.vite/deps/react.js'),React=R.default||R,D=await import('/node_modules/.vite/deps/react-dom_client.js');const {AnnotationLabelsPanel}=await import('/src/components/AnnotationLabelsPanel.tsx');(D.createRoot||D.default.createRoot)(document.getElementById('root')).render(React.createElement(AnnotationLabelsPanel,{projectId:'project',canManage:true,kind:${JSON.stringify(kind)}}));</script></body></html>`,
}),
);
await page.goto(base + "/__dictionary_reuse");
assert.equal(await page.title(), "项目字典复用验证");
const input = page.getByRole("textbox").first(),
add = page.getByRole("button", {
name: kind === "slice" ? "添加名称" : kind === "discard_reason" ? "添加原因" : "添加描述",
exact: true,
});
await input.fill("表面清洁");
await add.click();
await page.getByText("表面清洁", { exact: true }).waitFor();
assert.deepEqual(enabled, ["existing"]);
assert.equal(creates, 0);
assert.equal(labels[0].version, 3);
await input.fill("表面清洁");
await add.click();
await page.getByText("本项目已存在同名词条,无需重复添加", { exact: true }).waitFor();
assert.equal(await page.getByText(/已成功保存/).count(), 0);
assert.equal(creates, 0);
await page
.getByRole("button", {
name: kind === "slice" ? "删除切片名称" : kind === "discard_reason" ? "删除舍弃原因" : "删除描述",
exact: true,
})
.click();
await page.getByText("表面清洁", { exact: true }).waitFor({ state: "hidden" });
await input.fill("表面清洁");
await add.click();
await page.getByText("表面清洁", { exact: true }).waitFor();
assert.deepEqual(enabled, ["existing"]);
assert.equal(creates, 0);
assert.equal(await page.getByText("表面清洁", { exact: true }).count(), 1);
await page.reload();
await page.getByText("表面清洁", { exact: true }).waitFor();
await page
.getByRole("button", {
name: kind === "slice" ? "编辑切片名称" : kind === "discard_reason" ? "编辑舍弃原因" : "编辑描述",
exact: true,
})
.click();
await page.getByRole("button", { name: "保存修改", exact: true }).click();
await add.waitFor();
assert.equal(creates, 0);
assert.deepEqual(enabled, ["existing"]);
await input.fill("停用词条");
await add.click();
await page.getByText("停用词条", { exact: true }).waitFor();
assert.equal(creates, 1);
assert.equal(labels.find((x) => x.id === "disabled").status, "inactive");
assert.equal(await page.getByRole("checkbox").count(), 0);
assert.equal(await page.getByRole("button", { name: /启用|停用/ }).count(), 0);
failSelection = true;
await input.fill("新词条");
await add.click();
await page.getByText("项目配置保存失败,请重试", { exact: true }).waitFor();
assert.equal(creates, 2);
assert.equal(await page.getByText(/已成功保存/).count(), 0);
failSelection = false;
await add.click();
await page.getByText("新词条", { exact: true }).waitFor();
assert.equal(creates, 2, "retry reuses the previously created term");
assert.deepEqual(enabled, ["existing", "new-1", "new-2"]);
await page.getByText("表面清洁", { exact: true }).click();
await input.fill("当前项目新名称");
await page.getByRole("button", { name: "保存修改", exact: true }).click();
await page.getByText("当前项目新名称", { exact: true }).waitFor();
assert.equal(labels.find((x) => x.id === "existing").name, "表面清洁");
assert.ok(!enabled.includes("existing"));
assert.equal(creates, 3);
assert.deepEqual(errors, []);
await page.screenshot({ path: out + "/" + kind + ".png", fullPage: true });
console.log("PASS", kind, "reuse, remove/re-add, duplicate, disabled, unchanged edit, partial-save retry");
await page.close();
}
} finally {
await browser.close();
}