droboticflow/tests/browser/workspace-dictionary-pagination.mjs

156 lines
7.6 KiB
JavaScript

// Render the real shared component/CSS in Chromium, without API mocks or business writes.
import assert from "node:assert/strict";
import { chromium } from "playwright";
import { createServer } from "vite";
import react from "@vitejs/plugin-react";
const server = await createServer({
configFile: false,
plugins: [
react({ jsxRuntime: "automatic", babel: { plugins: [] } }),
{
name: "dictionary-browser-fixture",
configureServer(server) {
server.middlewares.use((req, res, next) => {
if (req.url !== "/dictionary-fixture") return next();
res.setHeader("Content-Type", "text/html");
res.end(
`<html><head><style>body{margin:0}*{box-sizing:border-box}#host{position:relative;width:420px;height:calc(100dvh - 300px)}</style></head><body><div id="host"></div><output id="selected"></output><script type="module" src="/dictionary-fixture.tsx"></script></body></html>`,
);
});
},
resolveId(id) {
if (id === "/dictionary-fixture.tsx") return "\0dictionary-fixture.tsx";
},
load(id) {
if (id === "\0dictionary-fixture.tsx")
return `import React from 'react';import{createRoot}from'react-dom/client';import{WorkspaceDictionaryPanel}from'/src/components/WorkspaceDictionaryPanel.tsx';import'/src/styles/capture-workspace.css';
window.leakedKeys=0;document.addEventListener('keydown',()=>window.leakedKeys++);
createRoot(document.getElementById('host')).render(React.createElement(WorkspaceDictionaryPanel,{title:'字典',searchLabel:'搜索字典',items:Array.from({length:45},(_,i)=>({id:String(i+1),text:'条目 '+String(i+1).padStart(2,'0')+' 很长的动作描述'.repeat(12)})),onSelect:id=>document.getElementById('selected').textContent=id,onClose:()=>document.getElementById('selected').textContent='closed',onDismiss:()=>{window.dismissed=(window.dismissed||0)+1;}}));`;
},
},
],
mode: "production",
server: { host: "127.0.0.1", port: 5186, strictPort: true, hmr: false },
});
await server.listen();
const browser = await chromium.launch({
executablePath: "/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge",
headless: true,
args: ["--no-proxy-server"],
});
try {
const page = await browser.newPage({ viewport: { width: 1280, height: 640 } });
const errors = [];
page.on("pageerror", (e) => errors.push(e.message));
await page.goto("http://127.0.0.1:5186/dictionary-fixture");
const dialog = page.getByRole("dialog"),
search = page.getByRole("textbox"),
options = dialog.locator("[data-dictionary-option]"),
nav = dialog.getByRole("navigation");
await options.first().waitFor();
const fits = async () => {
for (const el of [search, nav, ...(await options.all())])
assert.ok(
await el.evaluate((e) => {
const r = e.getBoundingClientRect(),
p = e.closest("[role=dialog]").getBoundingClientRect();
return (
r.top >= p.top &&
r.bottom <= p.bottom + 1 &&
r.bottom <= innerHeight &&
e.contains(document.elementFromPoint(r.x + r.width / 2, r.y + r.height / 2))
);
}),
"controls and all page entries visible without scrolling",
);
};
assert.ok((await options.count()) >= 4, "640px viewport must show at least four entries in a 340px panel, not two");
await fits();
await page.locator("#host").evaluate((e) => (e.style.height = "232px"));
await page.waitForTimeout(100);
assert.ok((await options.count()) >= 3, "production-sized 232px editor must show at least three entries");
await fits();
await page.locator("#host").evaluate((e) => (e.style.height = "calc(100dvh - 300px)"));
await page.waitForTimeout(100);
const firstCount = await options.count();
await search.press("ArrowRight");
assert.match(await options.first().innerText(), new RegExp("条目 " + String(firstCount + 1).padStart(2, "0")));
await page.keyboard.press("ArrowLeft");
assert.match(await options.first().innerText(), /条目 01/);
while (await nav.getByRole("button", { name: "下一页" }).isEnabled()) await page.keyboard.press("ArrowRight");
const lastPage = await options.allTextContents();
await page.keyboard.press("ArrowRight");
assert.deepEqual(await options.allTextContents(), lastPage, "last page does not wrap");
await fits();
// Mouse-clicking onto a boundary page disables the clicked button; the panel must survive.
await search.press("ArrowLeft");
while (await nav.getByRole("button", { name: "上一页" }).isEnabled())
await nav.getByRole("button", { name: "上一页" }).click();
assert.match(await nav.innerText(), /1 \/ \d+ 页/, "mouse-clicking back to page 1 keeps the panel open");
while (await nav.getByRole("button", { name: "下一页" }).isEnabled())
await nav.getByRole("button", { name: "下一页" }).click();
assert.match(await nav.innerText(), /· 45 项/, "mouse-clicking to the last page keeps the panel open");
assert.ok((await options.count()) > 0, "last page entries stay usable");
assert.equal(await page.evaluate(() => window.dismissed || 0), 0, "boundary paging never dismisses the panel");
await fits();
await search.fill("reset");
await search.fill("");
await search.press("ArrowLeft");
assert.match(await options.first().innerText(), /条目 01/);
await search.fill("条目");
await search.press("Home");
await search.press("ArrowRight");
assert.equal(await search.evaluate((e) => e.selectionStart), 1, "typed search retains arrow caret movement");
assert.match(await options.first().innerText(), /条目 01/);
await search.press("ArrowDown");
await page.keyboard.press("ArrowRight");
assert.ok(
await options.first().evaluate((e) => e === document.activeElement),
"paging from options focuses the new first option",
);
await page.keyboard.press("Enter");
assert.equal(
await page.locator("#selected").innerText(),
String(firstCount + 1),
"Enter chooses current page option",
);
await search.fill("条目 45");
assert.equal(await options.count(), 1);
await fits();
await search.fill("no match");
assert.equal(await options.count(), 0);
assert.match(await nav.innerText(), /0 项/);
await search.fill("");
await search.evaluate((e) =>
e.dispatchEvent(new KeyboardEvent("keydown", { key: "ArrowRight", isComposing: true, bubbles: true })),
);
assert.match(await options.first().innerText(), /条目 01/);
await search.press("Control+ArrowRight");
assert.match(await options.first().innerText(), /条目 01/);
for (const height of [900, 1440, 640]) {
await page.setViewportSize({ width: 1920, height });
await page.waitForTimeout(100);
await fits();
if (height > 640) assert.ok((await options.count()) > firstCount, "larger panels display more items");
}
await page.setViewportSize({ width: 1920, height: 1440 });
await page.waitForTimeout(100);
await options.last().focus();
await page.setViewportSize({ width: 1280, height: 640 });
await page.waitForTimeout(100);
assert.ok(
await dialog.evaluate((e) => e.contains(document.activeElement)),
"resizing must preserve keyboard ownership when focused row disappears",
);
await page.keyboard.press("ArrowRight");
assert.ok(await options.first().evaluate((e) => e === document.activeElement));
await page.keyboard.press("Escape");
assert.equal(await page.locator("#selected").innerText(), "closed");
assert.equal(await page.evaluate(() => window.leakedKeys), 0, "dictionary never dispatches video shortcuts");
assert.deepEqual(errors, []);
console.log("PASS adaptive entries, visible pagination, arrows/caret/IME/selection/Esc/resize");
} finally {
await browser.close();
await server.close();
}