icebear icebear0828 commited on
Commit
5c5129f
·
unverified ·
1 Parent(s): cb96245

fix: clear quota warnings when account is deleted (#100) (#102)

Browse files

DELETE /auth/accounts/:id was missing clearWarnings() call, causing
stale quota warning banners to persist in the dashboard after account
removal.

Co-authored-by: icebear0828 <icebear0828@users.noreply.github.com>

CHANGELOG.md CHANGED
@@ -47,6 +47,7 @@
47
 
48
  ### Fixed
49
 
 
50
  - macOS Electron 桌面版登录报 `spawn Unknown system error -86`:CI 在 arm64 runner 上同时构建 arm64/x64 DMG,但只下载 arm64 的 curl-impersonate,导致 Intel Mac 用户 spawn 失败(EBADARCH);拆分为 per-arch 构建 + `setup-curl.ts` 支持 `--arch` 交叉下载;错误提示改为明确的架构不匹配诊断 (#96)
51
  - 默认关闭 desktop context 注入:之前每次请求注入 ~1500 token 的 Codex Desktop 系统提示,导致 prompt_tokens 虚高;新增 `model.inject_desktop_context` 配置项(默认 `false`),需要时可手动开启 (#95)
52
 
 
47
 
48
  ### Fixed
49
 
50
+ - 删除账号后额度预警横幅未清除:`DELETE /auth/accounts/:id` 漏调 `clearWarnings()`,导致已删除账号的 quota warning 残留在前端 (#100)
51
  - macOS Electron 桌面版登录报 `spawn Unknown system error -86`:CI 在 arm64 runner 上同时构建 arm64/x64 DMG,但只下载 arm64 的 curl-impersonate,导致 Intel Mac 用户 spawn 失败(EBADARCH);拆分为 per-arch 构建 + `setup-curl.ts` 支持 `--arch` 交叉下载;错误提示改为明确的架构不匹配诊断 (#96)
52
  - 默认关闭 desktop context 注入:之前每次请求注入 ~1500 token 的 Codex Desktop 系统提示,导致 prompt_tokens 虚高;新增 `model.inject_desktop_context` 配置项(默认 `false`),需要时可手动开启 (#95)
53
 
src/routes/__tests__/accounts-delete-warnings.test.ts ADDED
@@ -0,0 +1,133 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * Tests that deleting an account also clears its quota warnings.
3
+ * Regression test for issue #100.
4
+ */
5
+
6
+ import { describe, it, expect, vi, beforeEach } from "vitest";
7
+
8
+ // Mock fs before importing anything
9
+ vi.mock("fs", () => ({
10
+ readFileSync: vi.fn(() => { throw new Error("ENOENT"); }),
11
+ writeFileSync: vi.fn(),
12
+ renameSync: vi.fn(),
13
+ existsSync: vi.fn(() => false),
14
+ mkdirSync: vi.fn(),
15
+ }));
16
+
17
+ vi.mock("../../paths.js", () => ({
18
+ getDataDir: vi.fn(() => "/tmp/test-data"),
19
+ getConfigDir: vi.fn(() => "/tmp/test-config"),
20
+ }));
21
+
22
+ vi.mock("../../config.js", () => ({
23
+ getConfig: vi.fn(() => ({
24
+ auth: {
25
+ jwt_token: null,
26
+ rotation_strategy: "least_used",
27
+ rate_limit_backoff_seconds: 60,
28
+ },
29
+ server: { proxy_api_key: null },
30
+ })),
31
+ }));
32
+
33
+ const mockIsTokenExpired = vi.hoisted(() => vi.fn(() => false));
34
+ vi.mock("../../auth/jwt-utils.js", () => ({
35
+ decodeJwtPayload: vi.fn(() => ({ exp: Math.floor(Date.now() / 1000) + 3600 })),
36
+ extractChatGptAccountId: vi.fn((token: string) => `acct-${token.slice(0, 8)}`),
37
+ extractUserProfile: vi.fn((token: string) => ({
38
+ email: `${token.slice(0, 4)}@test.com`,
39
+ chatgpt_plan_type: "free",
40
+ })),
41
+ isTokenExpired: mockIsTokenExpired,
42
+ }));
43
+
44
+ vi.mock("../../utils/jitter.js", () => ({
45
+ jitter: vi.fn((val: number) => val),
46
+ }));
47
+
48
+ vi.mock("../../models/model-store.js", () => ({
49
+ getModelPlanTypes: vi.fn(() => []),
50
+ }));
51
+
52
+ import { Hono } from "hono";
53
+ import { AccountPool } from "../../auth/account-pool.js";
54
+ import { createAccountRoutes } from "../../routes/accounts.js";
55
+ import { updateWarnings, getActiveWarnings, clearWarnings } from "../../auth/quota-warnings.js";
56
+
57
+ const mockScheduler = {
58
+ scheduleOne: vi.fn(),
59
+ clearOne: vi.fn(),
60
+ start: vi.fn(),
61
+ stop: vi.fn(),
62
+ };
63
+
64
+ describe("DELETE /auth/accounts/:id clears quota warnings", () => {
65
+ let pool: AccountPool;
66
+ let app: Hono;
67
+
68
+ beforeEach(() => {
69
+ mockIsTokenExpired.mockReturnValue(false);
70
+ pool = new AccountPool();
71
+ const routes = createAccountRoutes(pool, mockScheduler as never);
72
+ app = new Hono();
73
+ app.route("/", routes);
74
+
75
+ // Clean up warnings state between tests
76
+ for (const w of getActiveWarnings()) {
77
+ clearWarnings(w.accountId);
78
+ }
79
+ });
80
+
81
+ it("should clear quota warnings when an account is deleted", async () => {
82
+ // Add an account
83
+ const token = "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.test-delete-warnings";
84
+ const addResp = await app.request("/auth/accounts", {
85
+ method: "POST",
86
+ headers: { "Content-Type": "application/json" },
87
+ body: JSON.stringify({ token }),
88
+ });
89
+ expect(addResp.status).toBe(200);
90
+ const { account } = await addResp.json();
91
+ const accountId = account.id;
92
+
93
+ // Simulate quota warnings for this account
94
+ updateWarnings(accountId, [
95
+ {
96
+ accountId,
97
+ email: "test@test.com",
98
+ window: "primary",
99
+ level: "critical",
100
+ usedPercent: 95,
101
+ resetAt: null,
102
+ },
103
+ ]);
104
+ expect(getActiveWarnings().some((w) => w.accountId === accountId)).toBe(true);
105
+
106
+ // Delete the account
107
+ const delResp = await app.request(`/auth/accounts/${encodeURIComponent(accountId)}`, {
108
+ method: "DELETE",
109
+ });
110
+ expect(delResp.status).toBe(200);
111
+
112
+ // Warnings should be cleared
113
+ expect(getActiveWarnings().some((w) => w.accountId === accountId)).toBe(false);
114
+ });
115
+
116
+ it("should not fail when deleting account with no warnings", async () => {
117
+ const token = "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.test-no-warnings";
118
+ const addResp = await app.request("/auth/accounts", {
119
+ method: "POST",
120
+ headers: { "Content-Type": "application/json" },
121
+ body: JSON.stringify({ token }),
122
+ });
123
+ const { account } = await addResp.json();
124
+
125
+ // No warnings set — delete should still succeed
126
+ const delResp = await app.request(`/auth/accounts/${encodeURIComponent(account.id)}`, {
127
+ method: "DELETE",
128
+ });
129
+ expect(delResp.status).toBe(200);
130
+ const body = await delResp.json();
131
+ expect(body.success).toBe(true);
132
+ });
133
+ });
src/routes/accounts.ts CHANGED
@@ -26,7 +26,7 @@ import type { CodexQuota, AccountInfo } from "../auth/types.js";
26
  import type { CookieJar } from "../proxy/cookie-jar.js";
27
  import type { ProxyPool } from "../proxy/proxy-pool.js";
28
  import { toQuota } from "../auth/quota-utils.js";
29
- import { getActiveWarnings, getWarningsLastUpdated } from "../auth/quota-warnings.js";
30
 
31
  const BulkImportSchema = z.object({
32
  accounts: z.array(z.object({
@@ -216,6 +216,7 @@ export function createAccountRoutes(
216
  return c.json({ error: "Account not found" });
217
  }
218
  cookieJar?.clear(id);
 
219
  return c.json({ success: true });
220
  });
221
 
 
26
  import type { CookieJar } from "../proxy/cookie-jar.js";
27
  import type { ProxyPool } from "../proxy/proxy-pool.js";
28
  import { toQuota } from "../auth/quota-utils.js";
29
+ import { clearWarnings, getActiveWarnings, getWarningsLastUpdated } from "../auth/quota-warnings.js";
30
 
31
  const BulkImportSchema = z.object({
32
  accounts: z.array(z.object({
 
216
  return c.json({ error: "Account not found" });
217
  }
218
  cookieJar?.clear(id);
219
+ clearWarnings(id);
220
  return c.json({ success: true });
221
  });
222