|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | +import { |
| 3 | + ChatHeightCache, |
| 4 | + calculateOffsetForIndex, |
| 5 | + calculateTotalHeight |
| 6 | +} from './chatMeasurement.svelte.js' |
| 7 | + |
| 8 | +type Msg = { id: string } |
| 9 | +const getId = (m: Msg) => m.id |
| 10 | +const msgs = (ids: string[]): Msg[] => ids.map((id) => ({ id })) |
| 11 | + |
| 12 | +describe('ChatHeightCache', () => { |
| 13 | + it('returns undefined for unmeasured messages', () => { |
| 14 | + const cache = new ChatHeightCache() |
| 15 | + expect(cache.get('a')).toBeUndefined() |
| 16 | + }) |
| 17 | + |
| 18 | + it('stores and retrieves heights', () => { |
| 19 | + const cache = new ChatHeightCache() |
| 20 | + cache.set('a', 100) |
| 21 | + expect(cache.get('a')).toBe(100) |
| 22 | + }) |
| 23 | + |
| 24 | + it('returns true when height changes', () => { |
| 25 | + const cache = new ChatHeightCache() |
| 26 | + expect(cache.set('a', 100)).toBe(true) |
| 27 | + expect(cache.set('a', 200)).toBe(true) |
| 28 | + }) |
| 29 | + |
| 30 | + it('returns false when height is the same', () => { |
| 31 | + const cache = new ChatHeightCache() |
| 32 | + cache.set('a', 100) |
| 33 | + expect(cache.set('a', 100)).toBe(false) |
| 34 | + }) |
| 35 | + |
| 36 | + it('tracks size correctly', () => { |
| 37 | + const cache = new ChatHeightCache() |
| 38 | + expect(cache.size).toBe(0) |
| 39 | + cache.set('a', 50) |
| 40 | + expect(cache.size).toBe(1) |
| 41 | + cache.set('b', 60) |
| 42 | + expect(cache.size).toBe(2) |
| 43 | + }) |
| 44 | + |
| 45 | + it('has() works correctly', () => { |
| 46 | + const cache = new ChatHeightCache() |
| 47 | + expect(cache.has('a')).toBe(false) |
| 48 | + cache.set('a', 50) |
| 49 | + expect(cache.has('a')).toBe(true) |
| 50 | + }) |
| 51 | + |
| 52 | + it('delete removes an entry', () => { |
| 53 | + const cache = new ChatHeightCache() |
| 54 | + cache.set('a', 50) |
| 55 | + cache.set('b', 60) |
| 56 | + cache.delete('a') |
| 57 | + expect(cache.has('a')).toBe(false) |
| 58 | + expect(cache.get('a')).toBeUndefined() |
| 59 | + expect(cache.size).toBe(1) |
| 60 | + }) |
| 61 | + |
| 62 | + it('delete on nonexistent key is a no-op', () => { |
| 63 | + const cache = new ChatHeightCache() |
| 64 | + const vBefore = cache.version |
| 65 | + cache.delete('nonexistent') |
| 66 | + expect(cache.version).toBe(vBefore) |
| 67 | + }) |
| 68 | + |
| 69 | + it('clear removes all entries', () => { |
| 70 | + const cache = new ChatHeightCache() |
| 71 | + cache.set('a', 50) |
| 72 | + cache.set('b', 60) |
| 73 | + cache.clear() |
| 74 | + expect(cache.size).toBe(0) |
| 75 | + expect(cache.has('a')).toBe(false) |
| 76 | + expect(cache.has('b')).toBe(false) |
| 77 | + }) |
| 78 | + |
| 79 | + it('version increments on set, delete, clear', () => { |
| 80 | + const cache = new ChatHeightCache() |
| 81 | + const v0 = cache.version |
| 82 | + cache.set('a', 50) |
| 83 | + expect(cache.version).toBe(v0 + 1) |
| 84 | + cache.set('a', 60) |
| 85 | + expect(cache.version).toBe(v0 + 2) |
| 86 | + cache.delete('a') |
| 87 | + expect(cache.version).toBe(v0 + 3) |
| 88 | + cache.set('b', 70) |
| 89 | + cache.clear() |
| 90 | + expect(cache.version).toBe(v0 + 5) |
| 91 | + }) |
| 92 | + |
| 93 | + it('version does not increment on no-op set', () => { |
| 94 | + const cache = new ChatHeightCache() |
| 95 | + cache.set('a', 50) |
| 96 | + const v = cache.version |
| 97 | + cache.set('a', 50) |
| 98 | + expect(cache.version).toBe(v) |
| 99 | + }) |
| 100 | +}) |
| 101 | + |
| 102 | +describe('calculateTotalHeight', () => { |
| 103 | + it('returns 0 for empty messages', () => { |
| 104 | + const cache = new ChatHeightCache() |
| 105 | + expect(calculateTotalHeight([], getId, cache, 40)).toBe(0) |
| 106 | + }) |
| 107 | + |
| 108 | + it('uses estimated height for unmeasured messages', () => { |
| 109 | + const cache = new ChatHeightCache() |
| 110 | + const m = msgs(['1', '2', '3']) |
| 111 | + expect(calculateTotalHeight(m, getId, cache, 40)).toBe(120) |
| 112 | + }) |
| 113 | + |
| 114 | + it('uses measured height when available', () => { |
| 115 | + const cache = new ChatHeightCache() |
| 116 | + cache.set('1', 100) |
| 117 | + cache.set('2', 50) |
| 118 | + const m = msgs(['1', '2', '3']) |
| 119 | + // 100 + 50 + 40(estimated) |
| 120 | + expect(calculateTotalHeight(m, getId, cache, 40)).toBe(190) |
| 121 | + }) |
| 122 | + |
| 123 | + it('uses all measured heights when fully measured', () => { |
| 124 | + const cache = new ChatHeightCache() |
| 125 | + cache.set('1', 80) |
| 126 | + cache.set('2', 60) |
| 127 | + cache.set('3', 100) |
| 128 | + const m = msgs(['1', '2', '3']) |
| 129 | + expect(calculateTotalHeight(m, getId, cache, 40)).toBe(240) |
| 130 | + }) |
| 131 | +}) |
| 132 | + |
| 133 | +describe('calculateOffsetForIndex', () => { |
| 134 | + it('returns 0 for index 0', () => { |
| 135 | + const cache = new ChatHeightCache() |
| 136 | + const m = msgs(['1', '2', '3']) |
| 137 | + expect(calculateOffsetForIndex(m, 0, getId, cache, 40)).toBe(0) |
| 138 | + }) |
| 139 | + |
| 140 | + it('sums estimated heights for unmeasured messages', () => { |
| 141 | + const cache = new ChatHeightCache() |
| 142 | + const m = msgs(['1', '2', '3', '4']) |
| 143 | + // offset for index 2 = height[0] + height[1] = 40 + 40 |
| 144 | + expect(calculateOffsetForIndex(m, 2, getId, cache, 40)).toBe(80) |
| 145 | + }) |
| 146 | + |
| 147 | + it('uses measured heights when available', () => { |
| 148 | + const cache = new ChatHeightCache() |
| 149 | + cache.set('1', 100) |
| 150 | + cache.set('2', 50) |
| 151 | + const m = msgs(['1', '2', '3']) |
| 152 | + // offset for index 2 = 100 + 50 |
| 153 | + expect(calculateOffsetForIndex(m, 2, getId, cache, 40)).toBe(150) |
| 154 | + }) |
| 155 | + |
| 156 | + it('mixes measured and estimated', () => { |
| 157 | + const cache = new ChatHeightCache() |
| 158 | + cache.set('2', 80) |
| 159 | + const m = msgs(['1', '2', '3', '4']) |
| 160 | + // offset for index 3 = 40(est) + 80(measured) + 40(est) |
| 161 | + expect(calculateOffsetForIndex(m, 3, getId, cache, 40)).toBe(160) |
| 162 | + }) |
| 163 | + |
| 164 | + it('clamps to messages length', () => { |
| 165 | + const cache = new ChatHeightCache() |
| 166 | + const m = msgs(['1', '2']) |
| 167 | + // index 10 is beyond array — should sum all heights |
| 168 | + expect(calculateOffsetForIndex(m, 10, getId, cache, 40)).toBe(80) |
| 169 | + }) |
| 170 | + |
| 171 | + it('returns 0 for empty messages', () => { |
| 172 | + const cache = new ChatHeightCache() |
| 173 | + expect(calculateOffsetForIndex([], 5, getId, cache, 40)).toBe(0) |
| 174 | + }) |
| 175 | +}) |
0 commit comments