|
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 2 | +import type { CoreTextNodeProps } from '../CoreTextNode.js'; |
| 3 | + |
| 4 | +// Mock the font handler so renderText/generateTextLayout can run without a |
| 5 | +// real loaded font. getFontData is only invoked on a layout-cache MISS, so the |
| 6 | +// call count is our probe for cache hits vs misses. |
| 7 | +vi.mock('./SdfFontHandler.js', () => { |
| 8 | + const fontData = { |
| 9 | + data: { |
| 10 | + common: { base: 0, scaleW: 512, scaleH: 512, lineHeight: 50 }, |
| 11 | + info: { size: 42 }, |
| 12 | + distanceField: { distanceRange: 4 }, |
| 13 | + }, |
| 14 | + glyphMap: new Map(), |
| 15 | + kernings: {}, |
| 16 | + atlasTexture: {}, |
| 17 | + metrics: {}, |
| 18 | + maxCharHeight: 50, |
| 19 | + }; |
| 20 | + const metrics = { |
| 21 | + ascender: 40, |
| 22 | + descender: -10, |
| 23 | + lineGap: 0, |
| 24 | + capHeight: 30, |
| 25 | + xHeight: 20, |
| 26 | + }; |
| 27 | + return { |
| 28 | + type: 'sdf', |
| 29 | + init: vi.fn(), |
| 30 | + getFontData: vi.fn(() => fontData), |
| 31 | + getFontMetrics: vi.fn(() => metrics), |
| 32 | + measureText: vi.fn((text: string) => text.length * 10), |
| 33 | + getAtlas: vi.fn(() => null), |
| 34 | + }; |
| 35 | +}); |
| 36 | + |
| 37 | +import SdfTextRenderer from './SdfTextRenderer.js'; |
| 38 | +import * as SdfFontHandler from './SdfFontHandler.js'; |
| 39 | + |
| 40 | +const makeProps = (text: string): CoreTextNodeProps => |
| 41 | + ({ |
| 42 | + text, |
| 43 | + fontFamily: 'Test', |
| 44 | + fontStyle: 'normal', |
| 45 | + fontSize: 42, |
| 46 | + letterSpacing: 0, |
| 47 | + lineHeight: 0, |
| 48 | + maxHeight: 0, |
| 49 | + maxWidth: 100000, |
| 50 | + maxLines: 0, |
| 51 | + textAlign: 'left', |
| 52 | + wordBreak: 'normal', |
| 53 | + overflowSuffix: '', |
| 54 | + } as unknown as CoreTextNodeProps); |
| 55 | + |
| 56 | +const initRenderer = (cacheSize: number): void => { |
| 57 | + const fakeStage = { |
| 58 | + options: { textLayoutCacheSize: cacheSize }, |
| 59 | + shManager: { |
| 60 | + registerShaderType: vi.fn(), |
| 61 | + createShader: vi.fn(() => ({})), |
| 62 | + }, |
| 63 | + }; |
| 64 | + SdfTextRenderer.init(fakeStage as never); |
| 65 | +}; |
| 66 | + |
| 67 | +const render = (text: string): void => { |
| 68 | + SdfTextRenderer.renderText(makeProps(text)); |
| 69 | +}; |
| 70 | + |
| 71 | +describe('SdfTextRenderer layout cache', () => { |
| 72 | + beforeEach(() => { |
| 73 | + // Empty the module-level cache between tests, then reset call counts. |
| 74 | + initRenderer(0); |
| 75 | + SdfTextRenderer.cleanup(); |
| 76 | + vi.clearAllMocks(); |
| 77 | + }); |
| 78 | + |
| 79 | + it('reuses the cached layout for identical strings', () => { |
| 80 | + initRenderer(10); |
| 81 | + |
| 82 | + render('Badge'); |
| 83 | + render('Badge'); |
| 84 | + |
| 85 | + // Second render is a cache hit: no fresh layout generation. |
| 86 | + expect(SdfFontHandler.getFontData).toHaveBeenCalledTimes(1); |
| 87 | + }); |
| 88 | + |
| 89 | + it('caches long strings too (no length-based skip)', () => { |
| 90 | + initRenderer(10); |
| 91 | + const long = 'x'.repeat(500); |
| 92 | + |
| 93 | + render(long); |
| 94 | + render(long); |
| 95 | + |
| 96 | + // Bounded purely by the LRU cap, not by length. |
| 97 | + expect(SdfFontHandler.getFontData).toHaveBeenCalledTimes(1); |
| 98 | + }); |
| 99 | + |
| 100 | + it('cleanup trims to the cap and evicts least-recently-used first', () => { |
| 101 | + initRenderer(2); |
| 102 | + |
| 103 | + render('A'); |
| 104 | + render('B'); |
| 105 | + render('C'); |
| 106 | + // Re-access 'A' so it becomes most-recently-used; 'B' is now the LRU. |
| 107 | + render('A'); |
| 108 | + |
| 109 | + SdfTextRenderer.cleanup(); |
| 110 | + |
| 111 | + vi.clearAllMocks(); |
| 112 | + render('B'); // evicted -> miss |
| 113 | + render('A'); // survived -> hit |
| 114 | + render('C'); // survived -> hit |
| 115 | + |
| 116 | + expect(SdfFontHandler.getFontData).toHaveBeenCalledTimes(1); |
| 117 | + }); |
| 118 | + |
| 119 | + it('cleanup is a no-op while under the cap', () => { |
| 120 | + initRenderer(10); |
| 121 | + |
| 122 | + render('one'); |
| 123 | + render('two'); |
| 124 | + |
| 125 | + SdfTextRenderer.cleanup(); |
| 126 | + |
| 127 | + vi.clearAllMocks(); |
| 128 | + render('one'); // still cached -> hit |
| 129 | + render('two'); // still cached -> hit |
| 130 | + |
| 131 | + expect(SdfFontHandler.getFontData).toHaveBeenCalledTimes(0); |
| 132 | + }); |
| 133 | +}); |
0 commit comments