|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { normalizeDuplicateBlockIdentitiesInContent } from './normalizeDuplicateBlockIdentitiesInContent.js'; |
| 3 | + |
| 4 | +describe('normalizeDuplicateBlockIdentitiesInContent', () => { |
| 5 | + const paragraph = (attrs = {}, text = 'text') => ({ |
| 6 | + type: 'paragraph', |
| 7 | + attrs, |
| 8 | + marks: [], |
| 9 | + content: [{ type: 'text', text, marks: [] }], |
| 10 | + }); |
| 11 | + |
| 12 | + const table = (content = [], attrs = {}) => ({ type: 'table', attrs, marks: [], content }); |
| 13 | + const row = (content = [], attrs = {}) => ({ type: 'tableRow', attrs, marks: [], content }); |
| 14 | + const cell = (content = [], attrs = {}) => ({ type: 'tableCell', attrs, marks: [], content }); |
| 15 | + const image = (attrs = {}) => ({ type: 'image', attrs, marks: [] }); |
| 16 | + |
| 17 | + it('deduplicates duplicate paraId values while keeping the first occurrence unchanged', () => { |
| 18 | + const content = [paragraph({ paraId: 'DUPLICATE' }, 'A'), paragraph({ paraId: 'DUPLICATE' }, 'B')]; |
| 19 | + |
| 20 | + normalizeDuplicateBlockIdentitiesInContent(content); |
| 21 | + |
| 22 | + expect(content[0].attrs.paraId).toBe('DUPLICATE'); |
| 23 | + expect(content[1].attrs.paraId).not.toBe('DUPLICATE'); |
| 24 | + expect(content[1].attrs.paraId).toMatch(/^[0-9A-F]{8}$/); |
| 25 | + }); |
| 26 | + |
| 27 | + it('rewrites the field that actually provided the identity (sdBlockId fallback for paragraph)', () => { |
| 28 | + const content = [paragraph({ sdBlockId: 'SAME' }, 'A'), paragraph({ sdBlockId: 'SAME' }, 'B')]; |
| 29 | + |
| 30 | + normalizeDuplicateBlockIdentitiesInContent(content); |
| 31 | + |
| 32 | + expect(content[0].attrs.sdBlockId).toBe('SAME'); |
| 33 | + expect(content[1].attrs.sdBlockId).not.toBe('SAME'); |
| 34 | + expect(content[1].attrs.sdBlockId).toMatch(/^[0-9A-F]{8}$/); |
| 35 | + expect(content[1].attrs.paraId).toBeUndefined(); |
| 36 | + }); |
| 37 | + |
| 38 | + it('prioritizes sdBlockId over paraId when both are present on paragraphs', () => { |
| 39 | + const content = [ |
| 40 | + paragraph({ paraId: 'P1', sdBlockId: 'SAME' }, 'A'), |
| 41 | + paragraph({ paraId: 'P2', sdBlockId: 'SAME' }, 'B'), |
| 42 | + ]; |
| 43 | + |
| 44 | + normalizeDuplicateBlockIdentitiesInContent(content); |
| 45 | + |
| 46 | + expect(content[0].attrs.sdBlockId).toBe('SAME'); |
| 47 | + expect(content[1].attrs.sdBlockId).not.toBe('SAME'); |
| 48 | + expect(content[1].attrs.sdBlockId).toMatch(/^[0-9A-F]{8}$/); |
| 49 | + expect(content[0].attrs.paraId).toBe('P1'); |
| 50 | + expect(content[1].attrs.paraId).toBe('P2'); |
| 51 | + }); |
| 52 | + |
| 53 | + it('deduplicates table blockId when paraId/sdBlockId are not present', () => { |
| 54 | + const content = [table([], { blockId: 'TABLE-ID' }), table([], { blockId: 'TABLE-ID' })]; |
| 55 | + |
| 56 | + normalizeDuplicateBlockIdentitiesInContent(content); |
| 57 | + |
| 58 | + expect(content[0].attrs.blockId).toBe('TABLE-ID'); |
| 59 | + expect(content[1].attrs.blockId).not.toBe('TABLE-ID'); |
| 60 | + expect(content[1].attrs.blockId).toMatch(/^[0-9A-F]{8}$/); |
| 61 | + }); |
| 62 | + |
| 63 | + it('does not rewrite non-block identity fields (e.g. image attrs.id)', () => { |
| 64 | + const content = [image({ id: '42', src: 'a.png' }), image({ id: '42', src: 'b.png' })]; |
| 65 | + |
| 66 | + normalizeDuplicateBlockIdentitiesInContent(content); |
| 67 | + |
| 68 | + expect(content[0].attrs.id).toBe('42'); |
| 69 | + expect(content[1].attrs.id).toBe('42'); |
| 70 | + }); |
| 71 | + |
| 72 | + it('deduplicates identities across nested table block nodes', () => { |
| 73 | + const content = [ |
| 74 | + table( |
| 75 | + [ |
| 76 | + row( |
| 77 | + [ |
| 78 | + cell([paragraph({ paraId: 'CELLPARA' }, 'R1C1')], { paraId: 'CELLID' }), |
| 79 | + cell([paragraph({ paraId: 'CELLPARA' }, 'R1C2')], { paraId: 'CELLID' }), |
| 80 | + ], |
| 81 | + { paraId: 'ROWID' }, |
| 82 | + ), |
| 83 | + row([cell([paragraph({ paraId: 'ROWID' }, 'R2C1')], { paraId: 'CELLID' })], { paraId: 'ROWID' }), |
| 84 | + ], |
| 85 | + { paraId: 'TABLEID' }, |
| 86 | + ), |
| 87 | + ]; |
| 88 | + |
| 89 | + normalizeDuplicateBlockIdentitiesInContent(content); |
| 90 | + |
| 91 | + const identities = new Set(); |
| 92 | + const duplicates = new Set(); |
| 93 | + const collect = (node) => { |
| 94 | + if (!node || typeof node !== 'object') return; |
| 95 | + const attrs = node.attrs ?? {}; |
| 96 | + const id = |
| 97 | + (typeof attrs.paraId === 'string' && attrs.paraId) || |
| 98 | + (typeof attrs.sdBlockId === 'string' && attrs.sdBlockId) || |
| 99 | + (typeof attrs.blockId === 'string' && attrs.blockId) || |
| 100 | + (typeof attrs.id === 'string' && attrs.id) || |
| 101 | + (typeof attrs.uuid === 'string' && attrs.uuid); |
| 102 | + if (id) { |
| 103 | + if (identities.has(id)) duplicates.add(id); |
| 104 | + identities.add(id); |
| 105 | + } |
| 106 | + if (Array.isArray(node.content)) node.content.forEach(collect); |
| 107 | + }; |
| 108 | + |
| 109 | + content.forEach(collect); |
| 110 | + expect(duplicates.size).toBe(0); |
| 111 | + }); |
| 112 | +}); |
0 commit comments