droboticflow/tests/browser/discard-reason-dictionary.mjs

108 lines
5.4 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { chromium } from "playwright";
import assert from "node:assert/strict";
import fs from "node:fs/promises";
const base = process.env.DISCARD_TEST_URL || "http://127.0.0.1:5175",
out = "/tmp/discard-dictionary-browser";
await fs.mkdir(out, { recursive: true });
const browser = await chromium.launch({ channel: "chrome", headless: true });
try {
const page = await browser.newPage({ viewport: { width: 1440, height: 1000 } });
const errors = [];
page.on("pageerror", (e) => errors.push(e.message));
let labels = [
{ id: "adjust", name: "调整头环", code: "adjust", version: 1, kind: "discard_reason", status: "active" },
{
id: "other",
name: "其他",
code: "other",
version: 1,
kind: "discard_reason",
status: "active",
requiresNote: true,
},
],
enabled = ["adjust", "other"];
await page.route("**/api/**", async (route) => {
const path = new URL(route.request().url()).pathname;
if (!path.startsWith("/api/")) return route.continue();
const method = route.request().method(),
body = method === "GET" ? {} : route.request().postDataJSON();
if (path.endsWith("/discard-reason-labels")) {
if (method === "PUT") enabled = body.labelIds;
return route.fulfill({
json: { labelIds: enabled, items: labels.filter((x) => enabled.includes(x.id) && x.status === "active") },
});
}
if (path === "/api/annotation-labels" && method === "POST") {
const item = { ...body, id: "new", version: 1 };
labels.push(item);
return route.fulfill({ status: 201, json: item });
}
if (method === "PATCH") {
const item = labels.find((x) => path.endsWith("/" + x.id));
Object.assign(item, body, { version: item.version + 1 });
return route.fulfill({ json: item });
}
return route.fulfill({ json: { items: labels } });
});
await page.route("**/__discard_test", (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 Rf from '/@react-refresh';Rf.injectIntoGlobalHook(window);window.$RefreshReg$=()=>{};window.$RefreshSig$=()=>t=>t;window.__vite_plugin_react_preamble_installed__=true;
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 root=(D.createRoot||D.default.createRoot)(document.getElementById('root'));
await import('/src/index.css');const {CaptureAnnotationWorkbench}=await import('/src/components/CaptureAnnotationWorkbench.tsx');
window.calls=[];window.rejectName=false;let options=[];
window.showWorkbench=async()=>{const data=await (await fetch('/api/projects/demo/discard-reason-labels')).json();options=data.items;root.render(React.createElement(CaptureAnnotationWorkbench,{clipStart:0,clipEnd:10,slices:[{id:'a',name:'旧切片',start:0,end:10}],segments:[],statuses:{},phase:'slice',onChange:()=>{},discardReasons:options,onRefreshDiscardReasons:()=>{},onSliceDispositionChange:async(id,disposition,choice)=>{if(window.rejectName)throw Error('Validation failed (sliceSegments.0.name: 切片 1(旧切片)需要从本项目切片字典重新选择名称)');window.calls.push({id,disposition,...choice});return true}}))};
window.showWorkbench();
</script></body></html>`,
}),
);
await page.goto(base + "/__discard_test");
await page.waitForFunction(() => typeof window.showWorkbench === "function");
await page.evaluate(() => window.showWorkbench());
await page.getByRole("button", { name: "舍弃片段", exact: true }).click();
const dialog = page.locator("dialog[open]"),
confirm = dialog.getByRole("button", { name: "确认舍弃", exact: true });
assert.equal(await confirm.isDisabled(), true);
await dialog.getByRole("combobox", { name: "舍弃原因", exact: true }).selectOption("other");
assert.equal(await confirm.isDisabled(), true);
await dialog.getByRole("textbox", { name: "舍弃补充说明" }).fill("调整时画面晃动");
assert.equal(await confirm.isEnabled(), true);
await page.evaluate(() => (window.rejectName = true));
await confirm.click();
await dialog.getByRole("button", { name: "返回修正切片名称" }).click();
await page.getByRole("button", { name: "舍弃片段", exact: true }).click();
assert.equal(await dialog.getByRole("textbox", { name: "舍弃补充说明" }).inputValue(), "调整时画面晃动");
await page.evaluate(() => (window.rejectName = false));
await dialog.getByRole("combobox", { name: "舍弃原因", exact: true }).selectOption("adjust");
await page.screenshot({ path: out + "/picker.png" });
await confirm.click();
assert.deepEqual(await page.evaluate(() => window.calls), [
{ id: "a", disposition: "discard", reasonLabelId: "adjust", reasonVersion: 1, reasonNote: "调整时画面晃动" },
]);
assert.deepEqual(errors, []);
await fs.writeFile(
out + "/result.json",
JSON.stringify(
{
passed: true,
errors,
checks: [
"create and project enable",
"project disable",
"reason required",
"other note required",
"name error and draft preservation",
"dictionary choice payload",
],
},
null,
2,
),
);
console.log("PASS discard reason browser checks");
} finally {
await browser.close();
}