chatccc 0.2.231 → 0.2.232

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 (127) hide show
  1. package/.agents/skills/create-chatccc-feishu-app/SKILL.md +85 -85
  2. package/.claude/skills/create-chatccc-feishu-app/SKILL.md +85 -85
  3. package/.cursor/skills/create-chatccc-feishu-app/SKILL.md +85 -85
  4. package/README.md +90 -90
  5. package/agent-prompts/claude_specific.md +45 -45
  6. package/agent-prompts/codex_specific.md +2 -2
  7. package/agent-prompts/cursor_specific.md +13 -13
  8. package/bin/cccagent.mjs +17 -17
  9. package/im-skills/feishu-skill/receive-send-file.md +63 -63
  10. package/im-skills/feishu-skill/receive-send-image.md +24 -24
  11. package/im-skills/feishu-skill/skill.md +3 -3
  12. package/im-skills/wechat-file-skill/receive-send-file.md +38 -38
  13. package/im-skills/wechat-file-skill/send-file.mjs +83 -83
  14. package/im-skills/wechat-file-skill/skill.md +10 -10
  15. package/im-skills/wechat-image-skill/skill.md +10 -10
  16. package/im-skills/wechat-video-skill/receive-send-video.md +38 -38
  17. package/im-skills/wechat-video-skill/send-video.mjs +79 -79
  18. package/im-skills/wechat-video-skill/skill.md +10 -10
  19. package/package.json +1 -1
  20. package/scripts/postinstall-sharp-check.mjs +58 -58
  21. package/src/__tests__/agent-activity.test.ts +86 -86
  22. package/src/__tests__/agent-delegate-task-rpc.test.ts +165 -165
  23. package/src/__tests__/agent-reload-config-rpc.test.ts +99 -99
  24. package/src/__tests__/builtin-chat-session.test.ts +449 -393
  25. package/src/__tests__/builtin-cli-json.test.ts +39 -39
  26. package/src/__tests__/builtin-config.test.ts +26 -26
  27. package/src/__tests__/builtin-context.test.ts +319 -202
  28. package/src/__tests__/builtin-file-tools.test.ts +240 -240
  29. package/src/__tests__/builtin-permissions.test.ts +211 -211
  30. package/src/__tests__/builtin-session-search.test.ts +228 -0
  31. package/src/__tests__/builtin-session-select.test.ts +116 -116
  32. package/src/__tests__/builtin-sigint.test.ts +56 -56
  33. package/src/__tests__/builtin-skills.test.ts +284 -284
  34. package/src/__tests__/builtin-web-tools.test.ts +220 -220
  35. package/src/__tests__/card-action-routing.test.ts +18 -18
  36. package/src/__tests__/cardkit.test.ts +60 -60
  37. package/src/__tests__/ccc-adapter.test.ts +170 -170
  38. package/src/__tests__/chatgpt-subscription-rpc.test.ts +89 -89
  39. package/src/__tests__/chatgpt-subscription.test.ts +135 -135
  40. package/src/__tests__/chrome-devtools-guard.test.ts +165 -165
  41. package/src/__tests__/claude-adapter.test.ts +614 -614
  42. package/src/__tests__/claude-raw-stream-log.test.ts +87 -87
  43. package/src/__tests__/codex-adapter.test.ts +58 -58
  44. package/src/__tests__/codex-raw-stream-log.test.ts +170 -170
  45. package/src/__tests__/codex-reset-actions.test.ts +146 -146
  46. package/src/__tests__/cursor-adapter.test.ts +268 -268
  47. package/src/__tests__/feishu-api.test.ts +60 -60
  48. package/src/__tests__/feishu-avatar.test.ts +164 -164
  49. package/src/__tests__/feishu-message-ingress.test.ts +138 -138
  50. package/src/__tests__/feishu-platform.test.ts +22 -22
  51. package/src/__tests__/format-message.test.ts +47 -47
  52. package/src/__tests__/jsonl-stream.test.ts +79 -79
  53. package/src/__tests__/package-files.test.ts +24 -24
  54. package/src/__tests__/privacy.test.ts +198 -198
  55. package/src/__tests__/progress-reducer.test.ts +121 -121
  56. package/src/__tests__/raw-stream-log.test.ts +106 -106
  57. package/src/__tests__/response-stall.test.ts +49 -49
  58. package/src/__tests__/restart.test.ts +132 -132
  59. package/src/__tests__/session.test.ts +199 -199
  60. package/src/__tests__/shared-prefix.test.ts +36 -36
  61. package/src/__tests__/sim-platform.test.ts +16 -16
  62. package/src/__tests__/startup-lifecycle.test.ts +231 -231
  63. package/src/__tests__/stop-session.test.ts +34 -34
  64. package/src/__tests__/stream-state.test.ts +42 -42
  65. package/src/__tests__/terminal-error.test.ts +54 -54
  66. package/src/__tests__/terminal-renderer.test.ts +247 -247
  67. package/src/__tests__/update-command-guard.test.ts +144 -144
  68. package/src/__tests__/web-ui.test.ts +326 -326
  69. package/src/adapters/adapter-interface.ts +33 -33
  70. package/src/adapters/ccc-adapter.ts +141 -141
  71. package/src/adapters/claude-adapter.ts +620 -620
  72. package/src/adapters/claude-session-meta-store.ts +120 -120
  73. package/src/adapters/codex-adapter.ts +426 -426
  74. package/src/adapters/cursor-adapter.ts +681 -681
  75. package/src/adapters/jsonl-stream.ts +157 -157
  76. package/src/adapters/raw-stream-log.ts +124 -124
  77. package/src/adapters/resource-monitor.ts +140 -140
  78. package/src/agent-activity.ts +175 -175
  79. package/src/agent-delegate-task-rpc.ts +153 -153
  80. package/src/agent-delegate-task.ts +91 -91
  81. package/src/agent-reload-config-rpc.ts +34 -34
  82. package/src/agent-stop-stuck.ts +129 -129
  83. package/src/builtin/cli.ts +6 -0
  84. package/src/builtin/config.ts +84 -84
  85. package/src/builtin/context.ts +465 -391
  86. package/src/builtin/file-log.ts +38 -38
  87. package/src/builtin/file-tools.ts +34 -0
  88. package/src/builtin/index.ts +24 -23
  89. package/src/builtin/proc-tree-kill.ts +61 -61
  90. package/src/builtin/progress/cards-helpers.ts +76 -76
  91. package/src/builtin/progress/reducer.ts +113 -113
  92. package/src/builtin/progress/terminal-renderer.ts +294 -294
  93. package/src/builtin/progress/view.ts +77 -77
  94. package/src/builtin/raw-stream-log.ts +124 -124
  95. package/src/builtin/session-search.ts +363 -0
  96. package/src/builtin/session-select.ts +48 -48
  97. package/src/builtin/sigint.ts +50 -50
  98. package/src/builtin/skills.ts +205 -205
  99. package/src/builtin/web-tools.ts +313 -313
  100. package/src/card-action-routing.ts +14 -14
  101. package/src/chatgpt-subscription-rpc.ts +27 -27
  102. package/src/chatgpt-subscription.ts +299 -299
  103. package/src/chrome-devtools-guard.ts +318 -318
  104. package/src/codex-reset-actions.ts +184 -184
  105. package/src/feishu-api.ts +193 -193
  106. package/src/feishu-message-ingress.ts +195 -195
  107. package/src/feishu-platform.ts +20 -20
  108. package/src/format-message.ts +293 -293
  109. package/src/litellm-proxy.ts +374 -374
  110. package/src/orchestrator.ts +2494 -2494
  111. package/src/platform-adapter.ts +6 -6
  112. package/src/privacy.ts +118 -118
  113. package/src/progress/reducer.ts +113 -113
  114. package/src/progress/terminal-renderer.ts +294 -294
  115. package/src/progress/view.ts +77 -77
  116. package/src/response-stall.ts +28 -28
  117. package/src/runtime-reload.ts +34 -34
  118. package/src/session-chat-binding.ts +82 -82
  119. package/src/session-name.ts +8 -8
  120. package/src/session.ts +201 -201
  121. package/src/shared-prefix.ts +29 -29
  122. package/src/sim-platform.ts +20 -20
  123. package/src/startup-lifecycle.ts +250 -250
  124. package/src/stream-state.ts +21 -21
  125. package/src/terminal-error.ts +129 -129
  126. package/src/turn-cards.ts +117 -117
  127. package/src/update-command-guard.ts +165 -165
@@ -1,220 +1,220 @@
1
- import { describe, expect, it } from "vitest";
2
-
3
- import {
4
- decodeDdgHref,
5
- extractHtmlTitle,
6
- htmlToPlainText,
7
- parseDuckDuckGoHtml,
8
- webFetchForTool,
9
- webSearchForTool,
10
- type FetchLike,
11
- } from "../builtin/web-tools.ts";
12
-
13
- const DDG_HTML = `<!DOCTYPE html>
14
- <html>
15
- <head><title>Search results</title></head>
16
- <body>
17
- <div class="result results_links results_links_deep web-result">
18
- <h2 class="result__title">
19
- <a rel="nofollow" class="result__a" href="//duckduckgo.com/l/?uddg=https%3A%2F%2Fnodejs.org%2Fen%2Fdocs&rut=abc">Node.js <b>Docs</b></a>
20
- </h2>
21
- <a rel="nofollow" class="result__snippet" href="//duckduckgo.com/l/?uddg=...">Official documentation for Node.js &amp; npm.</a>
22
- </div>
23
- <div class="result">
24
- <a rel="nofollow" class="result__a" href="https://example.com/page">Example &quot;quoted&quot; page</a>
25
- <a rel="nofollow" class="result__snippet" href="https://example.com/page">A plain snippet without highlight.</a>
26
- </div>
27
- </body>
28
- </html>`;
29
-
30
- const WEB_FETCH_HTML = `<!DOCTYPE html>
31
- <html><head><title>Fetch &amp; Me</title></head>
32
- <body>
33
- <style>.x { color: red }</style>
34
- <script>alert("x")</script>
35
- <h1>Hello World</h1>
36
- <p>First paragraph with &amp; entity.</p>
37
- <p>Second paragraph<br>with a break.</p>
38
- <footer>bye</footer>
39
- </body></html>`;
40
-
41
- describe("parseDuckDuckGoHtml", () => {
42
- it("extracts title, decoded url, and snippet per result", () => {
43
- const results = parseDuckDuckGoHtml(DDG_HTML);
44
- expect(results).toHaveLength(2);
45
- expect(results[0]).toEqual({
46
- title: "Node.js Docs",
47
- url: "https://nodejs.org/en/docs",
48
- snippet: "Official documentation for Node.js & npm.",
49
- });
50
- expect(results[1]).toEqual({
51
- title: 'Example "quoted" page',
52
- url: "https://example.com/page",
53
- snippet: "A plain snippet without highlight.",
54
- });
55
- });
56
-
57
- it("returns empty array for pages without results", () => {
58
- expect(parseDuckDuckGoHtml("<html><body>no results here</body></html>")).toEqual([]);
59
- });
60
- });
61
-
62
- describe("decodeDdgHref", () => {
63
- it("decodes uddg redirect params and restores protocol", () => {
64
- expect(
65
- decodeDdgHref("//duckduckgo.com/l/?uddg=https%3A%2F%2Fnodejs.org%2Fen%2Fdocs&rut=abc"),
66
- ).toBe("https://nodejs.org/en/docs");
67
- });
68
-
69
- it("passes through plain urls", () => {
70
- expect(decodeDdgHref("https://example.com/a?b=1")).toBe("https://example.com/a?b=1");
71
- });
72
-
73
- it("returns input untouched when unparseable", () => {
74
- expect(decodeDdgHref("not a url")).toBe("not a url");
75
- });
76
- });
77
-
78
- describe("htmlToPlainText", () => {
79
- it("strips scripts/styles, keeps block boundaries, decodes entities, compresses whitespace", () => {
80
- const text = htmlToPlainText(WEB_FETCH_HTML);
81
- expect(text).not.toContain("alert");
82
- expect(text).not.toContain(".x {");
83
- expect(text).toContain("Hello World");
84
- expect(text).toContain("First paragraph with & entity.");
85
- expect(text).toContain("Second paragraph\nwith a break.");
86
- expect(text).toContain("bye");
87
- // 段落间允许单个空行,但不得出现连续空行
88
- expect(text).not.toContain("\n\n\n");
89
- });
90
- });
91
-
92
- describe("extractHtmlTitle", () => {
93
- it("extracts and decodes the title", () => {
94
- expect(extractHtmlTitle(WEB_FETCH_HTML)).toBe("Fetch & Me");
95
- });
96
-
97
- it("returns empty string when no title", () => {
98
- expect(extractHtmlTitle("<html><body>x</body></html>")).toBe("");
99
- });
100
- });
101
-
102
- describe("webSearchForTool", () => {
103
- function mockFetch(html: string, status = 200): FetchLike {
104
- return async () =>
105
- new Response(html, {
106
- status,
107
- headers: { "content-type": "text/html; charset=utf-8" },
108
- });
109
- }
110
-
111
- it("returns parsed results from the search page", async () => {
112
- const output = await webSearchForTool(
113
- { query: "nodejs docs" },
114
- { fetchImpl: mockFetch(DDG_HTML) },
115
- );
116
- expect(output.query).toBe("nodejs docs");
117
- expect(output.results).toHaveLength(2);
118
- expect(output.results[0].url).toBe("https://nodejs.org/en/docs");
119
- expect(output.results[0].snippet).toContain("Node.js");
120
- expect(typeof output.durationMs).toBe("number");
121
- });
122
-
123
- it("respects maxResults and caps at 10", async () => {
124
- const html = Array.from({ length: 12 }, (_, i) => {
125
- const title = `<a rel="nofollow" class="result__a" href="https://e.com/${i}">R${i}</a>`;
126
- const snippet = `<a rel="nofollow" class="result__snippet" href="https://e.com/${i}">s${i}</a>`;
127
- return `<div>${title}${snippet}</div>`;
128
- }).join("");
129
- const output = await webSearchForTool({ query: "many" }, { fetchImpl: mockFetch(html) });
130
- expect(output.results).toHaveLength(5);
131
- expect(output.truncated).toBe(true);
132
-
133
- const output2 = await webSearchForTool({ query: "many", maxResults: 20 }, { fetchImpl: mockFetch(html) });
134
- expect(output2.results).toHaveLength(10);
135
- });
136
-
137
- it("returns empty results (not an error) when the page has no matches", async () => {
138
- const output = await webSearchForTool(
139
- { query: "zzz-nothing" },
140
- { fetchImpl: mockFetch("<html><body>no results</body></html>") },
141
- );
142
- expect(output.results).toEqual([]);
143
- expect(output.truncated).toBe(false);
144
- });
145
-
146
- it("throws on empty query", async () => {
147
- await expect(webSearchForTool({ query: " " }, { fetchImpl: mockFetch("") })).rejects.toThrow(
148
- "query is required",
149
- );
150
- });
151
-
152
- it("throws on non-2xx response", async () => {
153
- await expect(
154
- webSearchForTool({ query: "x" }, { fetchImpl: mockFetch("blocked", 429) }),
155
- ).rejects.toThrow("HTTP 429");
156
- });
157
- });
158
-
159
- describe("webFetchForTool", () => {
160
- function mockFetch(html: string, status = 200, contentType = "text/html; charset=utf-8"): FetchLike {
161
- return async () =>
162
- new Response(html, {
163
- status,
164
- headers: { "content-type": contentType },
165
- });
166
- }
167
-
168
- it("fetches and converts html to plain text with title", async () => {
169
- const output = await webFetchForTool(
170
- { url: "https://example.com/page" },
171
- { fetchImpl: mockFetch(WEB_FETCH_HTML) },
172
- );
173
- expect(output.title).toBe("Fetch & Me");
174
- expect(output.text).toContain("Hello World");
175
- expect(output.text).toContain("Second paragraph\nwith a break.");
176
- expect(output.text).not.toContain("alert");
177
- expect(output.contentType).toContain("text/html");
178
- expect(output.chars).toBe(output.text.length);
179
- expect(output.truncated).toBe(false);
180
- });
181
-
182
- it("truncates text beyond maxChars", async () => {
183
- const output = await webFetchForTool(
184
- { url: "https://example.com/long", maxChars: 10 },
185
- { fetchImpl: mockFetch("<p>hello world and more</p>") },
186
- );
187
- expect(output.text).toBe("hello worl");
188
- expect(output.truncated).toBe(true);
189
- expect(output.chars).toBe(10);
190
- });
191
-
192
- it("truncates oversized response bodies", async () => {
193
- const big = `<!DOCTYPE html><html><body><p>${"a".repeat(600 * 1024)}</p></body></html>`;
194
- const output = await webFetchForTool(
195
- { url: "https://example.com/big" },
196
- { fetchImpl: mockFetch(big) },
197
- );
198
- expect(output.truncated).toBe(true);
199
- expect(output.text.length).toBeLessThan(600 * 1024);
200
- });
201
-
202
- it("rejects non-http(s) protocols", async () => {
203
- await expect(
204
- webFetchForTool({ url: "file:///etc/passwd" }, { fetchImpl: mockFetch("") }),
205
- ).rejects.toThrow("unsupported protocol");
206
- });
207
-
208
- it("rejects invalid urls", async () => {
209
- await expect(
210
- webFetchForTool({ url: "not a url" }, { fetchImpl: mockFetch("") }),
211
- ).rejects.toThrow("invalid URL");
212
- });
213
-
214
- it("throws on missing url and non-2xx", async () => {
215
- await expect(webFetchForTool({ url: " " })).rejects.toThrow("url is required");
216
- await expect(
217
- webFetchForTool({ url: "https://example.com/404" }, { fetchImpl: mockFetch("nf", 404) }),
218
- ).rejects.toThrow("HTTP 404");
219
- });
220
- });
1
+ import { describe, expect, it } from "vitest";
2
+
3
+ import {
4
+ decodeDdgHref,
5
+ extractHtmlTitle,
6
+ htmlToPlainText,
7
+ parseDuckDuckGoHtml,
8
+ webFetchForTool,
9
+ webSearchForTool,
10
+ type FetchLike,
11
+ } from "../builtin/web-tools.ts";
12
+
13
+ const DDG_HTML = `<!DOCTYPE html>
14
+ <html>
15
+ <head><title>Search results</title></head>
16
+ <body>
17
+ <div class="result results_links results_links_deep web-result">
18
+ <h2 class="result__title">
19
+ <a rel="nofollow" class="result__a" href="//duckduckgo.com/l/?uddg=https%3A%2F%2Fnodejs.org%2Fen%2Fdocs&rut=abc">Node.js <b>Docs</b></a>
20
+ </h2>
21
+ <a rel="nofollow" class="result__snippet" href="//duckduckgo.com/l/?uddg=...">Official documentation for Node.js &amp; npm.</a>
22
+ </div>
23
+ <div class="result">
24
+ <a rel="nofollow" class="result__a" href="https://example.com/page">Example &quot;quoted&quot; page</a>
25
+ <a rel="nofollow" class="result__snippet" href="https://example.com/page">A plain snippet without highlight.</a>
26
+ </div>
27
+ </body>
28
+ </html>`;
29
+
30
+ const WEB_FETCH_HTML = `<!DOCTYPE html>
31
+ <html><head><title>Fetch &amp; Me</title></head>
32
+ <body>
33
+ <style>.x { color: red }</style>
34
+ <script>alert("x")</script>
35
+ <h1>Hello World</h1>
36
+ <p>First paragraph with &amp; entity.</p>
37
+ <p>Second paragraph<br>with a break.</p>
38
+ <footer>bye</footer>
39
+ </body></html>`;
40
+
41
+ describe("parseDuckDuckGoHtml", () => {
42
+ it("extracts title, decoded url, and snippet per result", () => {
43
+ const results = parseDuckDuckGoHtml(DDG_HTML);
44
+ expect(results).toHaveLength(2);
45
+ expect(results[0]).toEqual({
46
+ title: "Node.js Docs",
47
+ url: "https://nodejs.org/en/docs",
48
+ snippet: "Official documentation for Node.js & npm.",
49
+ });
50
+ expect(results[1]).toEqual({
51
+ title: 'Example "quoted" page',
52
+ url: "https://example.com/page",
53
+ snippet: "A plain snippet without highlight.",
54
+ });
55
+ });
56
+
57
+ it("returns empty array for pages without results", () => {
58
+ expect(parseDuckDuckGoHtml("<html><body>no results here</body></html>")).toEqual([]);
59
+ });
60
+ });
61
+
62
+ describe("decodeDdgHref", () => {
63
+ it("decodes uddg redirect params and restores protocol", () => {
64
+ expect(
65
+ decodeDdgHref("//duckduckgo.com/l/?uddg=https%3A%2F%2Fnodejs.org%2Fen%2Fdocs&rut=abc"),
66
+ ).toBe("https://nodejs.org/en/docs");
67
+ });
68
+
69
+ it("passes through plain urls", () => {
70
+ expect(decodeDdgHref("https://example.com/a?b=1")).toBe("https://example.com/a?b=1");
71
+ });
72
+
73
+ it("returns input untouched when unparseable", () => {
74
+ expect(decodeDdgHref("not a url")).toBe("not a url");
75
+ });
76
+ });
77
+
78
+ describe("htmlToPlainText", () => {
79
+ it("strips scripts/styles, keeps block boundaries, decodes entities, compresses whitespace", () => {
80
+ const text = htmlToPlainText(WEB_FETCH_HTML);
81
+ expect(text).not.toContain("alert");
82
+ expect(text).not.toContain(".x {");
83
+ expect(text).toContain("Hello World");
84
+ expect(text).toContain("First paragraph with & entity.");
85
+ expect(text).toContain("Second paragraph\nwith a break.");
86
+ expect(text).toContain("bye");
87
+ // 段落间允许单个空行,但不得出现连续空行
88
+ expect(text).not.toContain("\n\n\n");
89
+ });
90
+ });
91
+
92
+ describe("extractHtmlTitle", () => {
93
+ it("extracts and decodes the title", () => {
94
+ expect(extractHtmlTitle(WEB_FETCH_HTML)).toBe("Fetch & Me");
95
+ });
96
+
97
+ it("returns empty string when no title", () => {
98
+ expect(extractHtmlTitle("<html><body>x</body></html>")).toBe("");
99
+ });
100
+ });
101
+
102
+ describe("webSearchForTool", () => {
103
+ function mockFetch(html: string, status = 200): FetchLike {
104
+ return async () =>
105
+ new Response(html, {
106
+ status,
107
+ headers: { "content-type": "text/html; charset=utf-8" },
108
+ });
109
+ }
110
+
111
+ it("returns parsed results from the search page", async () => {
112
+ const output = await webSearchForTool(
113
+ { query: "nodejs docs" },
114
+ { fetchImpl: mockFetch(DDG_HTML) },
115
+ );
116
+ expect(output.query).toBe("nodejs docs");
117
+ expect(output.results).toHaveLength(2);
118
+ expect(output.results[0].url).toBe("https://nodejs.org/en/docs");
119
+ expect(output.results[0].snippet).toContain("Node.js");
120
+ expect(typeof output.durationMs).toBe("number");
121
+ });
122
+
123
+ it("respects maxResults and caps at 10", async () => {
124
+ const html = Array.from({ length: 12 }, (_, i) => {
125
+ const title = `<a rel="nofollow" class="result__a" href="https://e.com/${i}">R${i}</a>`;
126
+ const snippet = `<a rel="nofollow" class="result__snippet" href="https://e.com/${i}">s${i}</a>`;
127
+ return `<div>${title}${snippet}</div>`;
128
+ }).join("");
129
+ const output = await webSearchForTool({ query: "many" }, { fetchImpl: mockFetch(html) });
130
+ expect(output.results).toHaveLength(5);
131
+ expect(output.truncated).toBe(true);
132
+
133
+ const output2 = await webSearchForTool({ query: "many", maxResults: 20 }, { fetchImpl: mockFetch(html) });
134
+ expect(output2.results).toHaveLength(10);
135
+ });
136
+
137
+ it("returns empty results (not an error) when the page has no matches", async () => {
138
+ const output = await webSearchForTool(
139
+ { query: "zzz-nothing" },
140
+ { fetchImpl: mockFetch("<html><body>no results</body></html>") },
141
+ );
142
+ expect(output.results).toEqual([]);
143
+ expect(output.truncated).toBe(false);
144
+ });
145
+
146
+ it("throws on empty query", async () => {
147
+ await expect(webSearchForTool({ query: " " }, { fetchImpl: mockFetch("") })).rejects.toThrow(
148
+ "query is required",
149
+ );
150
+ });
151
+
152
+ it("throws on non-2xx response", async () => {
153
+ await expect(
154
+ webSearchForTool({ query: "x" }, { fetchImpl: mockFetch("blocked", 429) }),
155
+ ).rejects.toThrow("HTTP 429");
156
+ });
157
+ });
158
+
159
+ describe("webFetchForTool", () => {
160
+ function mockFetch(html: string, status = 200, contentType = "text/html; charset=utf-8"): FetchLike {
161
+ return async () =>
162
+ new Response(html, {
163
+ status,
164
+ headers: { "content-type": contentType },
165
+ });
166
+ }
167
+
168
+ it("fetches and converts html to plain text with title", async () => {
169
+ const output = await webFetchForTool(
170
+ { url: "https://example.com/page" },
171
+ { fetchImpl: mockFetch(WEB_FETCH_HTML) },
172
+ );
173
+ expect(output.title).toBe("Fetch & Me");
174
+ expect(output.text).toContain("Hello World");
175
+ expect(output.text).toContain("Second paragraph\nwith a break.");
176
+ expect(output.text).not.toContain("alert");
177
+ expect(output.contentType).toContain("text/html");
178
+ expect(output.chars).toBe(output.text.length);
179
+ expect(output.truncated).toBe(false);
180
+ });
181
+
182
+ it("truncates text beyond maxChars", async () => {
183
+ const output = await webFetchForTool(
184
+ { url: "https://example.com/long", maxChars: 10 },
185
+ { fetchImpl: mockFetch("<p>hello world and more</p>") },
186
+ );
187
+ expect(output.text).toBe("hello worl");
188
+ expect(output.truncated).toBe(true);
189
+ expect(output.chars).toBe(10);
190
+ });
191
+
192
+ it("truncates oversized response bodies", async () => {
193
+ const big = `<!DOCTYPE html><html><body><p>${"a".repeat(600 * 1024)}</p></body></html>`;
194
+ const output = await webFetchForTool(
195
+ { url: "https://example.com/big" },
196
+ { fetchImpl: mockFetch(big) },
197
+ );
198
+ expect(output.truncated).toBe(true);
199
+ expect(output.text.length).toBeLessThan(600 * 1024);
200
+ });
201
+
202
+ it("rejects non-http(s) protocols", async () => {
203
+ await expect(
204
+ webFetchForTool({ url: "file:///etc/passwd" }, { fetchImpl: mockFetch("") }),
205
+ ).rejects.toThrow("unsupported protocol");
206
+ });
207
+
208
+ it("rejects invalid urls", async () => {
209
+ await expect(
210
+ webFetchForTool({ url: "not a url" }, { fetchImpl: mockFetch("") }),
211
+ ).rejects.toThrow("invalid URL");
212
+ });
213
+
214
+ it("throws on missing url and non-2xx", async () => {
215
+ await expect(webFetchForTool({ url: " " })).rejects.toThrow("url is required");
216
+ await expect(
217
+ webFetchForTool({ url: "https://example.com/404" }, { fetchImpl: mockFetch("nf", 404) }),
218
+ ).rejects.toThrow("HTTP 404");
219
+ });
220
+ });
@@ -1,18 +1,18 @@
1
- import { describe, expect, it } from "vitest";
2
-
3
- import { resolveFeishuCardActionChatType } from "../card-action-routing.ts";
4
-
5
- describe("resolveFeishuCardActionChatType", () => {
6
- it("keeps card commands in a persisted private chat on the p2p route", () => {
7
- expect(resolveFeishuCardActionChatType("private-chat", {
8
- "private-chat": { chatType: "p2p" },
9
- })).toBe("p2p");
10
- });
11
-
12
- it("defaults unknown and group chats to the group route", () => {
13
- expect(resolveFeishuCardActionChatType("group-chat", {
14
- "group-chat": { chatType: "group" },
15
- })).toBe("group");
16
- expect(resolveFeishuCardActionChatType("unknown-chat", {})).toBe("group");
17
- });
18
- });
1
+ import { describe, expect, it } from "vitest";
2
+
3
+ import { resolveFeishuCardActionChatType } from "../card-action-routing.ts";
4
+
5
+ describe("resolveFeishuCardActionChatType", () => {
6
+ it("keeps card commands in a persisted private chat on the p2p route", () => {
7
+ expect(resolveFeishuCardActionChatType("private-chat", {
8
+ "private-chat": { chatType: "p2p" },
9
+ })).toBe("p2p");
10
+ });
11
+
12
+ it("defaults unknown and group chats to the group route", () => {
13
+ expect(resolveFeishuCardActionChatType("group-chat", {
14
+ "group-chat": { chatType: "group" },
15
+ })).toBe("group");
16
+ expect(resolveFeishuCardActionChatType("unknown-chat", {})).toBe("group");
17
+ });
18
+ });
@@ -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
+ });