|
| 1 | +// SPDX-FileCopyrightText: 2026 LiveKit, Inc. |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | +import { describe, expect, it, vi } from 'vitest'; |
| 5 | +import { BoundedCache } from './utils.js'; |
| 6 | + |
| 7 | +class Entry { |
| 8 | + createdAt: number; |
| 9 | + totalDurationInS: number | undefined = undefined; |
| 10 | + predictionDurationInS: number | undefined = undefined; |
| 11 | + note: string | undefined = undefined; |
| 12 | + |
| 13 | + constructor(createdAt: number, note?: string) { |
| 14 | + this.createdAt = createdAt; |
| 15 | + this.note = note; |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +describe('BoundedCache', () => { |
| 20 | + it('evicts oldest entry when maxLen is exceeded', () => { |
| 21 | + const cache = new BoundedCache<number, Entry>(2); |
| 22 | + cache.set(1, new Entry(1)); |
| 23 | + cache.set(2, new Entry(2)); |
| 24 | + cache.set(3, new Entry(3)); |
| 25 | + |
| 26 | + expect(cache.size).toBe(2); |
| 27 | + expect([...cache.keys()]).toEqual([2, 3]); |
| 28 | + expect(cache.get(1)).toBeUndefined(); |
| 29 | + expect(cache.get(2)!.createdAt).toBe(2); |
| 30 | + expect(cache.get(3)!.createdAt).toBe(3); |
| 31 | + }); |
| 32 | + |
| 33 | + it('setOrUpdate creates a value via factory when key is missing', () => { |
| 34 | + const cache = new BoundedCache<number, Entry>(10); |
| 35 | + const factory = vi.fn(() => new Entry(100)); |
| 36 | + |
| 37 | + const value = cache.setOrUpdate(1, factory, { predictionDurationInS: 0.42 }); |
| 38 | + |
| 39 | + expect(factory).toHaveBeenCalledTimes(1); |
| 40 | + expect(value.createdAt).toBe(100); |
| 41 | + expect(value.predictionDurationInS).toBe(0.42); |
| 42 | + expect(cache.get(1)?.predictionDurationInS).toBe(0.42); |
| 43 | + }); |
| 44 | + |
| 45 | + it('setOrUpdate updates existing value and does not call factory', () => { |
| 46 | + const cache = new BoundedCache<number, Entry>(10); |
| 47 | + cache.set(1, new Entry(1, 'before')); |
| 48 | + const factory = vi.fn(() => new Entry(999)); |
| 49 | + |
| 50 | + const value = cache.setOrUpdate(1, factory, { note: 'after', totalDurationInS: 1.5 }); |
| 51 | + |
| 52 | + expect(factory).not.toHaveBeenCalled(); |
| 53 | + expect(value.createdAt).toBe(1); |
| 54 | + expect(value.note).toBe('after'); |
| 55 | + expect(value.totalDurationInS).toBe(1.5); |
| 56 | + }); |
| 57 | + |
| 58 | + it('updateValue returns undefined for missing key', () => { |
| 59 | + const cache = new BoundedCache<number, Entry>(10); |
| 60 | + const result = cache.updateValue(404, { note: 'missing' }); |
| 61 | + |
| 62 | + expect(result).toBeUndefined(); |
| 63 | + }); |
| 64 | + |
| 65 | + it('updateValue ignores undefined fields', () => { |
| 66 | + const cache = new BoundedCache<number, Entry>(10); |
| 67 | + cache.set(1, new Entry(1, 'keep')); |
| 68 | + |
| 69 | + const result = cache.updateValue(1, { |
| 70 | + note: undefined, |
| 71 | + predictionDurationInS: 0.1, |
| 72 | + }); |
| 73 | + |
| 74 | + expect(result?.createdAt).toBe(1); |
| 75 | + expect(result?.note).toBe('keep'); |
| 76 | + expect(result?.predictionDurationInS).toBe(0.1); |
| 77 | + }); |
| 78 | + |
| 79 | + it('pop without predicate removes the oldest entry (python parity)', () => { |
| 80 | + const cache = new BoundedCache<number, Entry>(10); |
| 81 | + cache.set(1, new Entry(1)); |
| 82 | + cache.set(2, new Entry(2)); |
| 83 | + cache.set(3, new Entry(3)); |
| 84 | + |
| 85 | + const popped = cache.pop(); |
| 86 | + |
| 87 | + expect(popped?.createdAt).toBe(1); |
| 88 | + expect([...cache.keys()]).toEqual([2, 3]); |
| 89 | + }); |
| 90 | + |
| 91 | + it('pop with predicate removes the most recent matching entry', () => { |
| 92 | + const cache = new BoundedCache<number, Entry>(10); |
| 93 | + const e1 = new Entry(1); |
| 94 | + e1.totalDurationInS = 0; |
| 95 | + const e2 = new Entry(2); |
| 96 | + e2.totalDurationInS = 1; |
| 97 | + const e3 = new Entry(3); |
| 98 | + e3.totalDurationInS = 2; |
| 99 | + cache.set(1, e1); |
| 100 | + cache.set(2, e2); |
| 101 | + cache.set(3, e3); |
| 102 | + |
| 103 | + const popped = cache.pop((entry) => (entry.totalDurationInS ?? 0) > 0); |
| 104 | + |
| 105 | + expect(popped?.createdAt).toBe(3); |
| 106 | + expect(popped?.totalDurationInS).toBe(2); |
| 107 | + expect([...cache.keys()]).toEqual([1, 2]); |
| 108 | + }); |
| 109 | + |
| 110 | + it('pop with predicate returns undefined when no match exists', () => { |
| 111 | + const cache = new BoundedCache<number, Entry>(10); |
| 112 | + const e1 = new Entry(1); |
| 113 | + e1.totalDurationInS = 0; |
| 114 | + cache.set(1, e1); |
| 115 | + |
| 116 | + const popped = cache.pop((entry) => (entry.totalDurationInS ?? 0) > 10); |
| 117 | + |
| 118 | + expect(popped).toBeUndefined(); |
| 119 | + expect(cache.size).toBe(1); |
| 120 | + }); |
| 121 | + |
| 122 | + it('clear removes all entries', () => { |
| 123 | + const cache = new BoundedCache<number, Entry>(10); |
| 124 | + cache.set(1, new Entry(1)); |
| 125 | + cache.set(2, new Entry(2)); |
| 126 | + |
| 127 | + cache.clear(); |
| 128 | + |
| 129 | + expect(cache.size).toBe(0); |
| 130 | + expect([...cache.keys()]).toEqual([]); |
| 131 | + }); |
| 132 | +}); |
0 commit comments