|
| 1 | +import { test, expect } from '../../fixtures/superdoc.js'; |
| 2 | +import { |
| 3 | + selectionScope, |
| 4 | + contentControlLifecycle, |
| 5 | + caretLocation, |
| 6 | + bodyMutation, |
| 7 | + type InlineSdtSnapshot, |
| 8 | + type InlineSdtRange, |
| 9 | +} from '../../helpers/sdt.js'; |
| 10 | + |
| 11 | +/** |
| 12 | + * Pure unit tests for the parity axis helpers - no browser. Synthetic |
| 13 | + * snapshots exercise each axis value so the consumer specs can rely on these |
| 14 | + * one-liners instead of re-deriving from/to math (which is where the |
| 15 | + * edge-direction bug crept in earlier). |
| 16 | + */ |
| 17 | + |
| 18 | +// Inline SDT at pos 8: node [8,24), content [9,23). " After" follows. |
| 19 | +const RANGE: InlineSdtRange = { id: '1', pos: 8, start: 9, end: 23, nodeEnd: 24, content: 'inline value' }; |
| 20 | + |
| 21 | +function snap(p: Partial<InlineSdtSnapshot>): InlineSdtSnapshot { |
| 22 | + return { |
| 23 | + from: 0, |
| 24 | + to: 0, |
| 25 | + empty: true, |
| 26 | + nodeType: null, |
| 27 | + sdtExists: true, |
| 28 | + sdtContent: 'inline value', |
| 29 | + sdtPos: 8, |
| 30 | + docText: 'Before inline value After', |
| 31 | + docSize: 33, |
| 32 | + paragraphCount: 1, |
| 33 | + ...p, |
| 34 | + }; |
| 35 | +} |
| 36 | + |
| 37 | +test.describe('selectionScope', () => { |
| 38 | + test('collapsed', () => { |
| 39 | + expect(selectionScope(snap({ from: 12, to: 12, empty: true }), RANGE)).toBe('collapsed'); |
| 40 | + }); |
| 41 | + test('cc-content (exact content range)', () => { |
| 42 | + expect(selectionScope(snap({ from: 9, to: 23, empty: false }), RANGE)).toBe('cc-content'); |
| 43 | + }); |
| 44 | + test('whole-content-control (node incl boundaries)', () => { |
| 45 | + expect(selectionScope(snap({ from: 8, to: 24, empty: false }), RANGE)).toBe('whole-content-control'); |
| 46 | + }); |
| 47 | + test('within-cc (sub-range of content)', () => { |
| 48 | + expect(selectionScope(snap({ from: 10, to: 14, empty: false }), RANGE)).toBe('within-cc'); |
| 49 | + }); |
| 50 | + test('cc-and-beyond (overlaps and spills out)', () => { |
| 51 | + expect(selectionScope(snap({ from: 12, to: 28, empty: false }), RANGE)).toBe('cc-and-beyond'); |
| 52 | + }); |
| 53 | + test('whole-document', () => { |
| 54 | + expect(selectionScope(snap({ from: 0, to: 33, empty: false, docSize: 33 }), RANGE)).toBe('whole-document'); |
| 55 | + }); |
| 56 | + test('outside-cc', () => { |
| 57 | + expect(selectionScope(snap({ from: 1, to: 5, empty: false }), RANGE)).toBe('outside-cc'); |
| 58 | + }); |
| 59 | +}); |
| 60 | + |
| 61 | +test.describe('contentControlLifecycle', () => { |
| 62 | + test('preserved (present, content unchanged)', () => { |
| 63 | + expect(contentControlLifecycle(snap({}), snap({}))).toBe('preserved'); |
| 64 | + }); |
| 65 | + test('preserved (present, content changed but not emptied)', () => { |
| 66 | + expect(contentControlLifecycle(snap({ sdtContent: 'inline value' }), snap({ sdtContent: 'inline valu' }))).toBe( |
| 67 | + 'preserved', |
| 68 | + ); |
| 69 | + }); |
| 70 | + test('emptied (non-empty to empty, still present)', () => { |
| 71 | + expect(contentControlLifecycle(snap({ sdtContent: 'inline value' }), snap({ sdtContent: '' }))).toBe('emptied'); |
| 72 | + }); |
| 73 | + test('deleted (present to absent)', () => { |
| 74 | + expect(contentControlLifecycle(snap({ sdtExists: true }), snap({ sdtExists: false, sdtContent: null }))).toBe( |
| 75 | + 'deleted', |
| 76 | + ); |
| 77 | + }); |
| 78 | + test('created (absent to present)', () => { |
| 79 | + expect(contentControlLifecycle(snap({ sdtExists: false, sdtContent: null }), snap({ sdtExists: true }))).toBe( |
| 80 | + 'created', |
| 81 | + ); |
| 82 | + }); |
| 83 | + test('none (absent throughout)', () => { |
| 84 | + expect( |
| 85 | + contentControlLifecycle( |
| 86 | + snap({ sdtExists: false, sdtContent: null }), |
| 87 | + snap({ sdtExists: false, sdtContent: null }), |
| 88 | + ), |
| 89 | + ).toBe('none'); |
| 90 | + }); |
| 91 | +}); |
| 92 | + |
| 93 | +test.describe('caretLocation', () => { |
| 94 | + test('inside-cc', () => { |
| 95 | + expect(caretLocation(snap({ from: 15, to: 15, empty: true }), RANGE)).toBe('inside-cc'); |
| 96 | + }); |
| 97 | + test('before-cc', () => { |
| 98 | + expect(caretLocation(snap({ from: 8, to: 8, empty: true }), RANGE)).toBe('before-cc'); |
| 99 | + }); |
| 100 | + test('after-cc', () => { |
| 101 | + expect(caretLocation(snap({ from: 24, to: 24, empty: true }), RANGE)).toBe('after-cc'); |
| 102 | + }); |
| 103 | + test('null for a range selection', () => { |
| 104 | + expect(caretLocation(snap({ from: 9, to: 23, empty: false }), RANGE)).toBeNull(); |
| 105 | + }); |
| 106 | +}); |
| 107 | + |
| 108 | +test.describe('bodyMutation', () => { |
| 109 | + test('none', () => { |
| 110 | + expect(bodyMutation(snap({}), snap({}))).toBe('none'); |
| 111 | + }); |
| 112 | + test('text-changed', () => { |
| 113 | + expect( |
| 114 | + bodyMutation(snap({ docText: 'Before inline value After' }), snap({ docText: 'Before inline valu After' })), |
| 115 | + ).toBe('text-changed'); |
| 116 | + }); |
| 117 | + test('structure-changed (paragraph count differs)', () => { |
| 118 | + expect(bodyMutation(snap({ paragraphCount: 1 }), snap({ paragraphCount: 2 }))).toBe('structure-changed'); |
| 119 | + }); |
| 120 | +}); |
0 commit comments