Files
RuneStone/tests/smoke/editor/undoHistory.smoke.mjs
T
Knotty Wyvern 724402729e Replace Monaco with one CodeMirror editor and nest list items with Tab
Phase 4b. DocumentEditor.svelte replaces LiveWriter.svelte and the Monaco
EditorHost.svelte. Each open document has one CodeMirror state. Write and
Source are two compartment configurations of it, so they share the undo
history. editorStates.ts keeps the state while the tab stays open. The
Monaco model, the diff bridge, its timers, and sessions.ts are gone.

- Source: syntax colors from the theme tokens, lazy language packages,
  line numbers, folding, and the current line in IDE, wrapped lines in
  Writing, several carets, and bracket matching and closing.
- Lists (card 088e778b): Tab nests a list item and Shift+Tab moves it
  up, in Write and Source. Both renumber the list. The source keeps
  "1.". Write and Read show numbers, letters, then Roman numerals.
- Indent (card 696c30ce): a fixed 4 spaces in Write and Source.
- The code menu uses CodeMirror commands. The command palette item and
  the Monaco styles, search, theme, and workers are removed.
- @codemirror/legacy-modes is pinned to 6.5.4. Version 6.5.5 uses
  parser types that do not match @codemirror/language 6.12.
- 26 smoke tests use CodeMirror instead of Monaco. New tests:
  editor/listNesting and the markdownLists unit tests.
- Docs: development, UI design, plugin host, user guide, and UI-040.

pnpm check, pnpm test (341 of 341), and pnpm build pass. The full smoke
suite passes, except one template test that passed on retry.
2026-10-28 10:48:44 -07:00

126 lines
5.9 KiB
JavaScript

// Undo history survives tab switches, window focus, IDE edits, and table cell edits.
import assert from "node:assert/strict";
import { mockTauri, runSmoke, smokeUrl } from "../_harness/index.mjs";
await runSmoke("editor/undoHistory", async ({ page }) => {
await mockTauri(page, {
commands: test => {
const workspace = test.workspace({
root: "C:\\smoke-workspace",
files: {
"notes/welcome.md": "---\ntitle: Welcome\n---\n\n# Working notes\n\nFirst paragraph.\n\n- [ ] A task\n\n[[second]]\n",
"notes/second.md": "# Second note\n",
"src/hello.py": "print('hello')\n",
},
writeEvents: true,
});
test.files = workspace.files;
return {
...workspace,
// Saves send the last saved text. Reject a save when the file changed, as the backend does.
workspace_write: args => {
if (args.expected !== undefined && workspace.files.get(args.rel) !== args.expected) throw new Error("Save blocked: file changed on disk");
return workspace.workspace_write(args);
},
};
},
});
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();
}
async function writer() {
await page.locator('.cm-content').waitFor();
return page.evaluate(async () => {
const { EditorView } = await import('/node_modules/@codemirror/view/dist/index.js');
window.__WRITER__ = EditorView.findFromDOM(document.querySelector('.cm-content'));
return window.__WRITER__.state.sliceDoc();
});
}
async function expectWriter(expected) {
await writer();
try { await page.waitForFunction(text => window.__WRITER__.state.sliceDoc() === text, expected); } catch (error) { console.log({ expected, actual: await writer(), errors: page.errors }); throw error; }
}
await page.goto(smokeUrl);
await page.getByText('smoke-workspace', { exact: true }).waitFor();
await open('welcome.md');
const original = await writer();
await page.locator('.cm-content').click();
await page.keyboard.press('Control+End');
await page.keyboard.type(' first edit');
await expectWriter(original + ' first edit');
await page.keyboard.press('Control+s');
await open('second.md');
await writer();
await page.getByRole('tab', { name: 'welcome.md', exact: true }).click();
await expectWriter(original + ' first edit');
await page.keyboard.press('Control+z');
await expectWriter(original);
await page.keyboard.press('Control+y');
await expectWriter(original + ' first edit');
console.log('PASS: saved Writer edit survives tab switch with undo and redo');
await page.evaluate(() => window.dispatchEvent(new Event('focus')));
await page.keyboard.press('Control+z');
await expectWriter(original);
await page.keyboard.press('Control+y');
await expectWriter(original + ' first edit');
assert.equal(await page.evaluate(() => window.__WRITER__.state.selection.main.head), (original + ' first edit').length);
console.log('PASS: Writer undo and redo survive window focus refresh and restore the caret');
await page.keyboard.press('Control+e');
await page.locator('.source-editor .cm-content').waitFor();
await page.evaluate(async () => {
const { EditorView } = await import('/node_modules/@codemirror/view/dist/index.js');
const view = EditorView.findFromDOM(document.querySelector('.source-editor .cm-content'));
view.dispatch({ selection: { anchor: view.state.doc.length } });
view.focus();
window.__IDE__ = view;
});
await page.keyboard.type(' second edit');
assert.equal(await page.evaluate(() => window.__IDE__.dom.isConnected), true, 'Typing keeps the same IDE editor mounted');
assert.equal(await page.evaluate(() => window.__IDE__.state.doc.toString()), original + ' first edit second edit');
await page.keyboard.press('Control+e');
await expectWriter(original + ' first edit second edit');
await page.locator('.cm-content').click();
// One burst of typing is one undo step, in Source as in Write.
await page.keyboard.press('Control+z');
await expectWriter(original + ' first edit');
await page.keyboard.press('Control+z');
await expectWriter(original);
await page.keyboard.press('Control+y');
await expectWriter(original + ' first edit');
await page.keyboard.press('Control+y');
await expectWriter(original + ' first edit second edit');
console.log('PASS: Writer undo includes edits from IDE mode');
const tableSource = '# Table\n\n| A | B |\n| --- | --- |\n| One | Two |\n\nTail.\n';
await page.evaluate(source => {
window.__TEST__.files.set('table.md', source);
window.__TEST__.emit('workspace-event', { root: window.__TEST__.root, kind: 'create', paths: ['table.md'] });
}, tableSource);
await open('table.md');
await writer();
const cells = page.locator('.writer-table tbody td');
await cells.nth(0).click();
await page.keyboard.press('Control+a');
await page.keyboard.type('Alpha');
await cells.nth(1).click();
await page.keyboard.press('Control+a');
await page.keyboard.type('Beta');
await page.keyboard.press('Escape');
const editedTable = await writer();
assert.ok(editedTable.includes('Alpha') && editedTable.includes('Beta'));
await page.getByRole('tab', { name: 'welcome.md', exact: true }).click();
await writer();
await page.getByRole('tab', { name: 'table.md', exact: true }).click();
await writer();
await page.keyboard.press('Control+z');
await expectWriter(editedTable.replace('Beta', 'Two'));
await page.keyboard.press('Control+z');
await expectWriter(tableSource);
await page.keyboard.press('Control+y');
await expectWriter(editedTable.replace('Beta', 'Two'));
await page.keyboard.press('Control+y');
await expectWriter(editedTable);
console.log('PASS: table cell typing stays grouped per cell across tab switches');
});