-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathrotation-token-refresh.test.ts
More file actions
297 lines (256 loc) · 9.42 KB
/
Copy pathrotation-token-refresh.test.ts
File metadata and controls
297 lines (256 loc) · 9.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { AccountManager } from "../lib/accounts.js";
import type { AccountStorageV3 } from "../lib/storage.js";
const { queuedRefreshMock, saveAccountsMock, withAccountStorageTransactionMock } =
vi.hoisted(() => ({
queuedRefreshMock: vi.fn(),
saveAccountsMock: vi.fn(),
withAccountStorageTransactionMock: vi.fn(),
}));
vi.mock("../lib/refresh-queue.js", async (importOriginal) => {
const actual = await importOriginal<typeof import("../lib/refresh-queue.js")>();
return { ...actual, queuedRefresh: queuedRefreshMock };
});
vi.mock("../lib/storage.js", async (importOriginal) => {
const actual = await importOriginal<typeof import("../lib/storage.js")>();
return {
...actual,
saveAccounts: saveAccountsMock,
withAccountStorageTransaction: withAccountStorageTransactionMock,
};
});
vi.mock("../lib/codex-cli/writer.js", () => ({
setCodexCliActiveSelection: vi.fn().mockResolvedValue(true),
}));
const {
applyMonotonicAuthCooldown,
DEFAULT_AUTH_FAILURE_COOLDOWN_MS,
ensureFreshAccessToken,
} = await import("../lib/runtime/rotation-token-refresh.js");
const NOW = Date.now();
const FAMILY = "gpt-5-codex" as const;
const SKEW_MS = 30_000;
const INVALIDATION_COOLDOWN_MS = 300_000;
function storageWith(expiresAt: number): AccountStorageV3 {
return {
version: 3,
activeIndex: 0,
activeIndexByFamily: { [FAMILY]: 0 },
accounts: [
{
email: "account-1@example.com",
accountId: "acc_1",
refreshToken: "refresh-1",
accessToken: "access-1",
expiresAt,
addedAt: NOW - 60_000,
lastUsed: NOW - 60_000,
enabled: true,
},
],
};
}
const FRESH_EXPIRES = NOW + 3_600_000;
// Inside the refresh skew window: the proxy must refresh before using it.
const STALE_EXPIRES = NOW + 10_000;
const openManagers: AccountManager[] = [];
function managerWith(expiresAt: number): AccountManager {
const accountManager = new AccountManager(undefined, storageWith(expiresAt));
openManagers.push(accountManager);
return accountManager;
}
function refreshParams(accountManager: AccountManager) {
const account = accountManager.getAccountByIndex(0);
if (!account) throw new Error("fixture account missing");
return {
accountManager,
account,
family: FAMILY,
model: null,
now: NOW,
tokenRefreshSkewMs: SKEW_MS,
tokenInvalidationCooldownMs: INVALIDATION_COOLDOWN_MS,
};
}
beforeEach(() => {
vi.clearAllMocks();
AccountManager.resetVolatileRuntimeState();
saveAccountsMock.mockResolvedValue(undefined);
withAccountStorageTransactionMock.mockImplementation(async (handler) =>
handler(null, async () => undefined),
);
});
afterEach(async () => {
for (const accountManager of openManagers.splice(0, openManagers.length)) {
await accountManager.flushPendingSave();
}
});
describe("ensureFreshAccessToken", () => {
it("uses a fresh token as-is without touching the refresh queue", async () => {
const accountManager = managerWith(FRESH_EXPIRES);
const result = await ensureFreshAccessToken(refreshParams(accountManager));
expect(result).toMatchObject({ ok: true, accessToken: "access-1" });
expect(queuedRefreshMock).not.toHaveBeenCalled();
});
it("refreshes a stale token and commits the rotated credentials", async () => {
const accountManager = managerWith(STALE_EXPIRES);
queuedRefreshMock.mockResolvedValue({
type: "success",
access: "access-new",
refresh: "refresh-new",
expires: NOW + 7_200_000,
});
const result = await ensureFreshAccessToken(refreshParams(accountManager));
expect(queuedRefreshMock).toHaveBeenCalledExactlyOnceWith("refresh-1");
expect(result.ok).toBe(true);
if (result.ok) {
expect(result.accessToken).toBe("access-new");
expect(result.account.access).toBe("access-new");
}
// The in-memory pool now carries the rotated credentials.
expect(accountManager.getAccountByIndex(0)).toMatchObject({
access: "access-new",
refreshToken: "refresh-new",
});
});
it("deduplicates concurrent commits for the same refreshed account", async () => {
const accountManager = managerWith(STALE_EXPIRES);
const commit = vi.spyOn(accountManager, "commitRefreshedAuth");
queuedRefreshMock.mockResolvedValue({
type: "success",
access: "access-new",
refresh: "refresh-new",
expires: NOW + 7_200_000,
});
// Hold the first commit open so the second caller arrives while it is
// still in flight — that is the window the dedup queue exists for.
let releaseCommit!: () => void;
const commitGate = new Promise<void>((resolve) => {
releaseCommit = resolve;
});
withAccountStorageTransactionMock.mockImplementation(async (handler) => {
await commitGate;
return handler(null, async () => undefined);
});
const params = refreshParams(accountManager);
const firstPending = ensureFreshAccessToken(params);
const secondPending = ensureFreshAccessToken({ ...params });
await new Promise<void>((resolve) => setImmediate(resolve));
releaseCommit();
const [first, second] = await Promise.all([firstPending, secondPending]);
// Both callers share the single in-flight commit and the same token.
expect(commit).toHaveBeenCalledTimes(1);
expect(first).toMatchObject({ ok: true, accessToken: "access-new" });
expect(second).toMatchObject({ ok: true, accessToken: "access-new" });
});
it("applies the short cooldown on a non-retryable auth failure", async () => {
const accountManager = managerWith(STALE_EXPIRES);
queuedRefreshMock.mockResolvedValue({
type: "failed",
reason: "http_error",
statusCode: 401,
message: "token expired",
});
const result = await ensureFreshAccessToken(refreshParams(accountManager));
expect(result).toMatchObject({
ok: false,
retryable: false,
invalidated: false,
});
const coolingDownUntil =
accountManager.getAccountByIndex(0)?.coolingDownUntil ?? 0;
expect(coolingDownUntil).toBeGreaterThan(NOW);
expect(coolingDownUntil).toBeLessThanOrEqual(
Date.now() + DEFAULT_AUTH_FAILURE_COOLDOWN_MS,
);
});
it("marks transient refresh failures retryable", async () => {
const accountManager = managerWith(STALE_EXPIRES);
queuedRefreshMock.mockResolvedValue({
type: "failed",
reason: "network_error",
message: "fetch failed",
});
const result = await ensureFreshAccessToken(refreshParams(accountManager));
expect(result).toMatchObject({ ok: false, retryable: true });
});
it("applies the long cooldown and signals invalidation on a revoked token", async () => {
const accountManager = managerWith(STALE_EXPIRES);
queuedRefreshMock.mockResolvedValue({
type: "failed",
reason: "http_error",
statusCode: 401,
message: "OAuth token has been invalidated",
});
const result = await ensureFreshAccessToken(refreshParams(accountManager));
expect(result).toMatchObject({ ok: false, invalidated: true });
const coolingDownUntil =
accountManager.getAccountByIndex(0)?.coolingDownUntil ?? 0;
// The invalidation cooldown is the long one, far beyond the 30s default.
expect(coolingDownUntil).toBeGreaterThan(
NOW + INVALIDATION_COOLDOWN_MS - 10_000,
);
});
it("never lets a later generic failure truncate an invalidation cooldown", async () => {
const accountManager = managerWith(STALE_EXPIRES);
const account = accountManager.getAccountByIndex(0);
if (!account) throw new Error("fixture account missing");
accountManager.markAccountCoolingDown(
account,
INVALIDATION_COOLDOWN_MS,
"auth-failure",
);
const longDeadline =
accountManager.getAccountByIndex(0)?.coolingDownUntil ?? 0;
queuedRefreshMock.mockResolvedValue({
type: "failed",
reason: "http_error",
statusCode: 401,
message: "token expired",
});
await ensureFreshAccessToken(refreshParams(accountManager));
// Monotonic guard: the 30s generic cooldown must not shorten the
// 5-minute invalidation cooldown set by a concurrent request.
expect(accountManager.getAccountByIndex(0)?.coolingDownUntil).toBe(
longDeadline,
);
});
it("cools down and stays retryable when the commit itself fails", async () => {
const accountManager = managerWith(STALE_EXPIRES);
queuedRefreshMock.mockResolvedValue({
type: "success",
access: "access-new",
refresh: "refresh-new",
expires: NOW + 7_200_000,
});
// Once: the post-test debounced-save flush must still see the working
// transaction implementation from beforeEach.
withAccountStorageTransactionMock.mockRejectedValueOnce(
Object.assign(new Error("locked"), { code: "EBUSY" }),
);
const result = await ensureFreshAccessToken(refreshParams(accountManager));
expect(result).toMatchObject({ ok: false, retryable: true });
expect(
accountManager.getAccountByIndex(0)?.coolingDownUntil ?? 0,
).toBeGreaterThan(NOW);
});
});
describe("applyMonotonicAuthCooldown", () => {
it("extends an absent cooldown but never shortens an existing one", () => {
const accountManager = managerWith(FRESH_EXPIRES);
const account = accountManager.getAccountByIndex(0);
if (!account) throw new Error("fixture account missing");
applyMonotonicAuthCooldown(accountManager, account, 60_000);
const firstDeadline =
accountManager.getAccountByIndex(0)?.coolingDownUntil ?? 0;
expect(firstDeadline).toBeGreaterThan(NOW);
applyMonotonicAuthCooldown(accountManager, account, 1_000);
expect(accountManager.getAccountByIndex(0)?.coolingDownUntil).toBe(
firstDeadline,
);
applyMonotonicAuthCooldown(accountManager, account, 600_000);
expect(
accountManager.getAccountByIndex(0)?.coolingDownUntil ?? 0,
).toBeGreaterThan(firstDeadline);
});
});