archivo del paquete→ .shtukbase/packages/lenses/test/card-host.test.ts
test/card-host.test.ts
94 líneas, 5.2 KB.
El archivo tal como está en el paquete: el código y sus comentarios no se traducen.
- import { expect, test } from "bun:test";
- import { spawnSync } from "node:child_process";
- import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
- import { tmpdir } from "node:os";
- import { join } from "node:path";
- import { findChrome } from "../../shtukbase-ui-html/lib/overflow.ts";
- import { designFiles } from "../kit/src/build.js";
- const chrome = findChrome();
- const card = join(import.meta.dir, "../../layout-minimal/pages/design/layout-minimal/card");
- test("the selected layout bundles the complete card host before mounting", () => {
- const root = mkdtempSync(join(tmpdir(), "sb-layout-bundle-"));
- try {
- const layout = join(root, "design", "sample");
- const assets = join(layout, "card");
- mkdirSync(assets, { recursive: true });
- writeFileSync(join(root, "design", "design.md"), "---\nlayout: sample\n---\n");
- writeFileSync(join(layout, "layout.css"), "/* layout */");
- writeFileSync(join(layout, "layout.js"), "// mount");
- writeFileSync(join(assets, "card.css"), "/* card */");
- expect(designFiles(root).layout).toMatchObject({ css: "/* layout */", js: "// mount" });
- writeFileSync(join(assets, "card.js"), "// define host");
- expect(designFiles(root).layout).toMatchObject({ css: "/* layout */\n/* card */", js: "// define host\n// mount" });
- } finally {
- rmSync(root, { recursive: true, force: true });
- }
- });
- test.skipIf(!chrome)("card host preserves the settings panel and returns focus across mode changes", () => {
- const dir = mkdtempSync(join(tmpdir(), "sb-layout-card-"));
- try {
- const file = join(dir, "card.html");
- writeFileSync(file, `<!doctype html><style>
- .shell { --radius: 10px; --foreground: #222; --background: white; }
- ${readFileSync(join(card, "card.css"), "utf8")}
- [data-slot="peek"] { width: 100%; height: 180px; }
- #source { position: fixed; right: 2px; bottom: 2px; }
- .shell-right { position: fixed; right: 12px; top: 12px; bottom: 12px; width: 240px; }
- </style>
- <div class="shell" data-panel="open">
- <aside class="shell-right">Settings</aside>
- <button id="source">Source card</button>
- <div id="host" data-slot="layout-card-host" data-state="closed" hidden></div>
- </div>
- <script>${readFileSync(join(card, "card.js"), "utf8")}</script>
- <script>
- const source = document.querySelector("#source");
- const host = document.querySelector("#host");
- const shell = document.querySelector(".shell");
- const panel = shell.querySelector("aside");
- const frame = { shell, cardHost: host, panel: () => panel };
- window.LayoutCard.mount(frame);
- const peek = document.createElement("section");
- peek.dataset.slot = "peek";
- peek.innerHTML = '<h2 data-slot="peek-title">Package</h2><button data-slot="peek-close">Close</button><p data-slot="peek-body">Details</p>';
- const check = (condition, message) => { if (!condition) throw new Error(message); };
- try {
- source.focus();
- const side = frame.openCard(peek, { mode: "side", anchor: source });
- const dialog = host.querySelector("dialog");
- check(!host.hidden && host.dataset.mode === "side", "side opened");
- check(getComputedStyle(panel).display === "none" && shell.dataset.panel === "open", "side preserves the saved panel");
- check(document.activeElement.dataset.slot === "peek-close", "initial focus");
- check(!!peek.querySelector('[data-slot="scroll-area-viewport"]'), "body uses the scroll-area viewport");
- frame.openCard(peek, { mode: "center" });
- side.close();
- check(dialog.matches(":modal") && getComputedStyle(panel).display !== "none", "center is modal and leaves the panel visible");
- frame.openCard(peek, { mode: "peek", anchor: source });
- const r = dialog.getBoundingClientRect();
- check(!dialog.matches(":modal") && getComputedStyle(panel).display !== "none", "card leaves the panel visible");
- check(r.left >= 12 && r.top >= 12 && r.right <= innerWidth - 12 && r.bottom <= innerHeight - 12, "card fits at the viewport edge");
- check(r.right <= panel.getBoundingClientRect().left - 12, "card clears the settings panel when there is room");
- document.activeElement.dispatchEvent(new KeyboardEvent("keydown", { key: "Escape", bubbles: true }));
- check(host.hidden && host.dataset.state === "closed", "Escape closes the host");
- check(document.activeElement === source, "focus returns to the source");
- check(getComputedStyle(panel).display !== "none", "the open settings panel comes back");
- document.body.dataset.result = "pass";
- } catch (error) { document.body.dataset.result = error.message; }
- </script>`);
- const result = spawnSync(chrome!, [
- "--headless=new", "--disable-gpu", "--no-first-run", "--no-default-browser-check", `--user-data-dir=${join(dir, "profile")}`,
- "--window-size=800,600", "--virtual-time-budget=3000", "--dump-dom", Bun.pathToFileURL(file).href,
- ], { encoding: "utf8", timeout: 30_000 });
- const outcome = result.stdout.match(/<body[^>]*data-result="([^"]+)"/)?.[1];
- expect(outcome, result.stderr.slice(0, 400)).toBe("pass");
- } finally {
- rmSync(dir, { recursive: true, force: true });
- }
- }, 40_000);