Unit tests go to tests/unit/<area>/, browser smoke tests to tests/smoke/<area>/, and tests against a running desktop app to tests/native/. Files named after dates, work passes, or Kanban cards get feature names, for example editorOctober12 -> editor/footnotes. scripts/ keeps the build, release, and package tools. Plugin repositories import scripts/packagePlugin.mjs, and the Terminal smoke test imports scripts/pluginMocks.mjs, which now re-exports the harness copy. The unit tests get their own type check (tests/tsconfig.json) with Node types. pnpm check runs it after the app check. Add @types/node and fix the three test mocks that it reported.
85 lines
5.6 KiB
JavaScript
85 lines
5.6 KiB
JavaScript
// Run against `pnpm dev`. PLAYWRIGHT_MODULE may point to a shared installation.
|
|
import { createRequire } from "node:module";
|
|
import assert from "node:assert/strict";
|
|
import { readFile } 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 page = await browser.newPage({ viewport: { width: 1400, height: 900 } });
|
|
const errors = [];
|
|
page.on("pageerror", error => errors.push(error.message));
|
|
await page.addInitScript(() => {
|
|
window.isTauri = true;
|
|
const callbacks = new Map(), listeners = new Map();
|
|
let sequence = 0;
|
|
const state = { maximized: false, confirm: "Cancel", calls: [] };
|
|
const emit = (event, payload) => {
|
|
for (const id of listeners.get(event) || []) callbacks.get(id)?.({ event, id, payload });
|
|
};
|
|
window.__TAURI_INTERNALS__ = {
|
|
metadata: { currentWindow: { label: "main" }, currentWebview: { label: "main" } },
|
|
transformCallback: callback => { const id = ++sequence; callbacks.set(id, callback); return id; },
|
|
unregisterCallback: id => callbacks.delete(id),
|
|
invoke: async (command, args = {}) => {
|
|
state.calls.push(command);
|
|
if (command === "plugin:event|listen") {
|
|
const list = listeners.get(args.event) || []; list.push(args.handler); listeners.set(args.event, list); return args.handler;
|
|
}
|
|
if (command === "plugin:event|unlisten") { callbacks.delete(args.eventId); return; }
|
|
if (command === "workspace_last_path") return null;
|
|
if (command === "plugin:window|is_maximized") return state.maximized;
|
|
if (command === "plugin:window|toggle_maximize") { state.maximized = !state.maximized; emit("tauri://resize", { width: 1400, height: 900 }); return; }
|
|
if (command === "plugin:window|minimize" || command === "plugin:window|destroy") return;
|
|
if (command === "plugin:window|close") { emit("tauri://close-requested"); return; }
|
|
if (command === "plugin:dialog|message") return state.confirm;
|
|
throw new Error(`Unhandled IPC: ${command}`);
|
|
},
|
|
};
|
|
window.__TAURI_EVENT_PLUGIN_INTERNALS__ = { unregisterListener: (_event, id) => callbacks.delete(id) };
|
|
window.__TEST__ = { state, emit };
|
|
});
|
|
try {
|
|
const config = JSON.parse(await readFile(new URL("../../../src-tauri/tauri.conf.json", import.meta.url), "utf8"));
|
|
assert.equal(config.app.windows[0].decorations, false);
|
|
const capability = JSON.parse(await readFile(new URL("../../../src-tauri/capabilities/default.json", import.meta.url), "utf8"));
|
|
for (const permission of ["close", "minimize", "toggle-maximize", "start-dragging"]) {
|
|
assert.ok(capability.permissions.includes(`core:window:allow-${permission}`));
|
|
}
|
|
await page.goto(process.env.SMOKE_URL || "http://localhost:1420");
|
|
await page.getByRole("button", { name: "Maximize window", exact: true }).click();
|
|
await page.getByRole("button", { name: "Restore window", exact: true }).waitFor();
|
|
await page.getByRole("button", { name: "Restore window", exact: true }).click();
|
|
await page.getByRole("button", { name: "Maximize window", exact: true }).waitFor();
|
|
// Native maximize/restore (e.g. double-clicking the drag region) updates the button too.
|
|
await page.evaluate(() => { window.__TEST__.state.maximized = true; window.__TEST__.emit("tauri://resize", { width: 1920, height: 1080 }); });
|
|
await page.getByRole("button", { name: "Restore window", exact: true }).waitFor();
|
|
await page.getByRole("button", { name: "Minimize window", exact: true }).click();
|
|
assert.ok(await page.evaluate(() => window.__TEST__.state.calls.includes("plugin:window|minimize")));
|
|
await page.getByRole("button", { name: "Close window", exact: true }).focus();
|
|
await page.getByRole("tooltip", { name: "Close window", exact: true }).waitFor();
|
|
// Opening the terminal engages the existing close guard; cancellation must not destroy the window.
|
|
await page.getByRole("button", { name: "Terminal", exact: true }).click();
|
|
await page.getByRole("button", { name: "Close window", exact: true }).click();
|
|
await page.waitForFunction(() => window.__TEST__.state.calls.includes("plugin:dialog|message"));
|
|
assert.equal(await page.evaluate(() => window.__TEST__.state.calls.includes("plugin:window|destroy")), false);
|
|
await page.evaluate(() => window.__TEST__.state.confirm = "Ok");
|
|
await page.getByRole("button", { name: "Close window", exact: true }).click();
|
|
await page.waitForFunction(() => window.__TEST__.state.calls.includes("plugin:window|destroy"));
|
|
await page.getByRole("button", { name: "Terminal", exact: true }).click();
|
|
for (const width of [1400, 880]) {
|
|
await page.setViewportSize({ width, height: 900 });
|
|
const layout = await page.evaluate(() => {
|
|
const header = document.querySelector(".header").getBoundingClientRect();
|
|
const controls = [...document.querySelectorAll(".header button")].map(el => el.getBoundingClientRect());
|
|
return { height: header.height, top: header.top, fits: controls.every(rect => rect.x >= 0 && rect.right <= innerWidth && rect.y >= header.y && rect.bottom <= header.bottom), drag: document.querySelector(".window-drag-space").getBoundingClientRect().width };
|
|
});
|
|
assert.equal(layout.height, 36);
|
|
assert.equal(layout.top, 0);
|
|
assert.ok(layout.fits);
|
|
assert.ok(layout.drag >= 40);
|
|
await page.screenshot({ path: `.audit/window-controls-${width}.png` });
|
|
}
|
|
assert.deepEqual(errors, []);
|
|
console.log("Window controls smoke passed: actions, restore state, close guard, keyboard tooltip, and 1400/880px layouts.");
|
|
} finally { await browser.close(); }
|