|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import { describe, it, expect, vi } from 'vitest'; |
| 4 | +import { cacheSecondaryStorage } from './secondary-storage.js'; |
| 5 | + |
| 6 | +/** Minimal in-memory ICacheService stand-in. */ |
| 7 | +function makeCache() { |
| 8 | + const store = new Map<string, unknown>(); |
| 9 | + return { |
| 10 | + store, |
| 11 | + get: vi.fn(async (k: string) => (store.has(k) ? store.get(k) : undefined)), |
| 12 | + set: vi.fn(async (k: string, v: unknown, _ttl?: number) => { store.set(k, v); }), |
| 13 | + delete: vi.fn(async (k: string) => store.delete(k)), |
| 14 | + has: vi.fn(async (k: string) => store.has(k)), |
| 15 | + clear: vi.fn(async () => store.clear()), |
| 16 | + stats: vi.fn(async () => ({ hits: 0, misses: 0, keys: store.size })), |
| 17 | + }; |
| 18 | +} |
| 19 | + |
| 20 | +describe('cacheSecondaryStorage (ADR-0069 D2 — shared rate-limit/session store)', () => { |
| 21 | + it('round-trips a value through the cache service', async () => { |
| 22 | + const cache = makeCache(); |
| 23 | + const ss = cacheSecondaryStorage(cache as any); |
| 24 | + await ss.set('k1', '{"count":1}', 60); |
| 25 | + expect(cache.set).toHaveBeenCalledWith('k1', '{"count":1}', 60); |
| 26 | + expect(await ss.get('k1')).toBe('{"count":1}'); |
| 27 | + }); |
| 28 | + |
| 29 | + it('maps a cache MISS (undefined) to null (better-auth contract)', async () => { |
| 30 | + const cache = makeCache(); |
| 31 | + const ss = cacheSecondaryStorage(cache as any); |
| 32 | + expect(await ss.get('absent')).toBeNull(); |
| 33 | + }); |
| 34 | + |
| 35 | + it('forwards the TTL (seconds) to the cache set', async () => { |
| 36 | + const cache = makeCache(); |
| 37 | + const ss = cacheSecondaryStorage(cache as any); |
| 38 | + await ss.set('k', 'v', 10); |
| 39 | + expect(cache.set).toHaveBeenCalledWith('k', 'v', 10); |
| 40 | + }); |
| 41 | + |
| 42 | + it('deletes via the cache service', async () => { |
| 43 | + const cache = makeCache(); |
| 44 | + const ss = cacheSecondaryStorage(cache as any); |
| 45 | + await ss.set('k', 'v'); |
| 46 | + await ss.delete('k'); |
| 47 | + expect(cache.delete).toHaveBeenCalledWith('k'); |
| 48 | + expect(await ss.get('k')).toBeNull(); |
| 49 | + }); |
| 50 | +}); |
0 commit comments