fix: avoid caching non-positive TTL entries

This commit is contained in:
Mo Elzubeir
2026-05-29 14:39:04 -05:00
parent d820a39223
commit 4ccf060a3e
3 changed files with 29 additions and 2 deletions
+23
View File
@@ -66,6 +66,19 @@ describe('SocialhoseClient', () => {
expect(fetchMock).toHaveBeenCalledTimes(1);
});
it('does not cache a GET request when per-request revalidateSeconds is zero', async () => {
const fetchMock = vi
.fn()
.mockResolvedValueOnce(ok({ count: 1, next: null, previous: null, results: [] }))
.mockResolvedValueOnce(ok({ count: 2, next: null, previous: null, results: [] }));
const client = new SocialhoseClient({ apiKey: 'test-key', fetch: fetchMock, cacheTtlMs: 60_000 });
await expect(client.getMentions({ page: 1 }, { revalidateSeconds: 0 })).resolves.toMatchObject({ count: 1 });
await expect(client.getMentions({ page: 1 }, { revalidateSeconds: 0 })).resolves.toMatchObject({ count: 2 });
expect(fetchMock).toHaveBeenCalledTimes(2);
});
it('retries rate limits and transient server failures', async () => {
const fetchMock = vi
.fn()
@@ -134,6 +147,16 @@ describe('MemoryCache', () => {
expect(await cache.get('k')).toBeUndefined();
});
it('does not store values when TTL is zero or negative', async () => {
const cache = new MemoryCache();
await cache.set('zero', 'v', 0);
await cache.set('negative', 'v', -1);
expect(await cache.get('zero')).toBeUndefined();
expect(await cache.get('negative')).toBeUndefined();
});
it('deletes a stored entry', async () => {
const cache = new MemoryCache();
await cache.set('k', 'v', 60_000);