Something went wrong. Try again.
A monorepo for writing specs and other serious text on the AT Protocol.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204// Structured ops become suggestions that accept and reject cleanly.import { test } from 'node:test';import assert from 'node:assert/strict';import { applySuggestion, revertSuggestion } from '@handlewithcare/prosemirror-suggest-changes';import type { EditorState } from '@tiptap/pm/state';import { opTransaction, OpError, type Op } from '../src/suggestions/ops.ts';import { listSuggestions } from '../src/suggestions/list.ts';import { blocksOf, findBlock } from '../src/doc.ts';import { SAMPLE } from '../demo/sample.ts';import { newId, stateOf, suggest, text } from './harness.ts';
const run = (state: EditorState, op: Op, id = 'S') => suggest(state, opTransaction(state, op, { newId }), id);const accept = (state: EditorState, id = 'S') => { let out = state; applySuggestion(id)(state, (tr) => (out = state.apply(tr))); return out;};const reject = (state: EditorState, id = 'S') => { let out = state; revertSuggestion(id)(state, (tr) => (out = state.apply(tr))); return out;};
test('replace: only the words that differ, under one id; accept keeps the new text, reject the old', () => { const s0 = stateOf(SAMPLE); const s1 = run(s0, { op: 'replace', block: 'b-intro-1', find: 'is a very small, self-describing record', with: 'is a small, self-describing record' }); const [item] = listSuggestions(s1.doc); assert.equal(listSuggestions(s1.doc).length, 1); assert.equal(item!.kind, 'deletion'); assert.equal(item!.deleted, 'very'); assert.equal(item!.inserted, ''); assert.equal(item!.block, 'b-intro-1'); assert.match(text(accept(s1), 'b-intro-1'), /is a small, self-describing/); assert.equal(text(reject(s1), 'b-intro-1'), text(s0, 'b-intro-1')); assert.equal(listSuggestions(accept(s1).doc).length, 0); // a word swapped is a replacement of that word const s2 = run(s0, { op: 'replace', block: 'b-intro-1', find: 'very small', with: 'very compact' }); const [swap] = listSuggestions(s2.doc); assert.equal(swap!.kind, 'replacement'); assert.equal(swap!.deleted, 'small'); assert.equal(swap!.inserted, 'compact');});
test('replace: the same text back, or only quotes and spacing changed, is refused', () => { const s0 = stateOf(SAMPLE); assert.throws(() => opTransaction(s0, { op: 'replace', block: 'b-intro-1', find: 'very small', with: 'very small' }, { newId }), /same as the old/); assert.throws(() => opTransaction(s0, { op: 'replace', block: 'b-intro-1', find: 'a very small, self-describing record', with: 'a very small, self-describing record' }, { newId }), /same as the old/); assert.throws(() => opTransaction(s0, { op: 'retitle', block: 's-intro', title: findBlock(s0.doc, 's-intro')!.text }, { newId }), /already that/);});
test('replace: new words take the marks of what they replace', () => { const s0 = stateOf({ type: 'doc', content: [ { type: 'section', attrs: { id: 's1', anchor: 's1', anchorTouched: false }, content: [ { type: 'sectionTitle', content: [{ type: 'text', text: 'T' }] }, { type: 'paragraph', attrs: { id: 'p1' }, content: [{ type: 'text', text: 'The ' }, { type: 'text', text: 'quick brown fox', marks: [{ type: 'bold' }] }, { type: 'text', text: ' jumps.' }] }, ], }, ], }); const s1 = accept(run(s0, { op: 'replace', block: 'p1', find: 'The quick brown fox jumps.', with: 'The quick red fox leaps.' })); const bold: string[] = []; s1.doc.descendants((n) => { if (n.isText && n.marks.some((m) => m.type.name === 'bold')) bold.push(n.text ?? ''); return true; }); assert.equal(text(s1, 'p1'), 'The quick red fox leaps.'); assert.equal(bold.join(''), 'quick red fox');});
test('delete and insert text, with nth and loose matching', () => { const s0 = stateOf(SAMPLE); const s1 = run(s0, { op: 'delete', block: 'b-intro-1', find: 'basically ' }); assert.equal(listSuggestions(s1.doc)[0]!.kind, 'deletion'); assert.match(text(accept(s1), 'b-intro-1'), /Lanterns are cheap to make/); const s2 = run(s0, { op: 'insert', block: 'b-model-1', text: ' (see below)', after: 'four fields' }); assert.match(text(accept(s2), 'b-model-1'), /four fields \(see below\)\./); // the second "the" in "the the" const s3 = run(s0, { op: 'delete', block: 'b-intro-1', find: 'the ', nth: 2 }); assert.ok(text(accept(s3), 'b-intro-1').includes('wich is the whole point')); // whitespace and case are forgiven in what is quoted (and the doc's own text is what gets kept) const s4 = run(s0, { op: 'replace', block: 'b-intro-1', find: 'A LANTERN is', with: 'A lantern was' }); assert.equal(listSuggestions(s4.doc).length, 1); assert.equal(listSuggestions(s4.doc)[0]!.deleted, 'is'); assert.equal(listSuggestions(s4.doc)[0]!.inserted, 'was');});
test('format marks are modifications', () => { const s0 = stateOf(SAMPLE); const s1 = run(s0, { op: 'format', block: 'b-model-1', find: 'four fields', mark: 'bold' }); const [item] = listSuggestions(s1.doc); assert.equal(item!.kind, 'formatting'); assert.deepEqual(item!.changes, ['add bold']); const bolded = (s: EditorState) => { let yes = false; s.doc.descendants((n) => { if (n.isText && n.text === 'four fields' && n.marks.some((m) => m.type.name === 'bold')) yes = true; return true; }); return yes; }; assert.equal(bolded(accept(s1)), true); assert.equal(bolded(reject(s1)), false);});
test('retitle a section', () => { const s0 = stateOf(SAMPLE); const s1 = run(s0, { op: 'retitle', block: 's-intro', title: 'Overview' }); assert.equal(findBlock(accept(s1).doc, 's-intro')!.text, 'Overview'); assert.equal(findBlock(reject(s1).doc, 's-intro')!.text, 'Introduction');});
test('delete-block and insert-block are block-level suggestions', () => { const s0 = stateOf(SAMPLE); const s1 = run(s0, { op: 'delete-block', block: 'b-intro-3' }); const [del] = listSuggestions(s1.doc); assert.equal(del!.kind, 'block-deletion'); assert.ok(del!.deleted.startsWith('The key words')); assert.equal(findBlock(accept(s1).doc, 'b-intro-3'), undefined); assert.ok(findBlock(reject(s1).doc, 'b-intro-3'));
const s2 = run(s0, { op: 'insert-block', after: 'b-intro-1', text: 'A **new** paragraph.' }); const [ins] = listSuggestions(s2.doc); assert.equal(ins!.kind, 'block-insertion'); assert.equal(ins!.inserted, 'A new paragraph.'); const ids = (s: EditorState) => blocksOf(s.doc).map((b) => b.id); assert.equal(ids(accept(s2)).length, ids(s0).length + 1); assert.deepEqual(ids(reject(s2)), ids(s0)); // a section at the end, with a title and a paragraph const s3 = run(s0, { op: 'insert-block', type: 'section', title: 'Open questions', text: 'What is undecided.' }); const last = blocksOf(accept(s3).doc).filter((b) => b.type === 'section').pop(); assert.equal(last!.text, 'Open questions'); // into a section: after its blocks, before its subsections const s4 = run(s0, { op: 'insert-block', in: 's-publish', text: 'Last word.' }); const pub = blocksOf(accept(s4).doc); const i = pub.findIndex((b) => b.id === 'b-pub-2'); assert.equal(pub[i + 1]!.text, 'Last word.');});
test('move-block is one suggestion that changes the order when accepted', () => { const s0 = stateOf(SAMPLE); const s1 = run(s0, { op: 'move-block', block: 'b-intro-3', before: 'b-intro-1' }); const [mv] = listSuggestions(s1.doc); assert.equal(mv!.kind, 'block-move'); const order = (s: EditorState) => blocksOf(s.doc).filter((b) => b.id.startsWith('b-intro')).map((b) => b.id); assert.deepEqual(order(accept(s1)), ['b-intro-3', 'b-intro-1', 'b-intro-2']); assert.deepEqual(order(reject(s1)), ['b-intro-1', 'b-intro-2', 'b-intro-3']); assert.throws(() => opTransaction(s0, { op: 'move-block', block: 's-intro', after: 'b-intro-1' }, { newId }), OpError);});
test('split-block and merge-block', () => { const s0 = stateOf(SAMPLE); const s1 = run(s0, { op: 'split-block', block: 'b-intro-1', at: 'at this point in time.' }); assert.equal(listSuggestions(s1.doc).length, 1); const after = accept(s1); const all = blocksOf(after.doc); const intro = all.slice(all.findIndex((b) => b.id === 's-intro') + 1, all.findIndex((b) => b.id === 's-model')).filter((b) => b.type === 'paragraph'); assert.equal(intro.length, 4); assert.ok(intro[0]!.text.endsWith('at this point in time.')); assert.ok(intro[1]!.text.startsWith('Lanterns are basically')); assert.notEqual(intro[0]!.id, intro[1]!.id); assert.equal(text(reject(s1), 'b-intro-1'), text(s0, 'b-intro-1'));
const s2 = run(s0, { op: 'merge-block', block: 'b-watch-1' }); assert.equal(listSuggestions(s2.doc).length, 1); const merged = accept(s2); assert.match(text(merged, 'b-watch-1'), /unreliable\. Watchers MUST NOT/); assert.equal(findBlock(merged.doc, 'b-watch-2'), undefined); assert.equal(text(reject(s2), 'b-watch-2'), text(s0, 'b-watch-2'));});
test('comments carry a range; bad ops fail loudly', () => { const s0 = stateOf(SAMPLE); const tr = opTransaction(s0, { op: 'comment', block: 'b-model-1', text: 'Say which ones.', find: 'etc.' }, { newId }); const meta = tr.getMeta('draft-comment') as { from: number; to: number }; assert.equal(s0.doc.textBetween(meta.from, meta.to), 'etc.'); assert.throws(() => opTransaction(s0, { op: 'replace', block: 'nope', find: 'x', with: 'y' }, { newId }), /no block with id/); assert.throws(() => opTransaction(s0, { op: 'replace', block: 'b-intro-1', find: 'not in there at all', with: 'y' }, { newId }), /is not in block/);});
test('insert-block with outline makes outline notes; a section can be outlined whole', () => { const s0 = stateOf(SAMPLE); const s1 = accept(run(s0, { op: 'insert-block', after: 'b-intro-1', text: 'What goes here.', outline: true })); const note = blocksOf(s1.doc)[blocksOf(s1.doc).findIndex((b) => b.id === 'b-intro-1') + 1]!; assert.equal(note.type, 'paragraph'); assert.equal(note.node.attrs.outline, true); assert.equal(note.text, 'What goes here.'); const s2 = accept(run(s0, { op: 'insert-block', type: 'section', title: 'Costs', text: 'Trade-offs.\n\nLimits.', outline: true })); const sec = blocksOf(s2.doc).filter((b) => b.type === 'section').pop()!; assert.equal(sec.text, 'Costs'); const notes: boolean[] = []; sec.node.forEach((c) => { if (c.type.name === 'paragraph') notes.push(c.attrs.outline === true); }); assert.deepEqual(notes, [true, true]); // plain prose stays plain const s3 = accept(run(s0, { op: 'insert-block', after: 'b-intro-1', text: 'Prose.' })); assert.equal(blocksOf(s3.doc)[blocksOf(s3.doc).findIndex((b) => b.id === 'b-intro-1') + 1]!.node.attrs.outline, false);});