126 lines
5.9 KiB
JavaScript
126 lines
5.9 KiB
JavaScript
// Undo history survives tab switches, window focus, Source 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 Source 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 Source 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');
|
|
});
|