import { chromium } from "playwright"; import assert from "node:assert/strict"; import { mkdir, readFile, writeFile } from "node:fs/promises"; import { parseLogText } from "../components/log-reader.js"; const base = process.env.BASE_URL || "http://127.0.0.1:4173"; const browser = await chromium.launch({ executablePath: process.env.PLAYWRIGHT_EXECUTABLE_PATH || "C:/Program Files (x86)/Microsoft/Edge/Application/msedge.exe", headless: true, }); const context = await browser.newContext({ acceptDownloads: true }); const p = await context.newPage(); const errors = []; p.on("pageerror", (e) => errors.push(e.message)); let passed = 0; await mkdir("references/workspaces", { recursive: true }); async function check(name, run) { await run(); passed++; console.log("PASS", name); } const url = (page, theme = "dark", locale = "ru") => `${base}/?page=${page}&theme=${theme}&locale=${locale}`; const screen = () => p.locator(".xterm-rows").innerText(); const terminal = () => p.locator(".xterm-helper-textarea"); async function command(text) { await terminal().focus(); await p.keyboard.type(text); await p.keyboard.press("Enter"); await p.waitForTimeout(80); } try { for (const page of process.env.SKIP_MATRIX ? [] : ["ssh", "logs"]) for (const theme of ["light", "dark"]) for (const locale of ["ru", "en"]) for (const [device, width, height] of [ ["desktop", 1440, 1000], ["tablet", 768, 1024], ["mobile", 390, 844], ]) { await check(`${page}/${theme}/${locale}/${device}`, async () => { await p.setViewportSize({ width, height }); await p.goto(url(page, theme, locale)); await p .locator(page === "ssh" ? ".xterm-rows" : ".log-row") .first() .waitFor(); await p.waitForTimeout(180); assert.ok( await p.evaluate( () => document.documentElement.scrollWidth <= innerWidth, ), ); if (page === "ssh") { assert.equal( await p .locator(".ssh-terminal") .evaluate((e) => getComputedStyle(e).backgroundColor), "rgb(0, 0, 0)", ); const box = await p.locator(".ssh-terminal").boundingBox(), canvas = await p.locator(".xterm-screen").boundingBox(); assert.ok( canvas.y + canvas.height <= box.y + box.height + 0.5, JSON.stringify({ box, canvas }), ); } if (locale === "en") assert.equal( /[А-Яа-яЁё]/.test(await p.locator("#content").innerText()), false, ); if (locale === "ru") await p.screenshot({ path: `references/workspaces/${page}-${theme}-${device}.png`, fullPage: true, }); }); } await p.setViewportSize({ width: 1440, height: 1000 }); await p.goto(url("ssh")); await terminal().waitFor({ state: "attached" }); await check( "demo commands, cursor editing, line kill/yank and history", async () => { await command("whoami"); assert.match(await screen(), /\ndev\n/); await terminal().focus(); await p.keyboard.type("echo ac"); await p.keyboard.press("ArrowLeft"); await p.keyboard.type("b"); await p.keyboard.press("End"); await p.keyboard.press("Enter"); await p.waitForTimeout(80); assert.match(await screen(), /\nabc\n/); await p.keyboard.type("bad text"); await p.keyboard.press("Control+u"); await p.keyboard.type("echo killed"); await p.keyboard.press("Control+u"); await p.keyboard.press("Control+y"); await p.keyboard.press("Enter"); await p.waitForTimeout(80); assert.match(await screen(), /\nkilled\n/); await p.keyboard.press("ArrowUp"); await p.keyboard.press("Enter"); await p.waitForTimeout(80); assert.ok((await screen()).match(/killed/g).length >= 4); await p.keyboard.type("who"); await p.keyboard.press("Tab"); await p.keyboard.press("Enter"); await p.waitForTimeout(80); assert.match(await screen(), /whoami/); }, ); await check("signals, streaming process and alternate screen", async () => { await command("top"); assert.match(await screen(), /PID USER/); await p.keyboard.press("Control+c"); await p.waitForTimeout(80); assert.doesNotMatch(await screen(), /Tasks: 5 total/); await command("less gateway.log"); assert.match(await screen(), /demo gateway.log/); await p.keyboard.press("PageDown"); await p.waitForTimeout(80); assert.match(await screen(), /gateway request_id/); await p.keyboard.press("q"); await command("tail -f gateway.log"); await p.waitForTimeout(1200); assert.match(await screen(), /demo request/); await p.keyboard.press("Control+z"); await p.waitForTimeout(80); assert.match(await screen(), /Stopped \(demo\)/); }); await check( "search and keyboard escape hatch; app shortcuts outside terminal", async () => { await terminal().focus(); await p.keyboard.press("Control+Shift+f"); await p.locator("[data-ssh-query]").fill("gateway"); assert.equal(await p.locator(".ssh-find").isVisible(), true); await p.keyboard.press("Escape"); assert.equal(await p.locator(".ssh-find").isVisible(), false); await p.keyboard.press("Control+k"); assert.equal(await p.locator("#dialog").evaluate((e) => e.open), false); await p.keyboard.press("Control+Shift+m"); assert.equal( await p.locator("[data-ssh=capture]").getAttribute("aria-pressed"), "false", ); assert.notEqual( await p.evaluate(() => document.activeElement?.className), "xterm-helper-textarea", ); await terminal().focus(); await p.keyboard.press("Tab"); assert.notEqual( await p.evaluate(() => document.activeElement?.className), "xterm-helper-textarea", ); await p.keyboard.press("Control+k"); await p.locator("#command-search").waitFor(); await p.keyboard.press("Escape"); await p.locator("[data-ssh=capture]").click(); assert.equal( await p.locator("[data-ssh=capture]").getAttribute("aria-pressed"), "true", ); }, ); await check("EOF and reconnect", async () => { await terminal().focus(); await p.keyboard.press("Control+u"); await p.keyboard.press("Control+d"); await p.waitForTimeout(80); assert.match(await p.locator("[data-ssh-state]").innerText(), /Отключено/); await p.locator("[data-ssh=connect]").click(); await p.waitForTimeout(80); assert.match(await screen(), /dev@edge-01/); }); await check("wrapped command editing on a narrow terminal", async () => { await p.setViewportSize({ width: 390, height: 844 }); await p.waitForTimeout(100); await command("clear"); const text = "abcdefghijklmnopqrst".repeat(7); await command("echo " + text); assert.ok((await screen()).replace(/\s/g, "").includes(text)); await p.setViewportSize({ width: 1440, height: 1000 }); }); await check( "transport boundary receives terminal control sequences", async () => { await p.goto(url("components")); await p.evaluate(async () => { const { sshScreen, mountSsh } = await import("/components/ssh-console.js"); const container = document.createElement("div"); container.id = "keyboard-fixture"; container.innerHTML = sshScreen({ t: (ru, en) => en, heading: () => "", }); document.querySelector("#content").append(container); window.terminalInput = []; window.fixtureDispose = await mountSsh( container.querySelector("#ssh-workspace"), { t: (ru, en) => en, notify: () => {}, createTransport: ({ write, onClose }) => { write("\x1b[?2004hREADY\r\n$ "); return { send(data) { window.terminalInput.push(data); }, resize() {}, close: onClose, dispose() { window.fixtureTransportDisposed = true; }, }; }, }, ); }); await p.locator("#keyboard-fixture .xterm-helper-textarea").focus(); const controls = { a: 1, b: 2, c: 3, d: 4, e: 5, f: 6, k: 11, l: 12, n: 14, p: 16, q: 17, r: 18, s: 19, t: 20, u: 21, v: 22, w: 23, y: 25, z: 26, }; for (const [key, code] of Object.entries(controls)) { await p.evaluate(() => (window.terminalInput = [])); await p .locator("#keyboard-fixture .xterm-helper-textarea") .dispatchEvent("keydown", { key, code: "Key" + key.toUpperCase(), keyCode: key.toUpperCase().charCodeAt(0), which: key.toUpperCase().charCodeAt(0), ctrlKey: true, bubbles: true, cancelable: true, }); assert.equal( await p.evaluate(() => window.terminalInput.join("")), String.fromCharCode(code), `Ctrl+${key}`, ); passed++; } for (const [key, code, value] of [ ["ArrowUp", "ArrowUp", "\x1b[A"], ["ArrowDown", "ArrowDown", "\x1b[B"], ["ArrowLeft", "ArrowLeft", "\x1b[D"], ["ArrowRight", "ArrowRight", "\x1b[C"], ["Home", "Home", "\x1b[H"], ["End", "End", "\x1b[F"], ["Delete", "Delete", "\x1b[3~"], ["PageUp", "PageUp", "\x1b[5~"], ["PageDown", "PageDown", "\x1b[6~"], ["Escape", "Escape", "\x1b"], ["Tab", "Tab", "\t"], ]) { await p.evaluate(() => (window.terminalInput = [])); await p .locator("#keyboard-fixture .xterm-helper-textarea") .dispatchEvent("keydown", { key, code, keyCode: { ArrowUp: 38, ArrowDown: 40, ArrowLeft: 37, ArrowRight: 39, Home: 36, End: 35, Delete: 46, PageUp: 33, PageDown: 34, Escape: 27, Tab: 9, }[key], bubbles: true, cancelable: true, }); assert.equal( await p.evaluate(() => window.terminalInput.join("")), value, key, ); passed++; } for (const [key, value] of [ ["b", "\x1bb"], ["f", "\x1bf"], ["d", "\x1bd"], ]) { await p.evaluate(() => (window.terminalInput = [])); await p .locator("#keyboard-fixture .xterm-helper-textarea") .dispatchEvent("keydown", { key, code: "Key" + key.toUpperCase(), keyCode: key.toUpperCase().charCodeAt(0), which: key.toUpperCase().charCodeAt(0), altKey: true, bubbles: true, cancelable: true, }); assert.equal( await p.evaluate(() => window.terminalInput.join("")), value, ); passed++; } for (const [n, value] of [ [1, "\x1bOP"], [2, "\x1bOQ"], [3, "\x1bOR"], [4, "\x1bOS"], [5, "\x1b[15~"], [6, "\x1b[17~"], [7, "\x1b[18~"], [8, "\x1b[19~"], [9, "\x1b[20~"], [10, "\x1b[21~"], [11, "\x1b[23~"], [12, "\x1b[24~"], ]) { await p.evaluate(() => (window.terminalInput = [])); await p .locator("#keyboard-fixture .xterm-helper-textarea") .dispatchEvent("keydown", { key: `F${n}`, code: `F${n}`, keyCode: 111 + n, bubbles: true, cancelable: true, }); assert.equal( await p.evaluate(() => window.terminalInput.join("")), value, ); passed++; } await p.evaluate(() => (window.terminalInput = [])); await p .locator("#keyboard-fixture .xterm-helper-textarea") .dispatchEvent("keydown", { key: "Tab", code: "Tab", keyCode: 9, shiftKey: true, bubbles: true, cancelable: true, }); assert.equal( await p.evaluate(() => window.terminalInput.join("")), "\x1b[Z", ); passed++; assert.equal(await p.locator("#dialog").evaluate((e) => e.open), false); }, ); await check( "fullscreen keyboard-lock lifecycle and permission fallback", async () => { await p.evaluate(() => { window.lockFixture = { locks: 0, unlocks: 0, denied: false }; Object.defineProperty(navigator, "keyboard", { configurable: true, value: { async lock() { window.lockFixture.locks++; if (window.lockFixture.denied) throw Error("Permission denied"); }, unlock() { window.lockFixture.unlocks++; }, }, }); }); await p.locator("#keyboard-fixture [data-ssh=fullscreen]").click(); await p.waitForFunction( () => document.fullscreenElement?.id === "ssh-workspace", ); await p.waitForTimeout(100); assert.ok(await p.evaluate(() => window.lockFixture.locks > 0)); assert.match( await p.locator("#keyboard-fixture [data-keyboard-status]").innerText(), /Keyboard Lock/, ); await p.locator("#keyboard-fixture [data-ssh=capture]").click(); assert.ok(await p.evaluate(() => window.lockFixture.unlocks > 0)); await p.evaluate(() => (window.lockFixture.denied = true)); await p.locator("#keyboard-fixture [data-ssh=capture]").click(); await p.waitForTimeout(100); assert.doesNotMatch( await p.locator("#keyboard-fixture [data-keyboard-status]").innerText(), /Keyboard Lock/, ); await p.locator("#keyboard-fixture [data-ssh=fullscreen]").click(); await p.waitForFunction(() => !document.fullscreenElement); await p.locator("#keyboard-fixture .xterm-helper-textarea").focus(); }, ); await check( "clipboard and bracketed paste at transport boundary", async () => { await context.grantPermissions(["clipboard-read", "clipboard-write"], { origin: base, }); const input = p.locator("#keyboard-fixture .xterm-helper-textarea"); await input.focus(); await input.dispatchEvent("keydown", { key: "a", code: "KeyA", keyCode: 65, metaKey: true, bubbles: true, cancelable: true, }); await p.keyboard.press("Control+Shift+c"); await p.waitForTimeout(100); assert.match( await p.evaluate(() => navigator.clipboard.readText()), /READY/, ); await p.evaluate(() => { window.terminalInput = []; const dt = new DataTransfer(); dt.setData("text/plain", "echo one\necho two"); document .querySelector("#keyboard-fixture .xterm-helper-textarea") .dispatchEvent( new ClipboardEvent("paste", { clipboardData: dt, bubbles: true, cancelable: true, }), ); }); const data = await p.evaluate(() => window.terminalInput.join("")); assert.ok(data.startsWith("\x1b[200~")); assert.ok(data.endsWith("\x1b[201~")); assert.match(data, /echo one/); assert.match(data, /echo two/); await p.evaluate(() => { window.fixtureDispose(); document.querySelector("#keyboard-fixture").remove(); }); }, ); await p.goto(url("logs")); await p.locator(".log-row").first().waitFor(); await check( "log filters, literal search and local find shortcut", async () => { await p.locator("[data-log=pause]").click(); await p.locator("[data-log-level]").selectOption("ERROR"); assert.ok((await p.locator(".log-row").count()) > 0); for (const r of await p.locator(".log-level").all()) assert.equal(await r.innerText(), "ERROR"); await p.locator("[data-log=reset]").click(); await p.locator("[data-log-source]").selectOption("gateway"); for (const r of await p.locator(".log-source").all()) assert.equal(await r.innerText(), "gateway"); await p.locator("[data-log-query]").fill("HTTP"); assert.ok((await p.locator("mark").count()) > 0); await p.locator("[data-log-query]").fill("no-such-event"); assert.equal(await p.locator(".log-empty").isVisible(), true); await p.locator("[data-log=reset]").click(); await p.locator(".log-viewport").focus(); await p.keyboard.press("Control+f"); assert.equal( await p .locator("[data-log-query]") .evaluate((e) => e === document.activeElement), true, ); }, ); await check("pause backlog, follow mode and jump to newest", async () => { const count = await p.locator(".log-row").count(); await p.waitForTimeout(1400); assert.equal(await p.locator(".log-row").count(), count); assert.match( await p.locator("[data-log-count]").innerText(), /новых событий/, ); await p.locator("[data-log=pause]").click(); assert.ok((await p.locator(".log-row").count()) > count); await p.locator(".log-viewport").evaluate((e) => (e.scrollTop = 0)); await p.waitForTimeout(100); assert.equal( await p.locator("[data-log=follow]").getAttribute("aria-pressed"), "false", ); await p.waitForTimeout(1400); assert.equal(await p.locator("[data-log-unseen]").isVisible(), true); await p.locator("[data-log=follow]").click(); assert.equal( await p.locator("[data-log=follow]").getAttribute("aria-pressed"), "true", ); }); await check( "file parsing, bounded windows, event JSON and filtered export", async () => { const lines = Array.from({ length: 2200 }, (_, i) => JSON.stringify({ timestamp: "2026-12-01T12:00:00Z", level: i % 4 === 0 ? "ERROR" : "INFO", source: "fixture", message: i === 2199 ? "" : `event ${i + 1}`, request_id: `file-${i + 1}`, }), ).join("\n"); await p.locator("[data-log-input]").setInputFiles({ name: "events.jsonl", mimeType: "application/json", buffer: Buffer.from(lines), }); await p.waitForTimeout(100); assert.equal(await p.locator(".log-row").count(), 150); assert.equal(await p.locator("[data-log=pause]").isDisabled(), true); assert.match(await p.locator("[data-log-count]").innerText(), /2000/); assert.equal(await p.locator("img[src=x]").count(), 0); const first = Number( await p.locator(".log-row").first().getAttribute("data-record"), ); await p.locator("[data-log=older]").click(); assert.equal( Number(await p.locator(".log-row").first().getAttribute("data-record")), first - 150, ); await p.locator("[data-log=follow]").click(); await p.locator(".log-row").last().click(); assert.equal( await p.locator("[data-log=follow]").getAttribute("aria-pressed"), "false", ); assert.match( await p.locator("[data-log-json]").innerText(), / 0); }, ); await check( "text/JSON parser preserves text and normalizes warning levels", async () => { const parsed = parseLogText( 'not json