import { test } from "node:test"; import assert from "node:assert/strict"; import { documentLinks, documentLinkAt } from "../src/lib/editor/documentLinks.ts"; test("document links keep complete source ranges and decode Markdown destinations", () => { const source = '[Local]( "Title") and [Web](https://example.com/a_(b)) and [[Another note|Alias]]'; const links = documentLinks(source); assert.deepEqual(links.map(link => [link.kind, link.target]), [["md", "notes/two words.md"], ["md", "https://example.com/a_(b)"], ["wiki", "Another note"]]); for (const link of links) { assert.equal(documentLinkAt(source, link.from + 2)?.target, link.target); assert.equal(documentLinkAt(source, link.to - 1)?.target, link.target); } }); test("literal code and images cannot become document links", () => { const source = '![Image](image.png) `https://example.com`\n\n```md\n[Hidden](hidden.md)\n[[Hidden]]\n```\n\n[Open](open.md)'; assert.deepEqual(documentLinks(source).map(link => link.target), ["open.md"]); }); test("bare and angle links omit prose punctuation and preserve balanced parentheses", () => { const source = 'Go to https://example.com/a_(b). Next (). Mail .'; assert.deepEqual(documentLinks(source).map(link => link.target), ["https://example.com/a_(b)", "https://example.org", "mailto:person@example.org"]); }); test("edit link selects only its destination, preserving labels, titles, brackets, and spacing", () => { const source = '[Local]( "Title") and [Web](https://example.com/a_\\(b\\)) and [[ Another note#Heading |Alias]] and .'; const links = documentLinks(source); assert.deepEqual(links.map(link => source.slice(link.targetFrom, link.targetTo)), ["notes/two words.md", "https://example.com/a_\\(b\\)", "Another note#Heading", "https://example.org"]); }); test("wiki image embeds do not expose document actions", () => { assert.deepEqual(documentLinks('![[image.png]] [[note|label]]').map(link => link.target), ["note"]); });