|
| 1 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import type { QuotaCacheData } from "../lib/quota-cache.js"; |
| 3 | +import type { AccountStorageV3 } from "../lib/storage.js"; |
| 4 | + |
| 5 | +const { loadQuotaCacheMock, saveQuotaCacheMock, fetchCodexQuotaSnapshotMock } = |
| 6 | + vi.hoisted(() => ({ |
| 7 | + loadQuotaCacheMock: vi.fn(), |
| 8 | + saveQuotaCacheMock: vi.fn(), |
| 9 | + fetchCodexQuotaSnapshotMock: vi.fn(), |
| 10 | + })); |
| 11 | + |
| 12 | +vi.mock("../lib/quota-cache.js", () => ({ |
| 13 | + loadQuotaCache: loadQuotaCacheMock, |
| 14 | + saveQuotaCache: saveQuotaCacheMock, |
| 15 | +})); |
| 16 | + |
| 17 | +vi.mock("../lib/quota-probe.js", () => ({ |
| 18 | + fetchCodexQuotaSnapshot: fetchCodexQuotaSnapshotMock, |
| 19 | +})); |
| 20 | + |
| 21 | +const { refreshQuotaCacheForMenu } = await import( |
| 22 | + "../lib/codex-manager/login-menu-data.js" |
| 23 | +); |
| 24 | + |
| 25 | +function createStorage(now: number): AccountStorageV3 { |
| 26 | + return { |
| 27 | + version: 3, |
| 28 | + activeIndex: 0, |
| 29 | + activeIndexByFamily: { codex: 0 }, |
| 30 | + accounts: [ |
| 31 | + { |
| 32 | + email: "a@example.com", |
| 33 | + accountId: "acc_a", |
| 34 | + refreshToken: "refresh-a", |
| 35 | + accessToken: "access-a", |
| 36 | + expiresAt: now + 3_600_000, |
| 37 | + addedAt: now - 60_000, |
| 38 | + lastUsed: now - 60_000, |
| 39 | + }, |
| 40 | + { |
| 41 | + email: "b@example.com", |
| 42 | + accountId: "acc_b", |
| 43 | + refreshToken: "refresh-b", |
| 44 | + accessToken: "access-b", |
| 45 | + expiresAt: now + 3_600_000, |
| 46 | + addedAt: now - 60_000, |
| 47 | + lastUsed: now - 60_000, |
| 48 | + }, |
| 49 | + ], |
| 50 | + }; |
| 51 | +} |
| 52 | + |
| 53 | +function emptyCache(): QuotaCacheData { |
| 54 | + return { byAccountId: {}, byEmail: {} }; |
| 55 | +} |
| 56 | + |
| 57 | +function snapshotFor(model: string) { |
| 58 | + return { |
| 59 | + status: 200, |
| 60 | + model, |
| 61 | + primary: { usedPercent: 10, windowMinutes: 300, resetAtMs: 1 }, |
| 62 | + secondary: { usedPercent: 5, windowMinutes: 10080, resetAtMs: 2 }, |
| 63 | + }; |
| 64 | +} |
| 65 | + |
| 66 | +const CONCURRENT_ENTRY = { |
| 67 | + updatedAt: 1, |
| 68 | + status: 429, |
| 69 | + model: "gpt-5-codex", |
| 70 | + primary: { usedPercent: 100, windowMinutes: 300, resetAtMs: 99 }, |
| 71 | + secondary: {}, |
| 72 | +}; |
| 73 | + |
| 74 | +beforeEach(() => { |
| 75 | + loadQuotaCacheMock.mockReset(); |
| 76 | + saveQuotaCacheMock.mockReset(); |
| 77 | + fetchCodexQuotaSnapshotMock.mockReset(); |
| 78 | + saveQuotaCacheMock.mockResolvedValue(undefined); |
| 79 | + fetchCodexQuotaSnapshotMock.mockResolvedValue(snapshotFor("gpt-5-codex")); |
| 80 | +}); |
| 81 | + |
| 82 | +describe("refreshQuotaCacheForMenu", () => { |
| 83 | + it("rebases its results onto the freshest persisted cache before saving", async () => { |
| 84 | + // Regression for the last-write-wins clobber: a concurrent writer (deep |
| 85 | + // check, second session) saved acc_concurrent while the menu refresh was |
| 86 | + // probing against its stale snapshot clone. The whole-file save must keep |
| 87 | + // that entry, not silently discard it. |
| 88 | + loadQuotaCacheMock.mockResolvedValue({ |
| 89 | + byAccountId: { acc_concurrent: { ...CONCURRENT_ENTRY } }, |
| 90 | + byEmail: {}, |
| 91 | + }); |
| 92 | + |
| 93 | + const result = await refreshQuotaCacheForMenu( |
| 94 | + createStorage(Date.now()), |
| 95 | + emptyCache(), |
| 96 | + 60_000, |
| 97 | + ); |
| 98 | + |
| 99 | + expect(saveQuotaCacheMock).toHaveBeenCalledTimes(1); |
| 100 | + const saved = saveQuotaCacheMock.mock.calls[0][0] as QuotaCacheData; |
| 101 | + expect(saved.byAccountId.acc_concurrent).toMatchObject({ status: 429 }); |
| 102 | + expect(saved.byAccountId.acc_a).toMatchObject({ status: 200 }); |
| 103 | + expect(saved.byAccountId.acc_b).toMatchObject({ status: 200 }); |
| 104 | + expect(result).toBe(saved); |
| 105 | + }); |
| 106 | + |
| 107 | + it("falls back to saving its own snapshot clone when the reload fails", async () => { |
| 108 | + loadQuotaCacheMock.mockRejectedValue(new Error("EBUSY")); |
| 109 | + |
| 110 | + const result = await refreshQuotaCacheForMenu( |
| 111 | + createStorage(Date.now()), |
| 112 | + emptyCache(), |
| 113 | + 60_000, |
| 114 | + ); |
| 115 | + |
| 116 | + expect(saveQuotaCacheMock).toHaveBeenCalledTimes(1); |
| 117 | + const saved = saveQuotaCacheMock.mock.calls[0][0] as QuotaCacheData; |
| 118 | + expect(saved.byAccountId.acc_a).toMatchObject({ status: 200 }); |
| 119 | + expect(saved.byAccountId.acc_b).toMatchObject({ status: 200 }); |
| 120 | + expect(result).toBe(saved); |
| 121 | + }); |
| 122 | + |
| 123 | + it("does not reload or save when every probe fails", async () => { |
| 124 | + fetchCodexQuotaSnapshotMock.mockRejectedValue(new Error("network")); |
| 125 | + |
| 126 | + const cache = emptyCache(); |
| 127 | + const result = await refreshQuotaCacheForMenu( |
| 128 | + createStorage(Date.now()), |
| 129 | + cache, |
| 130 | + 60_000, |
| 131 | + ); |
| 132 | + |
| 133 | + expect(loadQuotaCacheMock).not.toHaveBeenCalled(); |
| 134 | + expect(saveQuotaCacheMock).not.toHaveBeenCalled(); |
| 135 | + expect(result).toEqual(cache); |
| 136 | + }); |
| 137 | + |
| 138 | + it("returns the input cache untouched when there are no accounts", async () => { |
| 139 | + const cache = emptyCache(); |
| 140 | + const result = await refreshQuotaCacheForMenu( |
| 141 | + { version: 3, activeIndex: 0, activeIndexByFamily: {}, accounts: [] }, |
| 142 | + cache, |
| 143 | + 60_000, |
| 144 | + ); |
| 145 | + expect(result).toBe(cache); |
| 146 | + expect(fetchCodexQuotaSnapshotMock).not.toHaveBeenCalled(); |
| 147 | + expect(saveQuotaCacheMock).not.toHaveBeenCalled(); |
| 148 | + }); |
| 149 | +}); |
0 commit comments