|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | +import type { CodexQuotaState } from '@/types'; |
| 3 | + |
| 4 | +type StorageLike = { |
| 5 | + getItem: (key: string) => string | null; |
| 6 | + setItem: (key: string, value: string) => void; |
| 7 | + removeItem: (key: string) => void; |
| 8 | + clear: () => void; |
| 9 | +}; |
| 10 | + |
| 11 | +const createMemoryStorage = (): StorageLike => { |
| 12 | + const store = new Map<string, string>(); |
| 13 | + return { |
| 14 | + getItem: (key) => (store.has(key) ? (store.get(key) as string) : null), |
| 15 | + setItem: (key, value) => { |
| 16 | + store.set(key, value); |
| 17 | + }, |
| 18 | + removeItem: (key) => { |
| 19 | + store.delete(key); |
| 20 | + }, |
| 21 | + clear: () => { |
| 22 | + store.clear(); |
| 23 | + }, |
| 24 | + }; |
| 25 | +}; |
| 26 | + |
| 27 | +const readPersistedCodexQuota = async () => { |
| 28 | + const { STORAGE_KEY_QUOTA_CACHE } = await import('@/utils/constants'); |
| 29 | + const { obfuscatedStorage } = await import('@/services/storage/secureStorage'); |
| 30 | + const persisted = obfuscatedStorage.getItem<{ |
| 31 | + state?: { codexQuota?: Record<string, CodexQuotaState> }; |
| 32 | + }>(STORAGE_KEY_QUOTA_CACHE); |
| 33 | + return persisted?.state?.codexQuota ?? {}; |
| 34 | +}; |
| 35 | + |
| 36 | +describe('useQuotaStore persistence', () => { |
| 37 | + let storage: StorageLike; |
| 38 | + |
| 39 | + beforeEach(() => { |
| 40 | + vi.resetModules(); |
| 41 | + storage = createMemoryStorage(); |
| 42 | + vi.stubGlobal('localStorage', storage); |
| 43 | + }); |
| 44 | + |
| 45 | + afterEach(() => { |
| 46 | + vi.unstubAllGlobals(); |
| 47 | + }); |
| 48 | + |
| 49 | + it('persists only manually fetched Codex success states', async () => { |
| 50 | + const { useQuotaStore } = await import('./useQuotaStore'); |
| 51 | + |
| 52 | + useQuotaStore.getState().setCodexQuota({ |
| 53 | + manual: { |
| 54 | + status: 'success', |
| 55 | + windows: [], |
| 56 | + fetchedAtMs: 2_000, |
| 57 | + }, |
| 58 | + observed: { |
| 59 | + status: 'success', |
| 60 | + windows: [], |
| 61 | + observedFromUsageHeaders: true, |
| 62 | + observedAtMs: 1_000, |
| 63 | + }, |
| 64 | + failed: { |
| 65 | + status: 'error', |
| 66 | + windows: [], |
| 67 | + error: 'failed', |
| 68 | + }, |
| 69 | + }); |
| 70 | + |
| 71 | + expect(Object.keys(await readPersistedCodexQuota())).toEqual(['manual']); |
| 72 | + }); |
| 73 | + |
| 74 | + it('clears quota state and persisted quota cache together', async () => { |
| 75 | + const { useQuotaStore } = await import('./useQuotaStore'); |
| 76 | + |
| 77 | + useQuotaStore.getState().setCodexQuota({ |
| 78 | + manual: { |
| 79 | + status: 'success', |
| 80 | + windows: [], |
| 81 | + fetchedAtMs: 2_000, |
| 82 | + }, |
| 83 | + }); |
| 84 | + |
| 85 | + useQuotaStore.getState().clearQuotaCache(); |
| 86 | + |
| 87 | + expect(useQuotaStore.getState().codexQuota).toEqual({}); |
| 88 | + expect(await readPersistedCodexQuota()).toEqual({}); |
| 89 | + }); |
| 90 | +}); |
0 commit comments