|
| 1 | +/** |
| 2 | + * Regression tests for the "Cannot resolve a DOM point from Slate point" crash. |
| 3 | + * |
| 4 | + * Background |
| 5 | + * ---------- |
| 6 | + * withYjs#applyRemoteEvents caches editor.selection BEFORE applying remote |
| 7 | + * events to editor.children, then restores it after the events are applied. |
| 8 | + * For transactions with `origin === null` (e.g. bare `Y.applyUpdate(doc, state)` |
| 9 | + * calls in row prefetch, version reset, version revert, etc.) this overwrite |
| 10 | + * is unconditional — the cached selection is written back without re-validating |
| 11 | + * its offsets against the new text-node lengths. When a remote update shortens |
| 12 | + * the text under the local cursor, the restored selection keeps a path that |
| 13 | + * still exists but an offset past the end of the new text. Slate-react's |
| 14 | + * render-time selection sync then calls ReactEditor.toDOMPoint, which finds |
| 15 | + * the DOM text shorter than the requested offset and throws. |
| 16 | + * |
| 17 | + * These tests assert the DESIRED post-fix behavior: |
| 18 | + * • After a remote shrink, the selection is either cleared OR clamped so |
| 19 | + * that `isValidSelection` returns true (offset within new text length). |
| 20 | + * • A remote append leaves a valid selection unchanged. |
| 21 | + * |
| 22 | + * Until withYjs#applyRemoteEvents and #initializeDocumentContent are updated |
| 23 | + * to validate offsets (e.g. via `isValidSelection` + clamp/deselect), the two |
| 24 | + * shrink tests are expected to FAIL — that's how they protect against the |
| 25 | + * regression. |
| 26 | + */ |
| 27 | + |
| 28 | +import { expect } from '@jest/globals'; |
| 29 | +import { createEditor, Editor, Range, Transforms } from 'slate'; |
| 30 | +import * as Y from 'yjs'; |
| 31 | + |
| 32 | +import { yDocToSlateContent } from '@/application/slate-yjs/utils/convert'; |
| 33 | +import { isValidSelection } from '@/application/slate-yjs/utils/transformSelection'; |
| 34 | +import { YjsEditorKey, YTextMap } from '@/application/types'; |
| 35 | + |
| 36 | +import { |
| 37 | + generateId, |
| 38 | + getTestingDocData, |
| 39 | + insertBlock, |
| 40 | + withTestingYDoc, |
| 41 | + withTestingYjsEditor, |
| 42 | +} from './withTestingYjsEditor'; |
| 43 | + |
| 44 | +jest.mock('nanoid'); |
| 45 | +// __mocks__/lodash-es.ts and __mocks__/lodash-es/isEqual.ts are auto-applied |
| 46 | +// for npm modules. They provide a partial surface that breaks translateYEvents |
| 47 | +// here (auto-mocked `omit` is undefined; auto-mocked `isEqual` always returns |
| 48 | +// true so text events get routed into the block-update branch). Use the real |
| 49 | +// implementations so this test exercises the real applyRemoteEvents flow. |
| 50 | +jest.mock('lodash-es', () => jest.requireActual('lodash')); |
| 51 | +jest.mock('lodash-es/isEqual', () => jest.requireActual('lodash/isEqual')); |
| 52 | + |
| 53 | +/** Find the slate path of the first text leaf inside the first paragraph. */ |
| 54 | +function findFirstLeafPath(editor: Editor): number[] { |
| 55 | + for (const [node, path] of Editor.nodes(editor, { at: [], match: (n) => 'text' in n })) { |
| 56 | + if (typeof (node as { text: string }).text === 'string') { |
| 57 | + return path; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + throw new Error('No text leaf found in editor.children'); |
| 62 | +} |
| 63 | + |
| 64 | +/** |
| 65 | + * Mutate `remoteDoc` (which mirrors `localDoc`) and ship the diff to |
| 66 | + * `localDoc` with origin=Remote, so withYjs treats it as a remote event. |
| 67 | + */ |
| 68 | +function shipRemoteUpdate( |
| 69 | + remoteDoc: Y.Doc, |
| 70 | + localDoc: Y.Doc, |
| 71 | + mutate: (textMap: YTextMap) => void |
| 72 | +) { |
| 73 | + const before = Y.encodeStateVector(localDoc); |
| 74 | + const { meta } = getTestingDocData(remoteDoc); |
| 75 | + const textMap = meta.get(YjsEditorKey.text_map) as YTextMap; |
| 76 | + |
| 77 | + mutate(textMap); |
| 78 | + |
| 79 | + const diff = Y.encodeStateAsUpdateV2(remoteDoc, before); |
| 80 | + |
| 81 | + // Use origin=null to exercise withYjs#applyRemoteEvents's "restore cached |
| 82 | + // selection" branch (the buggy one). Other origins skip that branch and |
| 83 | + // rely on Slate's per-operation selection transforms, which clamp offsets |
| 84 | + // automatically and hide the bug. |
| 85 | + Y.applyUpdateV2(localDoc, diff); |
| 86 | +} |
| 87 | + |
| 88 | +function buildSyncedPair(blockId: string, initialText: string) { |
| 89 | + const pageId = generateId(); |
| 90 | + const remoteDoc = withTestingYDoc(pageId); |
| 91 | + const remoteBlock = insertBlock({ |
| 92 | + doc: remoteDoc, |
| 93 | + blockObject: { |
| 94 | + id: blockId, |
| 95 | + ty: 'paragraph', |
| 96 | + relation_id: blockId, |
| 97 | + text_id: blockId, |
| 98 | + data: '{}', |
| 99 | + }, |
| 100 | + }); |
| 101 | + |
| 102 | + remoteBlock.applyDelta([{ insert: initialText }]); |
| 103 | + |
| 104 | + const localDoc = new Y.Doc(); |
| 105 | + |
| 106 | + Y.applyUpdateV2(localDoc, Y.encodeStateAsUpdateV2(remoteDoc)); |
| 107 | + |
| 108 | + return { remoteDoc, localDoc }; |
| 109 | +} |
| 110 | + |
| 111 | +describe('withYjs selection restoration after remote events', () => { |
| 112 | + it('keeps selection valid when remote shrink occurs under cursor', () => { |
| 113 | + const blockId = generateId(); |
| 114 | + const { remoteDoc, localDoc } = buildSyncedPair(blockId, 'Hello World'); |
| 115 | + |
| 116 | + const editor = withTestingYjsEditor(createEditor(), localDoc); |
| 117 | + |
| 118 | + editor.connect(); |
| 119 | + |
| 120 | + // Sanity-check the slate tree mirrors the yjs tree. |
| 121 | + expect(editor.children).toEqual(yDocToSlateContent(localDoc)?.children ?? []); |
| 122 | + |
| 123 | + // Place the local "cursor" at the end of "Hello World" (offset 11). |
| 124 | + const leafPath = findFirstLeafPath(editor); |
| 125 | + |
| 126 | + expect(Editor.string(editor, leafPath)).toBe('Hello World'); |
| 127 | + |
| 128 | + Transforms.select(editor, { path: leafPath, offset: 11 }); |
| 129 | + |
| 130 | + expect(editor.selection).not.toBeNull(); |
| 131 | + expect(Range.isCollapsed(editor.selection!)).toBe(true); |
| 132 | + expect(editor.selection!.anchor.offset).toBe(11); |
| 133 | + expect(isValidSelection(editor, editor.selection!)).toBe(true); |
| 134 | + |
| 135 | + // Collaborator shrinks the same text to "Hi". |
| 136 | + shipRemoteUpdate(remoteDoc, localDoc, (textMap) => { |
| 137 | + const text = textMap.get(blockId); |
| 138 | + |
| 139 | + expect(text).toBeDefined(); |
| 140 | + expect(text!.toString()).toBe('Hello World'); |
| 141 | + |
| 142 | + text!.delete(0, text!.length); |
| 143 | + text!.insert(0, 'Hi'); |
| 144 | + }); |
| 145 | + |
| 146 | + // Editor tree reflects "Hi" (length 2). |
| 147 | + expect(Editor.string(editor, leafPath)).toBe('Hi'); |
| 148 | + |
| 149 | + // Post-fix expectation: the selection must NOT be left pointing past the |
| 150 | + // end of the new text. Either it is cleared, or its offset is clamped to |
| 151 | + // within the new text length. Anything else is the crash precondition |
| 152 | + // that fires "Cannot resolve a DOM point from Slate point" on next render. |
| 153 | + if (editor.selection !== null) { |
| 154 | + expect(isValidSelection(editor, editor.selection)).toBe(true); |
| 155 | + expect(editor.selection.anchor.offset).toBeLessThanOrEqual(2); |
| 156 | + expect(editor.selection.focus.offset).toBeLessThanOrEqual(2); |
| 157 | + } |
| 158 | + }); |
| 159 | + |
| 160 | + it('keeps selection valid when remote trims to before the cursor offset', () => { |
| 161 | + const blockId = generateId(); |
| 162 | + const { remoteDoc, localDoc } = buildSyncedPair(blockId, 'The quick brown fox'); |
| 163 | + |
| 164 | + const editor = withTestingYjsEditor(createEditor(), localDoc); |
| 165 | + |
| 166 | + editor.connect(); |
| 167 | + const leafPath = findFirstLeafPath(editor); |
| 168 | + |
| 169 | + // Cursor at offset 16 — the offending offset from the original error message. |
| 170 | + Transforms.select(editor, { path: leafPath, offset: 16 }); |
| 171 | + expect(isValidSelection(editor, editor.selection!)).toBe(true); |
| 172 | + |
| 173 | + shipRemoteUpdate(remoteDoc, localDoc, (textMap) => { |
| 174 | + const text = textMap.get(blockId)!; |
| 175 | + |
| 176 | + // Trim to "The quick" (length 9), well under offset 16. |
| 177 | + text.delete(9, text.length - 9); |
| 178 | + }); |
| 179 | + |
| 180 | + expect(Editor.string(editor, leafPath)).toBe('The quick'); |
| 181 | + |
| 182 | + // Post-fix: offset 16 is past the new length 9, so the selection must |
| 183 | + // be cleared or clamped (offset <= 9) — never left at 16. |
| 184 | + if (editor.selection !== null) { |
| 185 | + expect(isValidSelection(editor, editor.selection)).toBe(true); |
| 186 | + expect(editor.selection.anchor.offset).toBeLessThanOrEqual(9); |
| 187 | + expect(editor.selection.focus.offset).toBeLessThanOrEqual(9); |
| 188 | + } |
| 189 | + }); |
| 190 | + |
| 191 | + it('sanity: remote append leaves a valid selection unchanged', () => { |
| 192 | + // If a remote update only LENGTHENS text under the cursor, the cached |
| 193 | + // offset stays in-bounds and selection remains valid. This proves the bug |
| 194 | + // is specifically offset > new text length, not "any remote event |
| 195 | + // invalidates selection". |
| 196 | + const blockId = generateId(); |
| 197 | + const { remoteDoc, localDoc } = buildSyncedPair(blockId, 'Hello'); |
| 198 | + |
| 199 | + const editor = withTestingYjsEditor(createEditor(), localDoc); |
| 200 | + |
| 201 | + editor.connect(); |
| 202 | + const leafPath = findFirstLeafPath(editor); |
| 203 | + |
| 204 | + Transforms.select(editor, { path: leafPath, offset: 5 }); |
| 205 | + expect(isValidSelection(editor, editor.selection!)).toBe(true); |
| 206 | + |
| 207 | + shipRemoteUpdate(remoteDoc, localDoc, (textMap) => { |
| 208 | + const text = textMap.get(blockId)!; |
| 209 | + |
| 210 | + text.insert(text.length, ' World'); |
| 211 | + }); |
| 212 | + |
| 213 | + expect(Editor.string(editor, leafPath)).toBe('Hello World'); |
| 214 | + expect(editor.selection!.anchor.offset).toBe(5); |
| 215 | + expect(isValidSelection(editor, editor.selection!)).toBe(true); |
| 216 | + }); |
| 217 | +}); |
0 commit comments