import { createRequire } from "node:module"; import assert from "node:assert/strict"; import { mkdir } from "node:fs/promises"; const require = createRequire(import.meta.url); const { chromium } = require(process.env.PLAYWRIGHT_MODULE || "playwright"); const browser = await chromium.launch({ headless: true, ...(process.env.CHROME_PATH ? { executablePath: process.env.CHROME_PATH } : {}) }); const context = await browser.newContext({ viewport: { width: 1400, height: 900 } }); const pluginCode = `export function activate(api) { let state = "Board state"; api.registerView({ id: "board", title: "Board", icon: "kanban", placement: "editor", navigation: true, saveTabState: () => state, restoreTabState: value => { state = value; }, mount(container) { const input = document.createElement("input"); input.setAttribute("aria-label", "Board state"); input.value = state; input.oninput = () => { state = input.value; }; container.append(input); return () => input.remove(); } }); api.registerDocumentType({ id: "document", title: "Board document", extensions: ["board"], icon: "kanban", identify: (path, source) => source === "board document" ? { id: path } : null, mount(container) { container.textContent = "Saved board document"; return () => { container.textContent = ""; }; } }); }`; await context.addInitScript(({ pluginCode }) => { window.isTauri = true; localStorage.setItem("runestone.settings", JSON.stringify({ autosaveEnabled: false })); const params = new URLSearchParams(location.search); const root = params.has("other") ? "C:\\other-lifecycle" : params.has("plugins") ? "C:\\plugins-lifecycle" : "C:\\document-lifecycle"; const files = new Map(["a.md", "b.md", "c.md"].map(name => [name, `# ${name}\n\nSaved text.\n`])); files.set("saved.board", "board document"); if (params.has("missing")) files.delete("a.md"); let sequence = 0; const callbacks = new Map(), listeners = new Map(); const emit = async event => { for (const id of listeners.get(event) || []) await callbacks.get(id)?.({ event, id, payload: null }); }; window.lifecycle = { answer: "Cancel", close: () => emit("tauri://close-requested"), root, transfer: null }; const manifest = { schemaVersion: 1, id: "test.lifecycle", name: "Lifecycle", author: "Tests", description: "Tab fixture", version: "1.0.0", apiVersion: 1, entry: "main.js", access: "full-app" }; window.__TAURI_INTERNALS__ = { metadata: { currentWindow: { label: "main" }, currentWebview: { label: "main" } }, transformCallback: fn => { const id = ++sequence; callbacks.set(id, fn); return id; }, unregisterCallback: id => callbacks.delete(id), invoke: async (command, args = {}) => { if (command === "plugin:event|listen") { const ids = listeners.get(args.event) || []; ids.push(args.handler); listeners.set(args.event, ids); return args.handler; } if (command === "plugin:event|unlisten") return; if (command.startsWith("plugin:window|")) return; if (command === "plugin:dialog|message") return window.lifecycle.answer; if (command === "workspace_last_path") return root; if (command === "workspace_open") return { root, name: "Lifecycle", watch_mode: "native" }; if (command === "workspace_open_window") { window.lifecycle.transfer = args.transfer; return; } if (command === "workspace_read") { if (!files.has(args.rel)) throw new Error("Missing file"); return files.get(args.rel); } if (command === "workspace_list") return args.rel ? [] : [...files.keys()].map(name => ({ name, rel_path: name, kind: "file" })); if (command === "workspace_search") return []; if (command === "plugins_snapshot") return { root, plugins: params.has("disabled") ? [] : [{ manifest, hash: "test", enabled: true, path: root, error: null, removal: false }] }; if (command === "plugins_load") return pluginCode; if (command === "plugins_data") return "null"; if (command === "plugins_release") return; throw new Error(`Unhandled IPC: ${command}`); }, }; window.__TAURI_EVENT_PLUGIN_INTERNALS__ = { unregisterListener: () => {} }; }, { pluginCode }); const url = process.env.SMOKE_URL || "http://127.0.0.1:1420"; const errors = []; const tab = (page, name) => page.getByRole("tab", { name, exact: true }); const bar = page => page.getByRole("tablist", { name: "Working tabs" }); async function create(query = "") { const page = await context.newPage(); page.setDefaultTimeout(15000); page.on("pageerror", error => errors.push(error.message)); await page.goto(url + query); await page.getByRole("tab").first().waitFor(); return page; } async function open(page, name) { await page.bringToFront(); await page.keyboard.press("Control+p"); await page.getByRole("textbox", { name: "Find a file" }).fill(name); await page.getByRole("dialog").getByRole("button").filter({ hasText: name }).first().click(); await tab(page, name === "saved.board" ? "saved" : name).waitFor(); } async function openBoard(page) { await page.getByRole("navigation", { name: "Plugin shortcuts" }).getByRole("button", { name: "Board", exact: true }).click(); await page.getByRole("textbox", { name: "Board state" }).waitFor(); } async function pin(page, name) { await tab(page, name).click({ button: "right" }); await page.getByRole("menuitem", { name: "Pin", exact: true }).click(); } async function start(page, name) { const data = await tab(page, name).evaluate(node => { assertDraggable(node); function assertDraggable(node) { if (node.getAttribute("draggable") !== "true") throw new Error("The tab must support native transfer."); } const transfer = new DataTransfer(); node.dispatchEvent(new DragEvent("dragstart", { bubbles: true, dataTransfer: transfer })); return transfer.getData("application/x-runestone-tab"); }); return data; } async function screenshots(page, prefix) { await mkdir("output/tab-ribbon-drop", { recursive: true }); for (const mode of ["dark", "light"]) { await page.evaluate(async mode => { const { applyAppearance } = await import("/src/lib/theme/appearance.ts"); await applyAppearance({ theme: "default", mode }); }, mode); await page.screenshot({ path: `output/tab-ribbon-drop/${prefix}-${mode}.png` }); } await page.setViewportSize({ width: 700, height: 700 }); await page.screenshot({ path: `output/tab-ribbon-drop/${prefix}-narrow.png` }); await page.setViewportSize({ width: 1400, height: 900 }); } const order = page => bar(page).getByRole("tab").evaluateAll(nodes => nodes.map(node => node.textContent.trim())); async function eventAt(page, data, point, type = "dragover", mime = "application/x-runestone-tab") { return page.evaluate(({ data, point, type, mime }) => { const transfer = new DataTransfer(); transfer.setData(mime, data); const node = document.elementFromPoint(point.x, point.y) || document.body; const event = new DragEvent(type, { bubbles: true, cancelable: true, clientX: point.x, clientY: point.y, dataTransfer: transfer }); node.dispatchEvent(event); return event.defaultPrevented; }, { data, point, type, mime }); } async function receive(page, data, point) { assert.equal(await eventAt(page, data, point), true); assert.equal(await eventAt(page, data, point, "drop"), true); } async function point(page, kind) { return page.locator(".header").evaluate((header, kind) => { const bounds = header.getBoundingClientRect(); const bar = header.querySelector('[role="tablist"]').getBoundingClientRect(); const plus = header.querySelector('[aria-label="New tab (Ctrl T)"]').getBoundingClientRect(); const first = header.querySelector('.tab').getBoundingClientRect(); if (kind === "near") return { x: first.left + first.width / 2, y: bounds.bottom + 18 }; if (kind === "outside") return { x: first.left + first.width / 2, y: bounds.bottom + 80 }; if (kind === "precise") return { x: bar.left + 2, y: bar.top + bar.height / 2 }; if (kind === "controls") { const controls = header.querySelector('.window-controls').getBoundingClientRect(); return { x: controls.left + 10, y: controls.top + controls.height / 2 }; } if (kind === "blank") return { x: plus.right + 16, y: bounds.top + bounds.height / 2 }; return { x: plus.left + plus.width / 2, y: plus.top + plus.height / 2 }; }, kind); } try { const source = await create("?plugins"), target = await create("?plugins"); await open(target, "b.md"); await open(source, "a.md"); await pin(source, "a.md"); await source.locator(".cm-content").fill("Unsaved ribbon transfer"); let data = await start(source, "a.md •"); const broad = await point(target, "blank"); assert.equal(await eventAt(target, data, broad), true, "Blank titlebar space accepts transferred tabs"); assert.equal(await bar(target).evaluate(node => node.classList.contains("receiving")), true); assert.equal(await eventAt(target, data, await point(target, "outside")), false, "The document body stays outside the drop target"); assert.equal(await bar(target).evaluate(node => node.classList.contains("receiving")), false); assert.equal(await eventAt(target, data, await point(target, "controls")), false, "Window controls stay outside the drop target"); assert.equal(await eventAt(target, data, broad, "dragover", "text/plain"), false, "Ordinary file and text drags retain their behavior"); await mkdir("output/tab-ribbon-drop", { recursive: true }); for (const mode of ["light", "dark"]) { await target.evaluate(async mode => { const { applyAppearance } = await import("/src/lib/theme/appearance.ts"); await applyAppearance({ theme: "default", mode }); }, mode); await eventAt(target, data, broad); await target.screenshot({ path: `output/tab-ribbon-drop/${mode}-blank-receiving.png` }); } await receive(target, data, broad); await tab(target, "a.md •").waitFor(); await tab(source, "a.md •").waitFor({ state: "detached" }); assert.deepEqual(await order(target), ["b.md", "a.md •"]); assert.match(await target.locator(".cm-content").innerText(), /Unsaved ribbon transfer/); assert.ok(await tab(target, "a.md •").getAttribute("aria-describedby")); await open(source, "c.md"); data = await start(source, "c.md"); await receive(target, data, await point(target, "precise")); await tab(target, "c.md").waitFor(); await tab(source, "c.md").waitFor({ state: "detached" }); assert.deepEqual(await order(target), ["c.md", "b.md", "a.md •"]); await openBoard(source); await pin(source, "Board"); await source.getByRole("textbox", { name: "Board state" }).fill("Plugin ribbon state"); data = await start(source, "Board"); await target.setViewportSize({ width: 700, height: 700 }); const nearby = await point(target, "near"); await eventAt(target, data, nearby); await bar(target).and(target.locator('.receiving')).waitFor(); await target.screenshot({ path: "output/tab-ribbon-drop/narrow-near-receiving.png" }); await receive(target, data, nearby); await tab(target, "Board").waitFor(); await tab(source, "Board").waitFor({ state: "detached" }); assert.deepEqual(await order(target), ["c.md", "b.md", "a.md •", "Board"]); assert.equal(await target.getByRole("textbox", { name: "Board state" }).inputValue(), "Plugin ribbon state"); assert.ok(await tab(target, "Board").getAttribute("aria-describedby")); await screenshots(target, "ribbon-plugin-pinned"); data = await start(target, "Board"); assert.equal(await eventAt(target, data, await point(target, "near")), false, "Same-window reordering stays precise"); await receive(target, data, await point(target, "precise")); assert.deepEqual(await order(target), ["Board", "c.md", "b.md", "a.md •"]); await tab(target, "Board").evaluate(node => node.dispatchEvent(new DragEvent("dragend", { bubbles: true }))); assert.deepEqual(errors, []); console.log("PASS: broad titlebar and nearby ribbon drops, exact tab ordering, rejection outside the ribbon and over window controls, ordinary drag isolation, pinned unsaved documents, narrow plugin transfers, and source acknowledgement"); } catch (error) { await mkdir("output/tab-ribbon-drop", { recursive: true }); for (const [index, page] of context.pages().entries()) await page.screenshot({ path: `output/tab-ribbon-drop/failure-${index}.png` }); throw error; } finally { await browser.close(); }