chatccc 0.2.188 → 0.2.190

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. package/agent-prompts/claude_specific.md +45 -45
  2. package/agent-prompts/codex_specific.md +2 -2
  3. package/agent-prompts/cursor_specific.md +13 -13
  4. package/config.sample.json +14 -14
  5. package/im-skills/feishu-skill/receive-send-file.md +63 -63
  6. package/im-skills/feishu-skill/receive-send-image.md +24 -24
  7. package/im-skills/feishu-skill/skill.md +3 -3
  8. package/im-skills/wechat-file-skill/receive-send-file.md +38 -38
  9. package/im-skills/wechat-file-skill/send-file.mjs +83 -83
  10. package/im-skills/wechat-file-skill/skill.md +10 -10
  11. package/im-skills/wechat-image-skill/skill.md +10 -10
  12. package/im-skills/wechat-video-skill/receive-send-video.md +38 -38
  13. package/im-skills/wechat-video-skill/send-video.mjs +79 -79
  14. package/im-skills/wechat-video-skill/skill.md +10 -10
  15. package/package.json +1 -1
  16. package/scripts/postinstall-sharp-check.mjs +58 -58
  17. package/src/__tests__/agent-delegate-task-rpc.test.ts +165 -165
  18. package/src/__tests__/builtin-config.test.ts +33 -33
  19. package/src/__tests__/card-plain-text.test.ts +5 -5
  20. package/src/__tests__/cardkit.test.ts +60 -60
  21. package/src/__tests__/cards.test.ts +77 -77
  22. package/src/__tests__/chatgpt-subscription-rpc.test.ts +89 -89
  23. package/src/__tests__/chatgpt-subscription.test.ts +135 -135
  24. package/src/__tests__/chrome-devtools-guard.test.ts +165 -165
  25. package/src/__tests__/claude-adapter.test.ts +592 -592
  26. package/src/__tests__/codex-reset-actions.test.ts +146 -146
  27. package/src/__tests__/config-reload.test.ts +4 -4
  28. package/src/__tests__/config-sample.test.ts +17 -17
  29. package/src/__tests__/feishu-api.test.ts +60 -60
  30. package/src/__tests__/feishu-platform.test.ts +22 -22
  31. package/src/__tests__/format-message.test.ts +47 -47
  32. package/src/__tests__/orchestrator.test.ts +116 -112
  33. package/src/__tests__/privacy.test.ts +198 -198
  34. package/src/__tests__/raw-stream-log.test.ts +106 -106
  35. package/src/__tests__/session.test.ts +17 -17
  36. package/src/__tests__/shared-prefix.test.ts +36 -36
  37. package/src/__tests__/stream-state.test.ts +42 -42
  38. package/src/__tests__/web-ui.test.ts +209 -130
  39. package/src/adapters/claude-adapter.ts +566 -566
  40. package/src/adapters/claude-session-meta-store.ts +120 -120
  41. package/src/adapters/codex-adapter.ts +27 -28
  42. package/src/adapters/cursor-adapter.ts +46 -46
  43. package/src/adapters/raw-stream-log.ts +124 -124
  44. package/src/adapters/resource-monitor.ts +140 -140
  45. package/src/agent-delegate-task-rpc.ts +153 -153
  46. package/src/agent-delegate-task.ts +81 -81
  47. package/src/agent-stop-stuck.ts +129 -129
  48. package/src/builtin/cli.ts +189 -189
  49. package/src/builtin/index.ts +170 -168
  50. package/src/cards.ts +137 -137
  51. package/src/chatgpt-subscription-rpc.ts +27 -27
  52. package/src/chatgpt-subscription.ts +299 -299
  53. package/src/chrome-devtools-guard.ts +318 -318
  54. package/src/codex-reset-actions.ts +184 -184
  55. package/src/config.ts +86 -86
  56. package/src/feishu-platform.ts +20 -20
  57. package/src/format-message.ts +293 -293
  58. package/src/litellm-proxy.ts +374 -374
  59. package/src/orchestrator.ts +143 -143
  60. package/src/privacy.ts +118 -118
  61. package/src/session-chat-binding.ts +6 -6
  62. package/src/session-name.ts +8 -8
  63. package/src/session.ts +98 -98
  64. package/src/shared-prefix.ts +29 -29
  65. package/src/sim-platform.ts +20 -20
  66. package/src/turn-cards.ts +117 -117
  67. package/src/web-ui.ts +142 -24
@@ -1,18 +1,18 @@
1
1
  import { describe, it, expect } from "vitest";
2
- import {
3
- buildProgressCard,
4
- buildHelpCard,
2
+ import {
3
+ buildProgressCard,
4
+ buildHelpCard,
5
5
  buildCdContent,
6
6
  buildCdCard,
7
7
  buildSessionsCard,
8
- buildStatusCard,
9
- buildCodexUsageCard,
10
- buildCodexResetConfirmCard,
11
- buildButtons,
12
- truncateContent,
13
- getToolEmoji,
14
- } from "../cards.ts";
15
- import { ABD_HELP_LINE } from "../shared-prefix.ts";
8
+ buildStatusCard,
9
+ buildCodexUsageCard,
10
+ buildCodexResetConfirmCard,
11
+ buildButtons,
12
+ truncateContent,
13
+ getToolEmoji,
14
+ } from "../cards.ts";
15
+ import { ABD_HELP_LINE } from "../shared-prefix.ts";
16
16
 
17
17
  // ---------------------------------------------------------------------------
18
18
  // truncateContent
@@ -154,23 +154,23 @@ describe("buildHelpCard", () => {
154
154
  expect(parsed.elements[0].text.content).toContain("你好");
155
155
  });
156
156
 
157
- it("includes action buttons", () => {
158
- const card = buildHelpCard("test");
159
- const parsed = JSON.parse(card);
160
- const action = parsed.elements[2];
161
- expect(action.tag).toBe("action");
162
- expect(action.actions).toHaveLength(7);
163
- });
164
-
165
- it("adds ABD prefix help as the final help line", () => {
166
- const card = buildHelpCard("test");
167
- const parsed = JSON.parse(card);
168
- const lines = parsed.elements[1].text.content.split("\n");
169
-
170
- expect(lines).toContain("发送 **/usage** 查看 Codex 5h/周用量,以及查询/使用主动重置卡");
171
- expect(lines.at(-1)).toBe(ABD_HELP_LINE);
172
- });
173
- });
157
+ it("includes action buttons", () => {
158
+ const card = buildHelpCard("test");
159
+ const parsed = JSON.parse(card);
160
+ const action = parsed.elements[2];
161
+ expect(action.tag).toBe("action");
162
+ expect(action.actions).toHaveLength(7);
163
+ });
164
+
165
+ it("adds ABD prefix help as the final help line", () => {
166
+ const card = buildHelpCard("test");
167
+ const parsed = JSON.parse(card);
168
+ const lines = parsed.elements[1].text.content.split("\n");
169
+
170
+ expect(lines).toContain("发送 **/usage** 查看 Codex 5h/周用量,以及查询/使用主动重置卡");
171
+ expect(lines.at(-1)).toBe(ABD_HELP_LINE);
172
+ });
173
+ });
174
174
 
175
175
  // ---------------------------------------------------------------------------
176
176
  // buildCdContent
@@ -425,7 +425,7 @@ describe("buildSessionsCard", () => {
425
425
  // buildStatusCard
426
426
  // ---------------------------------------------------------------------------
427
427
 
428
- describe("buildStatusCard", () => {
428
+ describe("buildStatusCard", () => {
429
429
  it("returns valid JSON with status text", () => {
430
430
  const card = buildStatusCard("一切正常");
431
431
  const parsed = JSON.parse(card);
@@ -445,54 +445,54 @@ describe("buildStatusCard", () => {
445
445
  const action = parsed.elements[2];
446
446
  expect(action.actions[0].text.content).toBe("收起");
447
447
  });
448
- });
449
-
450
- // ---------------------------------------------------------------------------
451
- // buildCodexUsageCard / buildCodexResetConfirmCard
452
- // ---------------------------------------------------------------------------
453
-
454
- describe("Codex usage reset cards", () => {
455
- it("shows the reset button only when reset credits are available", () => {
456
- const card = buildCodexUsageCard("Codex 用量", 2);
457
- const parsed = JSON.parse(card);
458
- const action = parsed.elements.find((element: any) => element.tag === "action");
459
- expect(action.actions[0].text.content).toBe("发起重置");
460
- expect(action.actions[0].value).toEqual({ action: "codex_reset_request", availableCount: 2 });
461
-
462
- const noCreditCard = buildCodexUsageCard("Codex 用量", 0);
463
- const noCreditParsed = JSON.parse(noCreditCard);
464
- expect(noCreditParsed.elements.some((element: any) => element.tag === "action")).toBe(false);
465
- });
466
-
467
- it("builds yes/no confirmation buttons tied to the parent usage card", () => {
468
- const card = buildCodexResetConfirmCard({
469
- availableCount: 2,
470
- parentMessageId: "usage-message",
471
- requestId: "request-1",
472
- });
473
- const parsed = JSON.parse(card);
474
- const action = parsed.elements.find((element: any) => element.tag === "action");
475
-
476
- expect(action.actions[0].text.content).toBe("是,发起重置");
477
- expect(action.actions[0].value).toEqual({
478
- action: "codex_reset_confirm",
479
- decision: "yes",
480
- parentMessageId: "usage-message",
481
- requestId: "request-1",
482
- });
483
- expect(action.actions[1].text.content).toBe("否");
484
- expect(action.actions[1].value).toEqual({
485
- action: "codex_reset_confirm",
486
- decision: "no",
487
- parentMessageId: "usage-message",
488
- requestId: "request-1",
489
- });
490
- });
491
- });
492
-
493
- // ---------------------------------------------------------------------------
494
- // buildButtons
495
- // ---------------------------------------------------------------------------
448
+ });
449
+
450
+ // ---------------------------------------------------------------------------
451
+ // buildCodexUsageCard / buildCodexResetConfirmCard
452
+ // ---------------------------------------------------------------------------
453
+
454
+ describe("Codex usage reset cards", () => {
455
+ it("shows the reset button only when reset credits are available", () => {
456
+ const card = buildCodexUsageCard("Codex 用量", 2);
457
+ const parsed = JSON.parse(card);
458
+ const action = parsed.elements.find((element: any) => element.tag === "action");
459
+ expect(action.actions[0].text.content).toBe("发起重置");
460
+ expect(action.actions[0].value).toEqual({ action: "codex_reset_request", availableCount: 2 });
461
+
462
+ const noCreditCard = buildCodexUsageCard("Codex 用量", 0);
463
+ const noCreditParsed = JSON.parse(noCreditCard);
464
+ expect(noCreditParsed.elements.some((element: any) => element.tag === "action")).toBe(false);
465
+ });
466
+
467
+ it("builds yes/no confirmation buttons tied to the parent usage card", () => {
468
+ const card = buildCodexResetConfirmCard({
469
+ availableCount: 2,
470
+ parentMessageId: "usage-message",
471
+ requestId: "request-1",
472
+ });
473
+ const parsed = JSON.parse(card);
474
+ const action = parsed.elements.find((element: any) => element.tag === "action");
475
+
476
+ expect(action.actions[0].text.content).toBe("是,发起重置");
477
+ expect(action.actions[0].value).toEqual({
478
+ action: "codex_reset_confirm",
479
+ decision: "yes",
480
+ parentMessageId: "usage-message",
481
+ requestId: "request-1",
482
+ });
483
+ expect(action.actions[1].text.content).toBe("否");
484
+ expect(action.actions[1].value).toEqual({
485
+ action: "codex_reset_confirm",
486
+ decision: "no",
487
+ parentMessageId: "usage-message",
488
+ requestId: "request-1",
489
+ });
490
+ });
491
+ });
492
+
493
+ // ---------------------------------------------------------------------------
494
+ // buildButtons
495
+ // ---------------------------------------------------------------------------
496
496
 
497
497
  describe("buildButtons", () => {
498
498
  it("returns action with buttons", () => {
@@ -1,89 +1,89 @@
1
- import { Readable } from "node:stream";
2
- import { beforeEach, describe, expect, it, vi } from "vitest";
3
-
4
- const mockGetChatGptSubscriptionStatus = vi.hoisted(() => vi.fn());
5
-
6
- vi.mock("../chatgpt-subscription.ts", () => ({
7
- getChatGptSubscriptionStatus: mockGetChatGptSubscriptionStatus,
8
- }));
9
-
10
- import {
11
- CHATGPT_SUBSCRIPTION_PATH,
12
- handleChatGptSubscriptionRequest,
13
- } from "../chatgpt-subscription-rpc.ts";
14
-
15
- function request(path = CHATGPT_SUBSCRIPTION_PATH, method = "GET"): Readable & {
16
- url?: string;
17
- method?: string;
18
- headers: Record<string, string>;
19
- } {
20
- const req = Readable.from([]) as Readable & {
21
- url?: string;
22
- method?: string;
23
- headers: Record<string, string>;
24
- };
25
- req.url = path;
26
- req.method = method;
27
- req.headers = {};
28
- return req;
29
- }
30
-
31
- function response() {
32
- const res = {
33
- statusCode: 0,
34
- headers: {} as Record<string, string>,
35
- body: "",
36
- writeHead(status: number, headers: Record<string, string>) {
37
- this.statusCode = status;
38
- this.headers = headers;
39
- return this;
40
- },
41
- end(chunk?: string) {
42
- this.body += chunk ?? "";
43
- return this;
44
- },
45
- };
46
- return res;
47
- }
48
-
49
- describe("ChatGPT subscription RPC", () => {
50
- beforeEach(() => {
51
- mockGetChatGptSubscriptionStatus.mockReset();
52
- mockGetChatGptSubscriptionStatus.mockResolvedValue({
53
- ok: false,
54
- code: "chrome_cdp_disabled",
55
- reason: "Chrome CDP guard is disabled in ChatCCC config.",
56
- chromeCdp: { enabled: false, port: 15166, status: "skipped" },
57
- });
58
- });
59
-
60
- it("returns the structured subscription lookup JSON", async () => {
61
- const req = request();
62
- const res = response();
63
-
64
- await expect(handleChatGptSubscriptionRequest(req as never, res as never)).resolves.toBe(true);
65
-
66
- expect(res.statusCode).toBe(200);
67
- expect(JSON.parse(res.body)).toEqual({
68
- ok: false,
69
- code: "chrome_cdp_disabled",
70
- reason: "Chrome CDP guard is disabled in ChatCCC config.",
71
- chromeCdp: { enabled: false, port: 15166, status: "skipped" },
72
- });
73
- });
74
-
75
- it("does not handle other paths", async () => {
76
- const res = response();
77
-
78
- await expect(handleChatGptSubscriptionRequest(request("/api/other") as never, res as never)).resolves.toBe(false);
79
- expect(res.body).toBe("");
80
- });
81
-
82
- it("rejects non-GET methods", async () => {
83
- const res = response();
84
-
85
- await expect(handleChatGptSubscriptionRequest(request(CHATGPT_SUBSCRIPTION_PATH, "POST") as never, res as never)).resolves.toBe(true);
86
- expect(res.statusCode).toBe(405);
87
- expect(JSON.parse(res.body)).toMatchObject({ ok: false, code: "method_not_allowed" });
88
- });
89
- });
1
+ import { Readable } from "node:stream";
2
+ import { beforeEach, describe, expect, it, vi } from "vitest";
3
+
4
+ const mockGetChatGptSubscriptionStatus = vi.hoisted(() => vi.fn());
5
+
6
+ vi.mock("../chatgpt-subscription.ts", () => ({
7
+ getChatGptSubscriptionStatus: mockGetChatGptSubscriptionStatus,
8
+ }));
9
+
10
+ import {
11
+ CHATGPT_SUBSCRIPTION_PATH,
12
+ handleChatGptSubscriptionRequest,
13
+ } from "../chatgpt-subscription-rpc.ts";
14
+
15
+ function request(path = CHATGPT_SUBSCRIPTION_PATH, method = "GET"): Readable & {
16
+ url?: string;
17
+ method?: string;
18
+ headers: Record<string, string>;
19
+ } {
20
+ const req = Readable.from([]) as Readable & {
21
+ url?: string;
22
+ method?: string;
23
+ headers: Record<string, string>;
24
+ };
25
+ req.url = path;
26
+ req.method = method;
27
+ req.headers = {};
28
+ return req;
29
+ }
30
+
31
+ function response() {
32
+ const res = {
33
+ statusCode: 0,
34
+ headers: {} as Record<string, string>,
35
+ body: "",
36
+ writeHead(status: number, headers: Record<string, string>) {
37
+ this.statusCode = status;
38
+ this.headers = headers;
39
+ return this;
40
+ },
41
+ end(chunk?: string) {
42
+ this.body += chunk ?? "";
43
+ return this;
44
+ },
45
+ };
46
+ return res;
47
+ }
48
+
49
+ describe("ChatGPT subscription RPC", () => {
50
+ beforeEach(() => {
51
+ mockGetChatGptSubscriptionStatus.mockReset();
52
+ mockGetChatGptSubscriptionStatus.mockResolvedValue({
53
+ ok: false,
54
+ code: "chrome_cdp_disabled",
55
+ reason: "Chrome CDP guard is disabled in ChatCCC config.",
56
+ chromeCdp: { enabled: false, port: 15166, status: "skipped" },
57
+ });
58
+ });
59
+
60
+ it("returns the structured subscription lookup JSON", async () => {
61
+ const req = request();
62
+ const res = response();
63
+
64
+ await expect(handleChatGptSubscriptionRequest(req as never, res as never)).resolves.toBe(true);
65
+
66
+ expect(res.statusCode).toBe(200);
67
+ expect(JSON.parse(res.body)).toEqual({
68
+ ok: false,
69
+ code: "chrome_cdp_disabled",
70
+ reason: "Chrome CDP guard is disabled in ChatCCC config.",
71
+ chromeCdp: { enabled: false, port: 15166, status: "skipped" },
72
+ });
73
+ });
74
+
75
+ it("does not handle other paths", async () => {
76
+ const res = response();
77
+
78
+ await expect(handleChatGptSubscriptionRequest(request("/api/other") as never, res as never)).resolves.toBe(false);
79
+ expect(res.body).toBe("");
80
+ });
81
+
82
+ it("rejects non-GET methods", async () => {
83
+ const res = response();
84
+
85
+ await expect(handleChatGptSubscriptionRequest(request(CHATGPT_SUBSCRIPTION_PATH, "POST") as never, res as never)).resolves.toBe(true);
86
+ expect(res.statusCode).toBe(405);
87
+ expect(JSON.parse(res.body)).toMatchObject({ ok: false, code: "method_not_allowed" });
88
+ });
89
+ });
@@ -1,135 +1,135 @@
1
- import { beforeEach, describe, expect, it, vi } from "vitest";
2
-
3
- import { config } from "../config.ts";
4
- import { getChatGptSubscriptionStatus } from "../chatgpt-subscription.ts";
5
-
6
- describe("ChatGPT subscription lookup", () => {
7
- beforeEach(() => {
8
- config.chromeDevtools = { enabled: true, port: 15166, chromePath: "" };
9
- });
10
-
11
- it("returns disabled without probing the port when Chrome CDP is off", async () => {
12
- config.chromeDevtools = { enabled: false, port: 15166, chromePath: "" };
13
- const probeChromeCdpImpl = vi.fn();
14
- const fetchImpl = vi.fn() as unknown as typeof fetch;
15
-
16
- await expect(getChatGptSubscriptionStatus({ probeChromeCdpImpl, fetchImpl })).resolves.toMatchObject({
17
- ok: false,
18
- code: "chrome_cdp_disabled",
19
- chromeCdp: { enabled: false, port: 15166, status: "skipped" },
20
- });
21
- expect(probeChromeCdpImpl).not.toHaveBeenCalled();
22
- expect(fetchImpl).not.toHaveBeenCalled();
23
- });
24
-
25
- it("returns occupied when the configured port is not a healthy Chrome CDP endpoint", async () => {
26
- await expect(getChatGptSubscriptionStatus({
27
- probeChromeCdpImpl: vi.fn(async () => "occupied" as const),
28
- })).resolves.toMatchObject({
29
- ok: false,
30
- code: "chrome_cdp_occupied",
31
- reason: expect.stringContaining("not a healthy Chrome CDP endpoint"),
32
- });
33
- });
34
-
35
- it("returns page missing when CDP has no ChatGPT page and cannot create one", async () => {
36
- const fetchImpl = vi.fn(async (url: string | URL | Request) => {
37
- const text = String(url);
38
- if (text.endsWith("/json/list")) return new Response(JSON.stringify([]), { status: 200 });
39
- if (text.includes("/json/new?")) return new Response("nope", { status: 500 });
40
- throw new Error(`unexpected fetch: ${text}`);
41
- }) as unknown as typeof fetch;
42
-
43
- await expect(getChatGptSubscriptionStatus({
44
- fetchImpl,
45
- probeChromeCdpImpl: vi.fn(async () => "healthy" as const),
46
- })).resolves.toMatchObject({
47
- ok: false,
48
- code: "chatgpt_page_missing",
49
- });
50
- });
51
-
52
- it("closes only the temporary ChatGPT page created for this lookup", async () => {
53
- const closed: string[] = [];
54
- const fetchImpl = vi.fn(async (url: string | URL | Request) => {
55
- const text = String(url);
56
- if (text.endsWith("/json/list")) return new Response(JSON.stringify([]), { status: 200 });
57
- if (text.includes("/json/new?")) {
58
- return new Response(JSON.stringify({
59
- id: "temp-page",
60
- type: "page",
61
- url: "https://chatgpt.com/",
62
- webSocketDebuggerUrl: "ws://cdp/temp-page",
63
- }), { status: 200 });
64
- }
65
- if (text.endsWith("/json/close/temp-page")) {
66
- closed.push("temp-page");
67
- return new Response("Target is closing", { status: 200 });
68
- }
69
- throw new Error(`unexpected fetch: ${text}`);
70
- }) as unknown as typeof fetch;
71
-
72
- await expect(getChatGptSubscriptionStatus({
73
- fetchImpl,
74
- probeChromeCdpImpl: vi.fn(async () => "healthy" as const),
75
- evaluateInPage: vi.fn(async () => ({
76
- sessionOk: true,
77
- hasAccessToken: true,
78
- maskedEmail: "gg***@gmail.com",
79
- sessionExpires: "2026-09-20T09:30:07.340Z",
80
- account: {
81
- status: 200,
82
- ok: true,
83
- entitlement: {
84
- has_active_subscription: true,
85
- subscription_plan: "chatgptprolite",
86
- expires_at: "2026-07-12T10:20:11+00:00",
87
- },
88
- last_active_subscription: {
89
- will_renew: false,
90
- purchase_origin_platform: "chatgpt_web",
91
- },
92
- },
93
- })),
94
- now: () => new Date("2026-06-22T00:00:00+08:00"),
95
- })).resolves.toMatchObject({
96
- ok: true,
97
- code: "ok",
98
- subscription: {
99
- plan: "chatgptprolite",
100
- expiresAt: "2026-07-12T10:20:11+00:00",
101
- willRenew: false,
102
- remainingDays: 21,
103
- },
104
- });
105
- expect(closed).toEqual(["temp-page"]);
106
- });
107
-
108
- it("does not close an existing ChatGPT page", async () => {
109
- const fetchImpl = vi.fn(async (url: string | URL | Request) => {
110
- const text = String(url);
111
- if (text.endsWith("/json/list")) {
112
- return new Response(JSON.stringify([{
113
- id: "existing-page",
114
- type: "page",
115
- url: "https://chatgpt.com/",
116
- webSocketDebuggerUrl: "ws://cdp/existing-page",
117
- }]), { status: 200 });
118
- }
119
- if (text.includes("/json/close/")) throw new Error("should not close existing page");
120
- throw new Error(`unexpected fetch: ${text}`);
121
- }) as unknown as typeof fetch;
122
-
123
- await expect(getChatGptSubscriptionStatus({
124
- fetchImpl,
125
- probeChromeCdpImpl: vi.fn(async () => "healthy" as const),
126
- evaluateInPage: vi.fn(async () => ({
127
- sessionOk: true,
128
- hasAccessToken: false,
129
- })),
130
- })).resolves.toMatchObject({
131
- ok: false,
132
- code: "chatgpt_session_missing",
133
- });
134
- });
135
- });
1
+ import { beforeEach, describe, expect, it, vi } from "vitest";
2
+
3
+ import { config } from "../config.ts";
4
+ import { getChatGptSubscriptionStatus } from "../chatgpt-subscription.ts";
5
+
6
+ describe("ChatGPT subscription lookup", () => {
7
+ beforeEach(() => {
8
+ config.chromeDevtools = { enabled: true, port: 15166, chromePath: "" };
9
+ });
10
+
11
+ it("returns disabled without probing the port when Chrome CDP is off", async () => {
12
+ config.chromeDevtools = { enabled: false, port: 15166, chromePath: "" };
13
+ const probeChromeCdpImpl = vi.fn();
14
+ const fetchImpl = vi.fn() as unknown as typeof fetch;
15
+
16
+ await expect(getChatGptSubscriptionStatus({ probeChromeCdpImpl, fetchImpl })).resolves.toMatchObject({
17
+ ok: false,
18
+ code: "chrome_cdp_disabled",
19
+ chromeCdp: { enabled: false, port: 15166, status: "skipped" },
20
+ });
21
+ expect(probeChromeCdpImpl).not.toHaveBeenCalled();
22
+ expect(fetchImpl).not.toHaveBeenCalled();
23
+ });
24
+
25
+ it("returns occupied when the configured port is not a healthy Chrome CDP endpoint", async () => {
26
+ await expect(getChatGptSubscriptionStatus({
27
+ probeChromeCdpImpl: vi.fn(async () => "occupied" as const),
28
+ })).resolves.toMatchObject({
29
+ ok: false,
30
+ code: "chrome_cdp_occupied",
31
+ reason: expect.stringContaining("not a healthy Chrome CDP endpoint"),
32
+ });
33
+ });
34
+
35
+ it("returns page missing when CDP has no ChatGPT page and cannot create one", async () => {
36
+ const fetchImpl = vi.fn(async (url: string | URL | Request) => {
37
+ const text = String(url);
38
+ if (text.endsWith("/json/list")) return new Response(JSON.stringify([]), { status: 200 });
39
+ if (text.includes("/json/new?")) return new Response("nope", { status: 500 });
40
+ throw new Error(`unexpected fetch: ${text}`);
41
+ }) as unknown as typeof fetch;
42
+
43
+ await expect(getChatGptSubscriptionStatus({
44
+ fetchImpl,
45
+ probeChromeCdpImpl: vi.fn(async () => "healthy" as const),
46
+ })).resolves.toMatchObject({
47
+ ok: false,
48
+ code: "chatgpt_page_missing",
49
+ });
50
+ });
51
+
52
+ it("closes only the temporary ChatGPT page created for this lookup", async () => {
53
+ const closed: string[] = [];
54
+ const fetchImpl = vi.fn(async (url: string | URL | Request) => {
55
+ const text = String(url);
56
+ if (text.endsWith("/json/list")) return new Response(JSON.stringify([]), { status: 200 });
57
+ if (text.includes("/json/new?")) {
58
+ return new Response(JSON.stringify({
59
+ id: "temp-page",
60
+ type: "page",
61
+ url: "https://chatgpt.com/",
62
+ webSocketDebuggerUrl: "ws://cdp/temp-page",
63
+ }), { status: 200 });
64
+ }
65
+ if (text.endsWith("/json/close/temp-page")) {
66
+ closed.push("temp-page");
67
+ return new Response("Target is closing", { status: 200 });
68
+ }
69
+ throw new Error(`unexpected fetch: ${text}`);
70
+ }) as unknown as typeof fetch;
71
+
72
+ await expect(getChatGptSubscriptionStatus({
73
+ fetchImpl,
74
+ probeChromeCdpImpl: vi.fn(async () => "healthy" as const),
75
+ evaluateInPage: vi.fn(async () => ({
76
+ sessionOk: true,
77
+ hasAccessToken: true,
78
+ maskedEmail: "gg***@gmail.com",
79
+ sessionExpires: "2026-09-20T09:30:07.340Z",
80
+ account: {
81
+ status: 200,
82
+ ok: true,
83
+ entitlement: {
84
+ has_active_subscription: true,
85
+ subscription_plan: "chatgptprolite",
86
+ expires_at: "2026-07-12T10:20:11+00:00",
87
+ },
88
+ last_active_subscription: {
89
+ will_renew: false,
90
+ purchase_origin_platform: "chatgpt_web",
91
+ },
92
+ },
93
+ })),
94
+ now: () => new Date("2026-06-22T00:00:00+08:00"),
95
+ })).resolves.toMatchObject({
96
+ ok: true,
97
+ code: "ok",
98
+ subscription: {
99
+ plan: "chatgptprolite",
100
+ expiresAt: "2026-07-12T10:20:11+00:00",
101
+ willRenew: false,
102
+ remainingDays: 21,
103
+ },
104
+ });
105
+ expect(closed).toEqual(["temp-page"]);
106
+ });
107
+
108
+ it("does not close an existing ChatGPT page", async () => {
109
+ const fetchImpl = vi.fn(async (url: string | URL | Request) => {
110
+ const text = String(url);
111
+ if (text.endsWith("/json/list")) {
112
+ return new Response(JSON.stringify([{
113
+ id: "existing-page",
114
+ type: "page",
115
+ url: "https://chatgpt.com/",
116
+ webSocketDebuggerUrl: "ws://cdp/existing-page",
117
+ }]), { status: 200 });
118
+ }
119
+ if (text.includes("/json/close/")) throw new Error("should not close existing page");
120
+ throw new Error(`unexpected fetch: ${text}`);
121
+ }) as unknown as typeof fetch;
122
+
123
+ await expect(getChatGptSubscriptionStatus({
124
+ fetchImpl,
125
+ probeChromeCdpImpl: vi.fn(async () => "healthy" as const),
126
+ evaluateInPage: vi.fn(async () => ({
127
+ sessionOk: true,
128
+ hasAccessToken: false,
129
+ })),
130
+ })).resolves.toMatchObject({
131
+ ok: false,
132
+ code: "chatgpt_session_missing",
133
+ });
134
+ });
135
+ });