Files
RuneStone/tests/smoke/editor/listNesting.smoke.mjs
T
2026-11-16 04:21:21 -07:00

117 lines
6.5 KiB
JavaScript

// List nesting: Tab and Shift+Tab in Write, nested labels in Write and Read, one undo history in Write and Source, and the 4-space indent in Source.
import assert from "node:assert/strict";
import { chooseView, mockTauri, outputDir, runSmoke, smokeUrl } from "../_harness/index.mjs";
await runSmoke("editor/listNesting", async ({ page }) => {
await mockTauri(page, {
commands: test => ({
...test.workspace({
root: "C:\\smoke-workspace",
files: {
"notes/readme.md": "# Requirements\n\n1. Topic\n2. \n3. A\n",
"src/hello.py": "print('hello')\n",
},
}),
spelling_menu_install: () => null,
}),
});
const output = await outputDir("editor/listNesting");
await page.goto(smokeUrl);
await page.getByText("smoke-workspace", { exact: true }).waitFor();
async function open(name, editor) {
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 page.getByRole("textbox", { name: editor, exact: true }).waitFor();
await page.evaluate(async () => {
const { EditorView } = await import("/node_modules/@codemirror/view/dist/index.js");
window.__CM__ = EditorView.findFromDOM(document.querySelector(".cm-content"));
});
}
const doc = () => page.evaluate(() => window.__CM__.state.doc.toString());
const caretAt = (text, offset = text.length) => page.evaluate(([text, offset]) => {
window.__CM__.dispatch({ selection: { anchor: window.__CM__.state.doc.toString().indexOf(text) + offset } });
window.__CM__.focus();
}, [text, offset]);
const labels = () => page.locator(".live-writer .writer-list-label").allTextContents();
await open("readme.md", "Writing editor");
await caretAt("2. ");
await page.keyboard.press("Tab");
assert.equal(await doc(), "# Requirements\n\n1. Topic\n 1. \n2. A\n", "Tab nests the empty item and renumbers the next item");
assert.deepEqual(await labels(), ["a."], "The nested item shows a letter before it has text");
await page.keyboard.type("Sub item");
await page.keyboard.press("Enter");
await page.keyboard.type("Second");
assert.equal(await doc(), "# Requirements\n\n1. Topic\n 1. Sub item\n 2. Second\n2. A\n");
assert.deepEqual(await labels(), ["a.", "b."]);
for (const width of [1440, 600]) {
if (width === 600) {
await page.getByRole("button", { name: "Files", exact: true }).click();
await page.getByRole("button", { name: "Hide outline", exact: true }).click();
}
await page.setViewportSize({ width, height: 800 });
for (const mode of ["dark", "light"]) {
await page.evaluate(async mode => (await import("/src/lib/theme/appearance.ts")).saveAppearance({ theme: "default", mode }), mode);
await page.locator(".live-writer .cm-content").screenshot({ path: `${output}/writer-${mode}-${width}.png` });
}
}
await page.setViewportSize({ width: 1440, height: 900 });
await page.locator(".live-writer .cm-content").focus();
await page.keyboard.press("Shift+Tab");
assert.equal(await doc(), "# Requirements\n\n1. Topic\n 1. Sub item\n2. Second\n3. A\n", "Shift+Tab moves the item up and renumbers the outer list");
assert.deepEqual(await labels(), ["a."]);
await page.keyboard.press("Control+z");
assert.equal(await doc(), "# Requirements\n\n1. Topic\n 1. Sub item\n 2. Second\n2. A\n", "One undo step restores the nesting and the numbers");
await caretAt("1. Topic");
await page.keyboard.press("Tab");
assert.equal(await doc(), "# Requirements\n\n1. Topic\n 1. Sub item\n 2. Second\n2. A\n", "The first item stays in place");
await chooseView(page, "Read");
await page.locator(".md-page ol ol").waitFor();
assert.equal(await page.locator(".md-page ol ol").evaluate(list => getComputedStyle(list).listStyleType), "lower-alpha");
assert.equal(await page.locator(".md-page > .reading > ol, .md-page ol").first().evaluate(list => getComputedStyle(list).listStyleType), "decimal");
await page.locator(".md-page").screenshot({ path: `${output}/reader.png` });
await chooseView(page, "Write");
await page.getByRole("textbox", { name: "Writing editor", exact: true }).waitFor();
// Write and Source share the undo history of the document.
await caretAt("2. A");
await page.keyboard.type(" more");
await page.keyboard.press("Control+e");
await page.locator('.source-editor[data-chrome="Source"] .cm-gutters').waitFor();
assert.equal(await page.locator(".source-editor .cm-content").getAttribute("data-language"), "markdown");
await page.locator(".source-editor .cm-content").focus();
await page.keyboard.press("Control+z");
assert.equal(await doc(), "# Requirements\n\n1. Topic\n 1. Sub item\n 2. Second\n2. A\n", "Source undoes the last edit from Write");
await page.keyboard.press("Control+Shift+KeyZ");
assert.equal(await doc(), "# Requirements\n\n1. Topic\n 1. Sub item\n 2. Second\n2. A more\n");
await caretAt("2. A more");
await page.keyboard.press("Tab");
assert.equal(await doc(), "# Requirements\n\n1. Topic\n 1. Sub item\n 2. Second\n 3. A more\n", "Tab nests list items in Source too");
await page.keyboard.press("Control+e");
await page.getByRole("textbox", { name: "Writing editor", exact: true }).waitFor();
assert.deepEqual(await labels(), ["a.", "b.", "c."]);
// Source indents code to the next 4-column stop.
await open("hello.py", "Editor");
await caretAt("print", 0);
await page.keyboard.press("Tab");
assert.equal(await doc(), " print('hello')\n");
await caretAt("print", 2);
await page.keyboard.press("Tab");
assert.equal(await doc(), " pr int('hello')\n", "Tab inside a line fills to the next stop");
await page.keyboard.press("Control+z");
await page.keyboard.press("Control+z");
assert.equal(await doc(), "print('hello')\n");
await page.locator(".source-editor").screenshot({ path: `${output}/source-ide.png` });
// The history of a document stays while its tab stays open.
await open("readme.md", "Writing editor");
await page.locator(".live-writer .cm-content").focus();
await page.keyboard.press("Control+z");
assert.equal(await doc(), "# Requirements\n\n1. Topic\n 1. Sub item\n 2. Second\n2. A more\n", "Undo after a tab change reverts the Tab from Source");
console.log("PASS: Tab and Shift+Tab nesting, renumbering, Write labels, Read letters, shared undo in Write and Source and across tabs, Source list Tab, and 4-space code indent.");
});