Files
HealthLog/src/lib/ai/__tests__/codex-slug-cache.test.ts
T
Marc-André BombeckandClaude Opus 4.7 cd3b890067 refactor(v1.4.15): apply simplify-review safe suggestions
Drop dead exports + misleading comments + unreachable defaults flagged
by phase-D simplify review (autonomous-yes items only):

- channel-state: drop unused TEST_CONSTANTS re-export
- retry-policy: drop unused RejectKind type alias
- achievements: drop misleading "fallback" comment on exhaustive switch
- notification-status-card: drop unreachable default in stateBadgeFor
- withings: export isWithingsRefreshReauthFailure + dedupe inline
  classifier in /api/withings/status route
- codex-slug-cache: drop test-only inspectCodexSlugCache helper; tests
  read getCachedCodexSlug() instead

Findings 1, 2, 8, 9 deferred to v1.4.16 (require product call).
Finding 11 is an explicit anti-finding.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-09 22:14:43 +02:00

60 lines
1.8 KiB
TypeScript

import { describe, it, expect, beforeEach } from "vitest";
import {
getCachedCodexSlug,
setCachedCodexSlug,
invalidateCachedCodexSlug,
clearCodexSlugCache,
CODEX_SLUG_CACHE_TTL_MS,
} from "../codex-slug-cache";
/**
* Phase C1 — slug-drift defence positive cache.
* Cache TTL = 1 hour per `docs/codex-protocol-spec.md` §7b.
*/
describe("Codex slug cache", () => {
beforeEach(() => {
clearCodexSlugCache();
});
it("starts empty", () => {
expect(getCachedCodexSlug()).toBeNull();
});
it("set + get returns the cached slug within TTL", () => {
setCachedCodexSlug("gpt-5.3-codex", 1_000);
expect(getCachedCodexSlug(1_000)).toBe("gpt-5.3-codex");
expect(getCachedCodexSlug(1_000 + CODEX_SLUG_CACHE_TTL_MS - 1)).toBe(
"gpt-5.3-codex",
);
});
it("expires entries strictly after the TTL", () => {
setCachedCodexSlug("gpt-5.3-codex", 1_000);
// Exactly at TTL: still valid (the comparison is `now - cached > TTL`).
expect(getCachedCodexSlug(1_000 + CODEX_SLUG_CACHE_TTL_MS)).toBe(
"gpt-5.3-codex",
);
// Past TTL: gone.
expect(getCachedCodexSlug(1_000 + CODEX_SLUG_CACHE_TTL_MS + 1)).toBeNull();
});
it("expired entry is evicted on read", () => {
setCachedCodexSlug("gpt-5.3-codex", 1_000);
getCachedCodexSlug(1_000 + CODEX_SLUG_CACHE_TTL_MS + 1);
// Subsequent get should be null too (no resurrection).
expect(getCachedCodexSlug(1_000 + CODEX_SLUG_CACHE_TTL_MS + 1)).toBeNull();
});
it("invalidate clears the slot", () => {
setCachedCodexSlug("gpt-5.3-codex");
invalidateCachedCodexSlug();
expect(getCachedCodexSlug()).toBeNull();
});
it("set replaces the previous slot", () => {
setCachedCodexSlug("a", 1_000);
setCachedCodexSlug("b", 2_000);
expect(getCachedCodexSlug(2_500)).toBe("b");
});
});