forked from NoeFabris/opencode-antigravity-auth
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrefresh-queue.test.ts
More file actions
176 lines (154 loc) · 4.93 KB
/
Copy pathrefresh-queue.test.ts
File metadata and controls
176 lines (154 loc) · 4.93 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
import { beforeEach, describe, expect, it, vi } from "vitest";
import { ProactiveRefreshQueue } from "./refresh-queue";
import { AccountManager } from "./accounts";
import type { AccountStorageV4 } from "./storage";
import type { PluginClient } from "./types";
// Mock PluginClient
const mockClient: PluginClient = {
toast: vi.fn(),
auth: {
get: vi.fn(),
set: vi.fn(),
remove: vi.fn(),
},
} as unknown as PluginClient;
describe("ProactiveRefreshQueue", () => {
beforeEach(() => {
vi.useRealTimers();
});
describe("getAccountsNeedingRefresh", () => {
it("skips disabled accounts", () => {
const now = Date.now();
const stored: AccountStorageV4 = {
version: 4,
accounts: [
{
refreshToken: "r1",
projectId: "p1",
addedAt: now,
lastUsed: 0,
enabled: true,
},
{
refreshToken: "r2",
projectId: "p2",
addedAt: now,
lastUsed: 0,
enabled: false, // disabled account
},
{
refreshToken: "r3",
projectId: "p3",
addedAt: now,
lastUsed: 0,
enabled: true,
},
],
activeIndex: 0,
};
const manager = new AccountManager(undefined, stored);
const queue = new ProactiveRefreshQueue(mockClient, "test-provider", {
enabled: true,
bufferSeconds: 1800,
checkIntervalSeconds: 300,
});
queue.setAccountManager(manager);
// Set all accounts to expire soon (within buffer)
const accounts = manager.getAccounts();
const expiringSoon = now + 1000 * 60 * 10; // 10 minutes from now
accounts.forEach((acc) => {
acc.expires = expiringSoon;
});
const needsRefresh = queue.getAccountsNeedingRefresh();
// Should only include enabled accounts (indices 0 and 2)
expect(needsRefresh.length).toBe(2);
expect(needsRefresh.map((a) => a.index)).toEqual([0, 2]);
expect(needsRefresh.every((a) => a.enabled !== false)).toBe(true);
});
it("includes accounts with undefined enabled (default to enabled)", () => {
const now = Date.now();
const stored: AccountStorageV4 = {
version: 4,
accounts: [
{
refreshToken: "r1",
projectId: "p1",
addedAt: now,
lastUsed: 0,
// enabled is undefined - should be treated as enabled
},
],
activeIndex: 0,
};
const manager = new AccountManager(undefined, stored);
const queue = new ProactiveRefreshQueue(mockClient, "test-provider", {
enabled: true,
bufferSeconds: 1800,
checkIntervalSeconds: 300,
});
queue.setAccountManager(manager);
// Set account to expire soon
const accounts = manager.getAccounts();
accounts[0]!.expires = now + 1000 * 60 * 10; // 10 minutes from now
const needsRefresh = queue.getAccountsNeedingRefresh();
expect(needsRefresh.length).toBe(1);
expect(needsRefresh[0]!.index).toBe(0);
});
it("skips expired accounts", () => {
const now = Date.now();
const stored: AccountStorageV4 = {
version: 4,
accounts: [
{
refreshToken: "r1",
projectId: "p1",
addedAt: now,
lastUsed: 0,
enabled: true,
},
],
activeIndex: 0,
};
const manager = new AccountManager(undefined, stored);
const queue = new ProactiveRefreshQueue(mockClient, "test-provider", {
enabled: true,
bufferSeconds: 1800,
checkIntervalSeconds: 300,
});
queue.setAccountManager(manager);
// Set account to already expired
const accounts = manager.getAccounts();
accounts[0]!.expires = now - 1000; // 1 second ago
const needsRefresh = queue.getAccountsNeedingRefresh();
expect(needsRefresh.length).toBe(0);
});
it("skips accounts that don't need refresh yet", () => {
const now = Date.now();
const stored: AccountStorageV4 = {
version: 4,
accounts: [
{
refreshToken: "r1",
projectId: "p1",
addedAt: now,
lastUsed: 0,
enabled: true,
},
],
activeIndex: 0,
};
const manager = new AccountManager(undefined, stored);
const queue = new ProactiveRefreshQueue(mockClient, "test-provider", {
enabled: true,
bufferSeconds: 1800, // 30 minutes
checkIntervalSeconds: 300,
});
queue.setAccountManager(manager);
// Set account to expire in 1 hour (outside 30 min buffer)
const accounts = manager.getAccounts();
accounts[0]!.expires = now + 1000 * 60 * 60; // 1 hour from now
const needsRefresh = queue.getAccountsNeedingRefresh();
expect(needsRefresh.length).toBe(0);
});
});
});