Files
aigc-home/e2e/hel743.cjs
T

352 lines
11 KiB
JavaScript

// HEL-743: sample geometry, themes, breakpoint and search keyboard regression.
// Run using e2e/run-hel729.py with HEL729_TESTS=e2e/hel743.cjs.
const { chromium } = require(
process.env.PLAYWRIGHT_MODULE_PATH || "playwright",
);
const fs = require("node:fs"),
path = require("node:path"),
assert = require("node:assert/strict");
const out = "e2e/shots/hel743";
fs.mkdirSync(out, { recursive: true });
const checks = [],
measurements = [],
errors = [];
function ok(name, v) {
assert(v, name);
checks.push(name);
console.log("PASS", name);
}
const pause = (ms) => new Promise((r) => setTimeout(r, ms));
async function metrics(p, sample = false) {
return p.evaluate((sample) => {
const sel = sample
? {
head: ".cc > .ph",
name: "#hdName",
file: "#hdFile",
search: "#wfSearch",
box: "#wfSearch",
key: ".wfb-top kbd",
}
: {
head: ".studio-heading",
name: ".model-selector",
file: ".workflow-file",
search: ".rail-search input",
box: ".rail-search",
key: ".rail-search kbd",
};
const entries = Object.entries(sel).map(([k, sel]) => {
let e = document.querySelector(sel);
if (sample && k === "box") e = e.parentElement;
if (sample && k === "key")
e = document
.querySelector("#wfSearch")
.parentElement.querySelector("kbd");
let r = e.getBoundingClientRect(),
s = getComputedStyle(e);
return [
k,
{
x: r.x,
y: r.y,
width: r.width,
height: r.height,
font: s.font,
fontSize: s.fontSize,
fontWeight: s.fontWeight,
lineHeight: s.lineHeight,
padding: s.padding,
radius: s.borderRadius,
border: s.border,
bottomBorder: s.borderBottom,
background: s.backgroundColor,
color: s.color,
blur: s.backdropFilter,
text: e.textContent,
},
];
});
return {
...Object.fromEntries(entries),
overflow: document.documentElement.scrollWidth > innerWidth,
};
}, sample);
}
(async () => {
const b = await chromium.launch({
executablePath: process.env.CHROMIUM_EXECUTABLE_PATH,
args: ["--no-sandbox", "--disable-dev-shm-usage"],
});
try {
const c = await b.newContext({ viewport: { width: 1440, height: 900 } }),
p = await c.newPage(),
s = await c.newPage();
p.on("pageerror", (e) => errors.push(e.message));
p.setDefaultTimeout(10000);
const wfs = await (
await fetch("http://127.0.0.1:8247/api/workflows")
).json(),
wf = wfs.find((w) => w.name === "遮罩图生图测试");
assert(wf);
// The fixture file and metadata live only under the isolated test data directory.
const settings = await (
await fetch("http://127.0.0.1:8247/api/settings")
).json();
assert.equal(
path.resolve(settings.data_dir),
path.resolve("e2e/shots/reference/data"),
);
const detail = await (
await fetch("http://127.0.0.1:8247/api/workflows/" + wf.id)
).json();
const source = path.join(settings.data_dir, "hel743-mask-workflow.json");
fs.writeFileSync(source, JSON.stringify(detail.workflow_json, null, 2));
require("node:child_process").execFileSync(
"calliope-backend/.venv/bin/python",
[
"-c",
'import sqlite3,sys; c=sqlite3.connect(sys.argv[1]); c.execute("UPDATE workflows SET source_path=? WHERE id=?",(sys.argv[2],int(sys.argv[3]))); c.commit(); c.close()',
path.join(settings.data_dir, "calliope.db"),
source,
String(wf.id),
],
);
await p.goto("http://127.0.0.1:5173/create?wf=" + wf.id);
await p.locator("#desk-prompt").waitFor();
await s.goto(
"file://" +
path.resolve(process.env.HEL740_SAMPLE) +
"?still=1&fill=1#create/image",
);
await s.locator("#wfSearch").waitFor();
for (const [width, height] of [
[1440, 900],
[1366, 768],
[1280, 720],
]) {
await p.setViewportSize({ width, height });
await s.setViewportSize({ width, height });
for (const theme of ["night", "day", "grade", "bluehour"]) {
await p.getByRole("button", { name: "主题", exact: true }).click();
await p
.locator(".choice")
.filter({
hasText: {
night: "夜片场",
day: "日光棚",
grade: "调色间",
bluehour: "蓝调时刻",
}[theme],
})
.click();
await p.keyboard.press("Escape");
await s.evaluate((t) => {
document.documentElement.dataset.theme = t;
renderCreate();
}, theme);
await pause(450);
const actual = await metrics(p),
sample = await metrics(s, true);
measurements.push({ width, height, theme, actual, sample });
const tag = width + "/" + theme;
ok(
tag + " head width/height/name x",
actual.head.width === sample.head.width &&
actual.head.height === sample.head.height &&
actual.name.x === sample.name.x,
);
ok(
tag + " glass/bottom line/blur",
actual.head.background === sample.head.background &&
actual.head.bottomBorder === sample.head.bottomBorder &&
actual.head.blur === sample.head.blur,
);
ok(
tag + " search/input width",
Math.abs(actual.search.width - sample.search.width) < 0.1 &&
actual.box.width === sample.box.width,
);
ok(
tag + " kbd exact appearance",
[
"fontSize",
"fontWeight",
"lineHeight",
"padding",
"radius",
"border",
"background",
"color",
].every((k) => actual.key[k] === sample.key[k]) &&
actual.key.text === "/",
);
ok(
tag + " filename breakpoint",
width <= 1366
? actual.file.width === 0 && sample.file.width === 0
: actual.file.width > 0 && sample.file.width > 0,
);
ok(tag + " no overflow", !actual.overflow);
await p.screenshot({ path: out + `/${width}-${theme}-actual.png` });
await s.screenshot({ path: out + `/${width}-${theme}-sample.png` });
}
}
for (const width of [1367, 1366]) {
await p.setViewportSize({ width, height: 768 });
await s.setViewportSize({ width, height: 768 });
const a = await metrics(p),
v = await metrics(s, true);
ok(
width + " boundary filename matches sample",
(a.file.width === 0) === (v.file.width === 0),
);
}
await p.setViewportSize({ width: 1440, height: 900 });
const search = p.getByRole("searchbox", { name: "搜索工作流" }),
origin = p.getByRole("tab", { name: "图片", exact: true });
await origin.focus();
await p.keyboard.press("/");
ok(
"/ focuses search",
await search.evaluate((e) => e === document.activeElement),
);
await search.fill("遮罩");
await pause(200);
ok("search filters", (await p.locator(".workflow-pick").count()) === 1);
await p.keyboard.press("Escape");
ok(
"Esc returns prior focus",
await origin.evaluate((e) => e === document.activeElement),
);
ok("Esc preserves filter", (await search.inputValue()) === "遮罩");
await search.fill("");
await search.focus();
await p.keyboard.press("/");
ok(
"search typing slash remains literal",
(await search.inputValue()) === "/",
);
await search.fill("");
const prompt = p.locator("#desk-prompt");
await prompt.fill("");
await prompt.focus();
await p.keyboard.press("/");
ok("textarea keeps slash", (await prompt.inputValue()) === "/");
await p.evaluate(() => {
let e = document.createElement("div");
e.id = "editable-probe";
e.contentEditable = "true";
e.textContent = "IME";
document.body.append(e);
e.focus();
});
await p.keyboard.press("/");
ok(
"contenteditable keeps focus",
await p
.locator("#editable-probe")
.evaluate(
(e) => document.activeElement === e && e.textContent.includes("/"),
),
);
await p.locator("#editable-probe").evaluate((e) => e.remove());
await origin.focus();
ok(
"composition slash ignored",
await origin.evaluate((e) => {
let k = new KeyboardEvent("keydown", {
key: "/",
bubbles: true,
cancelable: true,
isComposing: true,
});
e.dispatchEvent(k);
return document.activeElement === e && !k.defaultPrevented;
}),
);
ok(
"legacy IME 229 ignored",
await origin.evaluate((e) => {
let k = new KeyboardEvent("keydown", {
key: "/",
keyCode: 229,
bubbles: true,
cancelable: true,
});
e.dispatchEvent(k);
return document.activeElement === e && !k.defaultPrevented;
}),
);
await p.keyboard.press("Control+/");
ok(
"modified slash ignored",
await origin.evaluate((e) => document.activeElement === e),
);
await prompt.focus();
ok(
"composition Escape keeps editor focus",
await prompt.evaluate((e) => {
let k = new KeyboardEvent("keydown", {
key: "Escape",
bubbles: true,
cancelable: true,
isComposing: true,
});
e.dispatchEvent(k);
return document.activeElement === e && !k.defaultPrevented;
}),
);
await p.setViewportSize({ width: 1440, height: 900 });
ok(
"1440 filename is genuine source basename",
(await p.locator(".workflow-file").textContent()) ===
path.basename(source) &&
(await p.locator(".workflow-file").getAttribute("title")) === source,
);
// Mouse focus and removal of the prior node use a stable rail control fallback.
await search.click();
await p.keyboard.press("Escape");
ok(
"mouse search Esc returns control",
await p.evaluate(
() =>
document.activeElement !==
document.querySelector(".rail-search input"),
),
);
await p
.getByRole("button", { name: "创作设置⌄", exact: true })
.first()
.click();
await p.locator(".modal-panel").waitFor();
await p.locator(".modal-panel button").first().focus();
await p.keyboard.press("/");
ok(
"dialog slash never jumps to rail",
!(await search.evaluate((e) => e === document.activeElement)),
);
await p.keyboard.press("Escape");
ok(
"dialog Escape closes normally",
(await p.locator(".modal-panel").count()) === 0,
);
await p.getByRole("button", { name: "收起历史栏", exact: true }).focus();
await p.keyboard.press("/");
await p.keyboard.press("Escape");
ok(
"keyboard origin restored exactly",
await p
.getByRole("button", { name: "收起历史栏", exact: true })
.evaluate((e) => document.activeElement === e),
);
ok("no browser errors", errors.length === 0);
fs.writeFileSync(
out + "/results.json",
JSON.stringify({ checks, measurements, errors }, null, 2),
);
} finally {
await b.close();
}
})();