chatccc 0.2.190 → 0.2.191

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. package/agent-prompts/claude_specific.md +45 -45
  2. package/agent-prompts/codex_specific.md +2 -2
  3. package/agent-prompts/cursor_specific.md +13 -13
  4. package/config.sample.json +14 -14
  5. package/im-skills/feishu-skill/receive-send-file.md +63 -63
  6. package/im-skills/feishu-skill/receive-send-image.md +24 -24
  7. package/im-skills/feishu-skill/skill.md +3 -3
  8. package/im-skills/wechat-file-skill/receive-send-file.md +38 -38
  9. package/im-skills/wechat-file-skill/send-file.mjs +83 -83
  10. package/im-skills/wechat-file-skill/skill.md +10 -10
  11. package/im-skills/wechat-image-skill/skill.md +10 -10
  12. package/im-skills/wechat-video-skill/receive-send-video.md +38 -38
  13. package/im-skills/wechat-video-skill/send-video.mjs +79 -79
  14. package/im-skills/wechat-video-skill/skill.md +10 -10
  15. package/package.json +1 -1
  16. package/scripts/postinstall-sharp-check.mjs +58 -58
  17. package/src/__tests__/agent-delegate-task-rpc.test.ts +165 -165
  18. package/src/__tests__/builtin-config.test.ts +33 -33
  19. package/src/__tests__/card-plain-text.test.ts +5 -5
  20. package/src/__tests__/cardkit.test.ts +60 -60
  21. package/src/__tests__/cards.test.ts +77 -77
  22. package/src/__tests__/chatgpt-subscription-rpc.test.ts +89 -89
  23. package/src/__tests__/chatgpt-subscription.test.ts +135 -135
  24. package/src/__tests__/chrome-devtools-guard.test.ts +165 -165
  25. package/src/__tests__/claude-adapter.test.ts +592 -592
  26. package/src/__tests__/codex-reset-actions.test.ts +146 -146
  27. package/src/__tests__/config-reload.test.ts +4 -4
  28. package/src/__tests__/config-sample.test.ts +17 -17
  29. package/src/__tests__/feishu-api.test.ts +60 -60
  30. package/src/__tests__/feishu-platform.test.ts +22 -22
  31. package/src/__tests__/format-message.test.ts +47 -47
  32. package/src/__tests__/orchestrator.test.ts +120 -120
  33. package/src/__tests__/privacy.test.ts +198 -198
  34. package/src/__tests__/raw-stream-log.test.ts +106 -106
  35. package/src/__tests__/session.test.ts +17 -17
  36. package/src/__tests__/shared-prefix.test.ts +36 -36
  37. package/src/__tests__/stream-state.test.ts +42 -42
  38. package/src/__tests__/web-ui.test.ts +209 -209
  39. package/src/adapters/claude-adapter.ts +566 -566
  40. package/src/adapters/claude-session-meta-store.ts +120 -120
  41. package/src/adapters/codex-adapter.ts +30 -30
  42. package/src/adapters/cursor-adapter.ts +46 -46
  43. package/src/adapters/raw-stream-log.ts +124 -124
  44. package/src/adapters/resource-monitor.ts +140 -140
  45. package/src/agent-delegate-task-rpc.ts +153 -153
  46. package/src/agent-delegate-task.ts +81 -81
  47. package/src/agent-stop-stuck.ts +129 -129
  48. package/src/builtin/cli.ts +204 -189
  49. package/src/builtin/index.ts +170 -170
  50. package/src/cards.ts +137 -137
  51. package/src/chatgpt-subscription-rpc.ts +27 -27
  52. package/src/chatgpt-subscription.ts +299 -299
  53. package/src/chrome-devtools-guard.ts +318 -318
  54. package/src/codex-reset-actions.ts +184 -184
  55. package/src/config.ts +86 -86
  56. package/src/feishu-platform.ts +20 -20
  57. package/src/format-message.ts +293 -293
  58. package/src/litellm-proxy.ts +374 -374
  59. package/src/orchestrator.ts +143 -143
  60. package/src/privacy.ts +118 -118
  61. package/src/session-chat-binding.ts +6 -6
  62. package/src/session-name.ts +8 -8
  63. package/src/session.ts +98 -98
  64. package/src/shared-prefix.ts +29 -29
  65. package/src/sim-platform.ts +20 -20
  66. package/src/turn-cards.ts +117 -117
  67. package/src/web-ui.ts +205 -205
@@ -77,14 +77,14 @@ import {
77
77
  _setAdapterForToolForTest,
78
78
  _setSessionRegistryFileForTest,
79
79
  _setSessionToolsFileForTest,
80
- loadSessionRegistryForBinding,
81
- recordSessionRegistry,
82
- resetState,
83
- sessionInfoMap,
84
- } from "../session.ts";
80
+ loadSessionRegistryForBinding,
81
+ recordSessionRegistry,
82
+ resetState,
83
+ sessionInfoMap,
84
+ } from "../session.ts";
85
85
  import { activePrompts, resetBindingState } from "../session-chat-binding.ts";
86
- import { ABD_APPEND_PROMPT } from "../shared-prefix.ts";
87
- import { config } from "../config.ts";
86
+ import { ABD_APPEND_PROMPT } from "../shared-prefix.ts";
87
+ import { config } from "../config.ts";
88
88
 
89
89
  function mockPlatform(kind: "wechat" | "feishu" = "wechat"): PlatformAdapter {
90
90
  return {
@@ -131,12 +131,12 @@ describe("handleCommand WeChat processing ack", () => {
131
131
  tempDir = await mkdtemp(join(tmpdir(), "chatccc-orchestrator-"));
132
132
  _setSessionRegistryFileForTest(join(tempDir, "session-registry.json"));
133
133
  _setSessionToolsFileForTest(join(tempDir, "sessions.json"));
134
- resetState();
135
- resetBindingState();
136
- config.claude.defaultAgent = true;
137
- config.cursor.defaultAgent = false;
138
- config.codex.defaultAgent = false;
139
- mockStreamStates.clear();
134
+ resetState();
135
+ resetBindingState();
136
+ config.claude.defaultAgent = true;
137
+ config.cursor.defaultAgent = false;
138
+ config.codex.defaultAgent = false;
139
+ mockStreamStates.clear();
140
140
  mockGetCodexUsageSummary.mockReset();
141
141
  mockGetCursorUsageSummary.mockReset();
142
142
  mockGetChatGptSubscriptionStatus.mockReset();
@@ -367,11 +367,11 @@ describe("handleCommand WeChat processing ack", () => {
367
367
  expect(userText).not.toContain("/abd");
368
368
  });
369
369
 
370
- it("cleans stale Feishu p2p binding but keeps valid commands from auto-creating a group", async () => {
371
- const platform = mockPlatform("feishu");
372
- await recordSessionRegistry({
373
- chatId: "feishu-p2p",
374
- sessionId: "stale-sid",
370
+ it("cleans stale Feishu p2p binding but keeps valid commands from auto-creating a group", async () => {
371
+ const platform = mockPlatform("feishu");
372
+ await recordSessionRegistry({
373
+ chatId: "feishu-p2p",
374
+ sessionId: "stale-sid",
375
375
  tool: "claude",
376
376
  chatName: "旧私聊绑定",
377
377
  running: false,
@@ -381,108 +381,108 @@ describe("handleCommand WeChat processing ack", () => {
381
381
 
382
382
  expect(platform.createGroup).not.toHaveBeenCalled();
383
383
  expect(platform.sendRawCard).toHaveBeenCalled();
384
- const registry = await loadSessionRegistryForBinding();
385
- expect(registry["feishu-p2p"]).toBeUndefined();
386
- });
387
-
388
- it("shows Claude effort switch card in an active Feishu session", async () => {
389
- const platform = mockPlatform("feishu");
390
- vi.mocked(platform.getChatInfo).mockResolvedValue({ name: "claude-session", description: "Claude Session: sid-claude-effort" });
391
- vi.mocked(platform.extractSessionInfo).mockReturnValue({ sessionId: "sid-claude-effort", tool: "claude" });
392
- await recordSessionRegistry({
393
- chatId: "feishu-chat",
394
- sessionId: "sid-claude-effort",
395
- tool: "claude",
396
- chatName: "claude-session",
397
- running: false,
398
- });
399
- sessionInfoMap.set("feishu-chat", {
400
- sessionId: "sid-claude-effort",
401
- tool: "claude",
402
- turnCount: 0,
403
- lastContextTokens: 0,
404
- startTime: Date.now(),
405
- });
406
-
407
- await handleCommand(platform, "/effort", "feishu-chat", "ou-user", Date.now(), "group");
408
-
409
- expect(platform.sendRawCard).toHaveBeenCalled();
410
- const card = JSON.parse(vi.mocked(platform.sendRawCard).mock.calls[0][1]);
411
- const raw = JSON.stringify(card);
412
- expect(raw).toContain("/effort low");
413
- expect(raw).toContain("/effort xhigh");
414
- expect(raw).toContain("/effort max");
415
- });
416
-
417
- it("switches Codex effort for the current session and reflects it in /state", async () => {
418
- const platform = mockPlatform("feishu");
419
- vi.mocked(platform.getChatInfo).mockResolvedValue({ name: "codex-session", description: "Codex Session: sid-codex-effort" });
420
- vi.mocked(platform.extractSessionInfo).mockReturnValue({ sessionId: "sid-codex-effort", tool: "codex" });
421
- _setAdapterForToolForTest("codex", mockAdapter("sid-codex-effort"));
422
- await recordSessionRegistry({
423
- chatId: "codex-chat",
424
- sessionId: "sid-codex-effort",
425
- tool: "codex",
426
- chatName: "codex-session",
427
- running: false,
428
- });
429
- sessionInfoMap.set("codex-chat", {
430
- sessionId: "sid-codex-effort",
431
- tool: "codex",
432
- turnCount: 0,
433
- lastContextTokens: 0,
434
- startTime: Date.now(),
435
- });
436
-
437
- await handleCommand(platform, "/effort xhigh", "codex-chat", "ou-user", Date.now(), "group");
438
- expect(platform.sendCard).toHaveBeenCalledWith(
439
- "codex-chat",
440
- "Effort 切换",
441
- expect.stringContaining("xhigh"),
442
- "green",
443
- );
444
-
445
- vi.mocked(platform.sendCard).mockClear();
446
- vi.mocked(platform.sendRawCard).mockClear();
447
- await handleCommand(platform, "/state", "codex-chat", "ou-user", Date.now() + 1, "group");
448
-
449
- expect(platform.sendRawCard).toHaveBeenCalled();
450
- const card = JSON.parse(vi.mocked(platform.sendRawCard).mock.calls[0][1]);
451
- expect(JSON.stringify(card)).toContain("xhigh");
452
- });
453
-
454
- it("rejects /effort in Cursor sessions", async () => {
455
- const platform = mockPlatform("feishu");
456
- vi.mocked(platform.getChatInfo).mockResolvedValue({ name: "cursor-session", description: "Cursor Session: sid-cursor-effort" });
457
- vi.mocked(platform.extractSessionInfo).mockReturnValue({ sessionId: "sid-cursor-effort", tool: "cursor" });
458
- _setAdapterForToolForTest("cursor", mockAdapter("sid-cursor-effort"));
459
- await recordSessionRegistry({
460
- chatId: "cursor-chat",
461
- sessionId: "sid-cursor-effort",
462
- tool: "cursor",
463
- chatName: "cursor-session",
464
- running: false,
465
- });
466
- sessionInfoMap.set("cursor-chat", {
467
- sessionId: "sid-cursor-effort",
468
- tool: "cursor",
469
- turnCount: 0,
470
- lastContextTokens: 0,
471
- startTime: Date.now(),
472
- });
473
-
474
- await handleCommand(platform, "/effort high", "cursor-chat", "ou-user", Date.now(), "group");
475
-
476
- expect(platform.sendCard).toHaveBeenCalledWith(
477
- "cursor-chat",
478
- "Effort 切换",
479
- expect.stringContaining("不支持 effort"),
480
- "red",
481
- );
482
- });
483
-
484
- it("handles /usage without creating a new Feishu group", async () => {
485
- const platform = mockPlatform("feishu");
384
+ const registry = await loadSessionRegistryForBinding();
385
+ expect(registry["feishu-p2p"]).toBeUndefined();
386
+ });
387
+
388
+ it("shows Claude effort switch card in an active Feishu session", async () => {
389
+ const platform = mockPlatform("feishu");
390
+ vi.mocked(platform.getChatInfo).mockResolvedValue({ name: "claude-session", description: "Claude Session: sid-claude-effort" });
391
+ vi.mocked(platform.extractSessionInfo).mockReturnValue({ sessionId: "sid-claude-effort", tool: "claude" });
392
+ await recordSessionRegistry({
393
+ chatId: "feishu-chat",
394
+ sessionId: "sid-claude-effort",
395
+ tool: "claude",
396
+ chatName: "claude-session",
397
+ running: false,
398
+ });
399
+ sessionInfoMap.set("feishu-chat", {
400
+ sessionId: "sid-claude-effort",
401
+ tool: "claude",
402
+ turnCount: 0,
403
+ lastContextTokens: 0,
404
+ startTime: Date.now(),
405
+ });
406
+
407
+ await handleCommand(platform, "/effort", "feishu-chat", "ou-user", Date.now(), "group");
408
+
409
+ expect(platform.sendRawCard).toHaveBeenCalled();
410
+ const card = JSON.parse(vi.mocked(platform.sendRawCard).mock.calls[0][1]);
411
+ const raw = JSON.stringify(card);
412
+ expect(raw).toContain("/effort low");
413
+ expect(raw).toContain("/effort xhigh");
414
+ expect(raw).toContain("/effort max");
415
+ });
416
+
417
+ it("switches Codex effort for the current session and reflects it in /state", async () => {
418
+ const platform = mockPlatform("feishu");
419
+ vi.mocked(platform.getChatInfo).mockResolvedValue({ name: "codex-session", description: "Codex Session: sid-codex-effort" });
420
+ vi.mocked(platform.extractSessionInfo).mockReturnValue({ sessionId: "sid-codex-effort", tool: "codex" });
421
+ _setAdapterForToolForTest("codex", mockAdapter("sid-codex-effort"));
422
+ await recordSessionRegistry({
423
+ chatId: "codex-chat",
424
+ sessionId: "sid-codex-effort",
425
+ tool: "codex",
426
+ chatName: "codex-session",
427
+ running: false,
428
+ });
429
+ sessionInfoMap.set("codex-chat", {
430
+ sessionId: "sid-codex-effort",
431
+ tool: "codex",
432
+ turnCount: 0,
433
+ lastContextTokens: 0,
434
+ startTime: Date.now(),
435
+ });
436
+
437
+ await handleCommand(platform, "/effort xhigh", "codex-chat", "ou-user", Date.now(), "group");
438
+ expect(platform.sendCard).toHaveBeenCalledWith(
439
+ "codex-chat",
440
+ "Effort 切换",
441
+ expect.stringContaining("xhigh"),
442
+ "green",
443
+ );
444
+
445
+ vi.mocked(platform.sendCard).mockClear();
446
+ vi.mocked(platform.sendRawCard).mockClear();
447
+ await handleCommand(platform, "/state", "codex-chat", "ou-user", Date.now() + 1, "group");
448
+
449
+ expect(platform.sendRawCard).toHaveBeenCalled();
450
+ const card = JSON.parse(vi.mocked(platform.sendRawCard).mock.calls[0][1]);
451
+ expect(JSON.stringify(card)).toContain("xhigh");
452
+ });
453
+
454
+ it("rejects /effort in Cursor sessions", async () => {
455
+ const platform = mockPlatform("feishu");
456
+ vi.mocked(platform.getChatInfo).mockResolvedValue({ name: "cursor-session", description: "Cursor Session: sid-cursor-effort" });
457
+ vi.mocked(platform.extractSessionInfo).mockReturnValue({ sessionId: "sid-cursor-effort", tool: "cursor" });
458
+ _setAdapterForToolForTest("cursor", mockAdapter("sid-cursor-effort"));
459
+ await recordSessionRegistry({
460
+ chatId: "cursor-chat",
461
+ sessionId: "sid-cursor-effort",
462
+ tool: "cursor",
463
+ chatName: "cursor-session",
464
+ running: false,
465
+ });
466
+ sessionInfoMap.set("cursor-chat", {
467
+ sessionId: "sid-cursor-effort",
468
+ tool: "cursor",
469
+ turnCount: 0,
470
+ lastContextTokens: 0,
471
+ startTime: Date.now(),
472
+ });
473
+
474
+ await handleCommand(platform, "/effort high", "cursor-chat", "ou-user", Date.now(), "group");
475
+
476
+ expect(platform.sendCard).toHaveBeenCalledWith(
477
+ "cursor-chat",
478
+ "Effort 切换",
479
+ expect.stringContaining("不支持 effort"),
480
+ "red",
481
+ );
482
+ });
483
+
484
+ it("handles /usage without creating a new Feishu group", async () => {
485
+ const platform = mockPlatform("feishu");
486
486
  const usage = {
487
487
  fiveHour: { usedPercent: 37, remainingPercent: 63, resetAtEpochSeconds: 1781528212, resetAfterSeconds: 10349 },
488
488
  weekly: { usedPercent: 12, remainingPercent: 88, resetAtEpochSeconds: 1781842926, resetAfterSeconds: 325063 },
@@ -1,198 +1,198 @@
1
- import { describe, it, expect, beforeEach, afterEach, afterAll, vi } from "vitest";
2
- import { mkdtemp, rm, writeFile } from "node:fs/promises";
3
- import { tmpdir } from "node:os";
4
- import { join } from "node:path";
5
-
6
- const TEST_DATA_DIR = await mkdtemp(join(tmpdir(), "chatccc-privacy-test-"));
7
- vi.mock("../config.ts", async () => {
8
- const actual = await vi.importActual<typeof import("../config.ts")>("../config.ts");
9
- return {
10
- ...actual,
11
- USER_DATA_DIR: TEST_DATA_DIR,
12
- ts: () => "test-ts",
13
- };
14
- });
15
-
16
- let applyPrivacy: (text: string) => string;
17
- let reloadPrivacyRules: () => void;
18
- let getPrivacyRules: () => Record<string, string>;
19
- let getPrivacyConfig: () => { enabled: boolean; rules: Record<string, string> };
20
-
21
- beforeEach(async () => {
22
- vi.resetModules();
23
- try {
24
- await rm(join(TEST_DATA_DIR, "privacy.json"), { force: true });
25
- } catch {}
26
- const mod = await import("../privacy.ts");
27
- applyPrivacy = mod.applyPrivacy;
28
- reloadPrivacyRules = mod.reloadPrivacyRules;
29
- getPrivacyRules = mod.getPrivacyRules;
30
- getPrivacyConfig = mod.getPrivacyConfig;
31
- });
32
-
33
- afterEach(async () => {
34
- try {
35
- await rm(join(TEST_DATA_DIR, "privacy.json"), { force: true });
36
- } catch {}
37
- });
38
-
39
- afterAll(async () => {
40
- try {
41
- await rm(TEST_DATA_DIR, { recursive: true, force: true });
42
- } catch {}
43
- });
44
-
45
- describe("applyPrivacy", () => {
46
- it("returns original text when privacy.json is missing", () => {
47
- reloadPrivacyRules();
48
- expect(applyPrivacy("hello weizhangjian")).toBe("hello weizhangjian");
49
- });
50
-
51
- it("supports legacy flat privacy rules", async () => {
52
- await writeFile(
53
- join(TEST_DATA_DIR, "privacy.json"),
54
- JSON.stringify({ weizhangjian: "wzj", secret: "***" }),
55
- "utf-8",
56
- );
57
- reloadPrivacyRules();
58
-
59
- expect(applyPrivacy("hello weizhangjian")).toBe("hello wzj");
60
- expect(applyPrivacy("my secret is safe")).toBe("my *** is safe");
61
- expect(applyPrivacy("weizhangjian and secret")).toBe("wzj and ***");
62
- });
63
-
64
- it("supports privacy.json schema with enabled=false", async () => {
65
- await writeFile(
66
- join(TEST_DATA_DIR, "privacy.json"),
67
- JSON.stringify({ enabled: false, rules: { weizhangjian: "wzj" } }),
68
- "utf-8",
69
- );
70
- reloadPrivacyRules();
71
-
72
- expect(getPrivacyConfig()).toEqual({ enabled: false, rules: { weizhangjian: "wzj" } });
73
- expect(getPrivacyRules()).toEqual({ weizhangjian: "wzj" });
74
- expect(applyPrivacy("hello weizhangjian")).toBe("hello weizhangjian");
75
- });
76
-
77
- it("supports privacy.json schema with enabled=true", async () => {
78
- await writeFile(
79
- join(TEST_DATA_DIR, "privacy.json"),
80
- JSON.stringify({ enabled: true, rules: { weizhangjian: "wzj" } }),
81
- "utf-8",
82
- );
83
- reloadPrivacyRules();
84
-
85
- expect(applyPrivacy("hello weizhangjian")).toBe("hello wzj");
86
- });
87
-
88
- it("accepts UTF-8 BOM in privacy.json", async () => {
89
- await writeFile(
90
- join(TEST_DATA_DIR, "privacy.json"),
91
- `\uFEFF${JSON.stringify({ enabled: false, rules: { weizhangjian: "wzj" } })}`,
92
- "utf-8",
93
- );
94
- reloadPrivacyRules();
95
-
96
- expect(getPrivacyConfig()).toEqual({ enabled: false, rules: { weizhangjian: "wzj" } });
97
- expect(applyPrivacy("hello weizhangjian")).toBe("hello weizhangjian");
98
- });
99
-
100
- it("auto reloads privacy.json changes without explicit reload", async () => {
101
- await writeFile(
102
- join(TEST_DATA_DIR, "privacy.json"),
103
- JSON.stringify({ weizhangjian: "wzj" }),
104
- "utf-8",
105
- );
106
- reloadPrivacyRules();
107
-
108
- expect(applyPrivacy("hello weizhangjian")).toBe("hello wzj");
109
-
110
- await writeFile(
111
- join(TEST_DATA_DIR, "privacy.json"),
112
- JSON.stringify({ enabled: false, rules: { weizhangjian: "wzj-disabled" } }),
113
- "utf-8",
114
- );
115
-
116
- expect(applyPrivacy("hello weizhangjian")).toBe("hello weizhangjian");
117
- expect(getPrivacyConfig()).toEqual({ enabled: false, rules: { weizhangjian: "wzj-disabled" } });
118
- });
119
-
120
- it("replaces multiple rules and repeated occurrences", async () => {
121
- await writeFile(
122
- join(TEST_DATA_DIR, "privacy.json"),
123
- JSON.stringify({ a: "A", b: "B" }),
124
- "utf-8",
125
- );
126
- reloadPrivacyRules();
127
-
128
- expect(applyPrivacy("a b a b")).toBe("A B A B");
129
- });
130
-
131
- it("returns empty text directly", async () => {
132
- await writeFile(
133
- join(TEST_DATA_DIR, "privacy.json"),
134
- JSON.stringify({ x: "y" }),
135
- "utf-8",
136
- );
137
- reloadPrivacyRules();
138
-
139
- expect(applyPrivacy("")).toBe("");
140
- });
141
-
142
- it("treats special characters in rule keys literally", async () => {
143
- await writeFile(
144
- join(TEST_DATA_DIR, "privacy.json"),
145
- JSON.stringify({ "a.b": "X", "(test)": "Y", "*star": "Z" }),
146
- "utf-8",
147
- );
148
- reloadPrivacyRules();
149
-
150
- expect(applyPrivacy("hello a.b world")).toBe("hello X world");
151
- expect(applyPrivacy("text (test) here")).toBe("text Y here");
152
- expect(applyPrivacy("a *star shines")).toBe("a Z shines");
153
- });
154
-
155
- it("reloadPrivacyRules forces a reload", async () => {
156
- await writeFile(
157
- join(TEST_DATA_DIR, "privacy.json"),
158
- JSON.stringify({ old: "OLD" }),
159
- "utf-8",
160
- );
161
- reloadPrivacyRules();
162
-
163
- expect(applyPrivacy("old")).toBe("OLD");
164
- expect(getPrivacyRules()).toEqual({ old: "OLD" });
165
-
166
- await writeFile(
167
- join(TEST_DATA_DIR, "privacy.json"),
168
- JSON.stringify({ new: "NEW" }),
169
- "utf-8",
170
- );
171
- reloadPrivacyRules();
172
-
173
- expect(applyPrivacy("new")).toBe("NEW");
174
- expect(getPrivacyRules()).toEqual({ new: "NEW" });
175
- });
176
-
177
- it("returns original text for malformed JSON", async () => {
178
- await writeFile(
179
- join(TEST_DATA_DIR, "privacy.json"),
180
- "not json",
181
- "utf-8",
182
- );
183
- reloadPrivacyRules();
184
-
185
- expect(applyPrivacy("hello")).toBe("hello");
186
- });
187
-
188
- it("returns original text for array JSON", async () => {
189
- await writeFile(
190
- join(TEST_DATA_DIR, "privacy.json"),
191
- JSON.stringify(["a", "b"]),
192
- "utf-8",
193
- );
194
- reloadPrivacyRules();
195
-
196
- expect(applyPrivacy("hello")).toBe("hello");
197
- });
198
- });
1
+ import { describe, it, expect, beforeEach, afterEach, afterAll, vi } from "vitest";
2
+ import { mkdtemp, rm, writeFile } from "node:fs/promises";
3
+ import { tmpdir } from "node:os";
4
+ import { join } from "node:path";
5
+
6
+ const TEST_DATA_DIR = await mkdtemp(join(tmpdir(), "chatccc-privacy-test-"));
7
+ vi.mock("../config.ts", async () => {
8
+ const actual = await vi.importActual<typeof import("../config.ts")>("../config.ts");
9
+ return {
10
+ ...actual,
11
+ USER_DATA_DIR: TEST_DATA_DIR,
12
+ ts: () => "test-ts",
13
+ };
14
+ });
15
+
16
+ let applyPrivacy: (text: string) => string;
17
+ let reloadPrivacyRules: () => void;
18
+ let getPrivacyRules: () => Record<string, string>;
19
+ let getPrivacyConfig: () => { enabled: boolean; rules: Record<string, string> };
20
+
21
+ beforeEach(async () => {
22
+ vi.resetModules();
23
+ try {
24
+ await rm(join(TEST_DATA_DIR, "privacy.json"), { force: true });
25
+ } catch {}
26
+ const mod = await import("../privacy.ts");
27
+ applyPrivacy = mod.applyPrivacy;
28
+ reloadPrivacyRules = mod.reloadPrivacyRules;
29
+ getPrivacyRules = mod.getPrivacyRules;
30
+ getPrivacyConfig = mod.getPrivacyConfig;
31
+ });
32
+
33
+ afterEach(async () => {
34
+ try {
35
+ await rm(join(TEST_DATA_DIR, "privacy.json"), { force: true });
36
+ } catch {}
37
+ });
38
+
39
+ afterAll(async () => {
40
+ try {
41
+ await rm(TEST_DATA_DIR, { recursive: true, force: true });
42
+ } catch {}
43
+ });
44
+
45
+ describe("applyPrivacy", () => {
46
+ it("returns original text when privacy.json is missing", () => {
47
+ reloadPrivacyRules();
48
+ expect(applyPrivacy("hello weizhangjian")).toBe("hello weizhangjian");
49
+ });
50
+
51
+ it("supports legacy flat privacy rules", async () => {
52
+ await writeFile(
53
+ join(TEST_DATA_DIR, "privacy.json"),
54
+ JSON.stringify({ weizhangjian: "wzj", secret: "***" }),
55
+ "utf-8",
56
+ );
57
+ reloadPrivacyRules();
58
+
59
+ expect(applyPrivacy("hello weizhangjian")).toBe("hello wzj");
60
+ expect(applyPrivacy("my secret is safe")).toBe("my *** is safe");
61
+ expect(applyPrivacy("weizhangjian and secret")).toBe("wzj and ***");
62
+ });
63
+
64
+ it("supports privacy.json schema with enabled=false", async () => {
65
+ await writeFile(
66
+ join(TEST_DATA_DIR, "privacy.json"),
67
+ JSON.stringify({ enabled: false, rules: { weizhangjian: "wzj" } }),
68
+ "utf-8",
69
+ );
70
+ reloadPrivacyRules();
71
+
72
+ expect(getPrivacyConfig()).toEqual({ enabled: false, rules: { weizhangjian: "wzj" } });
73
+ expect(getPrivacyRules()).toEqual({ weizhangjian: "wzj" });
74
+ expect(applyPrivacy("hello weizhangjian")).toBe("hello weizhangjian");
75
+ });
76
+
77
+ it("supports privacy.json schema with enabled=true", async () => {
78
+ await writeFile(
79
+ join(TEST_DATA_DIR, "privacy.json"),
80
+ JSON.stringify({ enabled: true, rules: { weizhangjian: "wzj" } }),
81
+ "utf-8",
82
+ );
83
+ reloadPrivacyRules();
84
+
85
+ expect(applyPrivacy("hello weizhangjian")).toBe("hello wzj");
86
+ });
87
+
88
+ it("accepts UTF-8 BOM in privacy.json", async () => {
89
+ await writeFile(
90
+ join(TEST_DATA_DIR, "privacy.json"),
91
+ `\uFEFF${JSON.stringify({ enabled: false, rules: { weizhangjian: "wzj" } })}`,
92
+ "utf-8",
93
+ );
94
+ reloadPrivacyRules();
95
+
96
+ expect(getPrivacyConfig()).toEqual({ enabled: false, rules: { weizhangjian: "wzj" } });
97
+ expect(applyPrivacy("hello weizhangjian")).toBe("hello weizhangjian");
98
+ });
99
+
100
+ it("auto reloads privacy.json changes without explicit reload", async () => {
101
+ await writeFile(
102
+ join(TEST_DATA_DIR, "privacy.json"),
103
+ JSON.stringify({ weizhangjian: "wzj" }),
104
+ "utf-8",
105
+ );
106
+ reloadPrivacyRules();
107
+
108
+ expect(applyPrivacy("hello weizhangjian")).toBe("hello wzj");
109
+
110
+ await writeFile(
111
+ join(TEST_DATA_DIR, "privacy.json"),
112
+ JSON.stringify({ enabled: false, rules: { weizhangjian: "wzj-disabled" } }),
113
+ "utf-8",
114
+ );
115
+
116
+ expect(applyPrivacy("hello weizhangjian")).toBe("hello weizhangjian");
117
+ expect(getPrivacyConfig()).toEqual({ enabled: false, rules: { weizhangjian: "wzj-disabled" } });
118
+ });
119
+
120
+ it("replaces multiple rules and repeated occurrences", async () => {
121
+ await writeFile(
122
+ join(TEST_DATA_DIR, "privacy.json"),
123
+ JSON.stringify({ a: "A", b: "B" }),
124
+ "utf-8",
125
+ );
126
+ reloadPrivacyRules();
127
+
128
+ expect(applyPrivacy("a b a b")).toBe("A B A B");
129
+ });
130
+
131
+ it("returns empty text directly", async () => {
132
+ await writeFile(
133
+ join(TEST_DATA_DIR, "privacy.json"),
134
+ JSON.stringify({ x: "y" }),
135
+ "utf-8",
136
+ );
137
+ reloadPrivacyRules();
138
+
139
+ expect(applyPrivacy("")).toBe("");
140
+ });
141
+
142
+ it("treats special characters in rule keys literally", async () => {
143
+ await writeFile(
144
+ join(TEST_DATA_DIR, "privacy.json"),
145
+ JSON.stringify({ "a.b": "X", "(test)": "Y", "*star": "Z" }),
146
+ "utf-8",
147
+ );
148
+ reloadPrivacyRules();
149
+
150
+ expect(applyPrivacy("hello a.b world")).toBe("hello X world");
151
+ expect(applyPrivacy("text (test) here")).toBe("text Y here");
152
+ expect(applyPrivacy("a *star shines")).toBe("a Z shines");
153
+ });
154
+
155
+ it("reloadPrivacyRules forces a reload", async () => {
156
+ await writeFile(
157
+ join(TEST_DATA_DIR, "privacy.json"),
158
+ JSON.stringify({ old: "OLD" }),
159
+ "utf-8",
160
+ );
161
+ reloadPrivacyRules();
162
+
163
+ expect(applyPrivacy("old")).toBe("OLD");
164
+ expect(getPrivacyRules()).toEqual({ old: "OLD" });
165
+
166
+ await writeFile(
167
+ join(TEST_DATA_DIR, "privacy.json"),
168
+ JSON.stringify({ new: "NEW" }),
169
+ "utf-8",
170
+ );
171
+ reloadPrivacyRules();
172
+
173
+ expect(applyPrivacy("new")).toBe("NEW");
174
+ expect(getPrivacyRules()).toEqual({ new: "NEW" });
175
+ });
176
+
177
+ it("returns original text for malformed JSON", async () => {
178
+ await writeFile(
179
+ join(TEST_DATA_DIR, "privacy.json"),
180
+ "not json",
181
+ "utf-8",
182
+ );
183
+ reloadPrivacyRules();
184
+
185
+ expect(applyPrivacy("hello")).toBe("hello");
186
+ });
187
+
188
+ it("returns original text for array JSON", async () => {
189
+ await writeFile(
190
+ join(TEST_DATA_DIR, "privacy.json"),
191
+ JSON.stringify(["a", "b"]),
192
+ "utf-8",
193
+ );
194
+ reloadPrivacyRules();
195
+
196
+ expect(applyPrivacy("hello")).toBe("hello");
197
+ });
198
+ });