chatccc 0.2.6 → 0.2.7
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/README.md +50 -11
- package/package.json +1 -1
- package/src/__tests__/adapter-interface.test.ts +151 -151
- package/src/__tests__/cards.test.ts +417 -413
- package/src/__tests__/claude-adapter.test.ts +528 -528
- package/src/__tests__/config.test.ts +123 -0
- package/src/__tests__/cursor-adapter.test.ts +662 -249
- package/src/__tests__/cursor-session-meta-store.test.ts +212 -0
- package/src/__tests__/fixtures/cursor_partial_only.jsonl +5 -0
- package/src/__tests__/fixtures/cursor_partial_with_final.jsonl +13 -0
- package/src/__tests__/fixtures/cursor_with_tool_call.jsonl +12 -0
- package/src/__tests__/git-command.test.ts +288 -0
- package/src/__tests__/session.test.ts +475 -296
- package/src/adapters/adapter-interface.ts +151 -126
- package/src/adapters/claude-adapter.ts +257 -257
- package/src/adapters/cursor-adapter.ts +401 -228
- package/src/adapters/cursor-session-meta-store.ts +154 -0
- package/src/cards.ts +33 -21
- package/src/config.ts +92 -2
- package/src/feishu-api.ts +1 -1
- package/src/git-command.ts +202 -0
- package/src/index.ts +58 -4
- package/src/session.ts +620 -513
|
@@ -1,414 +1,418 @@
|
|
|
1
|
-
import { describe, it, expect } from "vitest";
|
|
2
|
-
import {
|
|
3
|
-
buildProgressCard,
|
|
4
|
-
buildHelpCard,
|
|
5
|
-
buildCdContent,
|
|
6
|
-
buildCdCard,
|
|
7
|
-
buildSessionsCard,
|
|
8
|
-
buildStatusCard,
|
|
9
|
-
buildButtons,
|
|
10
|
-
truncateContent,
|
|
11
|
-
getToolEmoji,
|
|
12
|
-
} from "../cards.ts";
|
|
13
|
-
|
|
14
|
-
// ---------------------------------------------------------------------------
|
|
15
|
-
// truncateContent
|
|
16
|
-
// ---------------------------------------------------------------------------
|
|
17
|
-
|
|
18
|
-
describe("truncateContent", () => {
|
|
19
|
-
it("returns original text when under limits", () => {
|
|
20
|
-
expect(truncateContent("hello")).toBe("hello");
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
it("truncates lines when exceeding maxLines (default 20)", () => {
|
|
24
|
-
const lines = Array.from({ length: 25 }, (_, i) => `line ${i + 1}`);
|
|
25
|
-
const text = lines.join("\n");
|
|
26
|
-
const result = truncateContent(text);
|
|
27
|
-
const resultLines = result.split("\n");
|
|
28
|
-
expect(resultLines.length).toBe(21); // 1 first + 1 "..." + 19 last
|
|
29
|
-
expect(resultLines[0]).toBe("line 1");
|
|
30
|
-
expect(resultLines[1]).toBe("...");
|
|
31
|
-
expect(resultLines[2]).toBe("line 7");
|
|
32
|
-
expect(resultLines[20]).toBe("line 25");
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
it("truncates chars when exceeding maxChars", () => {
|
|
36
|
-
const long = "x".repeat(9000);
|
|
37
|
-
const result = truncateContent(long, 100, 500);
|
|
38
|
-
expect(result.length).toBeLessThanOrEqual(503); // "..." + 500 chars
|
|
39
|
-
expect(result.startsWith("...")).toBe(true);
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
it("returns empty string for empty input", () => {
|
|
43
|
-
expect(truncateContent("")).toBe("");
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
it("respects custom maxLines and maxChars", () => {
|
|
47
|
-
const lines = Array.from({ length: 10 }, (_, i) => `line ${i + 1}`);
|
|
48
|
-
const text = lines.join("\n");
|
|
49
|
-
const result = truncateContent(text, 5, 1000);
|
|
50
|
-
const resultLines = result.split("\n");
|
|
51
|
-
expect(resultLines[0]).toBe("line 1");
|
|
52
|
-
expect(resultLines[1]).toBe("...");
|
|
53
|
-
expect(resultLines[resultLines.length - 1]).toBe("line 10");
|
|
54
|
-
expect(resultLines.length).toBe(6); // 1 first + "..." + 4 last
|
|
55
|
-
});
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
// ---------------------------------------------------------------------------
|
|
59
|
-
// getToolEmoji
|
|
60
|
-
// ---------------------------------------------------------------------------
|
|
61
|
-
|
|
62
|
-
describe("getToolEmoji", () => {
|
|
63
|
-
it("returns correct emoji for each known tool name", () => {
|
|
64
|
-
expect(getToolEmoji("Read")).toBe("\u{1F4D6}"); // 📖
|
|
65
|
-
expect(getToolEmoji("Write")).toBe("\u{270D}\u{FE0F}"); // ✍️
|
|
66
|
-
expect(getToolEmoji("Edit")).toBe("\u{270F}\u{FE0F}"); // ✏️
|
|
67
|
-
expect(getToolEmoji("Grep")).toBe("\u{1F50E}"); // 🔎
|
|
68
|
-
expect(getToolEmoji("Glob")).toBe("\u{1F4C2}"); // 📂
|
|
69
|
-
expect(getToolEmoji("Bash")).toBe("\u{1F5A5}\u{FE0F}"); // 🖥️
|
|
70
|
-
expect(getToolEmoji("WebSearch")).toBe("\u{1F310}"); // 🌐
|
|
71
|
-
expect(getToolEmoji("WebFetch")).toBe("\u{1F4E5}"); // 📥
|
|
72
|
-
expect(getToolEmoji("TodoWrite")).toBe("\u{2705}"); // ✅
|
|
73
|
-
expect(getToolEmoji("Agent")).toBe("\u{1F916}"); // 🤖
|
|
74
|
-
expect(getToolEmoji("NotebookEdit")).toBe("\u{1F4D3}"); // 📓
|
|
75
|
-
expect(getToolEmoji("AskUserQuestion")).toBe("\u{2753}");// ❓
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
it("returns wrench for unknown tool names", () => {
|
|
79
|
-
expect(getToolEmoji("UnknownTool")).toBe("\u{1F527}");
|
|
80
|
-
expect(getToolEmoji("cat")).toBe("\u{1F527}");
|
|
81
|
-
expect(getToolEmoji("")).toBe("\u{1F527}");
|
|
82
|
-
});
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
// ---------------------------------------------------------------------------
|
|
86
|
-
// buildProgressCard
|
|
87
|
-
// ---------------------------------------------------------------------------
|
|
88
|
-
|
|
89
|
-
describe("buildProgressCard", () => {
|
|
90
|
-
it("returns valid JSON with correct schema", () => {
|
|
91
|
-
const card = buildProgressCard("test content");
|
|
92
|
-
const parsed = JSON.parse(card);
|
|
93
|
-
expect(parsed.schema).toBe("2.0");
|
|
94
|
-
expect(parsed.config.update_multi).toBe(true);
|
|
95
|
-
expect(parsed.config.streaming_mode).toBe(false);
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
it("uses default header title '生成中...'", () => {
|
|
99
|
-
const card = buildProgressCard("hello");
|
|
100
|
-
const parsed = JSON.parse(card);
|
|
101
|
-
expect(parsed.header.title.content).toBe("生成中...");
|
|
102
|
-
expect(parsed.header.template).toBe("blue");
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
it("includes stop button by default", () => {
|
|
106
|
-
const card = buildProgressCard("hello");
|
|
107
|
-
const parsed = JSON.parse(card);
|
|
108
|
-
const buttons = parsed.body.elements.filter((e: any) => e.tag === "button");
|
|
109
|
-
expect(buttons).toHaveLength(2);
|
|
110
|
-
expect(buttons[0].text.content).toBe("查看状态(/status)");
|
|
111
|
-
expect(buttons[1].text.content).toBe("停止生成(/stop)");
|
|
112
|
-
});
|
|
113
|
-
|
|
114
|
-
it("hides stop button when showStop is false", () => {
|
|
115
|
-
const card = buildProgressCard("hello", { showStop: false });
|
|
116
|
-
const parsed = JSON.parse(card);
|
|
117
|
-
const buttons = parsed.body.elements.filter((e: any) => e.tag === "button");
|
|
118
|
-
expect(buttons).toHaveLength(0);
|
|
119
|
-
});
|
|
120
|
-
|
|
121
|
-
it("uses custom header title and template", () => {
|
|
122
|
-
const card = buildProgressCard("hello", { headerTitle: "已完成", headerTemplate: "green" });
|
|
123
|
-
const parsed = JSON.parse(card);
|
|
124
|
-
expect(parsed.header.title.content).toBe("已完成");
|
|
125
|
-
expect(parsed.header.template).toBe("green");
|
|
126
|
-
});
|
|
127
|
-
|
|
128
|
-
it("includes markdown element with truncated content", () => {
|
|
129
|
-
const card = buildProgressCard("markdown text");
|
|
130
|
-
const parsed = JSON.parse(card);
|
|
131
|
-
const md = parsed.body.elements.find((e: any) => e.tag === "markdown");
|
|
132
|
-
expect(md).toBeDefined();
|
|
133
|
-
expect(md.element_id).toBe("main_content");
|
|
134
|
-
});
|
|
135
|
-
});
|
|
136
|
-
|
|
137
|
-
// ---------------------------------------------------------------------------
|
|
138
|
-
// buildHelpCard
|
|
139
|
-
// ---------------------------------------------------------------------------
|
|
140
|
-
|
|
141
|
-
describe("buildHelpCard", () => {
|
|
142
|
-
it("returns valid JSON with user text", () => {
|
|
143
|
-
const card = buildHelpCard("你好");
|
|
144
|
-
const parsed = JSON.parse(card);
|
|
145
|
-
expect(parsed.header.title.content).toBe("ChatCCC");
|
|
146
|
-
expect(parsed.elements[0].text.content).toContain("你好");
|
|
147
|
-
});
|
|
148
|
-
|
|
149
|
-
it("includes action buttons", () => {
|
|
150
|
-
const card = buildHelpCard("test");
|
|
151
|
-
const parsed = JSON.parse(card);
|
|
152
|
-
const action = parsed.elements[2];
|
|
153
|
-
expect(action.tag).toBe("action");
|
|
154
|
-
expect(action.actions).toHaveLength(4);
|
|
155
|
-
});
|
|
156
|
-
});
|
|
157
|
-
|
|
158
|
-
// ---------------------------------------------------------------------------
|
|
159
|
-
// buildCdContent
|
|
160
|
-
// ---------------------------------------------------------------------------
|
|
161
|
-
|
|
162
|
-
describe("buildCdContent", () => {
|
|
163
|
-
const entries = [
|
|
164
|
-
{ name: "src", isDir: true },
|
|
165
|
-
{ name: "README.md", isDir: false },
|
|
166
|
-
{ name: "package.json", isDir: false },
|
|
167
|
-
];
|
|
168
|
-
|
|
169
|
-
it("returns markdown with path and listing", () => {
|
|
170
|
-
const content = buildCdContent("/home/user/project", entries, false);
|
|
171
|
-
expect(content).toContain("/home/user/project");
|
|
172
|
-
expect(content).toContain("📁 src/");
|
|
173
|
-
expect(content).toContain("📄 README.md");
|
|
174
|
-
expect(content).toContain("📄 package.json");
|
|
175
|
-
});
|
|
176
|
-
|
|
177
|
-
it("shows 已切换 when isUpdate is true", () => {
|
|
178
|
-
const content = buildCdContent("/home/user/project", entries, true);
|
|
179
|
-
expect(content).toContain("(已切换)");
|
|
180
|
-
});
|
|
181
|
-
|
|
182
|
-
it("shows currentCwd when provided", () => {
|
|
183
|
-
const content = buildCdContent("/new/path", entries, false, "/old/path");
|
|
184
|
-
expect(content).toContain("当前会话工作路径");
|
|
185
|
-
expect(content).toContain("/old/path");
|
|
186
|
-
});
|
|
187
|
-
|
|
188
|
-
it("omits currentCwd line when not provided", () => {
|
|
189
|
-
const content = buildCdContent("/new/path", entries, false);
|
|
190
|
-
expect(content).not.toContain("当前会话工作路径");
|
|
191
|
-
});
|
|
192
|
-
|
|
193
|
-
it("caps listing at maxFiles", () => {
|
|
194
|
-
const many = Array.from({ length: 150 }, (_, i) => ({
|
|
195
|
-
name: `file${i}.txt`,
|
|
196
|
-
isDir: false,
|
|
197
|
-
}));
|
|
198
|
-
const content = buildCdContent("/path", many, false);
|
|
199
|
-
expect(content).toContain("仅显示前 100 个");
|
|
200
|
-
});
|
|
201
|
-
});
|
|
202
|
-
|
|
203
|
-
// ---------------------------------------------------------------------------
|
|
204
|
-
// buildCdCard
|
|
205
|
-
// ---------------------------------------------------------------------------
|
|
206
|
-
|
|
207
|
-
describe("buildCdCard", () => {
|
|
208
|
-
const entries = [
|
|
209
|
-
{ name: "src", isDir: true },
|
|
210
|
-
{ name: "README.md", isDir: false },
|
|
211
|
-
];
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
const
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
expect(
|
|
228
|
-
expect(
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
const
|
|
234
|
-
const
|
|
235
|
-
const
|
|
236
|
-
expect(
|
|
237
|
-
expect(
|
|
238
|
-
});
|
|
239
|
-
|
|
240
|
-
it("
|
|
241
|
-
const card = buildCdCard("/default", entries, []);
|
|
242
|
-
const parsed = JSON.parse(card);
|
|
243
|
-
const
|
|
244
|
-
|
|
245
|
-
expect(
|
|
246
|
-
});
|
|
247
|
-
|
|
248
|
-
it("
|
|
249
|
-
const
|
|
250
|
-
const
|
|
251
|
-
const
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
const
|
|
257
|
-
|
|
258
|
-
const
|
|
259
|
-
|
|
260
|
-
);
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
expect(
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
const
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
const
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
const
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
expect(obj.
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import {
|
|
3
|
+
buildProgressCard,
|
|
4
|
+
buildHelpCard,
|
|
5
|
+
buildCdContent,
|
|
6
|
+
buildCdCard,
|
|
7
|
+
buildSessionsCard,
|
|
8
|
+
buildStatusCard,
|
|
9
|
+
buildButtons,
|
|
10
|
+
truncateContent,
|
|
11
|
+
getToolEmoji,
|
|
12
|
+
} from "../cards.ts";
|
|
13
|
+
|
|
14
|
+
// ---------------------------------------------------------------------------
|
|
15
|
+
// truncateContent
|
|
16
|
+
// ---------------------------------------------------------------------------
|
|
17
|
+
|
|
18
|
+
describe("truncateContent", () => {
|
|
19
|
+
it("returns original text when under limits", () => {
|
|
20
|
+
expect(truncateContent("hello")).toBe("hello");
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it("truncates lines when exceeding maxLines (default 20)", () => {
|
|
24
|
+
const lines = Array.from({ length: 25 }, (_, i) => `line ${i + 1}`);
|
|
25
|
+
const text = lines.join("\n");
|
|
26
|
+
const result = truncateContent(text);
|
|
27
|
+
const resultLines = result.split("\n");
|
|
28
|
+
expect(resultLines.length).toBe(21); // 1 first + 1 "..." + 19 last
|
|
29
|
+
expect(resultLines[0]).toBe("line 1");
|
|
30
|
+
expect(resultLines[1]).toBe("...");
|
|
31
|
+
expect(resultLines[2]).toBe("line 7");
|
|
32
|
+
expect(resultLines[20]).toBe("line 25");
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it("truncates chars when exceeding maxChars", () => {
|
|
36
|
+
const long = "x".repeat(9000);
|
|
37
|
+
const result = truncateContent(long, 100, 500);
|
|
38
|
+
expect(result.length).toBeLessThanOrEqual(503); // "..." + 500 chars
|
|
39
|
+
expect(result.startsWith("...")).toBe(true);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it("returns empty string for empty input", () => {
|
|
43
|
+
expect(truncateContent("")).toBe("");
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it("respects custom maxLines and maxChars", () => {
|
|
47
|
+
const lines = Array.from({ length: 10 }, (_, i) => `line ${i + 1}`);
|
|
48
|
+
const text = lines.join("\n");
|
|
49
|
+
const result = truncateContent(text, 5, 1000);
|
|
50
|
+
const resultLines = result.split("\n");
|
|
51
|
+
expect(resultLines[0]).toBe("line 1");
|
|
52
|
+
expect(resultLines[1]).toBe("...");
|
|
53
|
+
expect(resultLines[resultLines.length - 1]).toBe("line 10");
|
|
54
|
+
expect(resultLines.length).toBe(6); // 1 first + "..." + 4 last
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
// ---------------------------------------------------------------------------
|
|
59
|
+
// getToolEmoji
|
|
60
|
+
// ---------------------------------------------------------------------------
|
|
61
|
+
|
|
62
|
+
describe("getToolEmoji", () => {
|
|
63
|
+
it("returns correct emoji for each known tool name", () => {
|
|
64
|
+
expect(getToolEmoji("Read")).toBe("\u{1F4D6}"); // 📖
|
|
65
|
+
expect(getToolEmoji("Write")).toBe("\u{270D}\u{FE0F}"); // ✍️
|
|
66
|
+
expect(getToolEmoji("Edit")).toBe("\u{270F}\u{FE0F}"); // ✏️
|
|
67
|
+
expect(getToolEmoji("Grep")).toBe("\u{1F50E}"); // 🔎
|
|
68
|
+
expect(getToolEmoji("Glob")).toBe("\u{1F4C2}"); // 📂
|
|
69
|
+
expect(getToolEmoji("Bash")).toBe("\u{1F5A5}\u{FE0F}"); // 🖥️
|
|
70
|
+
expect(getToolEmoji("WebSearch")).toBe("\u{1F310}"); // 🌐
|
|
71
|
+
expect(getToolEmoji("WebFetch")).toBe("\u{1F4E5}"); // 📥
|
|
72
|
+
expect(getToolEmoji("TodoWrite")).toBe("\u{2705}"); // ✅
|
|
73
|
+
expect(getToolEmoji("Agent")).toBe("\u{1F916}"); // 🤖
|
|
74
|
+
expect(getToolEmoji("NotebookEdit")).toBe("\u{1F4D3}"); // 📓
|
|
75
|
+
expect(getToolEmoji("AskUserQuestion")).toBe("\u{2753}");// ❓
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it("returns wrench for unknown tool names", () => {
|
|
79
|
+
expect(getToolEmoji("UnknownTool")).toBe("\u{1F527}");
|
|
80
|
+
expect(getToolEmoji("cat")).toBe("\u{1F527}");
|
|
81
|
+
expect(getToolEmoji("")).toBe("\u{1F527}");
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
// ---------------------------------------------------------------------------
|
|
86
|
+
// buildProgressCard
|
|
87
|
+
// ---------------------------------------------------------------------------
|
|
88
|
+
|
|
89
|
+
describe("buildProgressCard", () => {
|
|
90
|
+
it("returns valid JSON with correct schema", () => {
|
|
91
|
+
const card = buildProgressCard("test content");
|
|
92
|
+
const parsed = JSON.parse(card);
|
|
93
|
+
expect(parsed.schema).toBe("2.0");
|
|
94
|
+
expect(parsed.config.update_multi).toBe(true);
|
|
95
|
+
expect(parsed.config.streaming_mode).toBe(false);
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it("uses default header title '生成中...'", () => {
|
|
99
|
+
const card = buildProgressCard("hello");
|
|
100
|
+
const parsed = JSON.parse(card);
|
|
101
|
+
expect(parsed.header.title.content).toBe("生成中...");
|
|
102
|
+
expect(parsed.header.template).toBe("blue");
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it("includes stop button by default", () => {
|
|
106
|
+
const card = buildProgressCard("hello");
|
|
107
|
+
const parsed = JSON.parse(card);
|
|
108
|
+
const buttons = parsed.body.elements.filter((e: any) => e.tag === "button");
|
|
109
|
+
expect(buttons).toHaveLength(2);
|
|
110
|
+
expect(buttons[0].text.content).toBe("查看状态(/status)");
|
|
111
|
+
expect(buttons[1].text.content).toBe("停止生成(/stop)");
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it("hides stop button when showStop is false", () => {
|
|
115
|
+
const card = buildProgressCard("hello", { showStop: false });
|
|
116
|
+
const parsed = JSON.parse(card);
|
|
117
|
+
const buttons = parsed.body.elements.filter((e: any) => e.tag === "button");
|
|
118
|
+
expect(buttons).toHaveLength(0);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it("uses custom header title and template", () => {
|
|
122
|
+
const card = buildProgressCard("hello", { headerTitle: "已完成", headerTemplate: "green" });
|
|
123
|
+
const parsed = JSON.parse(card);
|
|
124
|
+
expect(parsed.header.title.content).toBe("已完成");
|
|
125
|
+
expect(parsed.header.template).toBe("green");
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it("includes markdown element with truncated content", () => {
|
|
129
|
+
const card = buildProgressCard("markdown text");
|
|
130
|
+
const parsed = JSON.parse(card);
|
|
131
|
+
const md = parsed.body.elements.find((e: any) => e.tag === "markdown");
|
|
132
|
+
expect(md).toBeDefined();
|
|
133
|
+
expect(md.element_id).toBe("main_content");
|
|
134
|
+
});
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
// ---------------------------------------------------------------------------
|
|
138
|
+
// buildHelpCard
|
|
139
|
+
// ---------------------------------------------------------------------------
|
|
140
|
+
|
|
141
|
+
describe("buildHelpCard", () => {
|
|
142
|
+
it("returns valid JSON with user text", () => {
|
|
143
|
+
const card = buildHelpCard("你好");
|
|
144
|
+
const parsed = JSON.parse(card);
|
|
145
|
+
expect(parsed.header.title.content).toBe("ChatCCC");
|
|
146
|
+
expect(parsed.elements[0].text.content).toContain("你好");
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it("includes action buttons", () => {
|
|
150
|
+
const card = buildHelpCard("test");
|
|
151
|
+
const parsed = JSON.parse(card);
|
|
152
|
+
const action = parsed.elements[2];
|
|
153
|
+
expect(action.tag).toBe("action");
|
|
154
|
+
expect(action.actions).toHaveLength(4);
|
|
155
|
+
});
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
// ---------------------------------------------------------------------------
|
|
159
|
+
// buildCdContent
|
|
160
|
+
// ---------------------------------------------------------------------------
|
|
161
|
+
|
|
162
|
+
describe("buildCdContent", () => {
|
|
163
|
+
const entries = [
|
|
164
|
+
{ name: "src", isDir: true },
|
|
165
|
+
{ name: "README.md", isDir: false },
|
|
166
|
+
{ name: "package.json", isDir: false },
|
|
167
|
+
];
|
|
168
|
+
|
|
169
|
+
it("returns markdown with path and listing", () => {
|
|
170
|
+
const content = buildCdContent("/home/user/project", entries, false);
|
|
171
|
+
expect(content).toContain("/home/user/project");
|
|
172
|
+
expect(content).toContain("📁 src/");
|
|
173
|
+
expect(content).toContain("📄 README.md");
|
|
174
|
+
expect(content).toContain("📄 package.json");
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
it("shows 已切换 when isUpdate is true", () => {
|
|
178
|
+
const content = buildCdContent("/home/user/project", entries, true);
|
|
179
|
+
expect(content).toContain("(已切换)");
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
it("shows currentCwd when provided", () => {
|
|
183
|
+
const content = buildCdContent("/new/path", entries, false, "/old/path");
|
|
184
|
+
expect(content).toContain("当前会话工作路径");
|
|
185
|
+
expect(content).toContain("/old/path");
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
it("omits currentCwd line when not provided", () => {
|
|
189
|
+
const content = buildCdContent("/new/path", entries, false);
|
|
190
|
+
expect(content).not.toContain("当前会话工作路径");
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
it("caps listing at maxFiles", () => {
|
|
194
|
+
const many = Array.from({ length: 150 }, (_, i) => ({
|
|
195
|
+
name: `file${i}.txt`,
|
|
196
|
+
isDir: false,
|
|
197
|
+
}));
|
|
198
|
+
const content = buildCdContent("/path", many, false);
|
|
199
|
+
expect(content).toContain("仅显示前 100 个");
|
|
200
|
+
});
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
// ---------------------------------------------------------------------------
|
|
204
|
+
// buildCdCard
|
|
205
|
+
// ---------------------------------------------------------------------------
|
|
206
|
+
|
|
207
|
+
describe("buildCdCard", () => {
|
|
208
|
+
const entries = [
|
|
209
|
+
{ name: "src", isDir: true },
|
|
210
|
+
{ name: "README.md", isDir: false },
|
|
211
|
+
];
|
|
212
|
+
|
|
213
|
+
// 提取 v1 卡片中 `tag:"div"` 的 markdown content(飞书 v1 富文本写在 text.content)
|
|
214
|
+
const mdContents = (parsed: any): string[] =>
|
|
215
|
+
parsed.elements
|
|
216
|
+
.filter((e: any) => e.tag === "div" && e.text?.tag === "lark_md")
|
|
217
|
+
.map((e: any) => e.text.content);
|
|
218
|
+
|
|
219
|
+
it("uses v1 interactive card format (no schema field, elements at top level)", () => {
|
|
220
|
+
// 必须用 v1 格式发送,否则通过 /im/v1/messages?msg_type=interactive 端点
|
|
221
|
+
// 直接发会被飞书静默拒绝(schema 2.0 卡片必须先经 CardKit 创建)
|
|
222
|
+
const card = buildCdCard("/home/project", entries, []);
|
|
223
|
+
const parsed = JSON.parse(card);
|
|
224
|
+
expect(parsed.schema).toBeUndefined();
|
|
225
|
+
expect(parsed.body).toBeUndefined();
|
|
226
|
+
expect(Array.isArray(parsed.elements)).toBe(true);
|
|
227
|
+
expect(parsed.header.title.content).toBe("工作路径");
|
|
228
|
+
expect(parsed.header.template).toBe("blue");
|
|
229
|
+
expect(parsed.config.wide_screen_mode).toBe(true);
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
it("shows current working directory in markdown", () => {
|
|
233
|
+
const card = buildCdCard("/home/project", entries, []);
|
|
234
|
+
const parsed = JSON.parse(card);
|
|
235
|
+
const cwdContent = mdContents(parsed).find((c) => c.includes("新会话默认工作路径"));
|
|
236
|
+
expect(cwdContent).toBeDefined();
|
|
237
|
+
expect(cwdContent).toContain("/home/project");
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
it("shows sessionCwd when provided", () => {
|
|
241
|
+
const card = buildCdCard("/default", entries, [], "/session/path");
|
|
242
|
+
const parsed = JSON.parse(card);
|
|
243
|
+
const sessionContent = mdContents(parsed).find((c) => c.includes("当前会话工作路径"));
|
|
244
|
+
expect(sessionContent).toBeDefined();
|
|
245
|
+
expect(sessionContent).toContain("/session/path");
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
it("does not show sessionCwd when not provided", () => {
|
|
249
|
+
const card = buildCdCard("/default", entries, []);
|
|
250
|
+
const parsed = JSON.parse(card);
|
|
251
|
+
const sessionContent = mdContents(parsed).find((c) => c.includes("当前会话工作路径"));
|
|
252
|
+
expect(sessionContent).toBeUndefined();
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
it("shows recent dirs section with buttons", () => {
|
|
256
|
+
const recentDirs = ["/home/user/project1", "/home/user/project2"];
|
|
257
|
+
const card = buildCdCard("/current", entries, recentDirs);
|
|
258
|
+
const parsed = JSON.parse(card);
|
|
259
|
+
const recentMd = mdContents(parsed).find((c) => c.includes("最近使用过的路径"));
|
|
260
|
+
expect(recentMd).toBeDefined();
|
|
261
|
+
|
|
262
|
+
const actionElements: any[] = parsed.elements.filter((e: any) => e.tag === "action");
|
|
263
|
+
expect(actionElements.length).toBeGreaterThanOrEqual(1);
|
|
264
|
+
const recentAction = actionElements.find((e: any) =>
|
|
265
|
+
e.actions.some((a: any) => a.value?.action === "cd")
|
|
266
|
+
);
|
|
267
|
+
expect(recentAction).toBeDefined();
|
|
268
|
+
expect(recentAction.actions).toHaveLength(2);
|
|
269
|
+
expect(recentAction.actions[0].value.action).toBe("cd");
|
|
270
|
+
expect(recentAction.actions[0].value.path).toBe("/home/user/project1");
|
|
271
|
+
expect(recentAction.actions[1].value.path).toBe("/home/user/project2");
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
it("does not show recent dirs section when empty", () => {
|
|
275
|
+
const card = buildCdCard("/current", entries, []);
|
|
276
|
+
const parsed = JSON.parse(card);
|
|
277
|
+
const recentMd = mdContents(parsed).find((c) => c.includes("最近使用过的路径"));
|
|
278
|
+
expect(recentMd).toBeUndefined();
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
it("truncates long paths in button text", () => {
|
|
282
|
+
const longPath = "/home/user/very/long/path/that/exceeds/thirty/six/chars";
|
|
283
|
+
const card = buildCdCard("/current", entries, [longPath]);
|
|
284
|
+
const parsed = JSON.parse(card);
|
|
285
|
+
const action: any = parsed.elements.find((e: any) => e.tag === "action");
|
|
286
|
+
const btnText = action.actions[0].text.content;
|
|
287
|
+
expect(btnText.startsWith("...")).toBe(true);
|
|
288
|
+
expect(btnText.length).toBeLessThanOrEqual(36);
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
it("shows directory listing", () => {
|
|
292
|
+
const card = buildCdCard("/current", entries, []);
|
|
293
|
+
const parsed = JSON.parse(card);
|
|
294
|
+
const listingContent = mdContents(parsed).find((c) => c.includes("📁 src/"));
|
|
295
|
+
expect(listingContent).toBeDefined();
|
|
296
|
+
expect(listingContent).toContain("📄 README.md");
|
|
297
|
+
});
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
// ---------------------------------------------------------------------------
|
|
301
|
+
// buildSessionsCard
|
|
302
|
+
// ---------------------------------------------------------------------------
|
|
303
|
+
|
|
304
|
+
describe("buildSessionsCard", () => {
|
|
305
|
+
it("returns valid JSON for empty sessions", () => {
|
|
306
|
+
const card = buildSessionsCard([]);
|
|
307
|
+
const parsed = JSON.parse(card);
|
|
308
|
+
expect(parsed.elements[0].text.content).toContain("没有会话记录");
|
|
309
|
+
expect(parsed.elements[0].text.content).toContain("/new");
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
it("returns valid JSON with session listing", () => {
|
|
313
|
+
const card = buildSessionsCard([
|
|
314
|
+
{ sessionId: "abc123", active: true, turnCount: 5, elapsedSeconds: 120, model: "Claude Opus 4.7", tool: "claude" },
|
|
315
|
+
]);
|
|
316
|
+
const parsed = JSON.parse(card);
|
|
317
|
+
expect(parsed.elements[0].text.content).toContain("共 **1** 个会话");
|
|
318
|
+
expect(parsed.elements[0].text.content).toContain("abc123");
|
|
319
|
+
expect(parsed.elements[0].text.content).toContain("🟢 活跃");
|
|
320
|
+
expect(parsed.elements[0].text.content).toContain("Claude Code");
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
it("shows idle status for inactive sessions", () => {
|
|
324
|
+
const card = buildSessionsCard([
|
|
325
|
+
{ sessionId: "xyz", active: false, turnCount: 0, elapsedSeconds: null, model: "Claude Sonnet 4.6", tool: "claude" },
|
|
326
|
+
]);
|
|
327
|
+
const parsed = JSON.parse(card);
|
|
328
|
+
expect(parsed.elements[0].text.content).toContain("⚪ 空闲");
|
|
329
|
+
});
|
|
330
|
+
|
|
331
|
+
it("shows elapsed time for active sessions", () => {
|
|
332
|
+
const card = buildSessionsCard([
|
|
333
|
+
{ sessionId: "active123", active: true, turnCount: 3, elapsedSeconds: 95, model: "Claude Opus 4.7", tool: "claude" },
|
|
334
|
+
]);
|
|
335
|
+
const parsed = JSON.parse(card);
|
|
336
|
+
expect(parsed.elements[0].text.content).toContain("1分35秒");
|
|
337
|
+
});
|
|
338
|
+
|
|
339
|
+
it("separates Claude Code and Cursor sessions", () => {
|
|
340
|
+
const card = buildSessionsCard([
|
|
341
|
+
{ sessionId: "c1", active: false, turnCount: 1, elapsedSeconds: null, model: "default", tool: "claude" },
|
|
342
|
+
{ sessionId: "c2", active: false, turnCount: 2, elapsedSeconds: null, model: "default", tool: "cursor" },
|
|
343
|
+
]);
|
|
344
|
+
const parsed = JSON.parse(card);
|
|
345
|
+
const content: string = parsed.elements[0].text.content;
|
|
346
|
+
expect(content).toContain("Claude Code 会话");
|
|
347
|
+
expect(content).toContain("Cursor 会话");
|
|
348
|
+
});
|
|
349
|
+
|
|
350
|
+
it("omits Cursor section when no Cursor sessions", () => {
|
|
351
|
+
const card = buildSessionsCard([
|
|
352
|
+
{ sessionId: "c1", active: false, turnCount: 1, elapsedSeconds: null, model: "default", tool: "claude" },
|
|
353
|
+
]);
|
|
354
|
+
const parsed = JSON.parse(card);
|
|
355
|
+
const content: string = parsed.elements[0].text.content;
|
|
356
|
+
expect(content).toContain("Claude Code 会话");
|
|
357
|
+
expect(content).not.toContain("Cursor 会话");
|
|
358
|
+
});
|
|
359
|
+
|
|
360
|
+
it("includes close button", () => {
|
|
361
|
+
const card = buildSessionsCard([]);
|
|
362
|
+
const parsed = JSON.parse(card);
|
|
363
|
+
const action = parsed.elements[2];
|
|
364
|
+
expect(action.actions[0].text.content).toBe("收起");
|
|
365
|
+
});
|
|
366
|
+
});
|
|
367
|
+
|
|
368
|
+
// ---------------------------------------------------------------------------
|
|
369
|
+
// buildStatusCard
|
|
370
|
+
// ---------------------------------------------------------------------------
|
|
371
|
+
|
|
372
|
+
describe("buildStatusCard", () => {
|
|
373
|
+
it("returns valid JSON with status text", () => {
|
|
374
|
+
const card = buildStatusCard("一切正常");
|
|
375
|
+
const parsed = JSON.parse(card);
|
|
376
|
+
expect(parsed.header.title.content).toBe("会话状态");
|
|
377
|
+
expect(parsed.elements[0].text.content).toBe("一切正常");
|
|
378
|
+
});
|
|
379
|
+
|
|
380
|
+
it("uses custom template color", () => {
|
|
381
|
+
const card = buildStatusCard("警告", "red");
|
|
382
|
+
const parsed = JSON.parse(card);
|
|
383
|
+
expect(parsed.header.template).toBe("red");
|
|
384
|
+
});
|
|
385
|
+
|
|
386
|
+
it("includes close button", () => {
|
|
387
|
+
const card = buildStatusCard("test");
|
|
388
|
+
const parsed = JSON.parse(card);
|
|
389
|
+
const action = parsed.elements[2];
|
|
390
|
+
expect(action.actions[0].text.content).toBe("收起");
|
|
391
|
+
});
|
|
392
|
+
});
|
|
393
|
+
|
|
394
|
+
// ---------------------------------------------------------------------------
|
|
395
|
+
// buildButtons
|
|
396
|
+
// ---------------------------------------------------------------------------
|
|
397
|
+
|
|
398
|
+
describe("buildButtons", () => {
|
|
399
|
+
it("returns action with buttons", () => {
|
|
400
|
+
const result = buildButtons([
|
|
401
|
+
{ text: "确认", value: "ok", type: "primary" },
|
|
402
|
+
]);
|
|
403
|
+
const obj = result as any;
|
|
404
|
+
expect(obj.tag).toBe("action");
|
|
405
|
+
expect(obj.actions).toHaveLength(1);
|
|
406
|
+
expect(obj.actions[0].text.content).toBe("确认");
|
|
407
|
+
expect(obj.actions[0].type).toBe("primary");
|
|
408
|
+
expect(obj.actions[0].value).toBe("ok");
|
|
409
|
+
});
|
|
410
|
+
|
|
411
|
+
it("defaults button type to primary", () => {
|
|
412
|
+
const result = buildButtons([
|
|
413
|
+
{ text: "取消", value: "cancel" },
|
|
414
|
+
]);
|
|
415
|
+
const obj = result as any;
|
|
416
|
+
expect(obj.actions[0].type).toBe("primary");
|
|
417
|
+
});
|
|
414
418
|
});
|