chatccc 0.2.243 → 0.2.245
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 +6 -6
- package/bin/cccagent.mjs +17 -17
- package/deepccc-agent/README.md +14 -8
- package/deepccc-agent/bin/deepccc.mjs +26 -26
- package/deepccc-agent/os-prompts/darwin.md +8 -0
- package/deepccc-agent/os-prompts/linux.md +8 -0
- package/deepccc-agent/os-prompts/win32.md +9 -0
- package/deepccc-agent/package-lock.json +2 -2
- package/deepccc-agent/package.json +63 -62
- package/deepccc-agent/src/__tests__/chat-session.test.ts +682 -578
- package/deepccc-agent/src/__tests__/cli-json.test.ts +49 -49
- package/deepccc-agent/src/__tests__/config.test.ts +26 -26
- package/deepccc-agent/src/__tests__/context.test.ts +319 -319
- package/deepccc-agent/src/__tests__/file-tools.test.ts +240 -240
- package/deepccc-agent/src/__tests__/permissions.test.ts +195 -195
- package/deepccc-agent/src/__tests__/privacy.test.ts +8 -5
- package/deepccc-agent/src/__tests__/progress-reducer.test.ts +121 -121
- package/deepccc-agent/src/__tests__/session-search.test.ts +262 -262
- package/deepccc-agent/src/__tests__/session-select.test.ts +116 -116
- package/deepccc-agent/src/__tests__/sigint.test.ts +56 -56
- package/deepccc-agent/src/__tests__/skills.test.ts +284 -284
- package/deepccc-agent/src/__tests__/terminal-renderer.test.ts +247 -247
- package/deepccc-agent/src/__tests__/web-tools.test.ts +220 -220
- package/deepccc-agent/src/cli.ts +7 -6
- package/deepccc-agent/src/config.ts +88 -84
- package/deepccc-agent/src/context.ts +465 -465
- package/deepccc-agent/src/file-log.ts +38 -38
- package/deepccc-agent/src/index.ts +103 -36
- package/deepccc-agent/src/proc-tree-kill.ts +61 -61
- package/deepccc-agent/src/progress/cards-helpers.ts +76 -76
- package/deepccc-agent/src/progress/reducer.ts +113 -113
- package/deepccc-agent/src/progress/terminal-renderer.ts +294 -294
- package/deepccc-agent/src/progress/view.ts +77 -77
- package/deepccc-agent/src/raw-stream-log.ts +124 -124
- package/deepccc-agent/src/session-search.ts +370 -370
- package/deepccc-agent/src/session-select.ts +48 -48
- package/deepccc-agent/src/sigint.ts +50 -50
- package/deepccc-agent/src/skills.ts +205 -205
- package/deepccc-agent/src/web-tools.ts +313 -313
- package/deepccc-agent/tsconfig.build.json +13 -13
- package/deepccc-agent/tsconfig.json +13 -13
- package/deepccc-agent/vitest.config.ts +7 -7
- package/package.json +1 -1
- package/src/__tests__/builtin-chat-session.test.ts +522 -522
- package/src/__tests__/builtin-config.test.ts +26 -26
- package/src/__tests__/builtin-context.test.ts +319 -319
- package/src/__tests__/builtin-file-tools.test.ts +240 -240
- package/src/__tests__/builtin-permissions.test.ts +211 -211
- package/src/__tests__/builtin-session-search.test.ts +262 -262
- package/src/__tests__/builtin-session-select.test.ts +116 -116
- package/src/__tests__/builtin-sigint.test.ts +56 -56
- package/src/__tests__/builtin-skills.test.ts +284 -284
- package/src/__tests__/builtin-web-tools.test.ts +220 -220
- package/src/__tests__/ccc-adapter.test.ts +15 -0
- package/src/__tests__/config.test.ts +17 -17
- package/src/__tests__/progress-reducer.test.ts +121 -121
- package/src/__tests__/session-ccc-config.test.ts +45 -45
- package/src/__tests__/session.test.ts +369 -306
- package/src/adapters/adapter-interface.ts +8 -2
- package/src/adapters/ccc-adapter.ts +149 -145
- package/src/config-utils.ts +13 -13
- package/src/config.ts +13 -13
- package/src/progress/reducer.ts +113 -113
- package/src/session-chat-binding.ts +83 -83
- package/src/session.ts +323 -320
|
@@ -1,247 +1,247 @@
|
|
|
1
|
-
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
2
|
-
|
|
3
|
-
import {
|
|
4
|
-
buildBlockLines,
|
|
5
|
-
charWidth,
|
|
6
|
-
clipToWidth,
|
|
7
|
-
TerminalProgressRenderer,
|
|
8
|
-
} from "../progress/terminal-renderer.js";
|
|
9
|
-
import { progressView } from "../progress/view.js";
|
|
10
|
-
|
|
11
|
-
class FakeOut {
|
|
12
|
-
chunks: string[] = [];
|
|
13
|
-
columns = 100;
|
|
14
|
-
write(s: string): boolean {
|
|
15
|
-
this.chunks.push(s);
|
|
16
|
-
return true;
|
|
17
|
-
}
|
|
18
|
-
get output(): string {
|
|
19
|
-
return this.chunks.join("");
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/** 测试桩:仅实现 write 的假输出流,类型上按 WritableStream 对待 */
|
|
24
|
-
function asOut(out: FakeOut): NodeJS.WritableStream & { columns?: number } {
|
|
25
|
-
return out as unknown as NodeJS.WritableStream & { columns?: number };
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
afterEach(() => {
|
|
29
|
-
vi.useRealTimers();
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
describe("buildBlockLines", () => {
|
|
33
|
-
it("renders generating status line with header title and stop hint", () => {
|
|
34
|
-
const lines = buildBlockLines(
|
|
35
|
-
progressView({ headerTitle: "正在启动 Agent · 0秒", text: "" }),
|
|
36
|
-
100,
|
|
37
|
-
);
|
|
38
|
-
expect(lines[0]).toContain("⏳ 正在启动 Agent · 0秒");
|
|
39
|
-
expect(lines[0]).toContain("Ctrl+C 停止");
|
|
40
|
-
expect(lines[1]).toContain("等待 Agent 输出...");
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
it("renders done / stopped / error status lines", () => {
|
|
44
|
-
const done = buildBlockLines(progressView({ status: "done", text: "ok" }), 100);
|
|
45
|
-
expect(done[0]).toContain("✅ 完成");
|
|
46
|
-
expect(done[0]).toContain("\x1b[1m"); // 完成状态行加粗,不用浅色
|
|
47
|
-
const stopped = buildBlockLines(progressView({ status: "stopped", text: "x" }), 100);
|
|
48
|
-
expect(stopped[0]).toContain("⏹ 已停止");
|
|
49
|
-
const error = buildBlockLines(progressView({ status: "error", text: "x" }), 100);
|
|
50
|
-
expect(error[0]).toContain("❌ 异常结束");
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
it("renders tool lines with emoji and status mark", () => {
|
|
54
|
-
const lines = buildBlockLines(
|
|
55
|
-
progressView({
|
|
56
|
-
text: "",
|
|
57
|
-
tools: [
|
|
58
|
-
{ id: "t1", name: "edit_file", status: "running", detail: "edit a.ts" },
|
|
59
|
-
{ id: "t2", name: "run_command", status: "ok", summary: "npm test passed" },
|
|
60
|
-
{ id: "t3", name: "search_code", status: "error", summary: "regex error" },
|
|
61
|
-
],
|
|
62
|
-
}),
|
|
63
|
-
100,
|
|
64
|
-
);
|
|
65
|
-
expect(lines[1]).toContain("edit_file");
|
|
66
|
-
expect(lines[1]).toContain("…");
|
|
67
|
-
expect(lines[1]).toContain("edit a.ts");
|
|
68
|
-
expect(lines[2]).toContain("✓");
|
|
69
|
-
expect(lines[2]).toContain("npm test passed");
|
|
70
|
-
expect(lines[3]).toContain("✗");
|
|
71
|
-
expect(lines[3]).toContain("regex error");
|
|
72
|
-
// 工具行摘要不用浅色字体(回复内容可读性优先)
|
|
73
|
-
for (const line of [lines[1], lines[2], lines[3]]) {
|
|
74
|
-
expect(line).not.toContain("\x1b[2m");
|
|
75
|
-
}
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
it("truncates long body to maxBodyLines", () => {
|
|
79
|
-
const text = Array.from({ length: 50 }, (_, i) => `line ${i}`).join("\n");
|
|
80
|
-
const lines = buildBlockLines(progressView({ text }), 100, 5);
|
|
81
|
-
expect(lines.filter((l) => l.startsWith("line"))).toHaveLength(5);
|
|
82
|
-
expect(lines.join("\n")).toContain("...");
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
it("clips lines wider than terminal width to avoid wrapping", () => {
|
|
86
|
-
const lines = buildBlockLines(
|
|
87
|
-
progressView({ text: "x".repeat(200) }),
|
|
88
|
-
30,
|
|
89
|
-
);
|
|
90
|
-
for (const line of lines) {
|
|
91
|
-
expect(line.length).toBeLessThanOrEqual(30);
|
|
92
|
-
}
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
it("charWidth counts CJK/emoji as 2 columns and control/ZWJ as 0", () => {
|
|
96
|
-
expect(charWidth("a")).toBe(1);
|
|
97
|
-
expect(charWidth("中")).toBe(2);
|
|
98
|
-
expect(charWidth("📂")).toBe(2);
|
|
99
|
-
expect(charWidth("\u200d")).toBe(0); // ZWJ
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
it("clipToWidth truncates by display width, not string length", () => {
|
|
103
|
-
expect(clipToWidth("中文abc", 4)).toBe("中文");
|
|
104
|
-
expect(clipToWidth("中文abc", 5)).toBe("中文a");
|
|
105
|
-
expect(clipToWidth("📂 x", 2)).toBe("📂");
|
|
106
|
-
expect(clipToWidth("📂 x", 3)).toBe("📂 ");
|
|
107
|
-
});
|
|
108
|
-
|
|
109
|
-
it("clipToWidth keeps ANSI tokens whole and closes colors at the cut", () => {
|
|
110
|
-
const ansi = "\x1b[32m📂\x1b[0m abc";
|
|
111
|
-
expect(clipToWidth(ansi, 6)).toBe("\x1b[32m📂\x1b[0m abc");
|
|
112
|
-
expect(clipToWidth(ansi, 5)).toBe("\x1b[32m📂\x1b[0m ab");
|
|
113
|
-
// 截断发生在未闭合颜色内时自动补 reset,避免颜色泄漏
|
|
114
|
-
expect(clipToWidth("\x1b[1m\x1b[32m✅ 完成\x1b[0m", 6)).toBe("\x1b[1m\x1b[32m✅ 完\x1b[0m");
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
it("emoji tool lines are clipped by display width so the block never wraps", () => {
|
|
118
|
-
// 模拟用户示例:emoji + 超长 JSON 工具行,宽 40 的终端必须整行不折行
|
|
119
|
-
const lines = buildBlockLines(
|
|
120
|
-
progressView({
|
|
121
|
-
text: "",
|
|
122
|
-
tools: [
|
|
123
|
-
{ id: "t1", name: "list_dir", status: "ok", summary: `{"path":"C:\\Users\\weizhangjian\\.chatccc","entries":[{"name":"builtin","path":"C:\\Users${'x'.repeat(300)}"}]}` },
|
|
124
|
-
],
|
|
125
|
-
}),
|
|
126
|
-
40,
|
|
127
|
-
);
|
|
128
|
-
const toolLine = lines.find((l) => l.includes("list_dir"))!;
|
|
129
|
-
// 显示宽度(剥离 ANSI 后按 charWidth 计算)不超过终端列宽,永不折行
|
|
130
|
-
const visible = [...toolLine.replace(/\x1b\[[0-9;]*m/g, "")].reduce((w, ch) => w + charWidth(ch), 0);
|
|
131
|
-
expect(visible).toBeLessThanOrEqual(40);
|
|
132
|
-
expect(toolLine).toContain("\x1b[0m"); // ANSI 完整闭合
|
|
133
|
-
});
|
|
134
|
-
});
|
|
135
|
-
|
|
136
|
-
describe("TerminalProgressRenderer", () => {
|
|
137
|
-
it("begin writes hide-cursor and the first frame", () => {
|
|
138
|
-
const out = new FakeOut();
|
|
139
|
-
const r = new TerminalProgressRenderer({ out: asOut(out) });
|
|
140
|
-
r.begin(progressView({ text: "hello" }));
|
|
141
|
-
expect(out.output.startsWith("\x1b[?25l")).toBe(true);
|
|
142
|
-
expect(out.output).toContain("hello");
|
|
143
|
-
r.dispose();
|
|
144
|
-
});
|
|
145
|
-
|
|
146
|
-
it("end finalizes the block and restores cursor, keeping block on screen", () => {
|
|
147
|
-
const out = new FakeOut();
|
|
148
|
-
const r = new TerminalProgressRenderer({ out: asOut(out) });
|
|
149
|
-
r.begin(progressView({ text: "a" }));
|
|
150
|
-
const before = out.chunks.length;
|
|
151
|
-
r.end(progressView({ status: "done", text: "final" }));
|
|
152
|
-
const delta = out.output.slice(out.chunks.slice(0, before).join("").length);
|
|
153
|
-
expect(delta).toContain("✅ 完成");
|
|
154
|
-
expect(delta.endsWith("\x1b[?25h")).toBe(true);
|
|
155
|
-
});
|
|
156
|
-
|
|
157
|
-
it("render is frame-throttled and flush forces immediate redraw", () => {
|
|
158
|
-
vi.useFakeTimers();
|
|
159
|
-
const out = new FakeOut();
|
|
160
|
-
const r = new TerminalProgressRenderer({ out: asOut(out), frameMs: 100 });
|
|
161
|
-
|
|
162
|
-
r.begin(progressView({ text: "" }));
|
|
163
|
-
const outputAfterBegin = out.output;
|
|
164
|
-
|
|
165
|
-
r.render(progressView({ text: "one" }));
|
|
166
|
-
r.render(progressView({ text: "two" }));
|
|
167
|
-
r.render(progressView({ text: "three" }));
|
|
168
|
-
// 节流窗口内多次 render 不应产生任何输出
|
|
169
|
-
expect(out.output).toBe(outputAfterBegin);
|
|
170
|
-
|
|
171
|
-
vi.advanceTimersByTime(100);
|
|
172
|
-
// 合并后只重绘一次,且展示的是最新视图
|
|
173
|
-
expect(out.output).toContain("three");
|
|
174
|
-
expect(out.output).not.toContain("two");
|
|
175
|
-
|
|
176
|
-
r.flush();
|
|
177
|
-
// flush 立即重绘当前视图(输出继续增长且仍是三帧内容)
|
|
178
|
-
expect(out.output.length).toBeGreaterThan(outputAfterBegin.length);
|
|
179
|
-
expect(out.output).toContain("three");
|
|
180
|
-
r.dispose();
|
|
181
|
-
});
|
|
182
|
-
|
|
183
|
-
it("clears leftover lines when the block shrinks", () => {
|
|
184
|
-
const out = new FakeOut();
|
|
185
|
-
const r = new TerminalProgressRenderer({ out: asOut(out) });
|
|
186
|
-
r.begin(progressView({ text: "a\nb\nc\nd" }));
|
|
187
|
-
r.end(progressView({ status: "done", text: "short" }));
|
|
188
|
-
expect(out.output).toContain("\x1b[2K\n");
|
|
189
|
-
expect(out.output).toContain("\x1b[J");
|
|
190
|
-
});
|
|
191
|
-
|
|
192
|
-
it("heartbeat animates generating dots so the block never freezes", () => {
|
|
193
|
-
vi.useFakeTimers();
|
|
194
|
-
const out = new FakeOut();
|
|
195
|
-
const r = new TerminalProgressRenderer({ out: asOut(out), animMs: 100 });
|
|
196
|
-
r.begin(progressView({ headerTitle: "生成中...", text: "" }));
|
|
197
|
-
const frame0 = out.output;
|
|
198
|
-
expect(frame0).toContain("·"); // 首帧即带动画点
|
|
199
|
-
vi.advanceTimersByTime(100);
|
|
200
|
-
const frame1 = out.output;
|
|
201
|
-
expect(frame1).not.toBe(frame0); // 无新事件也持续重绘
|
|
202
|
-
expect(frame1).toContain("··"); // 第二帧点号增长
|
|
203
|
-
r.dispose();
|
|
204
|
-
});
|
|
205
|
-
|
|
206
|
-
it("stops the heartbeat after end so the final block stays static", () => {
|
|
207
|
-
vi.useFakeTimers();
|
|
208
|
-
const out = new FakeOut();
|
|
209
|
-
const r = new TerminalProgressRenderer({ out: asOut(out), animMs: 100 });
|
|
210
|
-
r.begin(progressView({ text: "" }));
|
|
211
|
-
r.end(progressView({ status: "done", text: "final" }));
|
|
212
|
-
const afterEnd = out.output.length;
|
|
213
|
-
vi.advanceTimersByTime(500);
|
|
214
|
-
expect(out.output.length).toBe(afterEnd);
|
|
215
|
-
});
|
|
216
|
-
|
|
217
|
-
it("redraw moves up exactly blockLines-1 rows, never eating history above", () => {
|
|
218
|
-
// 回归测试:光标在区块最后一行时,回到第一行只需上移 N-1 行。
|
|
219
|
-
// 上移 N 行会每帧多上移 1 行,把上方历史内容逐行吃掉。
|
|
220
|
-
const out = new FakeOut();
|
|
221
|
-
const r = new TerminalProgressRenderer({ out: asOut(out), animMs: 0 });
|
|
222
|
-
// 状态行 1 行 + 正文 4 行 = 5 行区块
|
|
223
|
-
r.begin(progressView({ text: "a\nb\nc\nd" }));
|
|
224
|
-
const afterBegin = out.output;
|
|
225
|
-
r.flush(); // 第二帧强制重绘
|
|
226
|
-
const delta = out.output.slice(afterBegin.length);
|
|
227
|
-
expect(delta).toContain("\x1b[4A"); // 上移 5-1=4 行
|
|
228
|
-
expect(delta).not.toContain("\x1b[5A"); // 绝不出现上移 N 行
|
|
229
|
-
r.dispose();
|
|
230
|
-
});
|
|
231
|
-
|
|
232
|
-
it("multi-frame redraws keep moving up N-1 rows each time (no drift)", () => {
|
|
233
|
-
const out = new FakeOut();
|
|
234
|
-
const r = new TerminalProgressRenderer({ out: asOut(out), animMs: 0 });
|
|
235
|
-
r.begin(progressView({ text: "a\nb\nc\nd" }));
|
|
236
|
-
for (let i = 0; i < 3; i++) {
|
|
237
|
-
r.flush();
|
|
238
|
-
}
|
|
239
|
-
// 连续多帧重绘,每帧上移都是 4 行(5 行区块),不是 5/6/7... 逐帧递增
|
|
240
|
-
const moves = out.output.match(/\x1b\[(\d+)A/g) ?? [];
|
|
241
|
-
expect(moves.length).toBe(3);
|
|
242
|
-
for (const move of moves) {
|
|
243
|
-
expect(move).toBe("\x1b[4A");
|
|
244
|
-
}
|
|
245
|
-
r.dispose();
|
|
246
|
-
});
|
|
247
|
-
});
|
|
1
|
+
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
buildBlockLines,
|
|
5
|
+
charWidth,
|
|
6
|
+
clipToWidth,
|
|
7
|
+
TerminalProgressRenderer,
|
|
8
|
+
} from "../progress/terminal-renderer.js";
|
|
9
|
+
import { progressView } from "../progress/view.js";
|
|
10
|
+
|
|
11
|
+
class FakeOut {
|
|
12
|
+
chunks: string[] = [];
|
|
13
|
+
columns = 100;
|
|
14
|
+
write(s: string): boolean {
|
|
15
|
+
this.chunks.push(s);
|
|
16
|
+
return true;
|
|
17
|
+
}
|
|
18
|
+
get output(): string {
|
|
19
|
+
return this.chunks.join("");
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** 测试桩:仅实现 write 的假输出流,类型上按 WritableStream 对待 */
|
|
24
|
+
function asOut(out: FakeOut): NodeJS.WritableStream & { columns?: number } {
|
|
25
|
+
return out as unknown as NodeJS.WritableStream & { columns?: number };
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
afterEach(() => {
|
|
29
|
+
vi.useRealTimers();
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
describe("buildBlockLines", () => {
|
|
33
|
+
it("renders generating status line with header title and stop hint", () => {
|
|
34
|
+
const lines = buildBlockLines(
|
|
35
|
+
progressView({ headerTitle: "正在启动 Agent · 0秒", text: "" }),
|
|
36
|
+
100,
|
|
37
|
+
);
|
|
38
|
+
expect(lines[0]).toContain("⏳ 正在启动 Agent · 0秒");
|
|
39
|
+
expect(lines[0]).toContain("Ctrl+C 停止");
|
|
40
|
+
expect(lines[1]).toContain("等待 Agent 输出...");
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it("renders done / stopped / error status lines", () => {
|
|
44
|
+
const done = buildBlockLines(progressView({ status: "done", text: "ok" }), 100);
|
|
45
|
+
expect(done[0]).toContain("✅ 完成");
|
|
46
|
+
expect(done[0]).toContain("\x1b[1m"); // 完成状态行加粗,不用浅色
|
|
47
|
+
const stopped = buildBlockLines(progressView({ status: "stopped", text: "x" }), 100);
|
|
48
|
+
expect(stopped[0]).toContain("⏹ 已停止");
|
|
49
|
+
const error = buildBlockLines(progressView({ status: "error", text: "x" }), 100);
|
|
50
|
+
expect(error[0]).toContain("❌ 异常结束");
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it("renders tool lines with emoji and status mark", () => {
|
|
54
|
+
const lines = buildBlockLines(
|
|
55
|
+
progressView({
|
|
56
|
+
text: "",
|
|
57
|
+
tools: [
|
|
58
|
+
{ id: "t1", name: "edit_file", status: "running", detail: "edit a.ts" },
|
|
59
|
+
{ id: "t2", name: "run_command", status: "ok", summary: "npm test passed" },
|
|
60
|
+
{ id: "t3", name: "search_code", status: "error", summary: "regex error" },
|
|
61
|
+
],
|
|
62
|
+
}),
|
|
63
|
+
100,
|
|
64
|
+
);
|
|
65
|
+
expect(lines[1]).toContain("edit_file");
|
|
66
|
+
expect(lines[1]).toContain("…");
|
|
67
|
+
expect(lines[1]).toContain("edit a.ts");
|
|
68
|
+
expect(lines[2]).toContain("✓");
|
|
69
|
+
expect(lines[2]).toContain("npm test passed");
|
|
70
|
+
expect(lines[3]).toContain("✗");
|
|
71
|
+
expect(lines[3]).toContain("regex error");
|
|
72
|
+
// 工具行摘要不用浅色字体(回复内容可读性优先)
|
|
73
|
+
for (const line of [lines[1], lines[2], lines[3]]) {
|
|
74
|
+
expect(line).not.toContain("\x1b[2m");
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it("truncates long body to maxBodyLines", () => {
|
|
79
|
+
const text = Array.from({ length: 50 }, (_, i) => `line ${i}`).join("\n");
|
|
80
|
+
const lines = buildBlockLines(progressView({ text }), 100, 5);
|
|
81
|
+
expect(lines.filter((l) => l.startsWith("line"))).toHaveLength(5);
|
|
82
|
+
expect(lines.join("\n")).toContain("...");
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it("clips lines wider than terminal width to avoid wrapping", () => {
|
|
86
|
+
const lines = buildBlockLines(
|
|
87
|
+
progressView({ text: "x".repeat(200) }),
|
|
88
|
+
30,
|
|
89
|
+
);
|
|
90
|
+
for (const line of lines) {
|
|
91
|
+
expect(line.length).toBeLessThanOrEqual(30);
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it("charWidth counts CJK/emoji as 2 columns and control/ZWJ as 0", () => {
|
|
96
|
+
expect(charWidth("a")).toBe(1);
|
|
97
|
+
expect(charWidth("中")).toBe(2);
|
|
98
|
+
expect(charWidth("📂")).toBe(2);
|
|
99
|
+
expect(charWidth("\u200d")).toBe(0); // ZWJ
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it("clipToWidth truncates by display width, not string length", () => {
|
|
103
|
+
expect(clipToWidth("中文abc", 4)).toBe("中文");
|
|
104
|
+
expect(clipToWidth("中文abc", 5)).toBe("中文a");
|
|
105
|
+
expect(clipToWidth("📂 x", 2)).toBe("📂");
|
|
106
|
+
expect(clipToWidth("📂 x", 3)).toBe("📂 ");
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it("clipToWidth keeps ANSI tokens whole and closes colors at the cut", () => {
|
|
110
|
+
const ansi = "\x1b[32m📂\x1b[0m abc";
|
|
111
|
+
expect(clipToWidth(ansi, 6)).toBe("\x1b[32m📂\x1b[0m abc");
|
|
112
|
+
expect(clipToWidth(ansi, 5)).toBe("\x1b[32m📂\x1b[0m ab");
|
|
113
|
+
// 截断发生在未闭合颜色内时自动补 reset,避免颜色泄漏
|
|
114
|
+
expect(clipToWidth("\x1b[1m\x1b[32m✅ 完成\x1b[0m", 6)).toBe("\x1b[1m\x1b[32m✅ 完\x1b[0m");
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it("emoji tool lines are clipped by display width so the block never wraps", () => {
|
|
118
|
+
// 模拟用户示例:emoji + 超长 JSON 工具行,宽 40 的终端必须整行不折行
|
|
119
|
+
const lines = buildBlockLines(
|
|
120
|
+
progressView({
|
|
121
|
+
text: "",
|
|
122
|
+
tools: [
|
|
123
|
+
{ id: "t1", name: "list_dir", status: "ok", summary: `{"path":"C:\\Users\\weizhangjian\\.chatccc","entries":[{"name":"builtin","path":"C:\\Users${'x'.repeat(300)}"}]}` },
|
|
124
|
+
],
|
|
125
|
+
}),
|
|
126
|
+
40,
|
|
127
|
+
);
|
|
128
|
+
const toolLine = lines.find((l) => l.includes("list_dir"))!;
|
|
129
|
+
// 显示宽度(剥离 ANSI 后按 charWidth 计算)不超过终端列宽,永不折行
|
|
130
|
+
const visible = [...toolLine.replace(/\x1b\[[0-9;]*m/g, "")].reduce((w, ch) => w + charWidth(ch), 0);
|
|
131
|
+
expect(visible).toBeLessThanOrEqual(40);
|
|
132
|
+
expect(toolLine).toContain("\x1b[0m"); // ANSI 完整闭合
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
describe("TerminalProgressRenderer", () => {
|
|
137
|
+
it("begin writes hide-cursor and the first frame", () => {
|
|
138
|
+
const out = new FakeOut();
|
|
139
|
+
const r = new TerminalProgressRenderer({ out: asOut(out) });
|
|
140
|
+
r.begin(progressView({ text: "hello" }));
|
|
141
|
+
expect(out.output.startsWith("\x1b[?25l")).toBe(true);
|
|
142
|
+
expect(out.output).toContain("hello");
|
|
143
|
+
r.dispose();
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
it("end finalizes the block and restores cursor, keeping block on screen", () => {
|
|
147
|
+
const out = new FakeOut();
|
|
148
|
+
const r = new TerminalProgressRenderer({ out: asOut(out) });
|
|
149
|
+
r.begin(progressView({ text: "a" }));
|
|
150
|
+
const before = out.chunks.length;
|
|
151
|
+
r.end(progressView({ status: "done", text: "final" }));
|
|
152
|
+
const delta = out.output.slice(out.chunks.slice(0, before).join("").length);
|
|
153
|
+
expect(delta).toContain("✅ 完成");
|
|
154
|
+
expect(delta.endsWith("\x1b[?25h")).toBe(true);
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
it("render is frame-throttled and flush forces immediate redraw", () => {
|
|
158
|
+
vi.useFakeTimers();
|
|
159
|
+
const out = new FakeOut();
|
|
160
|
+
const r = new TerminalProgressRenderer({ out: asOut(out), frameMs: 100 });
|
|
161
|
+
|
|
162
|
+
r.begin(progressView({ text: "" }));
|
|
163
|
+
const outputAfterBegin = out.output;
|
|
164
|
+
|
|
165
|
+
r.render(progressView({ text: "one" }));
|
|
166
|
+
r.render(progressView({ text: "two" }));
|
|
167
|
+
r.render(progressView({ text: "three" }));
|
|
168
|
+
// 节流窗口内多次 render 不应产生任何输出
|
|
169
|
+
expect(out.output).toBe(outputAfterBegin);
|
|
170
|
+
|
|
171
|
+
vi.advanceTimersByTime(100);
|
|
172
|
+
// 合并后只重绘一次,且展示的是最新视图
|
|
173
|
+
expect(out.output).toContain("three");
|
|
174
|
+
expect(out.output).not.toContain("two");
|
|
175
|
+
|
|
176
|
+
r.flush();
|
|
177
|
+
// flush 立即重绘当前视图(输出继续增长且仍是三帧内容)
|
|
178
|
+
expect(out.output.length).toBeGreaterThan(outputAfterBegin.length);
|
|
179
|
+
expect(out.output).toContain("three");
|
|
180
|
+
r.dispose();
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
it("clears leftover lines when the block shrinks", () => {
|
|
184
|
+
const out = new FakeOut();
|
|
185
|
+
const r = new TerminalProgressRenderer({ out: asOut(out) });
|
|
186
|
+
r.begin(progressView({ text: "a\nb\nc\nd" }));
|
|
187
|
+
r.end(progressView({ status: "done", text: "short" }));
|
|
188
|
+
expect(out.output).toContain("\x1b[2K\n");
|
|
189
|
+
expect(out.output).toContain("\x1b[J");
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
it("heartbeat animates generating dots so the block never freezes", () => {
|
|
193
|
+
vi.useFakeTimers();
|
|
194
|
+
const out = new FakeOut();
|
|
195
|
+
const r = new TerminalProgressRenderer({ out: asOut(out), animMs: 100 });
|
|
196
|
+
r.begin(progressView({ headerTitle: "生成中...", text: "" }));
|
|
197
|
+
const frame0 = out.output;
|
|
198
|
+
expect(frame0).toContain("·"); // 首帧即带动画点
|
|
199
|
+
vi.advanceTimersByTime(100);
|
|
200
|
+
const frame1 = out.output;
|
|
201
|
+
expect(frame1).not.toBe(frame0); // 无新事件也持续重绘
|
|
202
|
+
expect(frame1).toContain("··"); // 第二帧点号增长
|
|
203
|
+
r.dispose();
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
it("stops the heartbeat after end so the final block stays static", () => {
|
|
207
|
+
vi.useFakeTimers();
|
|
208
|
+
const out = new FakeOut();
|
|
209
|
+
const r = new TerminalProgressRenderer({ out: asOut(out), animMs: 100 });
|
|
210
|
+
r.begin(progressView({ text: "" }));
|
|
211
|
+
r.end(progressView({ status: "done", text: "final" }));
|
|
212
|
+
const afterEnd = out.output.length;
|
|
213
|
+
vi.advanceTimersByTime(500);
|
|
214
|
+
expect(out.output.length).toBe(afterEnd);
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
it("redraw moves up exactly blockLines-1 rows, never eating history above", () => {
|
|
218
|
+
// 回归测试:光标在区块最后一行时,回到第一行只需上移 N-1 行。
|
|
219
|
+
// 上移 N 行会每帧多上移 1 行,把上方历史内容逐行吃掉。
|
|
220
|
+
const out = new FakeOut();
|
|
221
|
+
const r = new TerminalProgressRenderer({ out: asOut(out), animMs: 0 });
|
|
222
|
+
// 状态行 1 行 + 正文 4 行 = 5 行区块
|
|
223
|
+
r.begin(progressView({ text: "a\nb\nc\nd" }));
|
|
224
|
+
const afterBegin = out.output;
|
|
225
|
+
r.flush(); // 第二帧强制重绘
|
|
226
|
+
const delta = out.output.slice(afterBegin.length);
|
|
227
|
+
expect(delta).toContain("\x1b[4A"); // 上移 5-1=4 行
|
|
228
|
+
expect(delta).not.toContain("\x1b[5A"); // 绝不出现上移 N 行
|
|
229
|
+
r.dispose();
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
it("multi-frame redraws keep moving up N-1 rows each time (no drift)", () => {
|
|
233
|
+
const out = new FakeOut();
|
|
234
|
+
const r = new TerminalProgressRenderer({ out: asOut(out), animMs: 0 });
|
|
235
|
+
r.begin(progressView({ text: "a\nb\nc\nd" }));
|
|
236
|
+
for (let i = 0; i < 3; i++) {
|
|
237
|
+
r.flush();
|
|
238
|
+
}
|
|
239
|
+
// 连续多帧重绘,每帧上移都是 4 行(5 行区块),不是 5/6/7... 逐帧递增
|
|
240
|
+
const moves = out.output.match(/\x1b\[(\d+)A/g) ?? [];
|
|
241
|
+
expect(moves.length).toBe(3);
|
|
242
|
+
for (const move of moves) {
|
|
243
|
+
expect(move).toBe("\x1b[4A");
|
|
244
|
+
}
|
|
245
|
+
r.dispose();
|
|
246
|
+
});
|
|
247
|
+
});
|