// Code blocks: editing fenced and indented code in the Writer, with undo, cursor movement, preview, and save. import assert from "node:assert/strict"; import { mockTauri, runSmoke, smokeUrl } from "../_harness/index.mjs"; await runSmoke("editor/codeBlocks", 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); }, }; }, }); await page.goto(smokeUrl); await page.getByText("smoke-workspace", { exact: true }).waitFor(); await page.keyboard.press("Control+p"); await page.getByRole("textbox", { name: "Find a file" }).fill("welcome.md"); await page.getByRole("dialog").getByRole("button").filter({ hasText: "welcome.md" }).first().click(); await page.getByRole("textbox", { name: "Writing 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 contents = () => page.evaluate(() => window.__CM__.state.sliceDoc()); const cursor = () => page.evaluate(() => window.__CM__.state.selection.main.head); async function select(anchor, head = anchor) { await page.evaluate(({ anchor, head }) => { window.__CM__.dispatch({ selection: { anchor, head }, scrollIntoView: true }); window.__CM__.focus(); }, { anchor, head }); } for (const block of ["```js\nconst value = 123;\nsecond line\n```", "~~~text\nconst value = 123;\nsecond line\n~~~", " const value = 123;\n second line"]) { const original = `Above.\n\n${block}\n\nBelow.\n`; await page.evaluate(value => { window.__CM__.dispatch({ changes: { from: 0, to: window.__CM__.state.doc.length, insert: value }, selection: { anchor: 0 } }); }, original); await page.locator(".writer-code-line").first().click(); await page.locator(".writer-code-source").first().waitFor(); const position = original.indexOf("123") + 1; await select(position); await page.keyboard.press("Backspace"); assert.equal(await contents(), original.slice(0, position - 1) + original.slice(position)); await page.keyboard.press("Control+z"); assert.equal(await contents(), original); await select(position); await page.keyboard.press("Delete"); assert.equal(await contents(), original.slice(0, position) + original.slice(position + 1)); await page.keyboard.press("Control+z"); assert.equal(await contents(), original); await select(position); await page.keyboard.press("ArrowRight"); assert.equal(await cursor(), position + 1); await page.keyboard.press("ArrowLeft"); assert.equal(await cursor(), position); await page.keyboard.type("X"); assert.equal(await contents(), original.slice(0, position) + "X" + original.slice(position)); await page.keyboard.press("Control+z"); assert.equal(await contents(), original); await select(position - 1, position + 2); await page.keyboard.press("Backspace"); const edited = original.slice(0, position - 1) + original.slice(position + 2); assert.equal(await contents(), edited); await select(0); assert.ok((await page.locator(".writer-code-line").allTextContents()).join("\n").includes("const value = ;")); await page.keyboard.press("Control+s"); await page.waitForFunction(value => window.__TEST__.files.get("notes/welcome.md") === value, edited); } console.log("PASS: Fenced and indented code editing, character deletion, selection deletion, cursor movement, undo, preview and save."); });