// Run against pnpm dev with PLAYWRIGHT_MODULE set if Playwright is shared. import { createRequire } from "node:module"; import assert from "node:assert/strict"; 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 page = await browser.newPage({ viewport: { width: 1440, height: 1000 } }); page.setDefaultTimeout(10000); const errors = []; page.on("pageerror", error => errors.push(error.message)); await page.addInitScript(() => { window.isTauri = true; const root = "C:\\tab-order-workspace"; const files = new Map(Array.from({ length: 16 }, (_, i) => [`note-${String(i).padStart(2, "0")}.md`, `# Note ${i}\n\nKeep this text.\n`])); files.set("note-00.md", Array.from({ length: 200 }, (_, i) => `Paragraph ${i}.\n\n`).join("")); let sequence = 0; window.__TAURI_INTERNALS__ = { metadata: { currentWindow: { label: "main" }, currentWebview: { label: "main" } }, transformCallback: () => ++sequence, unregisterCallback: () => {}, invoke: async (command, args = {}) => { if (command === "plugin:event|listen") return ++sequence; if (command === "plugin:event|unlisten") return; if (command === "workspace_last_path") return root; if (command === "workspace_open") return { root, name: "tab-order-workspace", watch_mode: "native" }; if (command === "workspace_read") { if (!files.has(args.rel)) throw new Error("missing"); return files.get(args.rel); } if (command === "workspace_write") { files.set(args.rel, args.contents); return; } if (command === "workspace_list") return args.rel ? [] : [...files.keys()].map(name => ({ name, rel_path: name, kind: "file" })); if (command === "workspace_search") return []; if (command === "plugin:dialog|message") return "Cancel"; throw new Error(`Unhandled IPC: ${command}`); }, }; window.__TAURI_EVENT_PLUGIN_INTERNALS__ = { unregisterListener: () => {} }; }); const tab = name => page.getByRole("tab", { name, exact: true }); const order = () => page.getByRole("tablist", { name: "Working tabs" }).getByRole("tab").evaluateAll(nodes => nodes.map(node => node.dataset.path)); const settle = () => page.evaluate(() => new Promise(resolve => requestAnimationFrame(() => requestAnimationFrame(resolve)))); async function open(name) { 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(name).waitFor(); } async function drag(from, to, side, cancel) { const source = await tab(from).boundingBox(); const target = await tab(to).boundingBox(); await page.mouse.move(source.x + source.width / 2, source.y + source.height / 2); await page.mouse.down(); await page.mouse.move(side === "before" ? target.x + 2 : target.x + target.width - 2, target.y + target.height / 2, { steps: 10 }); if (cancel === "escape") await page.keyboard.press("Escape"); if (cancel === "outside") await page.mouse.move(target.x, target.y + 100); await page.mouse.up(); await settle(); } try { await page.goto(process.env.SMOKE_URL || "http://127.0.0.1:1420"); await page.getByText("tab-order-workspace", { exact: true }).waitFor(); for (const name of ["note-00.md", "note-01.md", "note-02.md"]) await open(name); await drag("note-00.md", "note-02.md", "after"); assert.deepEqual(await order(), ["note-01.md", "note-02.md", "note-00.md"]); assert.equal(await tab("note-02.md").getAttribute("aria-selected"), "true"); await drag("note-00.md", "note-01.md", "before"); assert.deepEqual(await order(), ["note-00.md", "note-01.md", "note-02.md"]); for (const cancel of ["escape", "outside"]) { await drag("note-00.md", "note-02.md", "after", cancel); assert.deepEqual(await order(), ["note-00.md", "note-01.md", "note-02.md"]); } await tab("note-00.md").click(); assert.equal(await tab("note-00.md").getAttribute("aria-selected"), "true"); await page.locator(".cm-scroller").evaluate(node => { node.scrollTop = 1200; }); await settle(); const scrollTop = await page.locator(".cm-scroller").evaluate(node => node.scrollTop); assert.ok(scrollTop > 1000); await tab("note-00.md").press("Alt+Shift+ArrowRight"); await settle(); assert.deepEqual(await order(), ["note-01.md", "note-00.md", "note-02.md"]); assert.ok(Math.abs(await page.locator(".cm-scroller").evaluate(node => node.scrollTop) - scrollTop) < 5); assert.equal(await tab("note-00.md").evaluate(node => document.activeElement === node), true); await tab("note-00.md").press("ArrowRight"); assert.equal(await tab("note-02.md").getAttribute("aria-selected"), "true"); await tab("note-02.md").press("Shift+F10"); await page.getByRole("menuitem", { name: "Move tab", exact: true }).click(); assert.equal(await page.getByRole("menuitem", { name: /Move tab right/ }).isDisabled(), true); await page.getByRole("menuitem", { name: /Move tab left/ }).click(); assert.deepEqual(await order(), ["note-01.md", "note-02.md", "note-00.md"]); await tab("note-01.md").click({ button: "right" }); await page.getByRole("menuitem", { name: "Move tab", exact: true }).click(); assert.equal(await page.getByRole("menuitem", { name: /Move tab left/ }).isDisabled(), true); await page.keyboard.press("Escape"); assert.equal(await page.getByRole("menu", { name: "Tab actions" }).count(), 0); console.log("PASS: pointer moves, cancellation, keyboard moves, and tab menus"); await page.getByRole("button", { name: "New tab (Ctrl T)", exact: true }).click(); await tab("New tab").press("Alt+Shift+Home"); assert.match((await order())[0], /^empty:/); await page.getByRole("button", { name: "Close New tab", exact: true }).click(); await tab("note-01.md").click({ button: "middle" }); assert.deepEqual(await order(), ["note-02.md", "note-00.md"]); console.log("PASS: empty tabs and middle-click close after reorder"); for (let i = 3; i < 16; i++) await open(`note-${String(i).padStart(2, "0")}.md`); const bar = page.getByRole("tablist", { name: "Working tabs" }); assert.ok(await bar.evaluate(node => node.scrollLeft > 0)); await tab("note-15.md").press("Alt+Shift+Home"); assert.equal((await order())[0], "note-15.md"); assert.equal(await bar.evaluate(node => node.scrollLeft), 0); const source = await tab("note-15.md").boundingBox(); const bounds = await bar.boundingBox(); await page.mouse.move(source.x + source.width / 2, source.y + source.height / 2); await page.mouse.down(); await page.mouse.move(bounds.x + bounds.width - 3, source.y + source.height / 2, { steps: 10 }); await page.waitForFunction(() => { const bar = document.querySelector('[aria-label="Working tabs"]'); return bar.scrollLeft >= bar.scrollWidth - bar.clientWidth - 2; }); await page.mouse.up(); await settle(); assert.equal((await order()).at(-1), "note-15.md"); assert.deepEqual(errors, []); console.log("PASS: automatic scrolling and drag to the end of an overflowing tab bar"); if (process.env.SCREENSHOT_PATH) await page.screenshot({ path: process.env.SCREENSHOT_PATH }); } finally { await browser.close(); }