|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | + |
| 3 | +const readFileMock = vi.fn(); |
| 4 | +vi.mock("node:fs", () => ({ |
| 5 | + existsSync: vi.fn(() => true), |
| 6 | + promises: { |
| 7 | + readFile: readFileMock, |
| 8 | + writeFile: vi.fn(async () => undefined), |
| 9 | + rename: vi.fn(async () => undefined), |
| 10 | + unlink: vi.fn(async () => undefined), |
| 11 | + mkdir: vi.fn(async () => undefined), |
| 12 | + }, |
| 13 | +})); |
| 14 | + |
| 15 | +vi.mock("../lib/runtime-paths.js", () => ({ |
| 16 | + getCodexMultiAuthDir: () => "/mock/.codex/multi-auth", |
| 17 | +})); |
| 18 | + |
| 19 | +describe("runtime observability snapshot versioning", () => { |
| 20 | + beforeEach(() => { |
| 21 | + vi.resetModules(); |
| 22 | + }); |
| 23 | + |
| 24 | + afterEach(() => { |
| 25 | + readFileMock.mockReset(); |
| 26 | + }); |
| 27 | + |
| 28 | + it("normalizes legacy unversioned snapshots", async () => { |
| 29 | + readFileMock.mockResolvedValueOnce( |
| 30 | + JSON.stringify({ |
| 31 | + updatedAt: 1, |
| 32 | + responsesRequests: 2, |
| 33 | + runtimeMetrics: { totalRequests: 3 }, |
| 34 | + }), |
| 35 | + ); |
| 36 | + |
| 37 | + const { loadPersistedRuntimeObservabilitySnapshot } = await import( |
| 38 | + "../lib/runtime/runtime-observability.js" |
| 39 | + ); |
| 40 | + const snapshot = await loadPersistedRuntimeObservabilitySnapshot(); |
| 41 | + |
| 42 | + expect(snapshot?.version).toBe(1); |
| 43 | + expect(snapshot?.responsesRequests).toBe(2); |
| 44 | + expect(snapshot?.runtimeMetrics.totalRequests).toBe(3); |
| 45 | + expect(snapshot?.runtimeMetrics.failedRequests).toBe(0); |
| 46 | + }); |
| 47 | + |
| 48 | + it("drops unknown future snapshot versions safely", async () => { |
| 49 | + readFileMock.mockResolvedValueOnce(JSON.stringify({ version: 99 })); |
| 50 | + |
| 51 | + const { loadPersistedRuntimeObservabilitySnapshot } = await import( |
| 52 | + "../lib/runtime/runtime-observability.js" |
| 53 | + ); |
| 54 | + const snapshot = await loadPersistedRuntimeObservabilitySnapshot(); |
| 55 | + |
| 56 | + expect(snapshot).toBeNull(); |
| 57 | + }); |
| 58 | +}); |
0 commit comments