|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import type { QuotaCacheData, QuotaCacheEntry } from "../lib/quota-cache.js"; |
| 3 | +import type { CodexQuotaSnapshot } from "../lib/quota-probe.js"; |
| 4 | +import { |
| 5 | + cloneQuotaCacheData, |
| 6 | + getPersistedQuotaViewForAccount, |
| 7 | + pruneUnsafeQuotaEmailCacheEntry, |
| 8 | + updateQuotaCacheForAccount, |
| 9 | +} from "../lib/codex-manager/quota-cache-helpers.js"; |
| 10 | +import { DEFAULT_MODEL } from "../lib/request/helpers/model-map.js"; |
| 11 | + |
| 12 | +const NOW = 1_700_000_000_000; |
| 13 | + |
| 14 | +function makeEntry(overrides: Partial<QuotaCacheEntry> = {}): QuotaCacheEntry { |
| 15 | + return { |
| 16 | + updatedAt: NOW - 1_000, |
| 17 | + status: 200, |
| 18 | + model: "gpt-5-codex", |
| 19 | + primary: { usedPercent: 50, windowMinutes: 300, resetAtMs: NOW + 10_000 }, |
| 20 | + secondary: { usedPercent: 10, windowMinutes: 10080, resetAtMs: NOW + 20_000 }, |
| 21 | + ...overrides, |
| 22 | + }; |
| 23 | +} |
| 24 | + |
| 25 | +function makeSnapshot(overrides: Partial<CodexQuotaSnapshot> = {}): CodexQuotaSnapshot { |
| 26 | + return { |
| 27 | + status: 200, |
| 28 | + model: "gpt-5-codex", |
| 29 | + primary: { usedPercent: 75, windowMinutes: 300, resetAtMs: NOW + 30_000 }, |
| 30 | + secondary: { usedPercent: 20, windowMinutes: 10080, resetAtMs: NOW + 40_000 }, |
| 31 | + ...overrides, |
| 32 | + }; |
| 33 | +} |
| 34 | + |
| 35 | +function makeCache(overrides: Partial<QuotaCacheData> = {}): QuotaCacheData { |
| 36 | + return { byAccountId: {}, byEmail: {}, ...overrides }; |
| 37 | +} |
| 38 | + |
| 39 | +const uniqueAccount = { accountId: "acc_a", email: "a@example.com" }; |
| 40 | +const accounts = [uniqueAccount, { accountId: "acc_b", email: "b@example.com" }]; |
| 41 | + |
| 42 | +describe("getPersistedQuotaViewForAccount", () => { |
| 43 | + it("returns the cached entry for a unique account id when no persisted reset exists", () => { |
| 44 | + const entry = makeEntry(); |
| 45 | + const cache = makeCache({ byAccountId: { acc_a: entry } }); |
| 46 | + expect( |
| 47 | + getPersistedQuotaViewForAccount(cache, uniqueAccount, accounts, NOW), |
| 48 | + ).toBe(entry); |
| 49 | + }); |
| 50 | + |
| 51 | + it("returns null with no cache and no persisted rate-limit reset", () => { |
| 52 | + expect(getPersistedQuotaViewForAccount(null, uniqueAccount, accounts, NOW)).toBeNull(); |
| 53 | + }); |
| 54 | + |
| 55 | + it("synthesizes a 429 view from a persisted reset when the cache is empty", () => { |
| 56 | + const account = { |
| 57 | + ...uniqueAccount, |
| 58 | + rateLimitResetTimes: { codex: NOW + 60_000 }, |
| 59 | + }; |
| 60 | + expect( |
| 61 | + getPersistedQuotaViewForAccount(null, account, accounts, NOW), |
| 62 | + ).toEqual({ |
| 63 | + updatedAt: NOW, |
| 64 | + status: 429, |
| 65 | + model: DEFAULT_MODEL, |
| 66 | + planType: undefined, |
| 67 | + primary: { resetAtMs: NOW + 60_000 }, |
| 68 | + secondary: {}, |
| 69 | + }); |
| 70 | + }); |
| 71 | + |
| 72 | + it("keeps a cached 429 that already covers the persisted reset", () => { |
| 73 | + const entry = makeEntry({ |
| 74 | + status: 429, |
| 75 | + primary: { usedPercent: 100, windowMinutes: 300, resetAtMs: NOW + 90_000 }, |
| 76 | + }); |
| 77 | + const cache = makeCache({ byAccountId: { acc_a: entry } }); |
| 78 | + const account = { |
| 79 | + ...uniqueAccount, |
| 80 | + rateLimitResetTimes: { codex: NOW + 60_000 }, |
| 81 | + }; |
| 82 | + expect(getPersistedQuotaViewForAccount(cache, account, accounts, NOW)).toBe(entry); |
| 83 | + }); |
| 84 | + |
| 85 | + it("upgrades a cached 200 to a 429 view with the max of cached and persisted resets", () => { |
| 86 | + const entry = makeEntry(); |
| 87 | + const cache = makeCache({ byAccountId: { acc_a: entry } }); |
| 88 | + const account = { |
| 89 | + ...uniqueAccount, |
| 90 | + rateLimitResetTimes: { codex: NOW + 60_000 }, |
| 91 | + }; |
| 92 | + const view = getPersistedQuotaViewForAccount(cache, account, accounts, NOW); |
| 93 | + expect(view).toMatchObject({ |
| 94 | + status: 429, |
| 95 | + model: "gpt-5-codex", |
| 96 | + updatedAt: entry.updatedAt, |
| 97 | + }); |
| 98 | + expect(view?.primary.resetAtMs).toBe(NOW + 60_000); |
| 99 | + expect(view?.primary.usedPercent).toBe(50); |
| 100 | + expect(view?.secondary).toBe(entry.secondary); |
| 101 | + }); |
| 102 | +}); |
| 103 | + |
| 104 | +describe("updateQuotaCacheForAccount", () => { |
| 105 | + beforeEach(() => { |
| 106 | + vi.useFakeTimers(); |
| 107 | + vi.setSystemTime(NOW); |
| 108 | + }); |
| 109 | + |
| 110 | + afterEach(() => { |
| 111 | + vi.useRealTimers(); |
| 112 | + }); |
| 113 | + |
| 114 | + it("writes by unique account id and drops the now-redundant email entry", () => { |
| 115 | + const cache = makeCache({ |
| 116 | + byEmail: { "a@example.com": makeEntry() }, |
| 117 | + }); |
| 118 | + expect(updateQuotaCacheForAccount(cache, uniqueAccount, makeSnapshot(), accounts)).toBe(true); |
| 119 | + expect(cache.byAccountId.acc_a).toMatchObject({ |
| 120 | + updatedAt: NOW, |
| 121 | + status: 200, |
| 122 | + primary: { usedPercent: 75, windowMinutes: 300, resetAtMs: NOW + 30_000 }, |
| 123 | + }); |
| 124 | + expect(cache.byEmail).toEqual({}); |
| 125 | + }); |
| 126 | + |
| 127 | + it("falls back to the email key when the account id is not unique", () => { |
| 128 | + const duplicated = [ |
| 129 | + { accountId: "acc_dup", email: "a@example.com" }, |
| 130 | + { accountId: "acc_dup", email: "b@example.com" }, |
| 131 | + ]; |
| 132 | + const cache = makeCache(); |
| 133 | + expect( |
| 134 | + updateQuotaCacheForAccount(cache, duplicated[0], makeSnapshot(), duplicated), |
| 135 | + ).toBe(true); |
| 136 | + expect(cache.byAccountId).toEqual({}); |
| 137 | + expect(cache.byEmail["a@example.com"]).toMatchObject({ updatedAt: NOW, status: 200 }); |
| 138 | + }); |
| 139 | + |
| 140 | + it("reports no change when neither key is safe to write", () => { |
| 141 | + // Same id AND same email across two accounts: no unique id, no safe |
| 142 | + // email fallback, and nothing cached to prune. |
| 143 | + const clones = [ |
| 144 | + { accountId: "acc_dup", email: "shared@example.com" }, |
| 145 | + { accountId: "acc_dup", email: "shared@example.com" }, |
| 146 | + ]; |
| 147 | + const cache = makeCache(); |
| 148 | + expect(updateQuotaCacheForAccount(cache, clones[0], makeSnapshot(), clones)).toBe(false); |
| 149 | + expect(cache).toEqual(makeCache()); |
| 150 | + }); |
| 151 | + |
| 152 | + it("prunes a stale email entry when the fallback became unsafe", () => { |
| 153 | + const clones = [ |
| 154 | + { accountId: "acc_dup", email: "shared@example.com" }, |
| 155 | + { accountId: "acc_dup", email: "shared@example.com" }, |
| 156 | + ]; |
| 157 | + const cache = makeCache({ |
| 158 | + byEmail: { "shared@example.com": makeEntry() }, |
| 159 | + }); |
| 160 | + expect(updateQuotaCacheForAccount(cache, clones[0], makeSnapshot(), clones)).toBe(true); |
| 161 | + expect(cache.byEmail).toEqual({}); |
| 162 | + }); |
| 163 | +}); |
| 164 | + |
| 165 | +describe("cloneQuotaCacheData", () => { |
| 166 | + it("clones the maps so mutations do not leak back", () => { |
| 167 | + const original = makeCache({ |
| 168 | + byAccountId: { acc_a: makeEntry() }, |
| 169 | + byEmail: { "a@example.com": makeEntry() }, |
| 170 | + }); |
| 171 | + const clone = cloneQuotaCacheData(original); |
| 172 | + expect(clone).toEqual(original); |
| 173 | + expect(clone).not.toBe(original); |
| 174 | + delete clone.byAccountId.acc_a; |
| 175 | + clone.byEmail["c@example.com"] = makeEntry(); |
| 176 | + expect(original.byAccountId.acc_a).toBeDefined(); |
| 177 | + expect(original.byEmail["c@example.com"]).toBeUndefined(); |
| 178 | + }); |
| 179 | +}); |
| 180 | + |
| 181 | +describe("pruneUnsafeQuotaEmailCacheEntry", () => { |
| 182 | + it("returns false when the email has no cache entry", () => { |
| 183 | + expect(pruneUnsafeQuotaEmailCacheEntry(makeCache(), "a@example.com", accounts)).toBe(false); |
| 184 | + expect(pruneUnsafeQuotaEmailCacheEntry(makeCache(), undefined, accounts)).toBe(false); |
| 185 | + }); |
| 186 | + |
| 187 | + it("keeps the entry while exactly one account still owns the email", () => { |
| 188 | + const cache = makeCache({ byEmail: { "a@example.com": makeEntry() } }); |
| 189 | + expect(pruneUnsafeQuotaEmailCacheEntry(cache, "A@Example.com ", accounts)).toBe(false); |
| 190 | + expect(cache.byEmail["a@example.com"]).toBeDefined(); |
| 191 | + }); |
| 192 | + |
| 193 | + it("prunes the entry once the email is shared by multiple accounts", () => { |
| 194 | + const shared = [ |
| 195 | + { accountId: "acc_a", email: "a@example.com" }, |
| 196 | + { accountId: "acc_c", email: "a@example.com" }, |
| 197 | + ]; |
| 198 | + const cache = makeCache({ byEmail: { "a@example.com": makeEntry() } }); |
| 199 | + expect(pruneUnsafeQuotaEmailCacheEntry(cache, "a@example.com", shared)).toBe(true); |
| 200 | + expect(cache.byEmail).toEqual({}); |
| 201 | + }); |
| 202 | +}); |
0 commit comments