|
| 1 | +/** |
| 2 | + * @fileoverview Tests for the wildcard-pattern getAll() path in |
| 3 | + * src/cache-with-ttl.ts that walks cacache.ls.stream. |
| 4 | + * |
| 5 | + * Mocks the cacache module's `getCacache().ls.stream` to yield fake |
| 6 | + * entries so we can exercise: prefix-mismatch skip, pattern-match |
| 7 | + * filter, in-memory-dedupe skip, expired-entry remove, and the |
| 8 | + * memoize-update branch. |
| 9 | + */ |
| 10 | + |
| 11 | +import { tmpdir } from 'node:os' |
| 12 | +import path from 'node:path' |
| 13 | + |
| 14 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 15 | + |
| 16 | +import { createTtlCache } from '../../src/cache-with-ttl' |
| 17 | +import { resetEnv, setEnv } from '../../src/env/rewire' |
| 18 | +import { safeDelete } from '../../src/fs' |
| 19 | +import { invalidateCaches } from '../../src/paths/rewire' |
| 20 | + |
| 21 | +import * as cacacheModule from '../../src/cacache' |
| 22 | + |
| 23 | +interface FakeStreamEntry { |
| 24 | + key: string |
| 25 | +} |
| 26 | + |
| 27 | +function makeFakeStream( |
| 28 | + entries: FakeStreamEntry[], |
| 29 | +): AsyncIterable<FakeStreamEntry> { |
| 30 | + return { |
| 31 | + async *[Symbol.asyncIterator]() { |
| 32 | + for (const e of entries) { |
| 33 | + yield e |
| 34 | + } |
| 35 | + }, |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +vi.mock('../../src/cacache', async importOriginal => { |
| 40 | + const original = await importOriginal<typeof import('../../src/cacache')>() |
| 41 | + return { |
| 42 | + ...original, |
| 43 | + getCacache: vi.fn(original.getCacache), |
| 44 | + safeGet: vi.fn(original.safeGet), |
| 45 | + remove: vi.fn(original.remove), |
| 46 | + } |
| 47 | +}) |
| 48 | + |
| 49 | +describe.sequential('cache-with-ttl — getAll wildcard', () => { |
| 50 | + let testCacheDir: string |
| 51 | + |
| 52 | + beforeEach(() => { |
| 53 | + invalidateCaches() |
| 54 | + testCacheDir = path.join( |
| 55 | + tmpdir(), |
| 56 | + `socket-getall-${Date.now()}-${Math.random().toString(36).slice(2)}`, |
| 57 | + ) |
| 58 | + setEnv('SOCKET_CACACHE_DIR', testCacheDir) |
| 59 | + vi.mocked(cacacheModule.getCacache).mockClear() |
| 60 | + vi.mocked(cacacheModule.safeGet).mockClear() |
| 61 | + vi.mocked(cacacheModule.remove).mockClear() |
| 62 | + }) |
| 63 | + |
| 64 | + afterEach(async () => { |
| 65 | + vi.restoreAllMocks() |
| 66 | + resetEnv() |
| 67 | + invalidateCaches() |
| 68 | + try { |
| 69 | + await safeDelete(testCacheDir, { force: true }) |
| 70 | + } catch {} |
| 71 | + }) |
| 72 | + |
| 73 | + it('iterates cacache stream and returns matched entries', async () => { |
| 74 | + const cache = createTtlCache({ |
| 75 | + ttl: 60_000, |
| 76 | + prefix: 'pfx', |
| 77 | + memoize: false, |
| 78 | + }) |
| 79 | + // Stub stream with 3 entries: 1 matches, 1 wrong-prefix, 1 expired. |
| 80 | + const validEntry = { data: 'value-a', expiresAt: Date.now() + 60_000 } |
| 81 | + const expiredEntry = { data: 'expired', expiresAt: Date.now() - 1000 } |
| 82 | + const fakeCacache = { |
| 83 | + ls: { |
| 84 | + stream: () => |
| 85 | + makeFakeStream([ |
| 86 | + { key: 'pfx:keep-a' }, |
| 87 | + { key: 'wrongprefix:skip' }, |
| 88 | + { key: 'pfx:expired' }, |
| 89 | + ]), |
| 90 | + }, |
| 91 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 92 | + } as any |
| 93 | + vi.mocked(cacacheModule.getCacache).mockReturnValue(fakeCacache) |
| 94 | + vi.mocked(cacacheModule.safeGet).mockImplementation(async (key: string) => { |
| 95 | + if (key === 'pfx:keep-a') { |
| 96 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 97 | + return { data: Buffer.from(JSON.stringify(validEntry), 'utf8') } as any |
| 98 | + } |
| 99 | + if (key === 'pfx:expired') { |
| 100 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 101 | + return { |
| 102 | + data: Buffer.from(JSON.stringify(expiredEntry), 'utf8'), |
| 103 | + } as any |
| 104 | + } |
| 105 | + return undefined |
| 106 | + }) |
| 107 | + |
| 108 | + const result = await cache.getAll<string>('*') |
| 109 | + // 'keep-a' present (originalKey strips prefix); 'skip' filtered; |
| 110 | + // 'expired' filtered. |
| 111 | + expect(result.size).toBe(1) |
| 112 | + expect(result.get('keep-a')).toBe('value-a') |
| 113 | + // Expired entry triggers cacache.remove. |
| 114 | + expect(cacacheModule.remove).toHaveBeenCalledWith('pfx:expired') |
| 115 | + }) |
| 116 | + |
| 117 | + it('updates memoize cache from persistent results when memoize:true', async () => { |
| 118 | + const cache = createTtlCache({ |
| 119 | + ttl: 60_000, |
| 120 | + prefix: 'memo', |
| 121 | + memoize: true, |
| 122 | + }) |
| 123 | + const validEntry = { data: 'memoized', expiresAt: Date.now() + 60_000 } |
| 124 | + const fakeCacache = { |
| 125 | + ls: { |
| 126 | + stream: () => makeFakeStream([{ key: 'memo:k1' }]), |
| 127 | + }, |
| 128 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 129 | + } as any |
| 130 | + vi.mocked(cacacheModule.getCacache).mockReturnValue(fakeCacache) |
| 131 | + vi.mocked(cacacheModule.safeGet).mockResolvedValue( |
| 132 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 133 | + { data: Buffer.from(JSON.stringify(validEntry), 'utf8') } as any, |
| 134 | + ) |
| 135 | + |
| 136 | + const result = await cache.getAll<string>('*') |
| 137 | + expect(result.get('k1')).toBe('memoized') |
| 138 | + // Subsequent get() should hit memo cache (we can't directly assert |
| 139 | + // memoSet, but the value is returned without a second persistent |
| 140 | + // lookup — verified by clearing safeGet and re-getting). |
| 141 | + vi.mocked(cacacheModule.safeGet).mockClear() |
| 142 | + const cached = await cache.get<string>('k1') |
| 143 | + expect(cached).toBe('memoized') |
| 144 | + expect(cacacheModule.safeGet).not.toHaveBeenCalled() |
| 145 | + }) |
| 146 | + |
| 147 | + it('skips entries already in in-memory results (memoize:true dedup)', async () => { |
| 148 | + const cache = createTtlCache({ |
| 149 | + ttl: 60_000, |
| 150 | + prefix: 'dedup', |
| 151 | + memoize: true, |
| 152 | + }) |
| 153 | + // Pre-populate memo cache via set(). |
| 154 | + await cache.set('shared', 'from-memo') |
| 155 | + // Stream yields the same key — getAll should dedupe. |
| 156 | + const fakeCacache = { |
| 157 | + ls: { |
| 158 | + stream: () => makeFakeStream([{ key: 'dedup:shared' }]), |
| 159 | + }, |
| 160 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 161 | + } as any |
| 162 | + vi.mocked(cacacheModule.getCacache).mockReturnValue(fakeCacache) |
| 163 | + // safeGet should NOT be called for the dedup'd entry. |
| 164 | + vi.mocked(cacacheModule.safeGet).mockClear() |
| 165 | + const result = await cache.getAll<string>('*') |
| 166 | + expect(result.get('shared')).toBe('from-memo') |
| 167 | + }) |
| 168 | + |
| 169 | + it('swallows safeGet errors during stream iteration', async () => { |
| 170 | + const cache = createTtlCache({ |
| 171 | + ttl: 60_000, |
| 172 | + prefix: 'errs', |
| 173 | + memoize: false, |
| 174 | + }) |
| 175 | + const fakeCacache = { |
| 176 | + ls: { |
| 177 | + stream: () => makeFakeStream([{ key: 'errs:bad' }]), |
| 178 | + }, |
| 179 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 180 | + } as any |
| 181 | + vi.mocked(cacacheModule.getCacache).mockReturnValue(fakeCacache) |
| 182 | + vi.mocked(cacacheModule.safeGet).mockRejectedValueOnce( |
| 183 | + new Error('safeGet-failed'), |
| 184 | + ) |
| 185 | + // getAll must not propagate the error. |
| 186 | + await expect(cache.getAll<string>('*')).resolves.toBeInstanceOf(Map) |
| 187 | + }) |
| 188 | + |
| 189 | + it('skips entries returning undefined from safeGet', async () => { |
| 190 | + const cache = createTtlCache({ |
| 191 | + ttl: 60_000, |
| 192 | + prefix: 'gone', |
| 193 | + memoize: false, |
| 194 | + }) |
| 195 | + const fakeCacache = { |
| 196 | + ls: { |
| 197 | + stream: () => makeFakeStream([{ key: 'gone:k' }]), |
| 198 | + }, |
| 199 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 200 | + } as any |
| 201 | + vi.mocked(cacacheModule.getCacache).mockReturnValue(fakeCacache) |
| 202 | + vi.mocked(cacacheModule.safeGet).mockResolvedValueOnce(undefined) |
| 203 | + const result = await cache.getAll<string>('*') |
| 204 | + expect(result.size).toBe(0) |
| 205 | + }) |
| 206 | +}) |
0 commit comments