|
3 | 3 | * SPDX-License-Identifier: AGPL-3.0-or-later |
4 | 4 | */ |
5 | 5 |
|
| 6 | +import { Collaboration } from '@tiptap/extension-collaboration' |
6 | 7 | import * as decoding from 'lib0/decoding' |
7 | 8 | import * as encoding from 'lib0/encoding' |
8 | 9 | import * as syncProtocol from 'y-protocols/sync' |
9 | 10 | import { Doc, encodeStateAsUpdate } from 'yjs' |
| 11 | +import { createRichEditor } from '../EditorFactory.js' |
| 12 | +import { createMarkdownSerializer } from '../extensions/Markdown.js' |
10 | 13 | import { decodeArrayBuffer } from '../helpers/base64.ts' |
11 | 14 | import recorded from './fixtures/recorded.js' |
| 15 | +import stepsTwoClients from './fixtures/steps_two_clients.js' |
12 | 16 |
|
13 | 17 | describe('recorded session', () => { |
14 | 18 | const flattened = recorded.flat() |
@@ -139,4 +143,44 @@ describe('recorded session', () => { |
139 | 143 | expect(replies.length).toBe(0) |
140 | 144 | expect(size(replies)).toBe(0) |
141 | 145 | }) |
| 146 | + |
| 147 | + test('detecting out of sync due to missing step via pending structs', () => { |
| 148 | + // 10 steps each inserting one character, alternating between two clients |
| 149 | + const syncSteps = stepsTwoClients.flat() |
| 150 | + const fullDocumentContent = 'abcdefghij' |
| 151 | + // Remove steps 2-5 from syncSteps and store them in missingSteps |
| 152 | + const missingSteps = syncSteps.splice(1, 4) |
| 153 | + |
| 154 | + // Create editor to access the content of the ydoc |
| 155 | + const ydoc = new Doc() |
| 156 | + const tiptap = createRichEditor({ |
| 157 | + extensions: [Collaboration.configure({ document: ydoc })], |
| 158 | + }) |
| 159 | + const serializer = createMarkdownSerializer(tiptap.schema) |
| 160 | + |
| 161 | + // Apply steps 1 and 6-10 (steps 2-5 are missing) |
| 162 | + // Pending structs detected and only first charcter visible in content |
| 163 | + syncSteps.map((step) => processStep(ydoc, step)) |
| 164 | + expect(ydoc.store.pendingStructs).not.toBeNull() |
| 165 | + expect(serializer.serialize(tiptap.state.doc)).toEqual( |
| 166 | + fullDocumentContent.slice(0, 1), |
| 167 | + ) |
| 168 | + |
| 169 | + // Apply missing steps 2-5 |
| 170 | + for (const [i, step] of missingSteps.entries()) { |
| 171 | + processStep(ydoc, step) |
| 172 | + if (i < missingSteps.length - 1) { |
| 173 | + // Each step except the last, one more character gets visible in content |
| 174 | + expect(ydoc.store.pendingStructs).not.toBeNull() |
| 175 | + expect(serializer.serialize(tiptap.state.doc)).toEqual( |
| 176 | + fullDocumentContent.slice(0, i + 2), |
| 177 | + ) |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + // After all missing steps got applied, content is complete and no pending structs detected anymore |
| 182 | + expect(ydoc.store.pendingStructs).toBeNull() |
| 183 | + expect(serializer.serialize(tiptap.state.doc)).toEqual(fullDocumentContent) |
| 184 | + tiptap.destroy() |
| 185 | + }) |
142 | 186 | }) |
0 commit comments