|
| 1 | +import { describe, expect, it } from 'bun:test' |
| 2 | +import { |
| 3 | + type Annotation, |
| 4 | + buildSvgOverlay, |
| 5 | +} from './annotations' |
| 6 | + |
| 7 | +describe('scripts/annotations.ts', () => { |
| 8 | + it('should render an empty SVG when annotations array is empty', () => { |
| 9 | + const svg = buildSvgOverlay([], 800, 450).toString() |
| 10 | + expect(svg).toContain('<svg width="800" height="450" viewBox="0 0 800 450"') |
| 11 | + expect(svg).not.toContain('<rect') |
| 12 | + expect(svg).not.toContain('<text') |
| 13 | + }) |
| 14 | + |
| 15 | + it('should render badge annotations with single and multi-character text', () => { |
| 16 | + const annotations: Annotation[] = [ |
| 17 | + { type: 'badge', x: 100, y: 50, text: '1' }, |
| 18 | + { type: 'badge', x: 200, y: 150, text: '12' }, |
| 19 | + ] |
| 20 | + |
| 21 | + const svg = buildSvgOverlay(annotations, 800, 450).toString() |
| 22 | + expect(svg).toContain('<svg width="800" height="450"') |
| 23 | + expect(svg).toContain('fill="#ffffff" stroke="#1a1a1a"') |
| 24 | + expect(svg).toContain('>1<') |
| 25 | + expect(svg).toContain('>12<') |
| 26 | + |
| 27 | + // Single character badge width is 20, multi-character badge width is 24 |
| 28 | + expect(svg).toContain('width="20"') |
| 29 | + expect(svg).toContain('width="24"') |
| 30 | + }) |
| 31 | + |
| 32 | + it('should render selection, box, and highlight annotations with default and custom styling', () => { |
| 33 | + const annotations: Annotation[] = [ |
| 34 | + { |
| 35 | + type: 'box', |
| 36 | + x: 24, |
| 37 | + y: 84, |
| 38 | + width: 104, |
| 39 | + height: 64, |
| 40 | + rx: 4, |
| 41 | + }, |
| 42 | + { |
| 43 | + type: 'highlight', |
| 44 | + x: 50, |
| 45 | + y: 100, |
| 46 | + width: 200, |
| 47 | + height: 80, |
| 48 | + stroke: '#ff0000', |
| 49 | + strokeWidth: 2, |
| 50 | + }, |
| 51 | + { |
| 52 | + type: 'selection', |
| 53 | + x: 10, |
| 54 | + y: 20, |
| 55 | + width: 50, |
| 56 | + height: 30, |
| 57 | + }, |
| 58 | + ] |
| 59 | + |
| 60 | + const svg = buildSvgOverlay(annotations, 800, 450).toString() |
| 61 | + expect(svg).toContain('x="24"') |
| 62 | + expect(svg).toContain('y="84"') |
| 63 | + expect(svg).toContain('width="104"') |
| 64 | + expect(svg).toContain('height="64"') |
| 65 | + expect(svg).toContain('rx="4"') |
| 66 | + expect(svg).toContain('fill="none"') |
| 67 | + expect(svg).toContain('stroke="#ffffff"') |
| 68 | + |
| 69 | + // Custom highlight stroke and strokeWidth |
| 70 | + expect(svg).toContain('stroke="#ff0000"') |
| 71 | + expect(svg).toContain('stroke-width="2"') |
| 72 | + |
| 73 | + // Selection defaults |
| 74 | + expect(svg).toContain('x="10"') |
| 75 | + expect(svg).toContain('y="20"') |
| 76 | + expect(svg).toContain('width="50"') |
| 77 | + expect(svg).toContain('height="30"') |
| 78 | + }) |
| 79 | + |
| 80 | + it('should scale box and highlight annotations proportionally for large resolutions (3200x1800)', () => { |
| 81 | + const annotations: Annotation[] = [ |
| 82 | + { |
| 83 | + type: 'box', |
| 84 | + x: 24, |
| 85 | + y: 84, |
| 86 | + width: 104, |
| 87 | + height: 64, |
| 88 | + }, |
| 89 | + { |
| 90 | + type: 'highlight', |
| 91 | + x: 50, |
| 92 | + y: 100, |
| 93 | + width: 200, |
| 94 | + height: 80, |
| 95 | + stroke: '#00ff00', |
| 96 | + strokeWidth: 2, |
| 97 | + }, |
| 98 | + ] |
| 99 | + |
| 100 | + const svgLarge = buildSvgOverlay(annotations, 3200, 1800).toString() |
| 101 | + expect(svgLarge).toContain('x="96"') |
| 102 | + expect(svgLarge).toContain('y="336"') |
| 103 | + expect(svgLarge).toContain('width="416"') |
| 104 | + expect(svgLarge).toContain('height="256"') |
| 105 | + expect(svgLarge).toContain('stroke="#00ff00"') |
| 106 | + expect(svgLarge).toContain('stroke-width="8"') |
| 107 | + }) |
| 108 | + |
| 109 | + it('should render text and label annotations with automatic width calculation', () => { |
| 110 | + const annotations: Annotation[] = [ |
| 111 | + { |
| 112 | + type: 'text', |
| 113 | + x: 24, |
| 114 | + y: 152, |
| 115 | + width: 188, |
| 116 | + height: 20, |
| 117 | + text: 'Placeholder content for the slot', |
| 118 | + }, |
| 119 | + { |
| 120 | + type: 'label', |
| 121 | + x: 100, |
| 122 | + y: 200, |
| 123 | + text: 'Auto width label', |
| 124 | + backgroundColor: '#000000', |
| 125 | + color: '#ffffff', |
| 126 | + fontWeight: 'normal', |
| 127 | + }, |
| 128 | + ] |
| 129 | + |
| 130 | + const svg = buildSvgOverlay(annotations, 800, 450).toString() |
| 131 | + expect(svg).toContain('x="24"') |
| 132 | + expect(svg).toContain('y="152"') |
| 133 | + expect(svg).toContain('width="188"') |
| 134 | + expect(svg).toContain('fill="#ffffff"') |
| 135 | + expect(svg).toContain('Placeholder content for the slot') |
| 136 | + expect(svg).toContain('text-anchor="middle"') |
| 137 | + |
| 138 | + // Label with custom colors and auto-computed width |
| 139 | + expect(svg).toContain('fill="#000000"') |
| 140 | + expect(svg).toContain('fill="#ffffff"') |
| 141 | + expect(svg).toContain('font-weight="normal"') |
| 142 | + expect(svg).toContain('Auto width label') |
| 143 | + }) |
| 144 | + |
| 145 | + it('should scale text annotations properly at 3200x1800', () => { |
| 146 | + const annotations: Annotation[] = [ |
| 147 | + { |
| 148 | + type: 'text', |
| 149 | + x: 24, |
| 150 | + y: 152, |
| 151 | + width: 188, |
| 152 | + height: 20, |
| 153 | + text: 'Scaled text', |
| 154 | + }, |
| 155 | + ] |
| 156 | + |
| 157 | + const svgLarge = buildSvgOverlay(annotations, 3200, 1800).toString() |
| 158 | + expect(svgLarge).toContain('x="96"') |
| 159 | + expect(svgLarge).toContain('y="608"') |
| 160 | + expect(svgLarge).toContain('width="752"') |
| 161 | + expect(svgLarge).toContain('height="80"') |
| 162 | + expect(svgLarge).toContain('font-size="46"') |
| 163 | + }) |
| 164 | + |
| 165 | + it('should ignore unrecognized annotation types gracefully', () => { |
| 166 | + const annotations = [ |
| 167 | + { type: 'unsupported-type', x: 0, y: 0 } as unknown as Annotation, |
| 168 | + ] |
| 169 | + |
| 170 | + const svg = buildSvgOverlay(annotations, 800, 450).toString() |
| 171 | + expect(svg).toContain('<svg') |
| 172 | + expect(svg).not.toContain('<rect') |
| 173 | + }) |
| 174 | +}) |
0 commit comments