perf(tui): lazily seed virtual history heights (#16523)

This commit is contained in:
kshitij 2026-04-27 07:55:45 -07:00 committed by GitHub
parent 9b55365f6f
commit 98d75dea5a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 75 additions and 22 deletions

View file

@ -0,0 +1,39 @@
import { describe, expect, it, vi } from 'vitest'
import { ensureVirtualItemHeight } from '../hooks/useVirtualHistory.js'
describe('ensureVirtualItemHeight', () => {
it('reuses cached heights without invoking the estimator', () => {
const heights = new Map([['a', 7]])
const estimateHeight = vi.fn(() => 99)
expect(ensureVirtualItemHeight(heights, 'a', 0, 4, estimateHeight)).toBe(7)
expect(estimateHeight).not.toHaveBeenCalled()
expect(heights.get('a')).toBe(7)
})
it('lazily seeds missing heights from the estimator', () => {
const heights = new Map<string, number>()
const estimateHeight = vi.fn((index: number) => 10 + index)
expect(ensureVirtualItemHeight(heights, 'b', 2, 4, estimateHeight)).toBe(12)
expect(estimateHeight).toHaveBeenCalledTimes(1)
expect(estimateHeight).toHaveBeenCalledWith(2, 'b')
expect(heights.get('b')).toBe(12)
})
it('falls back to the default estimate when no estimator is provided', () => {
const heights = new Map<string, number>()
expect(ensureVirtualItemHeight(heights, 'c', 0, 4)).toBe(4)
expect(heights.get('c')).toBe(4)
})
it('normalizes non-positive estimates to a minimum of one row', () => {
const heights = new Map<string, number>()
const estimateHeight = vi.fn(() => 0)
expect(ensureVirtualItemHeight(heights, 'd', 0, 0, estimateHeight)).toBe(1)
expect(heights.get('d')).toBe(1)
})
})