|
1 | 1 | import { describe, it, expect } from 'vitest'; |
2 | | -import { shiftBlockPositions, shiftCachedBlocks } from './cache.js'; |
| 2 | +import { FlowBlockCache, shiftBlockPositions, shiftCachedBlocks } from './cache.js'; |
3 | 3 | import type { FlowBlock, ParagraphBlock, ImageBlock, DrawingBlock, Run } from '@superdoc/contracts'; |
4 | 4 |
|
| 5 | +describe('FlowBlockCache', () => { |
| 6 | + const makeParagraphNode = (text: string, rev: number) => ({ |
| 7 | + type: 'paragraph', |
| 8 | + attrs: { sdBlockId: 'p1', sdBlockRev: rev, paraId: null }, |
| 9 | + content: [{ type: 'run', content: [{ type: 'text', text }] }], |
| 10 | + }); |
| 11 | + |
| 12 | + const mockBlocks: FlowBlock[] = [ |
| 13 | + { kind: 'paragraph', id: 'p1', runs: [{ text: 'hello', pmStart: 0, pmEnd: 5 } as Run] } as ParagraphBlock, |
| 14 | + ]; |
| 15 | + |
| 16 | + it('returns MISS when no cached entry exists', () => { |
| 17 | + const cache = new FlowBlockCache(); |
| 18 | + cache.begin(); |
| 19 | + |
| 20 | + const node = makeParagraphNode('hello', 1); |
| 21 | + const result = cache.get('p1', node); |
| 22 | + |
| 23 | + expect(result.entry).toBeNull(); |
| 24 | + }); |
| 25 | + |
| 26 | + it('returns HIT when sdBlockRev matches', () => { |
| 27 | + const cache = new FlowBlockCache(); |
| 28 | + const node = makeParagraphNode('hello', 1); |
| 29 | + |
| 30 | + // Populate cache |
| 31 | + cache.begin(); |
| 32 | + cache.set('p1', JSON.stringify(node), 1, mockBlocks, 0); |
| 33 | + cache.commit(); |
| 34 | + |
| 35 | + // Same node, same rev → HIT |
| 36 | + cache.begin(); |
| 37 | + const result = cache.get('p1', node); |
| 38 | + |
| 39 | + expect(result.entry).not.toBeNull(); |
| 40 | + expect(result.entry!.blocks).toBe(mockBlocks); |
| 41 | + }); |
| 42 | + |
| 43 | + it('retains serialized node across fast-path hits so external fallback stays incremental', () => { |
| 44 | + const cache = new FlowBlockCache(); |
| 45 | + const node = makeParagraphNode('hello', 5); |
| 46 | + |
| 47 | + // Render 1: cache is populated with serialized JSON. |
| 48 | + cache.begin(); |
| 49 | + cache.set('p1', JSON.stringify(node), 5, mockBlocks, 0); |
| 50 | + cache.commit(); |
| 51 | + |
| 52 | + // Render 2: local-only fast path hit, caller writes lookup payload into next generation. |
| 53 | + cache.begin(); |
| 54 | + const fastPathHit = cache.get('p1', node); |
| 55 | + expect(fastPathHit.entry).not.toBeNull(); |
| 56 | + cache.set('p1', fastPathHit.nodeJson, fastPathHit.nodeRev, fastPathHit.entry!.blocks, 0); |
| 57 | + cache.commit(); |
| 58 | + |
| 59 | + // Render 3: collaboration/external change mode requires JSON fallback. |
| 60 | + // With unchanged content this should still be a HIT. |
| 61 | + cache.setHasExternalChanges(true); |
| 62 | + cache.begin(); |
| 63 | + const externalFallback = cache.get('p1', node); |
| 64 | + |
| 65 | + expect(externalFallback.entry).not.toBeNull(); |
| 66 | + }); |
| 67 | + |
| 68 | + it('returns MISS when sdBlockRev differs', () => { |
| 69 | + const cache = new FlowBlockCache(); |
| 70 | + const nodeV1 = makeParagraphNode('hello', 1); |
| 71 | + const nodeV2 = makeParagraphNode('hello world', 2); |
| 72 | + |
| 73 | + // Populate with v1 |
| 74 | + cache.begin(); |
| 75 | + cache.set('p1', JSON.stringify(nodeV1), 1, mockBlocks, 0); |
| 76 | + cache.commit(); |
| 77 | + |
| 78 | + // v2 has different rev → MISS |
| 79 | + cache.begin(); |
| 80 | + const result = cache.get('p1', nodeV2); |
| 81 | + |
| 82 | + expect(result.entry).toBeNull(); |
| 83 | + }); |
| 84 | + |
| 85 | + it('returns MISS when content changes with same sdBlockRev and externalChanges flag is set', () => { |
| 86 | + const cache = new FlowBlockCache(); |
| 87 | + const nodeOriginal = makeParagraphNode('hello', 5); |
| 88 | + const nodeModifiedByYjs = makeParagraphNode('hello world from remote user', 5); // Same rev, different content! |
| 89 | + |
| 90 | + // Populate cache with original content |
| 91 | + cache.begin(); |
| 92 | + cache.set('p1', JSON.stringify(nodeOriginal), 5, mockBlocks, 0); |
| 93 | + cache.commit(); |
| 94 | + |
| 95 | + // Y.js-origin transaction changed content but blockNodePlugin didn't increment sdBlockRev. |
| 96 | + // With the externalChanges flag, the fast path falls through to JSON comparison. |
| 97 | + cache.setHasExternalChanges(true); |
| 98 | + cache.begin(); |
| 99 | + const result = cache.get('p1', nodeModifiedByYjs); |
| 100 | + |
| 101 | + expect(result.entry).toBeNull(); // Correct: JSON comparison catches the content change |
| 102 | + }); |
| 103 | + |
| 104 | + it('returns HIT when content is unchanged even with externalChanges flag', () => { |
| 105 | + const cache = new FlowBlockCache(); |
| 106 | + const node = makeParagraphNode('hello', 5); |
| 107 | + |
| 108 | + cache.begin(); |
| 109 | + cache.set('p1', JSON.stringify(node), 5, mockBlocks, 0); |
| 110 | + cache.commit(); |
| 111 | + |
| 112 | + // externalChanges flag is set but content is identical — should still HIT |
| 113 | + cache.setHasExternalChanges(true); |
| 114 | + cache.begin(); |
| 115 | + const result = cache.get('p1', node); |
| 116 | + |
| 117 | + expect(result.entry).not.toBeNull(); // JSON comparison confirms content is same |
| 118 | + }); |
| 119 | + |
| 120 | + it('without externalChanges flag, same sdBlockRev trusts fast path (HIT)', () => { |
| 121 | + const cache = new FlowBlockCache(); |
| 122 | + const nodeOriginal = makeParagraphNode('hello', 5); |
| 123 | + const nodeModified = makeParagraphNode('hello world', 5); // Same rev, different content |
| 124 | + |
| 125 | + cache.begin(); |
| 126 | + cache.set('p1', JSON.stringify(nodeOriginal), 5, mockBlocks, 0); |
| 127 | + cache.commit(); |
| 128 | + |
| 129 | + // Without the flag, fast path trusts sdBlockRev → HIT (this is the performance path) |
| 130 | + cache.begin(); |
| 131 | + const result = cache.get('p1', nodeModified); |
| 132 | + |
| 133 | + expect(result.entry).not.toBeNull(); // Fast path HIT — correct for local-only edits |
| 134 | + }); |
| 135 | + |
| 136 | + it('commit() clears externalChanges flag', () => { |
| 137 | + const cache = new FlowBlockCache(); |
| 138 | + const nodeOriginal = makeParagraphNode('hello', 5); |
| 139 | + const nodeModified = makeParagraphNode('hello world', 5); |
| 140 | + |
| 141 | + cache.begin(); |
| 142 | + cache.set('p1', JSON.stringify(nodeOriginal), 5, mockBlocks, 0); |
| 143 | + cache.commit(); |
| 144 | + |
| 145 | + // Set flag and commit (which should clear it) |
| 146 | + cache.setHasExternalChanges(true); |
| 147 | + cache.begin(); |
| 148 | + cache.set('p1', JSON.stringify(nodeOriginal), 5, mockBlocks, 0); |
| 149 | + cache.commit(); // This clears externalChanges |
| 150 | + |
| 151 | + // Now query with modified content — flag was cleared, so fast path applies |
| 152 | + cache.begin(); |
| 153 | + const result = cache.get('p1', nodeModified); |
| 154 | + |
| 155 | + expect(result.entry).not.toBeNull(); // Fast path HIT — flag was cleared by commit |
| 156 | + }); |
| 157 | + |
| 158 | + it('returns MISS via JSON fallback when sdBlockRev is unavailable', () => { |
| 159 | + const cache = new FlowBlockCache(); |
| 160 | + const nodeNoRev = { type: 'paragraph', attrs: { sdBlockId: 'p1' }, content: [{ type: 'text', text: 'hello' }] }; |
| 161 | + const nodeNoRevModified = { |
| 162 | + type: 'paragraph', |
| 163 | + attrs: { sdBlockId: 'p1' }, |
| 164 | + content: [{ type: 'text', text: 'hello world' }], |
| 165 | + }; |
| 166 | + |
| 167 | + // Populate without rev |
| 168 | + cache.begin(); |
| 169 | + cache.set('p1', JSON.stringify(nodeNoRev), null, mockBlocks, 0); |
| 170 | + cache.commit(); |
| 171 | + |
| 172 | + // Different content, no rev → falls to JSON comparison → MISS |
| 173 | + cache.begin(); |
| 174 | + const result = cache.get('p1', nodeNoRevModified); |
| 175 | + |
| 176 | + expect(result.entry).toBeNull(); // Correct: JSON comparison catches the change |
| 177 | + }); |
| 178 | + |
| 179 | + it('clear() resets all cache state', () => { |
| 180 | + const cache = new FlowBlockCache(); |
| 181 | + const node = makeParagraphNode('hello', 1); |
| 182 | + |
| 183 | + cache.begin(); |
| 184 | + cache.set('p1', JSON.stringify(node), 1, mockBlocks, 0); |
| 185 | + cache.commit(); |
| 186 | + |
| 187 | + cache.clear(); |
| 188 | + |
| 189 | + cache.begin(); |
| 190 | + const result = cache.get('p1', node); |
| 191 | + |
| 192 | + expect(result.entry).toBeNull(); // Cache was cleared |
| 193 | + }); |
| 194 | + |
| 195 | + it('commit() discards entries not seen in current render', () => { |
| 196 | + const cache = new FlowBlockCache(); |
| 197 | + const nodeA = makeParagraphNode('hello', 1); |
| 198 | + const nodeB = { |
| 199 | + ...makeParagraphNode('world', 1), |
| 200 | + attrs: { ...makeParagraphNode('world', 1).attrs, sdBlockId: 'p2' }, |
| 201 | + }; |
| 202 | + |
| 203 | + // Render 1: both paragraphs |
| 204 | + cache.begin(); |
| 205 | + cache.set('p1', JSON.stringify(nodeA), 1, mockBlocks, 0); |
| 206 | + cache.set('p2', JSON.stringify(nodeB), 1, mockBlocks, 10); |
| 207 | + cache.commit(); |
| 208 | + |
| 209 | + // Render 2: only p1 (p2 was deleted) |
| 210 | + cache.begin(); |
| 211 | + cache.get('p1', nodeA); // access p1 |
| 212 | + cache.set('p1', JSON.stringify(nodeA), 1, mockBlocks, 0); |
| 213 | + cache.commit(); |
| 214 | + |
| 215 | + // Render 3: p2 should be gone |
| 216 | + cache.begin(); |
| 217 | + const result = cache.get('p2', nodeB); |
| 218 | + |
| 219 | + expect(result.entry).toBeNull(); // p2 was pruned |
| 220 | + }); |
| 221 | +}); |
| 222 | + |
5 | 223 | describe('shiftBlockPositions', () => { |
6 | 224 | describe('paragraph blocks', () => { |
7 | 225 | it('shifts pmStart and pmEnd in runs', () => { |
|
0 commit comments