|
| 1 | +/** |
| 2 | + * @vitest-environment jsdom |
| 3 | + */ |
| 4 | +import { Editor } from '@tiptap/core' |
| 5 | +import { afterEach, beforeAll, describe, expect, it } from 'vitest' |
| 6 | +import { Awareness } from 'y-protocols/awareness' |
| 7 | +import * as Y from 'yjs' |
| 8 | +import { createMarkdownEditorExtensions } from '../editor-extensions' |
| 9 | +import { applyStreamedMarkdownToLiveDoc } from './apply-streamed-markdown' |
| 10 | + |
| 11 | +beforeAll(() => { |
| 12 | + // jsdom does not implement elementFromPoint; the Placeholder extension's viewport tracking calls it |
| 13 | + // on view mount. Returning null makes ProseMirror's posAtCoords fall back gracefully. |
| 14 | + if (!document.elementFromPoint) { |
| 15 | + document.elementFromPoint = () => null |
| 16 | + } |
| 17 | +}) |
| 18 | + |
| 19 | +/** A headless collaborative editor bound to a fresh Y.Doc — the same extension wiring the component uses. */ |
| 20 | +function makeCollabEditor() { |
| 21 | + const doc = new Y.Doc() |
| 22 | + const awareness = new Awareness(doc) |
| 23 | + const editor = new Editor({ |
| 24 | + extensions: createMarkdownEditorExtensions({ |
| 25 | + placeholder: '', |
| 26 | + collaboration: { |
| 27 | + doc, |
| 28 | + awareness, |
| 29 | + user: { name: 'Tester', color: '#ffffff', clientId: doc.clientID }, |
| 30 | + }, |
| 31 | + }), |
| 32 | + content: '', |
| 33 | + }) |
| 34 | + return { editor, doc, awareness } |
| 35 | +} |
| 36 | + |
| 37 | +const teardown: Array<() => void> = [] |
| 38 | +afterEach(() => { |
| 39 | + for (const fn of teardown.splice(0)) fn() |
| 40 | +}) |
| 41 | + |
| 42 | +function track(t: { editor: Editor; doc: Y.Doc; awareness: Awareness }) { |
| 43 | + teardown.push(() => { |
| 44 | + t.editor.destroy() |
| 45 | + t.awareness.destroy() |
| 46 | + t.doc.destroy() |
| 47 | + }) |
| 48 | + return t |
| 49 | +} |
| 50 | + |
| 51 | +describe('applyStreamedMarkdownToLiveDoc', () => { |
| 52 | + it('streams agent content into the live collaborative doc and broadcasts it as Yjs ops', () => { |
| 53 | + const { editor, doc } = track(makeCollabEditor()) |
| 54 | + |
| 55 | + expect(applyStreamedMarkdownToLiveDoc(editor, '# Title\n\nHello world.')).toBe(true) |
| 56 | + expect(editor.getText()).toContain('Hello world') |
| 57 | + |
| 58 | + // The write lands as ops on the shared doc, so any peer receives it (this is what makes a |
| 59 | + // collaborator on /files see the stream without ever holding `streamingContent`). |
| 60 | + const peer = new Y.Doc() |
| 61 | + Y.applyUpdate(peer, Y.encodeStateAsUpdate(doc)) |
| 62 | + expect(peer.getXmlFragment('default').toString()).toContain('Hello world') |
| 63 | + peer.destroy() |
| 64 | + }) |
| 65 | + |
| 66 | + it('returns false when the doc is not yet bound (no ySync binding)', () => { |
| 67 | + // A plain editor with no collaboration has no ySync binding, so the applier reports "not ready" |
| 68 | + // rather than throwing — the streaming tick re-arms until the doc is seeded. |
| 69 | + const editor = new Editor({ |
| 70 | + extensions: createMarkdownEditorExtensions({ placeholder: '' }), |
| 71 | + content: '', |
| 72 | + }) |
| 73 | + teardown.push(() => editor.destroy()) |
| 74 | + expect(applyStreamedMarkdownToLiveDoc(editor, '# Nope')).toBe(false) |
| 75 | + }) |
| 76 | + |
| 77 | + it('keeps agent-streamed ops out of the undo stack while user edits stay undoable', () => { |
| 78 | + const { editor } = track(makeCollabEditor()) |
| 79 | + |
| 80 | + applyStreamedMarkdownToLiveDoc(editor, '# Streamed\n\nAgent wrote this.') |
| 81 | + // The streamed op used AGENT_STREAM_ORIGIN, which the Collaboration UndoManager does not track — |
| 82 | + // so there is nothing to undo, and an undo must not revert the agent's content. |
| 83 | + expect(editor.can().undo()).toBe(false) |
| 84 | + editor.commands.undo() |
| 85 | + expect(editor.getText()).toContain('Agent wrote this') |
| 86 | + |
| 87 | + // A genuine user edit IS captured (origin ySyncPluginKey) — proving the test isn't vacuous: |
| 88 | + // undo works, and it reverts only the user edit, leaving the agent content intact. |
| 89 | + editor.commands.focus('end') |
| 90 | + editor.commands.insertContent(' USER-TYPED') |
| 91 | + expect(editor.getText()).toContain('USER-TYPED') |
| 92 | + expect(editor.can().undo()).toBe(true) |
| 93 | + editor.commands.undo() |
| 94 | + expect(editor.getText()).not.toContain('USER-TYPED') |
| 95 | + expect(editor.getText()).toContain('Agent wrote this') |
| 96 | + }) |
| 97 | + |
| 98 | + it('merges an agent write with a concurrent peer edit (minimal diff, no clobber)', () => { |
| 99 | + const { editor, doc } = track(makeCollabEditor()) |
| 100 | + applyStreamedMarkdownToLiveDoc(editor, 'Alpha paragraph.\n\nBeta paragraph.') |
| 101 | + |
| 102 | + // A peer forks the current state and edits the FIRST paragraph directly on the shared type… |
| 103 | + const remote = new Y.Doc() |
| 104 | + Y.applyUpdate(remote, Y.encodeStateAsUpdate(doc)) |
| 105 | + const remoteFrag = remote.getXmlFragment('default') |
| 106 | + remote.transact(() => { |
| 107 | + const firstPara = remoteFrag.get(0) as Y.XmlElement |
| 108 | + const textNode = firstPara.get(0) as Y.XmlText |
| 109 | + textNode.insert(textNode.toString().length, ' EDITED') |
| 110 | + }) |
| 111 | + |
| 112 | + // …while the agent rewrites the SECOND paragraph through the live editor binding. |
| 113 | + applyStreamedMarkdownToLiveDoc(editor, 'Alpha paragraph.\n\nBeta paragraph, expanded.') |
| 114 | + |
| 115 | + // Exchange updates both ways (as the relay would); a full-document replace would have clobbered |
| 116 | + // the peer's concurrent edit — a minimal `updateYFragment` diff preserves both. |
| 117 | + Y.applyUpdate(doc, Y.encodeStateAsUpdate(remote, Y.encodeStateVector(doc))) |
| 118 | + Y.applyUpdate(remote, Y.encodeStateAsUpdate(doc, Y.encodeStateVector(remote))) |
| 119 | + |
| 120 | + const merged = doc.getXmlFragment('default').toString() |
| 121 | + expect(merged).toContain('EDITED') |
| 122 | + expect(merged).toContain('expanded') |
| 123 | + remote.destroy() |
| 124 | + }) |
| 125 | +}) |
0 commit comments