oficina / mapa Public

Clone
git clone https://amadan.net/oficina/mapa

Plain git — no account needed to clone.

Download

Download this file

1// pagelink.test.mjs — Link to a page over a selection (build note 81):
2// the Markdown it writes, what it refuses, and ⌘L with nothing
3// selected. The corpus-wide run is in fidelity.test.mjs (L3).
4
5import { test } from 'node:test'
6import assert from 'node:assert/strict'
7import { TextSelection, AllSelection } from 'prosemirror-state'
8import { undo } from 'prosemirror-history'
9import { editor, schema } from './testkit.mjs'
10import { serialise } from './serialize.mjs'
11import { mount, prepareSave } from './fidelity.mjs'
12import { linkable, linkSelection, linkCheck, linkPage, pageLinkSource, refusals } from './pagelink.mjs'
13
14// select puts the selection over the first occurrence of words, in the
15// document's text (atoms count one character, as positions do).
16function select (view, words, occurrence = 0) {
17 let found = null
18 let seen = 0
19 view.state.doc.descendants((node, pos) => {
20 if (found !== null || !node.isTextblock) return found === null
21 const text = node.textBetween(0, node.content.size, null, '')
22 let at = text.indexOf(words)
23 while (at >= 0 && seen < occurrence) { seen++; at = text.indexOf(words, at + 1) }
24 if (at >= 0) found = pos + 1 + at
25 return false
26 })
27 assert.notEqual(found, null, `no text reads ${words}`)
28 view.dispatch(view.state.tr.setSelection(TextSelection.create(view.state.doc, found, found + words.length)))
29}
30
31// link selects words, links them to target, and answers what is saved.
32function link (body, words, target) {
33 const { view, fidelity } = editor(body)
34 select(view, words)
35 const found = linkable(view.state)
36 assert.ok(found && !found.refuse, `${words} is linkable: ${found?.refuse}`)
37 assert.equal(view.run(linkSelection(target, found.words)), true)
38 const saved = prepareSave(view, fidelity).body
39 const again = mount(schema, saved)
40 assert.equal(serialise(again.doc, again.fidelity), saved, 'the saved body reads back as written')
41 return { view, fidelity, saved }
42}
43
44test('the selected words become the chip\'s shown text', () => {
45 assert.equal(link('Met the robot firm today.', 'the robot firm', 'Halden Robotics').saved,
46 'Met [[Halden Robotics|the robot firm]] today.')
47})
48
49test('words that are the title make a plain [[Title]]', () => {
50 assert.equal(link('Met Halden Robotics today.', 'Halden Robotics', 'Halden Robotics').saved,
51 'Met [[Halden Robotics]] today.')
52 // The words are kept as written: a title in other case still shows them.
53 assert.equal(link('Met halden robotics today.', 'halden robotics', 'Halden Robotics').saved,
54 'Met [[Halden Robotics|halden robotics]] today.')
55})
56
57test('spaces at the selection\'s edges stay outside the chip', () => {
58 assert.equal(link('Met the firm today.', ' firm ', 'Acme').saved, 'Met the [[Acme|firm]] today.')
59})
60
61test('a date makes a date link, in a heading, an item and a quote', () => {
62 assert.equal(link('# Plan for friday\n\nText.', 'friday', '2026-11-13').saved,
63 '# Plan for [[2026-11-13|friday]]\n\nText.')
64 assert.equal(link('- one\n- call Ana\n- three', 'call Ana', 'Ana').saved,
65 '- one\n- [[Ana|call Ana]]\n- three')
66 assert.equal(link('> said so', 'said', 'Said').saved, '> [[Said|said]] so')
67})
68
69test('bold words lose their bold: a chip carries no marks in Mapa\'s Markdown', () => {
70 assert.equal(link('a **bold words** b', 'bold words', 'Acme').saved, 'a [[Acme|bold words]] b')
71})
72
73test('only the linked block\'s bytes change, and undo gives the body back', () => {
74 const body = '- wide\n\nMet the firm.\n\n##\tTabbed ^h1'
75 const { view, fidelity, saved } = link(body, 'firm', 'Acme')
76 assert.equal(saved, '- wide\n\nMet the [[Acme|firm]].\n\n##\tTabbed ^h1')
77 view.run(undo)
78 assert.equal(serialise(view.state.doc, fidelity), body)
79})
80
81test('a selection across blocks, over a chip or a break, or of brackets is refused', () => {
82 const cases = [
83 ['One here.\n\nTwo there.', 'here.', refusals.blocks, view => {
84 const end = view.state.doc.content.size - 2
85 view.dispatch(view.state.tr.setSelection(TextSelection.create(view.state.doc,
86 view.state.selection.from, end)))
87 }],
88 ['See [[Acme]] now.', 'See  now', refusals.atoms],
89 ['a [b] c', '[b]', refusals.syntax],
90 ['a x]] c', 'x]]', refusals.syntax],
91 ['a b', ' ', refusals.blank]
92 ]
93 for (const [body, words, refusal, widen] of cases) {
94 const { view } = editor(body)
95 select(view, words)
96 widen?.(view)
97 assert.equal(linkable(view.state)?.refuse, refusal, body)
98 assert.equal(view.run(linkSelection('Acme', words.trim())), false, `${body} is not linked`)
99 }
100})
101
102test('a target the words cannot be linked to as typed is refused', () => {
103 assert.equal(pageLinkSource('A|B', 'x'), null)
104 assert.equal(pageLinkSource('A#^b', 'x'), null)
105 assert.equal(pageLinkSource('A]]', ''), null)
106 assert.equal(pageLinkSource('Acme', 'a | b'), '[[Acme|a | b]]')
107 assert.equal(pageLinkSource('Acme', 'a # b'), '[[Acme|a # b]]')
108})
109
110test('⌘L: over a selection it opens Page link; with none it types [[; in code it does nothing', () => {
111 const opened = []
112 const command = linkPage((view, found) => opened.push(found))
113 const words = editor('Met the firm.')
114 select(words.view, 'firm')
115 assert.equal(command(words.view.state, words.view.dispatch.bind(words.view), words.view), true)
116 assert.deepEqual(opened.map(found => found.words), ['firm'])
117
118 const caret = editor('Met ')
119 caret.view.dispatch(caret.view.state.tr.setSelection(TextSelection.atEnd(caret.view.state.doc)))
120 assert.equal(caret.view.run(command), true)
121 assert.equal(serialise(caret.view.state.doc, caret.fidelity), 'Met [[')
122
123 const code = editor('```\ncode\n```')
124 code.view.dispatch(code.view.state.tr.setSelection(TextSelection.create(code.view.state.doc, 3)))
125 assert.equal(code.view.run(command), true)
126 assert.equal(serialise(code.view.state.doc, code.fidelity), '```\ncode\n```')
127})
128
129test('web-linked words are refused: the address would be lost (review M4)', () => {
130 const { view } = editor('See [the site](https://example.com) now.')
131 select(view, 'the site')
132 assert.equal(linkable(view.state)?.refuse, refusals.atoms)
133 select(view, 'site')
134 assert.equal(linkable(view.state)?.refuse, refusals.atoms)
135 assert.equal(view.run(linkSelection('Acme', 'site')), false)
136})
137
138test('words whose Markdown would read otherwise are refused (review M3)', () => {
139 const cases = [
140 ['x!word y', 'word'],
141 ['<!-- foo -->', '-- foo -->']
142 ]
143 for (const [body, words] of cases) {
144 const { view, fidelity } = editor(body)
145 select(view, words)
146 assert.equal(linkable(view.state)?.refuse, refusals.context, body)
147 assert.equal(view.run(linkSelection('Acme', words)), false, body)
148 assert.equal(serialise(view.state.doc, fidelity), body)
149 }
150 // A space before the words is enough after a !.
151 assert.equal(link('x! word y', 'word', 'Acme').saved, 'x! [[Acme|word]] y')
152})
153
154test('⌘A then ⌘L says why, instead of doing nothing (review low 1)', () => {
155 const opened = []
156 const { view } = editor('One.\n\nTwo.')
157 view.dispatch(view.state.tr.setSelection(new AllSelection(view.state.doc)))
158 assert.equal(view.run((state, dispatch) =>
159 linkPage((_, found) => opened.push(found))(state, dispatch, view)), true)
160 assert.deepEqual(opened, [{ refuse: refusals.blocks }])
161})
162
163test('linkCheck says why it refuses (review low 2)', () => {
164 const { view } = editor('Met the firm.')
165 select(view, 'firm')
166 assert.deepEqual(linkCheck(view.state, 'Acme', 'the firm'), { refuse: refusals.moved })
167 assert.deepEqual(linkCheck(view.state, 'A|B', 'firm'), { refuse: refusals.syntax })
168 assert.ok(linkCheck(view.state, 'Acme', 'firm').tr)
169})
170