// pagelink.test.mjs — Link to a page over a selection (build note 81): // the Markdown it writes, what it refuses, and ⌘L with nothing // selected. The corpus-wide run is in fidelity.test.mjs (L3). import { test } from 'node:test' import assert from 'node:assert/strict' import { TextSelection, AllSelection } from 'prosemirror-state' import { undo } from 'prosemirror-history' import { editor, schema } from './testkit.mjs' import { serialise } from './serialize.mjs' import { mount, prepareSave } from './fidelity.mjs' import { linkable, linkSelection, linkCheck, linkPage, pageLinkSource, refusals } from './pagelink.mjs' // select puts the selection over the first occurrence of words, in the // document's text (atoms count one character, as positions do). function select (view, words, occurrence = 0) { let found = null let seen = 0 view.state.doc.descendants((node, pos) => { if (found !== null || !node.isTextblock) return found === null const text = node.textBetween(0, node.content.size, null, '') let at = text.indexOf(words) while (at >= 0 && seen < occurrence) { seen++; at = text.indexOf(words, at + 1) } if (at >= 0) found = pos + 1 + at return false }) assert.notEqual(found, null, `no text reads ${words}`) view.dispatch(view.state.tr.setSelection(TextSelection.create(view.state.doc, found, found + words.length))) } // link selects words, links them to target, and answers what is saved. function link (body, words, target) { const { view, fidelity } = editor(body) select(view, words) const found = linkable(view.state) assert.ok(found && !found.refuse, `${words} is linkable: ${found?.refuse}`) assert.equal(view.run(linkSelection(target, found.words)), true) const saved = prepareSave(view, fidelity).body const again = mount(schema, saved) assert.equal(serialise(again.doc, again.fidelity), saved, 'the saved body reads back as written') return { view, fidelity, saved } } test('the selected words become the chip\'s shown text', () => { assert.equal(link('Met the robot firm today.', 'the robot firm', 'Halden Robotics').saved, 'Met [[Halden Robotics|the robot firm]] today.') }) test('words that are the title make a plain [[Title]]', () => { assert.equal(link('Met Halden Robotics today.', 'Halden Robotics', 'Halden Robotics').saved, 'Met [[Halden Robotics]] today.') // The words are kept as written: a title in other case still shows them. assert.equal(link('Met halden robotics today.', 'halden robotics', 'Halden Robotics').saved, 'Met [[Halden Robotics|halden robotics]] today.') }) test('spaces at the selection\'s edges stay outside the chip', () => { assert.equal(link('Met the firm today.', ' firm ', 'Acme').saved, 'Met the [[Acme|firm]] today.') }) test('a date makes a date link, in a heading, an item and a quote', () => { assert.equal(link('# Plan for friday\n\nText.', 'friday', '2026-11-13').saved, '# Plan for [[2026-11-13|friday]]\n\nText.') assert.equal(link('- one\n- call Ana\n- three', 'call Ana', 'Ana').saved, '- one\n- [[Ana|call Ana]]\n- three') assert.equal(link('> said so', 'said', 'Said').saved, '> [[Said|said]] so') }) test('bold words lose their bold: a chip carries no marks in Mapa\'s Markdown', () => { assert.equal(link('a **bold words** b', 'bold words', 'Acme').saved, 'a [[Acme|bold words]] b') }) test('only the linked block\'s bytes change, and undo gives the body back', () => { const body = '- wide\n\nMet the firm.\n\n##\tTabbed ^h1' const { view, fidelity, saved } = link(body, 'firm', 'Acme') assert.equal(saved, '- wide\n\nMet the [[Acme|firm]].\n\n##\tTabbed ^h1') view.run(undo) assert.equal(serialise(view.state.doc, fidelity), body) }) test('a selection across blocks, over a chip or a break, or of brackets is refused', () => { const cases = [ ['One here.\n\nTwo there.', 'here.', refusals.blocks, view => { const end = view.state.doc.content.size - 2 view.dispatch(view.state.tr.setSelection(TextSelection.create(view.state.doc, view.state.selection.from, end))) }], ['See [[Acme]] now.', 'See  now', refusals.atoms], ['a [b] c', '[b]', refusals.syntax], ['a x]] c', 'x]]', refusals.syntax], ['a b', ' ', refusals.blank] ] for (const [body, words, refusal, widen] of cases) { const { view } = editor(body) select(view, words) widen?.(view) assert.equal(linkable(view.state)?.refuse, refusal, body) assert.equal(view.run(linkSelection('Acme', words.trim())), false, `${body} is not linked`) } }) test('a target the words cannot be linked to as typed is refused', () => { assert.equal(pageLinkSource('A|B', 'x'), null) assert.equal(pageLinkSource('A#^b', 'x'), null) assert.equal(pageLinkSource('A]]', ''), null) assert.equal(pageLinkSource('Acme', 'a | b'), '[[Acme|a | b]]') assert.equal(pageLinkSource('Acme', 'a # b'), '[[Acme|a # b]]') }) test('⌘L: over a selection it opens Page link; with none it types [[; in code it does nothing', () => { const opened = [] const command = linkPage((view, found) => opened.push(found)) const words = editor('Met the firm.') select(words.view, 'firm') assert.equal(command(words.view.state, words.view.dispatch.bind(words.view), words.view), true) assert.deepEqual(opened.map(found => found.words), ['firm']) const caret = editor('Met ') caret.view.dispatch(caret.view.state.tr.setSelection(TextSelection.atEnd(caret.view.state.doc))) assert.equal(caret.view.run(command), true) assert.equal(serialise(caret.view.state.doc, caret.fidelity), 'Met [[') const code = editor('```\ncode\n```') code.view.dispatch(code.view.state.tr.setSelection(TextSelection.create(code.view.state.doc, 3))) assert.equal(code.view.run(command), true) assert.equal(serialise(code.view.state.doc, code.fidelity), '```\ncode\n```') }) test('web-linked words are refused: the address would be lost (review M4)', () => { const { view } = editor('See [the site](https://example.com) now.') select(view, 'the site') assert.equal(linkable(view.state)?.refuse, refusals.atoms) select(view, 'site') assert.equal(linkable(view.state)?.refuse, refusals.atoms) assert.equal(view.run(linkSelection('Acme', 'site')), false) }) test('words whose Markdown would read otherwise are refused (review M3)', () => { const cases = [ ['x!word y', 'word'], ['', '-- foo -->'] ] for (const [body, words] of cases) { const { view, fidelity } = editor(body) select(view, words) assert.equal(linkable(view.state)?.refuse, refusals.context, body) assert.equal(view.run(linkSelection('Acme', words)), false, body) assert.equal(serialise(view.state.doc, fidelity), body) } // A space before the words is enough after a !. assert.equal(link('x! word y', 'word', 'Acme').saved, 'x! [[Acme|word]] y') }) test('⌘A then ⌘L says why, instead of doing nothing (review low 1)', () => { const opened = [] const { view } = editor('One.\n\nTwo.') view.dispatch(view.state.tr.setSelection(new AllSelection(view.state.doc))) assert.equal(view.run((state, dispatch) => linkPage((_, found) => opened.push(found))(state, dispatch, view)), true) assert.deepEqual(opened, [{ refuse: refusals.blocks }]) }) test('linkCheck says why it refuses (review low 2)', () => { const { view } = editor('Met the firm.') select(view, 'firm') assert.deepEqual(linkCheck(view.state, 'Acme', 'the firm'), { refuse: refusals.moved }) assert.deepEqual(linkCheck(view.state, 'A|B', 'firm'), { refuse: refusals.syntax }) assert.ok(linkCheck(view.state, 'Acme', 'firm').tr) })