Files
RuneStone/tests/native/spelling.native.mjs
T
2026-11-03 00:23:07 -07:00

100 lines
6.4 KiB
JavaScript

// Native spelling menu in Writer, inputs, textareas, and dialogs: WebView2 Undo, spelling corrections, and editor undo.
// Start a debug app with the isolated identifier com.runestone.spelling-test, a hidden window, a separate
// WEBVIEW2_USER_DATA_FOLDER outside Vite's watch paths, and remote debugging on port 9224:
// set WEBVIEW2_ADDITIONAL_BROWSER_ARGUMENTS=--remote-debugging-port=9224
// pnpm tauri dev --config "{\"identifier\":\"com.runestone.spelling-test\"}"
// Run `node tests/native/spelling.native.mjs`. NATIVE_CDP_URL (or the older SPELLING_CDP_URL) overrides http://127.0.0.1:9224.
// SPELLING_APP_URL overrides http://localhost:1420. SPELLING_WORKSPACE overrides the workspace folder in output/smoke/native/spelling/.
// The normal app identifier is rejected before this test writes any workspace files.
import { mkdir, writeFile } from "node:fs/promises";
import path from "node:path";
import assert from "node:assert/strict";
import { outputDir, playwright } from "../smoke/_harness/index.mjs";
const { chromium } = playwright();
const browser = await chromium.connectOverCDP(process.env.NATIVE_CDP_URL || process.env.SPELLING_CDP_URL || "http://127.0.0.1:9224");
const page = browser.contexts()[0].pages()[0];
page.setDefaultTimeout(15000);
try {
await page.goto(process.env.SPELLING_APP_URL || "http://localhost:1420");
const data = await page.evaluate(async () => {
const { appDataDir } = await import("/node_modules/@tauri-apps/api/path.js");
return appDataDir();
});
assert.match(data, /[\\/]com\.runestone\.spelling-test[\\/]?$/, "Use the isolated spelling-test app identifier");
const output = await outputDir("native/spelling");
const workspace = path.resolve(process.env.SPELLING_WORKSPACE || path.join(output, "workspace"));
await mkdir(workspace, { recursive: true });
await writeFile(path.join(workspace, "Spelling.md"), "# Spelling\n\nI recieved a letter.\n");
await page.evaluate(async path => {
const { invoke } = await import("/node_modules/@tauri-apps/api/core.js");
await invoke("workspace_open", { path });
}, workspace);
await page.reload();
await page.locator('[data-tree-rel="Spelling.md"]').click();
const editor = page.locator(".cm-content");
await editor.waitFor();
const line = page.getByText("I recieved a letter.", { exact: true });
const rect = await line.boundingBox();
await line.click();
const before = await editor.innerText();
await page.keyboard.press("End");
await page.keyboard.type(" TEST");
const typed = await editor.innerText();
assert.notEqual(typed, before);
await page.mouse.click(rect.x + 42, rect.y + rect.height / 2, { button: "right" });
const menu = page.getByRole("menu", { name: "Spelling actions", exact: true });
await menu.waitFor();
await page.screenshot({ path: path.join(output, "spelling-menu.png") });
const undo = menu.getByRole("menuitem", { name: "Undo Ctrl+Z", exact: true });
assert.equal(await undo.isEnabled(), true, "The native Undo command is enabled after typing");
await undo.click();
await page.waitForFunction(previous => document.querySelector(".cm-content")?.innerText !== previous, typed);
const remaining = (await editor.innerText()).match(/^I recieved a letter\.(.*)$/m)?.[1];
assert.ok(remaining !== undefined && remaining.length < " TEST".length && " TEST".startsWith(remaining), "Native Undo reduces the appended text and preserves the original paragraph");
// WebView2 and CodeMirror can group typing differently. Restore the fixture through editor history.
for (let attempt = 0; attempt < 8 && await editor.innerText() !== before; attempt++) {
await page.keyboard.press("Control+z");
await page.evaluate(() => new Promise(resolve => requestAnimationFrame(() => requestAnimationFrame(resolve))));
}
assert.equal(await editor.innerText(), before, "Bounded keyboard Undo restores the exact original editor text");
console.log("PASS: real WebView2 Undo reduces the typed text, and bounded editor Undo restores the fixture.");
await page.mouse.click(rect.x + 42, rect.y + rect.height / 2, { button: "right" });
await menu.waitFor();
const suggestion = menu.getByRole("menuitem", { name: "received", exact: true });
if (await suggestion.count()) {
await suggestion.click();
await page.waitForFunction(() => document.querySelector(".cm-content")?.innerText.includes("I received a letter."));
await page.keyboard.press("Control+z");
await page.waitForFunction(() => document.querySelector(".cm-content")?.innerText.includes("I recieved a letter."));
console.log("PASS: native spelling correction and editor undo.");
} else {
await page.keyboard.press("Escape");
assert.notEqual(process.env.REQUIRE_NATIVE_SUGGESTIONS, "1", "The test WebView2 profile supplied no spelling suggestions");
console.log("SKIP: native spelling correction. This WebView2 profile supplied no suggestions. Desktop acceptance is still required.");
}
for (const kind of ["input", "textarea", "dialog"]) {
await page.evaluate(kind => {
const field = document.createElement(kind === "input" ? "input" : "textarea");
field.id = "native-spelling-field"; field.spellcheck = true;
field.value = "I recieved a letter.";
field.style.cssText = "position:fixed;left:250px;top:200px;width:500px;height:100px;font:16px/24px Arial;padding:10px;z-index:100";
if (kind === "dialog") { const dialog = document.createElement("dialog"); dialog.id = "native-spelling-dialog"; dialog.append(field); document.body.append(dialog); dialog.showModal(); }
else document.body.append(field);
field.focus();
}, kind);
const field = page.locator("#native-spelling-field"), box = await field.boundingBox();
await page.mouse.click(box.x + 48, box.y + (kind === "input" ? box.height / 2 : 22), { button: "right" });
await menu.getByRole("menuitem", { name: "received", exact: true }).click();
assert.equal(await field.inputValue(), "I received a letter.", kind + " corrects the clicked word");
await page.keyboard.press("Control+z");
assert.equal(await field.inputValue(), "I recieved a letter.", kind + " retains undo");
await page.evaluate(() => { document.getElementById("native-spelling-field")?.remove(); document.getElementById("native-spelling-dialog")?.remove(); });
}
console.log("PASS: native spelling corrections and undo in inputs, card textareas, and top-layer dialogs.");
} finally {
await page.keyboard.press("Escape").catch(() => {});
await browser.close();
}