chatccc 0.2.3 → 0.2.5

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chatccc",
3
- "version": "0.2.3",
3
+ "version": "0.2.5",
4
4
  "description": "Feishu bot bridge for Claude Code",
5
5
  "license": "Apache-2.0",
6
6
  "type": "module",
@@ -23,7 +23,9 @@
23
23
  "demo:create-group": "tsx --env-file=.env src/index.ts",
24
24
  "demo:create-group:local": "tsx --env-file=.env src/index.ts --local",
25
25
  "demo:permission-check": "tsx --env-file=.env demo/permission_check.ts",
26
- "demo:claude-hi": "tsx --env-file=.env demo/claude_say_hi.ts"
26
+ "demo:claude-hi": "tsx --env-file=.env demo/claude_say_hi.ts",
27
+ "test": "vitest run",
28
+ "test:watch": "vitest"
27
29
  },
28
30
  "dependencies": {
29
31
  "@anthropic-ai/claude-agent-sdk": "0.2.133",
@@ -34,7 +36,8 @@
34
36
  "devDependencies": {
35
37
  "@types/node": "^20.0.0",
36
38
  "@types/ws": "^8.18.1",
37
- "typescript": "^5.0.0"
39
+ "typescript": "^5.0.0",
40
+ "vitest": "^4.1.5"
38
41
  },
39
42
  "engines": {
40
43
  "node": ">=20"
@@ -0,0 +1,152 @@
1
+ import { describe, it, expect } from "vitest";
2
+ import type {
3
+ UnifiedBlock,
4
+ UnifiedStreamMessage,
5
+ UnifiedThinkingBlock,
6
+ UnifiedTextBlock,
7
+ UnifiedToolUseBlock,
8
+ UnifiedToolResultBlock,
9
+ UnifiedRedactedThinkingBlock,
10
+ UnifiedSearchResultBlock,
11
+ UnifiedCompactBoundaryBlock,
12
+ CreateSessionResult,
13
+ SessionInfo,
14
+ ToolAdapter,
15
+ } from "../adapters/adapter-interface.ts";
16
+
17
+ // ---------------------------------------------------------------------------
18
+ // 编译期类型验证:确保 UnifiedBlock 联合类型覆盖了所有预期的 block 形状
19
+ // ---------------------------------------------------------------------------
20
+
21
+ describe("UnifiedBlock union type coverage", () => {
22
+ it("accepts thinking block", () => {
23
+ const b: UnifiedBlock = { type: "thinking", thinking: "Let me think..." };
24
+ expect(b.type).toBe("thinking");
25
+ });
26
+
27
+ it("accepts text block", () => {
28
+ const b: UnifiedBlock = { type: "text", text: "Hello" };
29
+ expect(b.type).toBe("text");
30
+ });
31
+
32
+ it("accepts tool_use block", () => {
33
+ const b: UnifiedBlock = { type: "tool_use", name: "Read", input: { file_path: "/tmp" } };
34
+ expect(b.type).toBe("tool_use");
35
+ });
36
+
37
+ it("accepts tool_result block with string content", () => {
38
+ const b: UnifiedBlock = { type: "tool_result", tool_use_id: "abc123", content: "result" };
39
+ expect(b.type).toBe("tool_result");
40
+ });
41
+
42
+ it("accepts tool_result block with error flag", () => {
43
+ const b: UnifiedBlock = { type: "tool_result", tool_use_id: "abc123", content: "error", is_error: true };
44
+ expect(b.is_error).toBe(true);
45
+ });
46
+
47
+ it("accepts redacted_thinking block", () => {
48
+ const b: UnifiedBlock = { type: "redacted_thinking" };
49
+ expect(b.type).toBe("redacted_thinking");
50
+ });
51
+
52
+ it("accepts search_result block", () => {
53
+ const b: UnifiedBlock = { type: "search_result", query: "what is TypeScript" };
54
+ expect(b.type).toBe("search_result");
55
+ });
56
+
57
+ it("accepts compact_boundary block", () => {
58
+ const b: UnifiedBlock = { type: "compact_boundary", trigger: "auto", pre_tokens: 10000, post_tokens: 5000 };
59
+ expect(b.type).toBe("compact_boundary");
60
+ });
61
+
62
+ it("accepts compact_boundary block without post_tokens", () => {
63
+ const b: UnifiedBlock = { type: "compact_boundary", trigger: "manual", pre_tokens: 20000 };
64
+ expect(b.type).toBe("compact_boundary");
65
+ });
66
+ });
67
+
68
+ // ---------------------------------------------------------------------------
69
+ // UnifiedStreamMessage
70
+ // ---------------------------------------------------------------------------
71
+
72
+ describe("UnifiedStreamMessage", () => {
73
+ it("accepts assistant message with blocks", () => {
74
+ const m: UnifiedStreamMessage = {
75
+ type: "assistant",
76
+ blocks: [
77
+ { type: "thinking", thinking: "..." },
78
+ { type: "text", text: "answer" },
79
+ ],
80
+ };
81
+ expect(m.type).toBe("assistant");
82
+ expect(m.blocks).toHaveLength(2);
83
+ });
84
+
85
+ it("accepts user message with tool_result blocks", () => {
86
+ const m: UnifiedStreamMessage = {
87
+ type: "user",
88
+ blocks: [{ type: "tool_result", tool_use_id: "abc", content: "ok" }],
89
+ };
90
+ expect(m.type).toBe("user");
91
+ });
92
+
93
+ it("accepts system message", () => {
94
+ const m: UnifiedStreamMessage = {
95
+ type: "system",
96
+ blocks: [{ type: "compact_boundary", trigger: "auto", pre_tokens: 100 }],
97
+ };
98
+ expect(m.type).toBe("system");
99
+ });
100
+ });
101
+
102
+ // ---------------------------------------------------------------------------
103
+ // CreateSessionResult
104
+ // ---------------------------------------------------------------------------
105
+
106
+ describe("CreateSessionResult", () => {
107
+ it("accepts valid result", () => {
108
+ const r: CreateSessionResult = { sessionId: "uuid-12345" };
109
+ expect(r.sessionId).toBe("uuid-12345");
110
+ });
111
+ });
112
+
113
+ // ---------------------------------------------------------------------------
114
+ // SessionInfo
115
+ // ---------------------------------------------------------------------------
116
+
117
+ describe("SessionInfo", () => {
118
+ it("accepts full info", () => {
119
+ const info: SessionInfo = {
120
+ sessionId: "abc",
121
+ cwd: "/home/user",
122
+ summary: "A session",
123
+ lastModified: 1234567890,
124
+ };
125
+ expect(info.sessionId).toBe("abc");
126
+ });
127
+
128
+ it("accepts minimal info (only sessionId)", () => {
129
+ const info: SessionInfo = { sessionId: "minimal" };
130
+ expect(info.cwd).toBeUndefined();
131
+ });
132
+ });
133
+
134
+ // ---------------------------------------------------------------------------
135
+ // ToolAdapter interface 结构断言
136
+ // ---------------------------------------------------------------------------
137
+
138
+ describe("ToolAdapter interface structure", () => {
139
+ it("has correct property names via mock object", () => {
140
+ // 如果 ToolAdapter 接口的字段名或函数签名变更,该对象字面量将无法编译
141
+ const mock: ToolAdapter = {
142
+ displayName: "Test",
143
+ sessionDescPrefix: "Test Session:",
144
+ createSession: async () => ({ sessionId: "s" }),
145
+ prompt: async function* () {},
146
+ getSessionInfo: async () => undefined,
147
+ closeSession: async () => {},
148
+ };
149
+ expect(mock.displayName).toBe("Test");
150
+ expect(mock.sessionDescPrefix).toBe("Test Session:");
151
+ });
152
+ });
@@ -0,0 +1,297 @@
1
+ import { describe, it, expect } from "vitest";
2
+ import {
3
+ buildProgressCard,
4
+ buildHelpCard,
5
+ buildCdContent,
6
+ buildSessionsCard,
7
+ buildStatusCard,
8
+ buildButtons,
9
+ truncateContent,
10
+ getToolEmoji,
11
+ } from "../cards.ts";
12
+
13
+ // ---------------------------------------------------------------------------
14
+ // truncateContent
15
+ // ---------------------------------------------------------------------------
16
+
17
+ describe("truncateContent", () => {
18
+ it("returns original text when under limits", () => {
19
+ expect(truncateContent("hello")).toBe("hello");
20
+ });
21
+
22
+ it("truncates lines when exceeding maxLines (default 20)", () => {
23
+ const lines = Array.from({ length: 25 }, (_, i) => `line ${i + 1}`);
24
+ const text = lines.join("\n");
25
+ const result = truncateContent(text);
26
+ const resultLines = result.split("\n");
27
+ expect(resultLines.length).toBe(21); // 1 first + 1 "..." + 19 last
28
+ expect(resultLines[0]).toBe("line 1");
29
+ expect(resultLines[1]).toBe("...");
30
+ expect(resultLines[2]).toBe("line 7");
31
+ expect(resultLines[20]).toBe("line 25");
32
+ });
33
+
34
+ it("truncates chars when exceeding maxChars", () => {
35
+ const long = "x".repeat(9000);
36
+ const result = truncateContent(long, 100, 500);
37
+ expect(result.length).toBeLessThanOrEqual(503); // "..." + 500 chars
38
+ expect(result.startsWith("...")).toBe(true);
39
+ });
40
+
41
+ it("returns empty string for empty input", () => {
42
+ expect(truncateContent("")).toBe("");
43
+ });
44
+
45
+ it("respects custom maxLines and maxChars", () => {
46
+ const lines = Array.from({ length: 10 }, (_, i) => `line ${i + 1}`);
47
+ const text = lines.join("\n");
48
+ const result = truncateContent(text, 5, 1000);
49
+ const resultLines = result.split("\n");
50
+ expect(resultLines[0]).toBe("line 1");
51
+ expect(resultLines[1]).toBe("...");
52
+ expect(resultLines[resultLines.length - 1]).toBe("line 10");
53
+ expect(resultLines.length).toBe(6); // 1 first + "..." + 4 last
54
+ });
55
+ });
56
+
57
+ // ---------------------------------------------------------------------------
58
+ // getToolEmoji
59
+ // ---------------------------------------------------------------------------
60
+
61
+ describe("getToolEmoji", () => {
62
+ it("returns correct emoji for each known tool name", () => {
63
+ expect(getToolEmoji("Read")).toBe("\u{1F4D6}"); // 📖
64
+ expect(getToolEmoji("Write")).toBe("\u{270D}\u{FE0F}"); // ✍️
65
+ expect(getToolEmoji("Edit")).toBe("\u{270F}\u{FE0F}"); // ✏️
66
+ expect(getToolEmoji("Grep")).toBe("\u{1F50E}"); // 🔎
67
+ expect(getToolEmoji("Glob")).toBe("\u{1F4C2}"); // 📂
68
+ expect(getToolEmoji("Bash")).toBe("\u{1F5A5}\u{FE0F}"); // 🖥️
69
+ expect(getToolEmoji("WebSearch")).toBe("\u{1F310}"); // 🌐
70
+ expect(getToolEmoji("WebFetch")).toBe("\u{1F4E5}"); // 📥
71
+ expect(getToolEmoji("TodoWrite")).toBe("\u{2705}"); // ✅
72
+ expect(getToolEmoji("Agent")).toBe("\u{1F916}"); // 🤖
73
+ expect(getToolEmoji("NotebookEdit")).toBe("\u{1F4D3}"); // 📓
74
+ expect(getToolEmoji("AskUserQuestion")).toBe("\u{2753}");// ❓
75
+ });
76
+
77
+ it("returns wrench for unknown tool names", () => {
78
+ expect(getToolEmoji("UnknownTool")).toBe("\u{1F527}");
79
+ expect(getToolEmoji("cat")).toBe("\u{1F527}");
80
+ expect(getToolEmoji("")).toBe("\u{1F527}");
81
+ });
82
+ });
83
+
84
+ // ---------------------------------------------------------------------------
85
+ // buildProgressCard
86
+ // ---------------------------------------------------------------------------
87
+
88
+ describe("buildProgressCard", () => {
89
+ it("returns valid JSON with correct schema", () => {
90
+ const card = buildProgressCard("test content");
91
+ const parsed = JSON.parse(card);
92
+ expect(parsed.schema).toBe("2.0");
93
+ expect(parsed.config.update_multi).toBe(true);
94
+ expect(parsed.config.streaming_mode).toBe(false);
95
+ });
96
+
97
+ it("uses default header title '生成中...'", () => {
98
+ const card = buildProgressCard("hello");
99
+ const parsed = JSON.parse(card);
100
+ expect(parsed.header.title.content).toBe("生成中...");
101
+ expect(parsed.header.template).toBe("blue");
102
+ });
103
+
104
+ it("includes stop button by default", () => {
105
+ const card = buildProgressCard("hello");
106
+ const parsed = JSON.parse(card);
107
+ const buttons = parsed.body.elements.filter((e: any) => e.tag === "button");
108
+ expect(buttons).toHaveLength(2);
109
+ expect(buttons[0].text.content).toBe("查看状态(/status)");
110
+ expect(buttons[1].text.content).toBe("停止生成(/stop)");
111
+ });
112
+
113
+ it("hides stop button when showStop is false", () => {
114
+ const card = buildProgressCard("hello", { showStop: false });
115
+ const parsed = JSON.parse(card);
116
+ const buttons = parsed.body.elements.filter((e: any) => e.tag === "button");
117
+ expect(buttons).toHaveLength(0);
118
+ });
119
+
120
+ it("uses custom header title and template", () => {
121
+ const card = buildProgressCard("hello", { headerTitle: "已完成", headerTemplate: "green" });
122
+ const parsed = JSON.parse(card);
123
+ expect(parsed.header.title.content).toBe("已完成");
124
+ expect(parsed.header.template).toBe("green");
125
+ });
126
+
127
+ it("includes markdown element with truncated content", () => {
128
+ const card = buildProgressCard("markdown text");
129
+ const parsed = JSON.parse(card);
130
+ const md = parsed.body.elements.find((e: any) => e.tag === "markdown");
131
+ expect(md).toBeDefined();
132
+ expect(md.element_id).toBe("main_content");
133
+ });
134
+ });
135
+
136
+ // ---------------------------------------------------------------------------
137
+ // buildHelpCard
138
+ // ---------------------------------------------------------------------------
139
+
140
+ describe("buildHelpCard", () => {
141
+ it("returns valid JSON with user text", () => {
142
+ const card = buildHelpCard("你好");
143
+ const parsed = JSON.parse(card);
144
+ expect(parsed.header.title.content).toBe("ChatCCC");
145
+ expect(parsed.elements[0].text.content).toContain("你好");
146
+ });
147
+
148
+ it("includes action buttons", () => {
149
+ const card = buildHelpCard("test");
150
+ const parsed = JSON.parse(card);
151
+ const action = parsed.elements[1];
152
+ expect(action.tag).toBe("action");
153
+ expect(action.actions).toHaveLength(3);
154
+ });
155
+ });
156
+
157
+ // ---------------------------------------------------------------------------
158
+ // buildCdContent
159
+ // ---------------------------------------------------------------------------
160
+
161
+ describe("buildCdContent", () => {
162
+ const entries = [
163
+ { name: "src", isDir: true },
164
+ { name: "README.md", isDir: false },
165
+ { name: "package.json", isDir: false },
166
+ ];
167
+
168
+ it("returns markdown with path and listing", () => {
169
+ const content = buildCdContent("/home/user/project", entries, false);
170
+ expect(content).toContain("/home/user/project");
171
+ expect(content).toContain("📁 src/");
172
+ expect(content).toContain("📄 README.md");
173
+ expect(content).toContain("📄 package.json");
174
+ });
175
+
176
+ it("shows 已切换 when isUpdate is true", () => {
177
+ const content = buildCdContent("/home/user/project", entries, true);
178
+ expect(content).toContain("(已切换)");
179
+ });
180
+
181
+ it("shows currentCwd when provided", () => {
182
+ const content = buildCdContent("/new/path", entries, false, "/old/path");
183
+ expect(content).toContain("当前会话工作路径");
184
+ expect(content).toContain("/old/path");
185
+ });
186
+
187
+ it("omits currentCwd line when not provided", () => {
188
+ const content = buildCdContent("/new/path", entries, false);
189
+ expect(content).not.toContain("当前会话工作路径");
190
+ });
191
+
192
+ it("caps listing at maxFiles", () => {
193
+ const many = Array.from({ length: 150 }, (_, i) => ({
194
+ name: `file${i}.txt`,
195
+ isDir: false,
196
+ }));
197
+ const content = buildCdContent("/path", many, false);
198
+ expect(content).toContain("仅显示前 100 个");
199
+ });
200
+ });
201
+
202
+ // ---------------------------------------------------------------------------
203
+ // buildSessionsCard
204
+ // ---------------------------------------------------------------------------
205
+
206
+ describe("buildSessionsCard", () => {
207
+ it("returns valid JSON for empty sessions", () => {
208
+ const card = buildSessionsCard([]);
209
+ const parsed = JSON.parse(card);
210
+ expect(parsed.elements[0].text.content).toContain("没有会话记录");
211
+ });
212
+
213
+ it("returns valid JSON with session listing", () => {
214
+ const card = buildSessionsCard([
215
+ { sessionId: "abc123", active: true, turnCount: 5, elapsedSeconds: 120, model: "Claude Opus 4.7" },
216
+ ]);
217
+ const parsed = JSON.parse(card);
218
+ expect(parsed.elements[0].text.content).toContain("共 **1** 个会话");
219
+ expect(parsed.elements[0].text.content).toContain("abc123");
220
+ expect(parsed.elements[0].text.content).toContain("🟢 活跃");
221
+ });
222
+
223
+ it("shows idle status for inactive sessions", () => {
224
+ const card = buildSessionsCard([
225
+ { sessionId: "xyz", active: false, turnCount: 0, elapsedSeconds: null, model: "Claude Sonnet 4.6" },
226
+ ]);
227
+ const parsed = JSON.parse(card);
228
+ expect(parsed.elements[0].text.content).toContain("⚪ 空闲");
229
+ });
230
+
231
+ it("shows elapsed time for active sessions", () => {
232
+ const card = buildSessionsCard([
233
+ { sessionId: "active123", active: true, turnCount: 3, elapsedSeconds: 95, model: "Claude Opus 4.7" },
234
+ ]);
235
+ const parsed = JSON.parse(card);
236
+ expect(parsed.elements[0].text.content).toContain("1分35秒");
237
+ });
238
+
239
+ it("includes close button", () => {
240
+ const card = buildSessionsCard([]);
241
+ const parsed = JSON.parse(card);
242
+ const action = parsed.elements[2];
243
+ expect(action.actions[0].text.content).toBe("收起");
244
+ });
245
+ });
246
+
247
+ // ---------------------------------------------------------------------------
248
+ // buildStatusCard
249
+ // ---------------------------------------------------------------------------
250
+
251
+ describe("buildStatusCard", () => {
252
+ it("returns valid JSON with status text", () => {
253
+ const card = buildStatusCard("一切正常");
254
+ const parsed = JSON.parse(card);
255
+ expect(parsed.header.title.content).toBe("会话状态");
256
+ expect(parsed.elements[0].text.content).toBe("一切正常");
257
+ });
258
+
259
+ it("uses custom template color", () => {
260
+ const card = buildStatusCard("警告", "red");
261
+ const parsed = JSON.parse(card);
262
+ expect(parsed.header.template).toBe("red");
263
+ });
264
+
265
+ it("includes close button", () => {
266
+ const card = buildStatusCard("test");
267
+ const parsed = JSON.parse(card);
268
+ const action = parsed.elements[2];
269
+ expect(action.actions[0].text.content).toBe("收起");
270
+ });
271
+ });
272
+
273
+ // ---------------------------------------------------------------------------
274
+ // buildButtons
275
+ // ---------------------------------------------------------------------------
276
+
277
+ describe("buildButtons", () => {
278
+ it("returns action with buttons", () => {
279
+ const result = buildButtons([
280
+ { text: "确认", value: "ok", type: "primary" },
281
+ ]);
282
+ const obj = result as any;
283
+ expect(obj.tag).toBe("action");
284
+ expect(obj.actions).toHaveLength(1);
285
+ expect(obj.actions[0].text.content).toBe("确认");
286
+ expect(obj.actions[0].type).toBe("primary");
287
+ expect(obj.actions[0].value).toBe("ok");
288
+ });
289
+
290
+ it("defaults button type to primary", () => {
291
+ const result = buildButtons([
292
+ { text: "取消", value: "cancel" },
293
+ ]);
294
+ const obj = result as any;
295
+ expect(obj.actions[0].type).toBe("primary");
296
+ });
297
+ });