chatccc 0.2.189 → 0.2.191

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 +120 -116
  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 -209
  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 +30 -30
  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 +204 -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 +205 -205
@@ -1,165 +1,165 @@
1
- import { Readable } from "node:stream";
2
- import { describe, expect, it, vi, beforeEach } from "vitest";
3
-
4
- import type { PlatformAdapter } from "../platform-adapter.ts";
5
-
6
- const mockDelegateAgentTask = vi.hoisted(() => vi.fn());
7
-
8
- vi.mock("../agent-delegate-task.ts", () => ({
9
- delegateAgentTask: mockDelegateAgentTask,
10
- }));
11
-
12
- import {
13
- AGENT_DELEGATE_TASK_PATH,
14
- buildAgentDelegateTaskCapabilityPrompt,
15
- handleAgentDelegateTaskRequest,
16
- } from "../agent-delegate-task-rpc.ts";
17
- import { ABD_APPEND_PROMPT } from "../shared-prefix.ts";
18
-
19
- function request(body: unknown, path = AGENT_DELEGATE_TASK_PATH, method = "POST"): Readable & {
20
- url?: string;
21
- method?: string;
22
- headers: Record<string, string>;
23
- } {
24
- const req = Readable.from([Buffer.from(JSON.stringify(body), "utf8")]) as Readable & {
25
- url?: string;
26
- method?: string;
27
- headers: Record<string, string>;
28
- };
29
- req.url = path;
30
- req.method = method;
31
- req.headers = { "content-type": "application/json; charset=utf-8" };
32
- return req;
33
- }
34
-
35
- function response() {
36
- const res = {
37
- statusCode: 0,
38
- headers: {} as Record<string, string>,
39
- body: "",
40
- writeHead(status: number, headers: Record<string, string>) {
41
- this.statusCode = status;
42
- this.headers = headers;
43
- return this;
44
- },
45
- end(chunk?: string) {
46
- this.body += chunk ?? "";
47
- return this;
48
- },
49
- };
50
- return res;
51
- }
52
-
53
- function platform(kind: "feishu" | "wechat" = "feishu"): PlatformAdapter {
54
- return {
55
- kind,
56
- sendText: vi.fn(async () => true),
57
- sendCard: vi.fn(async () => true),
58
- sendRawCard: vi.fn(async () => true),
59
- createGroup: vi.fn(async () => "chat-id"),
60
- updateChatInfo: vi.fn(async () => {}),
61
- getChatInfo: vi.fn(async () => ({ name: "chat", description: "" })),
62
- disbandChat: vi.fn(async () => {}),
63
- setChatAvatar: vi.fn(async () => {}),
64
- extractSessionInfo: vi.fn(() => null),
65
- cardCreate: vi.fn(async () => "card-id"),
66
- cardSend: vi.fn(async () => "message-id"),
67
- cardUpdate: vi.fn(async () => {}),
68
- };
69
- }
70
-
71
- describe("agent delegate task RPC", () => {
72
- beforeEach(() => {
73
- mockDelegateAgentTask.mockReset();
74
- mockDelegateAgentTask.mockResolvedValue({
75
- chatId: "chat-new",
76
- sessionId: "sid-new",
77
- tool: "codex",
78
- cwd: "F:\\repo",
79
- });
80
- });
81
-
82
- it("delegates a task through the local API", async () => {
83
- const req = request({
84
- tool: "codex",
85
- cwd: "F:\\repo",
86
- open_id: "ou-user",
87
- prompt: "帮我分析日志",
88
- });
89
- const res = response();
90
-
91
- await handleAgentDelegateTaskRequest(req as never, res as never, platform());
92
-
93
- expect(res.statusCode).toBe(200);
94
- expect(JSON.parse(res.body)).toMatchObject({
95
- ok: true,
96
- chat_id: "chat-new",
97
- session_id: "sid-new",
98
- tool: "codex",
99
- });
100
- expect(mockDelegateAgentTask).toHaveBeenCalledWith(expect.objectContaining({
101
- tool: "codex",
102
- cwd: expect.stringContaining("repo"),
103
- promptText: "帮我分析日志",
104
- openIds: ["ou-user"],
105
- chatNamePrefix: "帮我分析日志",
106
- }));
107
- });
108
-
109
- it("applies the shared ABD prefix to API prompts", async () => {
110
- const req = request({
111
- tool: "claude",
112
- cwd: "F:\\repo",
113
- openIds: ["ou-user"],
114
- prompt: "/abd帮我分析",
115
- });
116
- const res = response();
117
-
118
- await handleAgentDelegateTaskRequest(req as never, res as never, platform());
119
-
120
- expect(res.statusCode).toBe(200);
121
- expect(mockDelegateAgentTask).toHaveBeenCalledWith(expect.objectContaining({
122
- promptText: `帮我分析\n\n---\n${ABD_APPEND_PROMPT}`,
123
- chatNamePrefix: "帮我分析",
124
- }));
125
- });
126
-
127
- it("rejects non-Feishu platforms", async () => {
128
- const req = request({
129
- tool: "codex",
130
- cwd: "F:\\repo",
131
- open_id: "ou-user",
132
- prompt: "task",
133
- });
134
- const res = response();
135
-
136
- await handleAgentDelegateTaskRequest(req as never, res as never, platform("wechat"));
137
-
138
- expect(res.statusCode).toBe(409);
139
- expect(mockDelegateAgentTask).not.toHaveBeenCalled();
140
- });
141
-
142
- it("rejects missing required parameters", async () => {
143
- const req = request({ tool: "codex", cwd: "F:\\repo", prompt: "task" });
144
- const res = response();
145
-
146
- await handleAgentDelegateTaskRequest(req as never, res as never, platform());
147
-
148
- expect(res.statusCode).toBe(400);
149
- expect(JSON.parse(res.body).error).toContain("open_id");
150
- expect(mockDelegateAgentTask).not.toHaveBeenCalled();
151
- });
152
-
153
- it("builds prompt instructions for the delegate endpoint", () => {
154
- const prompt = buildAgentDelegateTaskCapabilityPrompt({
155
- url: `http://127.0.0.1:18080${AGENT_DELEGATE_TASK_PATH}`,
156
- cwd: "F:/repo",
157
- });
158
-
159
- expect(prompt).toContain("POST http://127.0.0.1:18080/api/agent/delegate-task");
160
- expect(prompt).toContain('"tool":"codex|claude|cursor"');
161
- expect(prompt).toContain('"cwd":"absolute working directory"');
162
- expect(prompt).toContain("project prompt injection and IM skills still apply");
163
- expect(prompt).toContain("Current working directory: F:/repo");
164
- });
165
- });
1
+ import { Readable } from "node:stream";
2
+ import { describe, expect, it, vi, beforeEach } from "vitest";
3
+
4
+ import type { PlatformAdapter } from "../platform-adapter.ts";
5
+
6
+ const mockDelegateAgentTask = vi.hoisted(() => vi.fn());
7
+
8
+ vi.mock("../agent-delegate-task.ts", () => ({
9
+ delegateAgentTask: mockDelegateAgentTask,
10
+ }));
11
+
12
+ import {
13
+ AGENT_DELEGATE_TASK_PATH,
14
+ buildAgentDelegateTaskCapabilityPrompt,
15
+ handleAgentDelegateTaskRequest,
16
+ } from "../agent-delegate-task-rpc.ts";
17
+ import { ABD_APPEND_PROMPT } from "../shared-prefix.ts";
18
+
19
+ function request(body: unknown, path = AGENT_DELEGATE_TASK_PATH, method = "POST"): Readable & {
20
+ url?: string;
21
+ method?: string;
22
+ headers: Record<string, string>;
23
+ } {
24
+ const req = Readable.from([Buffer.from(JSON.stringify(body), "utf8")]) as Readable & {
25
+ url?: string;
26
+ method?: string;
27
+ headers: Record<string, string>;
28
+ };
29
+ req.url = path;
30
+ req.method = method;
31
+ req.headers = { "content-type": "application/json; charset=utf-8" };
32
+ return req;
33
+ }
34
+
35
+ function response() {
36
+ const res = {
37
+ statusCode: 0,
38
+ headers: {} as Record<string, string>,
39
+ body: "",
40
+ writeHead(status: number, headers: Record<string, string>) {
41
+ this.statusCode = status;
42
+ this.headers = headers;
43
+ return this;
44
+ },
45
+ end(chunk?: string) {
46
+ this.body += chunk ?? "";
47
+ return this;
48
+ },
49
+ };
50
+ return res;
51
+ }
52
+
53
+ function platform(kind: "feishu" | "wechat" = "feishu"): PlatformAdapter {
54
+ return {
55
+ kind,
56
+ sendText: vi.fn(async () => true),
57
+ sendCard: vi.fn(async () => true),
58
+ sendRawCard: vi.fn(async () => true),
59
+ createGroup: vi.fn(async () => "chat-id"),
60
+ updateChatInfo: vi.fn(async () => {}),
61
+ getChatInfo: vi.fn(async () => ({ name: "chat", description: "" })),
62
+ disbandChat: vi.fn(async () => {}),
63
+ setChatAvatar: vi.fn(async () => {}),
64
+ extractSessionInfo: vi.fn(() => null),
65
+ cardCreate: vi.fn(async () => "card-id"),
66
+ cardSend: vi.fn(async () => "message-id"),
67
+ cardUpdate: vi.fn(async () => {}),
68
+ };
69
+ }
70
+
71
+ describe("agent delegate task RPC", () => {
72
+ beforeEach(() => {
73
+ mockDelegateAgentTask.mockReset();
74
+ mockDelegateAgentTask.mockResolvedValue({
75
+ chatId: "chat-new",
76
+ sessionId: "sid-new",
77
+ tool: "codex",
78
+ cwd: "F:\\repo",
79
+ });
80
+ });
81
+
82
+ it("delegates a task through the local API", async () => {
83
+ const req = request({
84
+ tool: "codex",
85
+ cwd: "F:\\repo",
86
+ open_id: "ou-user",
87
+ prompt: "帮我分析日志",
88
+ });
89
+ const res = response();
90
+
91
+ await handleAgentDelegateTaskRequest(req as never, res as never, platform());
92
+
93
+ expect(res.statusCode).toBe(200);
94
+ expect(JSON.parse(res.body)).toMatchObject({
95
+ ok: true,
96
+ chat_id: "chat-new",
97
+ session_id: "sid-new",
98
+ tool: "codex",
99
+ });
100
+ expect(mockDelegateAgentTask).toHaveBeenCalledWith(expect.objectContaining({
101
+ tool: "codex",
102
+ cwd: expect.stringContaining("repo"),
103
+ promptText: "帮我分析日志",
104
+ openIds: ["ou-user"],
105
+ chatNamePrefix: "帮我分析日志",
106
+ }));
107
+ });
108
+
109
+ it("applies the shared ABD prefix to API prompts", async () => {
110
+ const req = request({
111
+ tool: "claude",
112
+ cwd: "F:\\repo",
113
+ openIds: ["ou-user"],
114
+ prompt: "/abd帮我分析",
115
+ });
116
+ const res = response();
117
+
118
+ await handleAgentDelegateTaskRequest(req as never, res as never, platform());
119
+
120
+ expect(res.statusCode).toBe(200);
121
+ expect(mockDelegateAgentTask).toHaveBeenCalledWith(expect.objectContaining({
122
+ promptText: `帮我分析\n\n---\n${ABD_APPEND_PROMPT}`,
123
+ chatNamePrefix: "帮我分析",
124
+ }));
125
+ });
126
+
127
+ it("rejects non-Feishu platforms", async () => {
128
+ const req = request({
129
+ tool: "codex",
130
+ cwd: "F:\\repo",
131
+ open_id: "ou-user",
132
+ prompt: "task",
133
+ });
134
+ const res = response();
135
+
136
+ await handleAgentDelegateTaskRequest(req as never, res as never, platform("wechat"));
137
+
138
+ expect(res.statusCode).toBe(409);
139
+ expect(mockDelegateAgentTask).not.toHaveBeenCalled();
140
+ });
141
+
142
+ it("rejects missing required parameters", async () => {
143
+ const req = request({ tool: "codex", cwd: "F:\\repo", prompt: "task" });
144
+ const res = response();
145
+
146
+ await handleAgentDelegateTaskRequest(req as never, res as never, platform());
147
+
148
+ expect(res.statusCode).toBe(400);
149
+ expect(JSON.parse(res.body).error).toContain("open_id");
150
+ expect(mockDelegateAgentTask).not.toHaveBeenCalled();
151
+ });
152
+
153
+ it("builds prompt instructions for the delegate endpoint", () => {
154
+ const prompt = buildAgentDelegateTaskCapabilityPrompt({
155
+ url: `http://127.0.0.1:18080${AGENT_DELEGATE_TASK_PATH}`,
156
+ cwd: "F:/repo",
157
+ });
158
+
159
+ expect(prompt).toContain("POST http://127.0.0.1:18080/api/agent/delegate-task");
160
+ expect(prompt).toContain('"tool":"codex|claude|cursor"');
161
+ expect(prompt).toContain('"cwd":"absolute working directory"');
162
+ expect(prompt).toContain("project prompt injection and IM skills still apply");
163
+ expect(prompt).toContain("Current working directory: F:/repo");
164
+ });
165
+ });
@@ -1,33 +1,33 @@
1
- import { afterEach, describe, expect, it } from "vitest";
2
-
3
- import { ChatSession } from "../builtin/index.ts";
4
- import { config } from "../config.ts";
5
-
6
- const originalDeepSeekApiKey = process.env.DEEPSEEK_API_KEY;
7
- const originalCccConfig = structuredClone(config.ccc);
8
-
9
- afterEach(() => {
10
- if (originalDeepSeekApiKey === undefined) {
11
- delete process.env.DEEPSEEK_API_KEY;
12
- } else {
13
- process.env.DEEPSEEK_API_KEY = originalDeepSeekApiKey;
14
- }
15
- config.ccc = structuredClone(originalCccConfig);
16
- });
17
-
18
- describe("builtin ChatSession config", () => {
19
- it("does not fall back to DEEPSEEK_API_KEY environment variable", () => {
20
- config.ccc = {
21
- DEEPSEEK_API_KEY: "",
22
- DEEPSEEK_BASE_URL: "https://api.deepseek.com/v1",
23
- model: "deepseek-v4-pro[1m]",
24
- };
25
- process.env.DEEPSEEK_API_KEY = "sk-env-should-not-be-used";
26
-
27
- expect(() => new ChatSession()).toThrow("ccc.DEEPSEEK_API_KEY 未设置");
28
- });
29
-
30
- it("allows constructor parameters to override config defaults", () => {
31
- expect(() => new ChatSession({ apiKey: "sk-test" })).not.toThrow();
32
- });
33
- });
1
+ import { afterEach, describe, expect, it } from "vitest";
2
+
3
+ import { ChatSession } from "../builtin/index.ts";
4
+ import { config } from "../config.ts";
5
+
6
+ const originalDeepSeekApiKey = process.env.DEEPSEEK_API_KEY;
7
+ const originalCccConfig = structuredClone(config.ccc);
8
+
9
+ afterEach(() => {
10
+ if (originalDeepSeekApiKey === undefined) {
11
+ delete process.env.DEEPSEEK_API_KEY;
12
+ } else {
13
+ process.env.DEEPSEEK_API_KEY = originalDeepSeekApiKey;
14
+ }
15
+ config.ccc = structuredClone(originalCccConfig);
16
+ });
17
+
18
+ describe("builtin ChatSession config", () => {
19
+ it("does not fall back to DEEPSEEK_API_KEY environment variable", () => {
20
+ config.ccc = {
21
+ DEEPSEEK_API_KEY: "",
22
+ DEEPSEEK_BASE_URL: "https://api.deepseek.com/v1",
23
+ model: "deepseek-v4-pro",
24
+ };
25
+ process.env.DEEPSEEK_API_KEY = "sk-env-should-not-be-used";
26
+
27
+ expect(() => new ChatSession()).toThrow("ccc.DEEPSEEK_API_KEY 未设置");
28
+ });
29
+
30
+ it("allows constructor parameters to override config defaults", () => {
31
+ expect(() => new ChatSession({ apiKey: "sk-test" })).not.toThrow();
32
+ });
33
+ });
@@ -16,11 +16,11 @@ describe("cardJsonToPlainText", () => {
16
16
  expect(text).toContain("/new");
17
17
  expect(text).toContain("/new cursor");
18
18
  expect(text).toContain("/new codex");
19
- expect(text).toContain("/restart");
20
- expect(text).toContain("/update");
21
- expect(text).toContain("/cd");
22
- expect(text).toContain("/abd");
23
- });
19
+ expect(text).toContain("/restart");
20
+ expect(text).toContain("/update");
21
+ expect(text).toContain("/cd");
22
+ expect(text).toContain("/abd");
23
+ });
24
24
 
25
25
  it("converts status cards from v1 card format", () => {
26
26
  const text = cardJsonToPlainText(buildStatusCard("status body", "green"));
@@ -1,60 +1,60 @@
1
- import { afterEach, describe, expect, it, vi } from "vitest";
2
-
3
- import { updateCardKitCard } from "../cardkit.ts";
4
-
5
- function abortError(): Error {
6
- const err = new Error("aborted");
7
- err.name = "AbortError";
8
- return err;
9
- }
10
-
11
- describe("CardKit request timeout", () => {
12
- afterEach(() => {
13
- vi.useRealTimers();
14
- vi.restoreAllMocks();
15
- vi.unstubAllGlobals();
16
- });
17
-
18
- it("aborts a hung card update so display.cardBusy can be released", async () => {
19
- vi.useFakeTimers();
20
- vi.spyOn(console, "error").mockImplementation(() => {});
21
-
22
- const fetchMock = vi.fn((_url: Parameters<typeof fetch>[0], init?: Parameters<typeof fetch>[1]) => (
23
- new Promise<Response>((_resolve, reject) => {
24
- init?.signal?.addEventListener("abort", () => reject(abortError()));
25
- })
26
- ));
27
- vi.stubGlobal("fetch", fetchMock);
28
-
29
- const update = expect(updateCardKitCard("token", "card-1", "{}", 7))
30
- .rejects.toThrow("updateCard cardId=card-1 seq=7 timeout after 15000ms");
31
- await vi.advanceTimersByTimeAsync(15_000);
32
-
33
- await update;
34
- expect(fetchMock).toHaveBeenCalledWith(
35
- expect.stringContaining("/cardkit/v1/cards/card-1"),
36
- expect.objectContaining({ signal: expect.any(AbortSignal) }),
37
- );
38
- });
39
-
40
- it("also aborts when response body reading hangs", async () => {
41
- vi.useFakeTimers();
42
- vi.spyOn(console, "error").mockImplementation(() => {});
43
-
44
- const fetchMock = vi.fn((_url: Parameters<typeof fetch>[0], init?: Parameters<typeof fetch>[1]) => (
45
- Promise.resolve({
46
- status: 200,
47
- text: () => new Promise<string>((_resolve, reject) => {
48
- init?.signal?.addEventListener("abort", () => reject(abortError()));
49
- }),
50
- } as Response)
51
- ));
52
- vi.stubGlobal("fetch", fetchMock);
53
-
54
- const update = expect(updateCardKitCard("token", "card-2", "{}", 8))
55
- .rejects.toThrow("updateCard cardId=card-2 seq=8 timeout after 15000ms");
56
- await vi.advanceTimersByTimeAsync(15_000);
57
-
58
- await update;
59
- });
60
- });
1
+ import { afterEach, describe, expect, it, vi } from "vitest";
2
+
3
+ import { updateCardKitCard } from "../cardkit.ts";
4
+
5
+ function abortError(): Error {
6
+ const err = new Error("aborted");
7
+ err.name = "AbortError";
8
+ return err;
9
+ }
10
+
11
+ describe("CardKit request timeout", () => {
12
+ afterEach(() => {
13
+ vi.useRealTimers();
14
+ vi.restoreAllMocks();
15
+ vi.unstubAllGlobals();
16
+ });
17
+
18
+ it("aborts a hung card update so display.cardBusy can be released", async () => {
19
+ vi.useFakeTimers();
20
+ vi.spyOn(console, "error").mockImplementation(() => {});
21
+
22
+ const fetchMock = vi.fn((_url: Parameters<typeof fetch>[0], init?: Parameters<typeof fetch>[1]) => (
23
+ new Promise<Response>((_resolve, reject) => {
24
+ init?.signal?.addEventListener("abort", () => reject(abortError()));
25
+ })
26
+ ));
27
+ vi.stubGlobal("fetch", fetchMock);
28
+
29
+ const update = expect(updateCardKitCard("token", "card-1", "{}", 7))
30
+ .rejects.toThrow("updateCard cardId=card-1 seq=7 timeout after 15000ms");
31
+ await vi.advanceTimersByTimeAsync(15_000);
32
+
33
+ await update;
34
+ expect(fetchMock).toHaveBeenCalledWith(
35
+ expect.stringContaining("/cardkit/v1/cards/card-1"),
36
+ expect.objectContaining({ signal: expect.any(AbortSignal) }),
37
+ );
38
+ });
39
+
40
+ it("also aborts when response body reading hangs", async () => {
41
+ vi.useFakeTimers();
42
+ vi.spyOn(console, "error").mockImplementation(() => {});
43
+
44
+ const fetchMock = vi.fn((_url: Parameters<typeof fetch>[0], init?: Parameters<typeof fetch>[1]) => (
45
+ Promise.resolve({
46
+ status: 200,
47
+ text: () => new Promise<string>((_resolve, reject) => {
48
+ init?.signal?.addEventListener("abort", () => reject(abortError()));
49
+ }),
50
+ } as Response)
51
+ ));
52
+ vi.stubGlobal("fetch", fetchMock);
53
+
54
+ const update = expect(updateCardKitCard("token", "card-2", "{}", 8))
55
+ .rejects.toThrow("updateCard cardId=card-2 seq=8 timeout after 15000ms");
56
+ await vi.advanceTimersByTimeAsync(15_000);
57
+
58
+ await update;
59
+ });
60
+ });