// Repeated copy and paste of formatted Markdown stays exact across saves, tab switches, Source switches, and line endings. import assert from "node:assert/strict"; import { mockTauri, runSmoke, smokeUrl } from "../_harness/index.mjs"; await runSmoke("editor/pasteRepeat", 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({ expectedLength: expected.length, actualLength: (await writer()).length, errors: page.errors }); throw error; } } await page.goto(smokeUrl); await page.getByText('smoke-workspace', { exact: true }).waitFor(); const formatted = '# Copied heading\n\n## Links\n\n[Reference](https://example.com) and [[second|Another note]]\n\n**Bold**, *italic*, and `code`.\n\n- A list item\n'; await page.evaluate(text => { window.__TEST__.files.set('notes/source.md', text); window.__TEST__.files.set('notes/pasted.md', ''); }, formatted); await open('source.md'); await writer(); await page.locator('.cm-content').click(); await page.keyboard.press('Control+a'); await page.keyboard.press('Control+c'); await open('pasted.md'); await writer(); for (let iteration = 0; iteration < 12; iteration++) { await page.locator('.cm-content').click(); await page.keyboard.press('Control+a'); await page.keyboard.press('Backspace'); await expectWriter(''); await page.keyboard.press('Control+v'); await expectWriter(formatted); assert.equal(await page.locator('.live-writer .cm-editor').count(), 1, 'One mounted editor'); await page.keyboard.press('Control+s'); assert.equal(await page.evaluate(() => window.__TEST__.files.get('notes/pasted.md').replace(/\r\n?/g, '\n')), formatted); if (iteration === 5) { await page.keyboard.press('Control+e'); await page.locator('.source-editor .cm-content').waitFor(); await page.keyboard.press('Control+e'); await expectWriter(formatted); } await open('second.md'); await writer(); await page.getByRole('tab', { name: 'pasted.md', exact: true }).click(); await expectWriter(formatted); } // Clipboard formats can use different line endings than the destination. for (const lineBreak of ['\n', '\r\n', '\r']) { await page.evaluate(({ text, lineBreak }) => { const view = window.__WRITER__; view.dispatch({ selection: { anchor: 0, head: view.state.doc.length } }); const clipboardData = new DataTransfer(); clipboardData.setData('text/plain', text.replace(/\n/g, lineBreak)); view.contentDOM.dispatchEvent(new ClipboardEvent('paste', { bubbles: true, cancelable: true, clipboardData })); }, { text: formatted, lineBreak }); await expectWriter(formatted); assert.equal(await page.evaluate(() => window.__WRITER__.state.doc.lines), formatted.split('\n').length); await page.keyboard.press('Control+s'); // The saved file keeps one line break. The Writer text with that line break is the saved source. const synchronized = await page.evaluate(() => { const saved = window.__TEST__.files.get('notes/pasted.md'); const lineBreak = saved.includes('\r\n') ? '\r\n' : '\n'; return window.__WRITER__.state.doc.sliceString(0, undefined, lineBreak) === saved; }); assert.equal(synchronized, true, 'Writer and saved source agree'); } console.log('PASS: formatted Markdown copied across notes stays exact through 12 paste/delete cycles, saves, tab reopen, Source reopen, and LF/CRLF/CR clipboard replacement'); });