Files
RuneStone/tests/smoke/links/linkEditing.smoke.mjs
2026-11-16 04:21:21 -07:00

101 lines
6.2 KiB
JavaScript

// Link editing in the Writer: link tooltips and source colors, Edit link in prose, table, and wiki cells with undo, multiple links in a cell, and table tooltips at narrow widths.
import assert from "node:assert/strict";
import path from "node:path";
import { mockTauri, outputDir, runSmoke, smokeUrl } from "../_harness/index.mjs";
await runSmoke("links/linkEditing", async ({ page }) => {
const output = await outputDir("links/linkEditing");
await mockTauri(page, {
commands: test => {
const note = name => `# ${name}\n\nOriginal [guide](docs/guide.md) text.\n\n| Name | Value |\n| --- | --- |\n| [Roadmap](docs/roadmap.md) | [[guide\\|Guide]] |\n| [One](same.md) + [Two](same.md) | Empty |\n`;
return {
...test.workspace({ root: "C:\\tab-menu-workspace", folders: ["notes"], files: Object.fromEntries(["a.md", "b.md", "c.md", "d.md"].map(name => [name, note(name)])) }),
spelling_menu_install: () => {},
};
},
});
await page.goto(smokeUrl);
await page.locator('[data-tree-rel="a.md"]').click();
await page.locator('.writer-table').waitFor();
for(const theme of ['dark','light']) {
await page.evaluate(async mode => (await import('/src/lib/theme/appearance.ts')).saveAppearance({theme:'default',mode}), theme);
const prose = page.locator('.writer-link').filter({hasText:'guide'}).first();
await prose.hover();
// A focused table cell can keep its own tooltip open (src/lib/ui/tooltip.ts:32), so wait for the link's tooltip.
await page.getByRole('tooltip',{name:'Ctrl+click to open link',exact:true}).waitFor();
assert.equal(await prose.getAttribute('title'),null);
await page.screenshot({path:path.join(output, 'editor-link-tooltip-'+theme+'.png')});
await page.keyboard.press('Escape');
await prose.click();
assert.equal(await page.locator('.writer-link-source').count(),1);
const colored = await page.locator('.writer-link-source').evaluate(el => ({color:getComputedStyle(el).color, parent:getComputedStyle(el.parentElement).color}));
assert.notEqual(colored.color,colored.parent);
await page.screenshot({path:path.join(output, 'editor-link-source-'+theme+'.png')});
const cell=page.locator('.writer-table td').first();
await cell.locator('a').click({position:{x:22,y:8}});
assert.ok(await page.evaluate(()=>getSelection().anchorOffset > 0));
await cell.click({button:'right'});
await page.getByRole('menuitem',{name:'Edit link',exact:true}).click();
assert.equal(await page.getByRole('dialog').count(),0);
assert.equal(await page.evaluate(()=>getSelection().toString()),theme === 'dark' ? 'docs/roadmap.md' : 'docs/new-roadmap.md');
await page.screenshot({path:path.join(output, 'editor-table-source-'+theme+'.png')});
await page.keyboard.type('docs/new-roadmap.md');
await page.keyboard.press('Enter');
assert.equal(await cell.locator('a').getAttribute('href'),'docs/new-roadmap.md');
if(theme === 'dark') {
await page.locator('.cm-line').filter({hasText:'Original'}).click();
await page.keyboard.press('Control+z');
assert.equal(await cell.locator('a').getAttribute('href'),'docs/roadmap.md');
await page.keyboard.press('Control+Shift+z');
assert.equal(await cell.locator('a').getAttribute('href'),'docs/new-roadmap.md');
}
await cell.locator('a').click({position:{x:22,y:8}});
const selection=await page.evaluate(()=>{const s=getSelection();return {offset:s.anchorOffset,length:s.anchorNode.textContent.length};});
assert.ok(selection.offset>0 && selection.offset<selection.length,JSON.stringify(selection));
await page.keyboard.type('X');
assert.ok((await cell.innerText()).includes('X'));
await page.keyboard.press('Escape');
const wiki=page.locator('.writer-table td').nth(1);
await wiki.click({button:'right'});
await page.getByRole('menuitem',{name:'Edit link',exact:true}).click();
assert.equal(await page.evaluate(()=>getSelection().toString()),theme==='dark'?'guide':'new-guide');
await page.keyboard.type('new-guide');
await page.keyboard.press('Enter');
assert.equal(await wiki.locator('a').getAttribute('href'),'new-guide');
}
const multi=page.locator('.writer-table td').nth(2);
await multi.locator('a').nth(1).click({button:'right'});
await page.getByRole('menuitem',{name:'Edit link',exact:true}).click();
assert.equal(await page.evaluate(()=>getSelection().toString()),'same.md');
await page.keyboard.type('second.md');
await page.keyboard.press('Enter');
assert.equal(await multi.locator('a').first().getAttribute('href'),'same.md');
assert.equal(await multi.locator('a').nth(1).getAttribute('href'),'second.md');
await page.getByRole('button',{name:'Hide outline',exact:true}).click();
await page.getByRole('button',{name:'Files',exact:true}).click();
await page.setViewportSize({width:620,height:700});
await page.waitForTimeout(250);
await page.locator('.table-add-row').scrollIntoViewIfNeeded();
await page.waitForTimeout(100);
await page.locator('.table-add-row').focus();
await page.locator('.rs-tooltip:popover-open').waitFor();
const rect=await page.locator('.rs-tooltip:popover-open').boundingBox();
assert.ok(rect.x>=0 && rect.x+rect.width<=620);
await page.screenshot({path:path.join(output, 'editor-tooltip-narrow-focus.png')});
await page.keyboard.press('Escape');
assert.equal(await page.locator('.rs-tooltip:popover-open').count(),0);
assert.equal(await page.locator('.table-edit-help:visible').count(),0);
for (const mode of ['light','dark']) {
await page.evaluate(async mode => (await import('/src/lib/theme/appearance.ts')).saveAppearance({theme:'default',mode}),mode);
await page.locator('.writer-table td').nth(1).hover();
// Only a formula cell has a hover hint. The footer shows the address of the cell with focus (src/lib/editor/tableControls.ts).
await page.waitForTimeout(500);
assert.ok((await page.locator('.rs-tooltip:popover-open').allInnerTexts()).every(text => !/^[A-Z]+\d+/.test(text)));
await page.screenshot({path:path.join(output, 'editor-cell-tooltip-narrow-'+mode+'.png')});
await page.keyboard.press('Escape');
await page.mouse.move(5,90);
}
console.log('Editor link and tooltip checks passed');
}, { timeout: 15000 });