|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | + |
| 3 | +import { collectEntityHitsFromChain } from './entity-at.js'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Build a chain of nested HTMLElements with the dataset stamps the |
| 7 | + * painter would set. `layers[0]` becomes the innermost element; |
| 8 | + * subsequent layers wrap it. Returns the innermost element — the one |
| 9 | + * `document.elementFromPoint` would normally return for a click on |
| 10 | + * the innermost painted run. |
| 11 | + */ |
| 12 | +function buildPaintedChain(layers: Array<{ trackChangeId?: string; commentIds?: string }>): HTMLElement { |
| 13 | + const innerLayer = layers[0]!; |
| 14 | + const inner = document.createElement('span'); |
| 15 | + if (innerLayer.trackChangeId) inner.dataset.trackChangeId = innerLayer.trackChangeId; |
| 16 | + if (innerLayer.commentIds) inner.dataset.commentIds = innerLayer.commentIds; |
| 17 | + |
| 18 | + let outer: HTMLElement = inner; |
| 19 | + for (let i = 1; i < layers.length; i += 1) { |
| 20 | + const layer = layers[i]!; |
| 21 | + const wrapper = document.createElement('span'); |
| 22 | + if (layer.trackChangeId) wrapper.dataset.trackChangeId = layer.trackChangeId; |
| 23 | + if (layer.commentIds) wrapper.dataset.commentIds = layer.commentIds; |
| 24 | + wrapper.appendChild(outer); |
| 25 | + outer = wrapper; |
| 26 | + } |
| 27 | + document.body.appendChild(outer); |
| 28 | + return inner; |
| 29 | +} |
| 30 | + |
| 31 | +describe('collectEntityHitsFromChain', () => { |
| 32 | + it('returns hits for tracked-change and comment data attributes, innermost first', () => { |
| 33 | + const inner = buildPaintedChain([{ trackChangeId: 'tc-1' }, { commentIds: 'c-1' }]); |
| 34 | + |
| 35 | + expect(collectEntityHitsFromChain(inner)).toEqual([ |
| 36 | + { type: 'trackedChange', id: 'tc-1' }, |
| 37 | + { type: 'comment', id: 'c-1' }, |
| 38 | + ]); |
| 39 | + }); |
| 40 | + |
| 41 | + it('expands comma-separated comment ids into one hit per id', () => { |
| 42 | + const inner = buildPaintedChain([{ commentIds: 'c-1,c-2,c-3' }]); |
| 43 | + |
| 44 | + expect(collectEntityHitsFromChain(inner)).toEqual([ |
| 45 | + { type: 'comment', id: 'c-1' }, |
| 46 | + { type: 'comment', id: 'c-2' }, |
| 47 | + { type: 'comment', id: 'c-3' }, |
| 48 | + ]); |
| 49 | + }); |
| 50 | + |
| 51 | + it('deduplicates the same id when it appears multiple times in the chain', () => { |
| 52 | + const inner = buildPaintedChain([{ commentIds: 'c-1' }, { commentIds: 'c-1' }]); |
| 53 | + |
| 54 | + expect(collectEntityHitsFromChain(inner)).toEqual([{ type: 'comment', id: 'c-1' }]); |
| 55 | + }); |
| 56 | + |
| 57 | + it('combines trackedChange + comment + outer comment in document order (innermost → outermost)', () => { |
| 58 | + const inner = buildPaintedChain([{ trackChangeId: 'tc-1' }, { commentIds: 'c-inner' }, { commentIds: 'c-outer' }]); |
| 59 | + |
| 60 | + expect(collectEntityHitsFromChain(inner)).toEqual([ |
| 61 | + { type: 'trackedChange', id: 'tc-1' }, |
| 62 | + { type: 'comment', id: 'c-inner' }, |
| 63 | + { type: 'comment', id: 'c-outer' }, |
| 64 | + ]); |
| 65 | + }); |
| 66 | + |
| 67 | + it('returns [] when the chain has no painted entities', () => { |
| 68 | + const inner = buildPaintedChain([{}]); |
| 69 | + |
| 70 | + expect(collectEntityHitsFromChain(inner)).toEqual([]); |
| 71 | + }); |
| 72 | + |
| 73 | + it('returns [] for null or non-Element starts', () => { |
| 74 | + expect(collectEntityHitsFromChain(null)).toEqual([]); |
| 75 | + expect(collectEntityHitsFromChain({} as never)).toEqual([]); |
| 76 | + }); |
| 77 | + |
| 78 | + it('skips empty ids in a malformed comma list', () => { |
| 79 | + const inner = buildPaintedChain([{ commentIds: ',c-1,,c-2,' }]); |
| 80 | + |
| 81 | + expect(collectEntityHitsFromChain(inner)).toEqual([ |
| 82 | + { type: 'comment', id: 'c-1' }, |
| 83 | + { type: 'comment', id: 'c-2' }, |
| 84 | + ]); |
| 85 | + }); |
| 86 | +}); |
0 commit comments