chatccc 0.2.252 → 0.2.254
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 +10 -1
- package/bin/cccagent.mjs +12 -3
- package/bin/chatccc.mjs +15 -6
- package/config.sample.json +2 -1
- package/deepccc-agent/README.md +40 -35
- package/deepccc-agent/os-prompts/darwin.md +8 -8
- package/deepccc-agent/os-prompts/linux.md +8 -8
- package/deepccc-agent/os-prompts/win32.md +11 -11
- package/deepccc-agent/package.json +65 -65
- package/dist/deepccc-agent/src/cli.js +634 -0
- package/dist/deepccc-agent/src/config.js +81 -0
- package/dist/deepccc-agent/src/context.js +348 -0
- package/dist/deepccc-agent/src/file-log.js +34 -0
- package/dist/deepccc-agent/src/file-tools.js +1208 -0
- package/dist/deepccc-agent/src/index.js +601 -0
- package/dist/deepccc-agent/src/permissions.js +170 -0
- package/dist/deepccc-agent/src/privacy.js +124 -0
- package/dist/deepccc-agent/src/proc-tree-kill.js +60 -0
- package/dist/deepccc-agent/src/progress/cards-helpers.js +70 -0
- package/dist/deepccc-agent/src/progress/reducer.js +102 -0
- package/dist/deepccc-agent/src/progress/terminal-renderer.js +264 -0
- package/dist/deepccc-agent/src/progress/view.js +30 -0
- package/dist/deepccc-agent/src/raw-stream-log.js +106 -0
- package/dist/deepccc-agent/src/session-search.js +276 -0
- package/dist/deepccc-agent/src/session-select.js +23 -0
- package/dist/deepccc-agent/src/sigint.js +26 -0
- package/dist/deepccc-agent/src/skills.js +178 -0
- package/dist/deepccc-agent/src/web-tools.js +246 -0
- package/dist/src/adapters/adapter-interface.js +19 -0
- package/dist/src/adapters/ccc-adapter.js +112 -0
- package/dist/src/adapters/claude-adapter.js +497 -0
- package/dist/src/adapters/claude-session-meta-store.js +92 -0
- package/dist/src/adapters/codex-adapter.js +279 -0
- package/dist/src/adapters/codex-session-meta-store.js +94 -0
- package/dist/src/adapters/cursor-adapter.js +491 -0
- package/dist/src/adapters/cursor-session-meta-store.js +116 -0
- package/dist/src/adapters/jsonl-stream.js +104 -0
- package/{src/adapters/proc-tree-kill.ts → dist/src/adapters/proc-tree-kill.js} +94 -97
- package/dist/src/adapters/raw-stream-log.js +106 -0
- package/dist/src/adapters/resource-monitor.js +113 -0
- package/dist/src/agent-activity.js +133 -0
- package/dist/src/agent-delegate-task-rpc.js +129 -0
- package/dist/src/agent-delegate-task.js +48 -0
- package/dist/src/agent-file-rpc.js +152 -0
- package/dist/src/agent-image-rpc.js +148 -0
- package/dist/src/agent-platform-routing.js +13 -0
- package/dist/src/agent-reload-config-rpc.js +23 -0
- package/dist/src/agent-rpc-body.js +87 -0
- package/dist/src/agent-stop-stuck.js +110 -0
- package/dist/src/agent-team/application/board-service.js +142 -0
- package/dist/src/agent-team/application/main-agent-service.js +151 -0
- package/dist/src/agent-team/domain/board.js +69 -0
- package/dist/src/agent-team/http/board-routes.js +213 -0
- package/dist/src/agent-team/infrastructure/filesystem-browser.js +112 -0
- package/dist/src/agent-team/infrastructure/main-agent-session-runtime.js +30 -0
- package/dist/src/agent-team/main-agent-bootstrap.js +15 -0
- package/dist/src/agent-team/repositories/board-repository.js +10 -0
- package/dist/src/agent-team/repositories/feishu-p2p-contact-store.js +64 -0
- package/dist/src/agent-team/repositories/json-board-repository.js +209 -0
- package/dist/src/agent-team/repositories/main-agent-binding-repository.js +84 -0
- package/dist/src/agent-team/sync/board-sync-adapter.js +1 -0
- package/dist/src/agent-team/web/agent-team-page.js +219 -0
- package/dist/src/agent-tool.js +10 -0
- package/dist/src/card-action-routing.js +7 -0
- package/dist/src/card-plain-text.js +101 -0
- package/dist/src/cardkit.js +158 -0
- package/dist/src/cards.js +573 -0
- package/dist/src/chatgpt-subscription-rpc.js +18 -0
- package/dist/src/chatgpt-subscription.js +199 -0
- package/dist/src/chrome-devtools-guard.js +238 -0
- package/dist/src/claude-sdk-installer.js +249 -0
- package/dist/src/codex-reset-actions.js +143 -0
- package/dist/src/config-utils.js +149 -0
- package/dist/src/config.js +808 -0
- package/dist/src/cursor-usage.js +77 -0
- package/dist/src/exit-banner.js +28 -0
- package/dist/src/feishu-api.js +1404 -0
- package/dist/src/feishu-message-ingress.js +137 -0
- package/dist/src/feishu-platform.js +97 -0
- package/dist/src/format-message.js +252 -0
- package/dist/src/git-command.js +155 -0
- package/dist/src/im-skills.js +121 -0
- package/dist/src/index.js +835 -0
- package/dist/src/litellm-proxy.js +300 -0
- package/dist/src/orchestrator.js +2096 -0
- package/dist/src/package-root.js +26 -0
- package/dist/src/platform-adapter.js +7 -0
- package/dist/src/platform-startup.js +6 -0
- package/dist/src/privacy.js +100 -0
- package/dist/src/progress/reducer.js +102 -0
- package/dist/src/progress/terminal-renderer.js +264 -0
- package/dist/src/progress/view.js +30 -0
- package/dist/src/response-stall.js +14 -0
- package/dist/src/runtime-entry.js +13 -0
- package/dist/src/runtime-reload.js +19 -0
- package/dist/src/session-chat-binding.js +183 -0
- package/dist/src/session-name.js +7 -0
- package/dist/src/session.js +2147 -0
- package/dist/src/shared-prefix.js +16 -0
- package/dist/src/shared.js +493 -0
- package/dist/src/sim-agent.js +105 -0
- package/dist/src/sim-platform.js +142 -0
- package/dist/src/sim-store.js +231 -0
- package/dist/src/simplify.js +99 -0
- package/dist/src/startup-lifecycle.js +209 -0
- package/dist/src/stream-state.js +141 -0
- package/dist/src/terminal-error.js +100 -0
- package/dist/src/trace.js +50 -0
- package/dist/src/turn-cards.js +92 -0
- package/dist/src/update-command-guard.js +114 -0
- package/{src/web-ui.ts → dist/src/web-ui.js} +753 -839
- package/dist/src/wechat-platform.js +545 -0
- package/package.json +76 -74
- package/deepccc-agent/LICENSE +0 -201
- package/deepccc-agent/bin/deepccc.mjs +0 -26
- package/deepccc-agent/docs/cache-hit-rate-1.jpg +0 -0
- package/deepccc-agent/docs/cache-hit-rate-2.jpg +0 -0
- package/deepccc-agent/package-lock.json +0 -2027
- package/deepccc-agent/src/__tests__/chat-session.test.ts +0 -877
- package/deepccc-agent/src/__tests__/cli-json.test.ts +0 -49
- package/deepccc-agent/src/__tests__/config.test.ts +0 -34
- package/deepccc-agent/src/__tests__/context.test.ts +0 -341
- package/deepccc-agent/src/__tests__/file-tools.test.ts +0 -240
- package/deepccc-agent/src/__tests__/permissions.test.ts +0 -199
- package/deepccc-agent/src/__tests__/privacy.test.ts +0 -318
- package/deepccc-agent/src/__tests__/progress-reducer.test.ts +0 -121
- package/deepccc-agent/src/__tests__/session-search.test.ts +0 -262
- package/deepccc-agent/src/__tests__/session-select.test.ts +0 -116
- package/deepccc-agent/src/__tests__/sigint.test.ts +0 -56
- package/deepccc-agent/src/__tests__/skills.test.ts +0 -284
- package/deepccc-agent/src/__tests__/terminal-renderer.test.ts +0 -247
- package/deepccc-agent/src/__tests__/web-tools.test.ts +0 -220
- package/deepccc-agent/src/cli.ts +0 -682
- package/deepccc-agent/src/config.ts +0 -101
- package/deepccc-agent/src/context.ts +0 -465
- package/deepccc-agent/src/file-log.ts +0 -38
- package/deepccc-agent/src/file-tools.ts +0 -1493
- package/deepccc-agent/src/index.ts +0 -710
- package/deepccc-agent/src/permissions.ts +0 -226
- package/deepccc-agent/src/privacy.ts +0 -141
- package/deepccc-agent/src/proc-tree-kill.ts +0 -61
- package/deepccc-agent/src/progress/cards-helpers.ts +0 -76
- package/deepccc-agent/src/progress/reducer.ts +0 -113
- package/deepccc-agent/src/progress/terminal-renderer.ts +0 -294
- package/deepccc-agent/src/progress/view.ts +0 -77
- package/deepccc-agent/src/raw-stream-log.ts +0 -124
- package/deepccc-agent/src/session-search.ts +0 -370
- package/deepccc-agent/src/session-select.ts +0 -48
- package/deepccc-agent/src/sigint.ts +0 -50
- package/deepccc-agent/src/skills.ts +0 -205
- package/deepccc-agent/src/web-tools.ts +0 -313
- package/deepccc-agent/tsconfig.build.json +0 -13
- package/deepccc-agent/tsconfig.json +0 -13
- package/deepccc-agent/vitest.config.ts +0 -7
- package/src/__tests__/adapter-interface.test.ts +0 -152
- package/src/__tests__/agent-activity.test.ts +0 -86
- package/src/__tests__/agent-delegate-task-rpc.test.ts +0 -165
- package/src/__tests__/agent-image-rpc.test.ts +0 -34
- package/src/__tests__/agent-platform-routing.test.ts +0 -26
- package/src/__tests__/agent-reload-config-rpc.test.ts +0 -99
- package/src/__tests__/agent-rpc-body.test.ts +0 -42
- package/src/__tests__/builtin-chat-session.test.ts +0 -532
- package/src/__tests__/builtin-cli-json.test.ts +0 -39
- package/src/__tests__/builtin-config.test.ts +0 -26
- package/src/__tests__/builtin-context.test.ts +0 -319
- package/src/__tests__/builtin-file-tools.test.ts +0 -240
- package/src/__tests__/builtin-permissions.test.ts +0 -219
- package/src/__tests__/builtin-session-search.test.ts +0 -262
- package/src/__tests__/builtin-session-select.test.ts +0 -116
- package/src/__tests__/builtin-sigint.test.ts +0 -56
- package/src/__tests__/builtin-skills.test.ts +0 -284
- package/src/__tests__/builtin-web-tools.test.ts +0 -220
- package/src/__tests__/card-action-routing.test.ts +0 -18
- package/src/__tests__/card-plain-text.test.ts +0 -45
- package/src/__tests__/cardkit.test.ts +0 -60
- package/src/__tests__/cards.test.ts +0 -607
- package/src/__tests__/ccc-adapter.test.ts +0 -194
- package/src/__tests__/chatgpt-subscription-rpc.test.ts +0 -89
- package/src/__tests__/chatgpt-subscription.test.ts +0 -135
- package/src/__tests__/chrome-devtools-guard.test.ts +0 -165
- package/src/__tests__/claude-adapter.test.ts +0 -614
- package/src/__tests__/claude-raw-stream-log.test.ts +0 -96
- package/src/__tests__/claude-sdk-installer.test.ts +0 -285
- package/src/__tests__/codex-adapter.test.ts +0 -331
- package/src/__tests__/codex-raw-stream-log.test.ts +0 -170
- package/src/__tests__/codex-reset-actions.test.ts +0 -146
- package/src/__tests__/config-reload.test.ts +0 -284
- package/src/__tests__/config-sample.test.ts +0 -97
- package/src/__tests__/config-utils.test.ts +0 -40
- package/src/__tests__/config.test.ts +0 -395
- package/src/__tests__/crash-logging.test.ts +0 -360
- package/src/__tests__/cursor-adapter.test.ts +0 -890
- package/src/__tests__/cursor-session-meta-store.test.ts +0 -212
- package/src/__tests__/feishu-api.test.ts +0 -60
- package/src/__tests__/feishu-avatar.test.ts +0 -504
- package/src/__tests__/feishu-message-ingress.test.ts +0 -138
- package/src/__tests__/feishu-platform.test.ts +0 -75
- package/src/__tests__/fixtures/codex_simple_text.jsonl +0 -4
- package/src/__tests__/fixtures/codex_with_tool.jsonl +0 -6
- package/src/__tests__/fixtures/cursor_partial_only.jsonl +0 -5
- package/src/__tests__/fixtures/cursor_partial_with_final.jsonl +0 -13
- package/src/__tests__/fixtures/cursor_with_tool_call.jsonl +0 -12
- package/src/__tests__/format-message.test.ts +0 -316
- package/src/__tests__/git-command.test.ts +0 -288
- package/src/__tests__/im-skills.test.ts +0 -125
- package/src/__tests__/jsonl-stream.test.ts +0 -79
- package/src/__tests__/orchestrator.test.ts +0 -1268
- package/src/__tests__/package-files.test.ts +0 -24
- package/src/__tests__/platform-startup.test.ts +0 -19
- package/src/__tests__/privacy.test.ts +0 -198
- package/src/__tests__/proc-tree-kill.test.ts +0 -108
- package/src/__tests__/progress-reducer.test.ts +0 -121
- package/src/__tests__/raw-stream-log.test.ts +0 -106
- package/src/__tests__/response-stall.test.ts +0 -49
- package/src/__tests__/restart.test.ts +0 -232
- package/src/__tests__/session-ccc-config.test.ts +0 -66
- package/src/__tests__/session.test.ts +0 -3004
- package/src/__tests__/shared-prefix.test.ts +0 -36
- package/src/__tests__/sim-agent.test.ts +0 -174
- package/src/__tests__/sim-platform.test.ts +0 -93
- package/src/__tests__/sim-store.test.ts +0 -214
- package/src/__tests__/simplify.test.ts +0 -283
- package/src/__tests__/startup-lifecycle.test.ts +0 -231
- package/src/__tests__/stop-session.test.ts +0 -162
- package/src/__tests__/stream-state.test.ts +0 -164
- package/src/__tests__/terminal-error.test.ts +0 -54
- package/src/__tests__/terminal-renderer.test.ts +0 -247
- package/src/__tests__/update-command-guard.test.ts +0 -144
- package/src/__tests__/web-ui.test.ts +0 -438
- package/src/__tests__/wechat-platform.test.ts +0 -111
- package/src/adapters/adapter-interface.ts +0 -217
- package/src/adapters/ccc-adapter.ts +0 -150
- package/src/adapters/claude-adapter.ts +0 -673
- package/src/adapters/claude-session-meta-store.ts +0 -120
- package/src/adapters/codex-adapter.ts +0 -426
- package/src/adapters/codex-session-meta-store.ts +0 -131
- package/src/adapters/cursor-adapter.ts +0 -681
- package/src/adapters/cursor-session-meta-store.ts +0 -154
- package/src/adapters/jsonl-stream.ts +0 -157
- package/src/adapters/raw-stream-log.ts +0 -124
- package/src/adapters/resource-monitor.ts +0 -141
- package/src/agent-activity.ts +0 -175
- package/src/agent-delegate-task-rpc.ts +0 -153
- package/src/agent-delegate-task.ts +0 -91
- package/src/agent-file-rpc.ts +0 -172
- package/src/agent-image-rpc.ts +0 -168
- package/src/agent-platform-routing.ts +0 -28
- package/src/agent-reload-config-rpc.ts +0 -34
- package/src/agent-rpc-body.ts +0 -92
- package/src/agent-stop-stuck.ts +0 -129
- package/src/card-action-routing.ts +0 -14
- package/src/card-plain-text.ts +0 -108
- package/src/cardkit.ts +0 -179
- package/src/cards.ts +0 -684
- package/src/chatgpt-subscription-rpc.ts +0 -27
- package/src/chatgpt-subscription.ts +0 -299
- package/src/chrome-devtools-guard.ts +0 -318
- package/src/claude-sdk-installer.ts +0 -324
- package/src/codex-reset-actions.ts +0 -184
- package/src/config-utils.ts +0 -211
- package/src/config.ts +0 -1063
- package/src/cursor-usage.ts +0 -128
- package/src/exit-banner.ts +0 -33
- package/src/feishu-api.ts +0 -1616
- package/src/feishu-message-ingress.ts +0 -195
- package/src/feishu-platform.ts +0 -159
- package/src/format-message.ts +0 -293
- package/src/git-command.ts +0 -202
- package/src/im-skills.ts +0 -149
- package/src/index.ts +0 -1089
- package/src/litellm-proxy.ts +0 -374
- package/src/orchestrator.ts +0 -2543
- package/src/platform-adapter.ts +0 -70
- package/src/platform-startup.ts +0 -16
- package/src/privacy.ts +0 -118
- package/src/progress/reducer.ts +0 -113
- package/src/progress/terminal-renderer.ts +0 -294
- package/src/progress/view.ts +0 -77
- package/src/response-stall.ts +0 -28
- package/src/runtime-reload.ts +0 -34
- package/src/session-chat-binding.ts +0 -292
- package/src/session-name.ts +0 -8
- package/src/session.ts +0 -2659
- package/src/shared-prefix.ts +0 -29
- package/src/shared.ts +0 -552
- package/src/sim-agent.ts +0 -167
- package/src/sim-platform.ts +0 -177
- package/src/sim-store.ts +0 -317
- package/src/simplify.ts +0 -120
- package/src/startup-lifecycle.ts +0 -250
- package/src/stream-state.ts +0 -177
- package/src/terminal-error.ts +0 -129
- package/src/trace.ts +0 -51
- package/src/turn-cards.ts +0 -118
- package/src/update-command-guard.ts +0 -165
- package/src/wechat-platform.ts +0 -680
|
@@ -1,164 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
|
|
2
|
-
import { mkdtemp, rm, readFile, readdir, writeFile } from "node:fs/promises";
|
|
3
|
-
import { tmpdir } from "node:os";
|
|
4
|
-
import { join } from "node:path";
|
|
5
|
-
|
|
6
|
-
// 在 import stream-state 之前先 mock config,让 USER_DATA_DIR 指向临时目录
|
|
7
|
-
const TEST_DATA_DIR = await mkdtemp(join(tmpdir(), "chatccc-stream-state-test-"));
|
|
8
|
-
vi.mock("../config.ts", async () => {
|
|
9
|
-
const actual = await vi.importActual<typeof import("../config.ts")>("../config.ts");
|
|
10
|
-
return {
|
|
11
|
-
...actual,
|
|
12
|
-
USER_DATA_DIR: TEST_DATA_DIR,
|
|
13
|
-
ts: () => "test-ts",
|
|
14
|
-
};
|
|
15
|
-
});
|
|
16
|
-
|
|
17
|
-
const {
|
|
18
|
-
writeStreamState,
|
|
19
|
-
readStreamState,
|
|
20
|
-
createEmptyStreamState,
|
|
21
|
-
isFinalReplySentForTurn,
|
|
22
|
-
markFinalReplySent,
|
|
23
|
-
STREAMS_DIR,
|
|
24
|
-
_setRenameForTest,
|
|
25
|
-
_resetRenameForTest,
|
|
26
|
-
} = await import("../stream-state.ts");
|
|
27
|
-
|
|
28
|
-
describe("writeStreamState — atomic rename", () => {
|
|
29
|
-
beforeEach(async () => {
|
|
30
|
-
// 清空测试目录
|
|
31
|
-
try {
|
|
32
|
-
const entries = await readdir(STREAMS_DIR);
|
|
33
|
-
for (const e of entries) await rm(join(STREAMS_DIR, e), { force: true });
|
|
34
|
-
} catch { /* dir not yet exist */ }
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
afterEach(async () => {
|
|
38
|
-
vi.restoreAllMocks();
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
it("writeStreamState 写完后 readStreamState 能读到完整内容", async () => {
|
|
42
|
-
const state = createEmptyStreamState("sid-1", "/tmp", "claude", 1);
|
|
43
|
-
state.accumulatedContent = "hello";
|
|
44
|
-
state.finalReply = "world";
|
|
45
|
-
|
|
46
|
-
await writeStreamState(state);
|
|
47
|
-
|
|
48
|
-
const got = await readStreamState("sid-1");
|
|
49
|
-
expect(got).not.toBeNull();
|
|
50
|
-
expect(got!.accumulatedContent).toBe("hello");
|
|
51
|
-
expect(got!.finalReply).toBe("world");
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
it("写入完成后,临时 .tmp 文件不应残留(rename 成功路径)", async () => {
|
|
55
|
-
const state = createEmptyStreamState("sid-2", "/tmp", "claude", 1);
|
|
56
|
-
await writeStreamState(state);
|
|
57
|
-
|
|
58
|
-
const entries = await readdir(STREAMS_DIR);
|
|
59
|
-
// 只应有 sid-2.json,没有 sid-2.json.tmp
|
|
60
|
-
expect(entries).toContain("sid-2.json");
|
|
61
|
-
expect(entries.some((e) => e.endsWith(".tmp"))).toBe(false);
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
it("覆盖写入时,reader 永远只读到完整 JSON(不读到半截)", async () => {
|
|
65
|
-
// 此测试验证:先写一个版本,再写第二个版本,reader 中间读取必然是某个完整版本
|
|
66
|
-
const state1 = createEmptyStreamState("sid-3", "/tmp", "claude", 1);
|
|
67
|
-
state1.accumulatedContent = "v1";
|
|
68
|
-
await writeStreamState(state1);
|
|
69
|
-
|
|
70
|
-
// 直接读原始字节,确认是完整 JSON
|
|
71
|
-
const raw1 = await readFile(join(STREAMS_DIR, "sid-3.json"), "utf-8");
|
|
72
|
-
expect(() => JSON.parse(raw1)).not.toThrow();
|
|
73
|
-
expect(JSON.parse(raw1).accumulatedContent).toBe("v1");
|
|
74
|
-
|
|
75
|
-
const state2 = createEmptyStreamState("sid-3", "/tmp", "claude", 2);
|
|
76
|
-
state2.accumulatedContent = "v2";
|
|
77
|
-
await writeStreamState(state2);
|
|
78
|
-
|
|
79
|
-
const raw2 = await readFile(join(STREAMS_DIR, "sid-3.json"), "utf-8");
|
|
80
|
-
expect(() => JSON.parse(raw2)).not.toThrow();
|
|
81
|
-
expect(JSON.parse(raw2).accumulatedContent).toBe("v2");
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
it("rename 失败时降级为直接覆盖写,不抛错 + 数据仍写入", async () => {
|
|
85
|
-
let renameCalls = 0;
|
|
86
|
-
_setRenameForTest(async () => {
|
|
87
|
-
renameCalls++;
|
|
88
|
-
throw Object.assign(new Error("EPERM mocked"), { code: "EPERM" });
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
try {
|
|
92
|
-
const state = createEmptyStreamState("sid-fallback", "/tmp", "claude", 1);
|
|
93
|
-
state.accumulatedContent = "fallback-content";
|
|
94
|
-
|
|
95
|
-
// 不应抛错
|
|
96
|
-
await expect(writeStreamState(state)).resolves.toBeUndefined();
|
|
97
|
-
|
|
98
|
-
// 数据仍应写入(降级路径走 writeFile 覆盖)
|
|
99
|
-
const got = await readStreamState("sid-fallback");
|
|
100
|
-
expect(got).not.toBeNull();
|
|
101
|
-
expect(got!.accumulatedContent).toBe("fallback-content");
|
|
102
|
-
|
|
103
|
-
expect(renameCalls).toBe(1);
|
|
104
|
-
} finally {
|
|
105
|
-
_resetRenameForTest();
|
|
106
|
-
}
|
|
107
|
-
});
|
|
108
|
-
|
|
109
|
-
it("readStreamState 对损坏的 JSON 返回 null(reader 兜底契约)", async () => {
|
|
110
|
-
// 直接写一段半截 JSON 到目标路径
|
|
111
|
-
const filePath = join(STREAMS_DIR, "sid-broken.json");
|
|
112
|
-
await writeFile(filePath, '{"sessionId":"sid-broken","accum', "utf-8");
|
|
113
|
-
|
|
114
|
-
const got = await readStreamState("sid-broken");
|
|
115
|
-
expect(got).toBeNull();
|
|
116
|
-
});
|
|
117
|
-
|
|
118
|
-
it("readStreamState 对不存在文件返回 null", async () => {
|
|
119
|
-
const got = await readStreamState("never-existed");
|
|
120
|
-
expect(got).toBeNull();
|
|
121
|
-
});
|
|
122
|
-
it("marks final reply delivery for the matching terminal turn only", async () => {
|
|
123
|
-
const state = createEmptyStreamState("sid-delivered", "/tmp", "claude", 2);
|
|
124
|
-
state.status = "done";
|
|
125
|
-
state.finalReply = "done";
|
|
126
|
-
await writeStreamState(state);
|
|
127
|
-
|
|
128
|
-
await markFinalReplySent("sid-delivered", 2, 12345);
|
|
129
|
-
|
|
130
|
-
const got = await readStreamState("sid-delivered");
|
|
131
|
-
expect(got).not.toBeNull();
|
|
132
|
-
expect(got!.finalReplySentTurn).toBe(2);
|
|
133
|
-
expect(got!.finalReplySentAt).toBe(12345);
|
|
134
|
-
expect(isFinalReplySentForTurn(got!)).toBe(true);
|
|
135
|
-
});
|
|
136
|
-
|
|
137
|
-
it("does not mark a running state or a different turn as delivered", async () => {
|
|
138
|
-
const state = createEmptyStreamState("sid-running", "/tmp", "claude", 3);
|
|
139
|
-
await writeStreamState(state);
|
|
140
|
-
|
|
141
|
-
await markFinalReplySent("sid-running", 2, 12345);
|
|
142
|
-
await markFinalReplySent("sid-running", 3, 12345);
|
|
143
|
-
|
|
144
|
-
const got = await readStreamState("sid-running");
|
|
145
|
-
expect(got).not.toBeNull();
|
|
146
|
-
expect(got!.finalReplySentTurn).toBeUndefined();
|
|
147
|
-
expect(isFinalReplySentForTurn(got!)).toBe(false);
|
|
148
|
-
});
|
|
149
|
-
});
|
|
150
|
-
|
|
151
|
-
describe("createEmptyStreamState", () => {
|
|
152
|
-
it("生成的 state 字段正确且 status=running", () => {
|
|
153
|
-
const s = createEmptyStreamState("sid-X", "/cwd", "cursor", 5);
|
|
154
|
-
expect(s.sessionId).toBe("sid-X");
|
|
155
|
-
expect(s.cwd).toBe("/cwd");
|
|
156
|
-
expect(s.tool).toBe("cursor");
|
|
157
|
-
expect(s.turnCount).toBe(5);
|
|
158
|
-
expect(s.status).toBe("running");
|
|
159
|
-
expect(s.accumulatedContent).toBe("");
|
|
160
|
-
expect(s.finalReply).toBe("");
|
|
161
|
-
expect(s.finalReplySentTurn).toBeUndefined();
|
|
162
|
-
expect(s.chunkCount).toBe(0);
|
|
163
|
-
});
|
|
164
|
-
});
|
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from "vitest";
|
|
2
|
-
|
|
3
|
-
import { classifyTerminalError, formatTerminalErrorNotice } from "../terminal-error.ts";
|
|
4
|
-
|
|
5
|
-
describe("terminal error classification", () => {
|
|
6
|
-
it("turns a retried connection timeout into an actionable root-cause summary", () => {
|
|
7
|
-
const error = classifyTerminalError(new Error(
|
|
8
|
-
"Failed after 3 attempts. Last error: Cannot connect to API: Connect Timeout Error " +
|
|
9
|
-
"(attempted addresses: 172.19.67.251:443, 172.19.68.1:443, timeout: 10000ms)",
|
|
10
|
-
), 123);
|
|
11
|
-
|
|
12
|
-
expect(error).toEqual({
|
|
13
|
-
kind: "network_timeout",
|
|
14
|
-
title: "网络连接超时",
|
|
15
|
-
message: "连接模型服务失败,已重试 3 次,单次连接等待 10 秒。请检查网络、VPN或模型服务状态后重试。",
|
|
16
|
-
occurredAt: 123,
|
|
17
|
-
});
|
|
18
|
-
expect(JSON.stringify(error)).not.toContain("172.19.67.251");
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
it("redacts credentials from an otherwise unknown error", () => {
|
|
22
|
-
const error = classifyTerminalError(
|
|
23
|
-
new Error("custom provider rejected api_key=secret-value Bearer eyJ.private sk-live-secret"),
|
|
24
|
-
456,
|
|
25
|
-
);
|
|
26
|
-
|
|
27
|
-
expect(error.kind).toBe("unknown");
|
|
28
|
-
expect(error.message).toContain("custom provider rejected");
|
|
29
|
-
expect(error.message).not.toContain("secret-value");
|
|
30
|
-
expect(error.message).not.toContain("eyJ.private");
|
|
31
|
-
expect(error.message).not.toContain("sk-live-secret");
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
it("marks an existing reply as potentially incomplete", () => {
|
|
35
|
-
const error = classifyTerminalError(new Error("HTTP 429 rate limit"), 789);
|
|
36
|
-
const notice = formatTerminalErrorNotice(error, "partial answer");
|
|
37
|
-
|
|
38
|
-
expect(notice).toContain("请求受到限流");
|
|
39
|
-
expect(notice).toContain("以下回复可能不完整");
|
|
40
|
-
expect(notice).toContain("partial answer");
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
it.each([
|
|
44
|
-
["HTTP 401 unauthorized", "authentication", "模型服务鉴权失败"],
|
|
45
|
-
["HTTP 429 too many requests", "rate_limit", "请求受到限流"],
|
|
46
|
-
["HTTP 503 service unavailable", "provider", "模型服务暂时不可用"],
|
|
47
|
-
["getaddrinfo ENOTFOUND api.example.test", "network", "无法连接模型服务"],
|
|
48
|
-
] as const)("classifies %s as %s", (message, kind, title) => {
|
|
49
|
-
const error = classifyTerminalError(new Error(message), 999);
|
|
50
|
-
|
|
51
|
-
expect(error.kind).toBe(kind);
|
|
52
|
-
expect(error.title).toBe(title);
|
|
53
|
-
});
|
|
54
|
-
});
|
|
@@ -1,247 +0,0 @@
|
|
|
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.ts";
|
|
9
|
-
import { progressView } from "../progress/view.ts";
|
|
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,144 +0,0 @@
|
|
|
1
|
-
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
-
import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
|
|
3
|
-
import { tmpdir } from "node:os";
|
|
4
|
-
import { join } from "node:path";
|
|
5
|
-
|
|
6
|
-
import {
|
|
7
|
-
acquireUpdateCommandGuard,
|
|
8
|
-
buildUpdateCommandId,
|
|
9
|
-
extractFeishuEventId,
|
|
10
|
-
} from "../update-command-guard.ts";
|
|
11
|
-
|
|
12
|
-
describe("Feishu update command IDs", () => {
|
|
13
|
-
it("extracts and namespaces a card callback event_id", () => {
|
|
14
|
-
const eventId = extractFeishuEventId({
|
|
15
|
-
schema: "2.0",
|
|
16
|
-
header: { event_id: "evt_update_001" },
|
|
17
|
-
event: { action: { value: { action: "update" } } },
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
expect(buildUpdateCommandId("card", eventId)).toBe("card:evt_update_001");
|
|
21
|
-
expect(buildUpdateCommandId("message", "om_update_001")).toBe("message:om_update_001");
|
|
22
|
-
});
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
describe("acquireUpdateCommandGuard", () => {
|
|
26
|
-
let tempDir: string;
|
|
27
|
-
let guardFile: string;
|
|
28
|
-
|
|
29
|
-
beforeEach(async () => {
|
|
30
|
-
tempDir = await mkdtemp(join(tmpdir(), "chatccc-update-guard-"));
|
|
31
|
-
guardFile = join(tempDir, "state", "update-command-guard.json");
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
afterEach(async () => {
|
|
35
|
-
await rm(tempDir, { recursive: true, force: true });
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
it("persists an accepted update ID before returning", async () => {
|
|
39
|
-
const result = acquireUpdateCommandGuard({
|
|
40
|
-
filePath: guardFile,
|
|
41
|
-
commandId: "message:om_update_001",
|
|
42
|
-
now: 1_000,
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
expect(result).toEqual({ allowed: true, reason: "accepted" });
|
|
46
|
-
const saved = JSON.parse(await readFile(guardFile, "utf8"));
|
|
47
|
-
expect(saved).toEqual({
|
|
48
|
-
version: 1,
|
|
49
|
-
processed: [{ id: "message:om_update_001", recordedAt: 1_000 }],
|
|
50
|
-
});
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
it("rejects the same ID after a simulated process restart", () => {
|
|
54
|
-
expect(acquireUpdateCommandGuard({
|
|
55
|
-
filePath: guardFile,
|
|
56
|
-
commandId: "message:om_update_001",
|
|
57
|
-
now: 1_000,
|
|
58
|
-
}).allowed).toBe(true);
|
|
59
|
-
|
|
60
|
-
// 第二次调用不共享任何内存状态,只通过落盘文件模拟新进程重启后的判断。
|
|
61
|
-
expect(acquireUpdateCommandGuard({
|
|
62
|
-
filePath: guardFile,
|
|
63
|
-
commandId: "message:om_update_001",
|
|
64
|
-
now: 9_999_999,
|
|
65
|
-
})).toEqual({ allowed: false, reason: "duplicate_id" });
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
it("accepts different IDs immediately without a cooldown", () => {
|
|
69
|
-
expect(acquireUpdateCommandGuard({
|
|
70
|
-
filePath: guardFile,
|
|
71
|
-
commandId: "message:om_update_001",
|
|
72
|
-
now: 1_000,
|
|
73
|
-
}).allowed).toBe(true);
|
|
74
|
-
|
|
75
|
-
expect(acquireUpdateCommandGuard({
|
|
76
|
-
filePath: guardFile,
|
|
77
|
-
commandId: "message:om_update_002",
|
|
78
|
-
now: 1_001,
|
|
79
|
-
})).toEqual({ allowed: true, reason: "accepted" });
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
it("repairs a corrupt state file and warns without creating an update loop", async () => {
|
|
83
|
-
await mkdir(join(tempDir, "state"), { recursive: true });
|
|
84
|
-
await writeFile(guardFile, "not-json", "utf8");
|
|
85
|
-
const warn = vi.fn();
|
|
86
|
-
|
|
87
|
-
expect(acquireUpdateCommandGuard({
|
|
88
|
-
filePath: guardFile,
|
|
89
|
-
commandId: "message:om_update_001",
|
|
90
|
-
now: 1_000,
|
|
91
|
-
warn,
|
|
92
|
-
})).toEqual({ allowed: true, reason: "accepted" });
|
|
93
|
-
expect(warn).toHaveBeenCalledOnce();
|
|
94
|
-
|
|
95
|
-
// 损坏文件已被有效状态覆盖,因此重启后的重复投递仍会被拦截。
|
|
96
|
-
expect(acquireUpdateCommandGuard({
|
|
97
|
-
filePath: guardFile,
|
|
98
|
-
commandId: "message:om_update_001",
|
|
99
|
-
now: 2_000,
|
|
100
|
-
warn,
|
|
101
|
-
})).toEqual({ allowed: false, reason: "duplicate_id" });
|
|
102
|
-
});
|
|
103
|
-
|
|
104
|
-
it("fails closed when the accepted ID cannot be persisted", async () => {
|
|
105
|
-
const blocker = join(tempDir, "not-a-directory");
|
|
106
|
-
await writeFile(blocker, "block", "utf8");
|
|
107
|
-
const warn = vi.fn();
|
|
108
|
-
|
|
109
|
-
expect(acquireUpdateCommandGuard({
|
|
110
|
-
filePath: join(blocker, "update-command-guard.json"),
|
|
111
|
-
commandId: "message:om_update_001",
|
|
112
|
-
now: 1_000,
|
|
113
|
-
warn,
|
|
114
|
-
})).toEqual({ allowed: false, reason: "state_write_failed" });
|
|
115
|
-
expect(warn).toHaveBeenCalledOnce();
|
|
116
|
-
});
|
|
117
|
-
|
|
118
|
-
it("allows sources without a stable ID without writing a misleading record", async () => {
|
|
119
|
-
expect(acquireUpdateCommandGuard({
|
|
120
|
-
filePath: guardFile,
|
|
121
|
-
commandId: undefined,
|
|
122
|
-
now: 1_000,
|
|
123
|
-
})).toEqual({ allowed: true, reason: "missing_id" });
|
|
124
|
-
await expect(readFile(guardFile, "utf8")).rejects.toMatchObject({ code: "ENOENT" });
|
|
125
|
-
});
|
|
126
|
-
|
|
127
|
-
it("keeps only the newest configured number of update IDs", async () => {
|
|
128
|
-
for (let i = 0; i < 4; i++) {
|
|
129
|
-
expect(acquireUpdateCommandGuard({
|
|
130
|
-
filePath: guardFile,
|
|
131
|
-
commandId: `message:om_update_${i}`,
|
|
132
|
-
now: i,
|
|
133
|
-
maxEntries: 3,
|
|
134
|
-
}).allowed).toBe(true);
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
const saved = JSON.parse(await readFile(guardFile, "utf8"));
|
|
138
|
-
expect(saved.processed.map((entry: { id: string }) => entry.id)).toEqual([
|
|
139
|
-
"message:om_update_1",
|
|
140
|
-
"message:om_update_2",
|
|
141
|
-
"message:om_update_3",
|
|
142
|
-
]);
|
|
143
|
-
});
|
|
144
|
-
});
|