chatccc 0.2.187 → 0.2.189
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/agent-prompts/claude_specific.md +45 -45
- package/agent-prompts/codex_specific.md +2 -2
- package/agent-prompts/cursor_specific.md +13 -13
- package/config.sample.json +5 -0
- package/im-skills/feishu-skill/receive-send-file.md +63 -63
- package/im-skills/feishu-skill/receive-send-image.md +24 -24
- package/im-skills/feishu-skill/skill.md +3 -3
- package/im-skills/wechat-file-skill/receive-send-file.md +38 -38
- package/im-skills/wechat-file-skill/send-file.mjs +83 -83
- package/im-skills/wechat-file-skill/skill.md +10 -10
- package/im-skills/wechat-image-skill/skill.md +10 -10
- package/im-skills/wechat-video-skill/receive-send-video.md +38 -38
- package/im-skills/wechat-video-skill/send-video.mjs +79 -79
- package/im-skills/wechat-video-skill/skill.md +10 -10
- package/package.json +1 -1
- package/scripts/postinstall-sharp-check.mjs +58 -58
- package/src/__tests__/agent-delegate-task-rpc.test.ts +165 -165
- package/src/__tests__/builtin-config.test.ts +33 -0
- package/src/__tests__/card-plain-text.test.ts +5 -5
- package/src/__tests__/cardkit.test.ts +60 -60
- package/src/__tests__/cards.test.ts +77 -77
- package/src/__tests__/chatgpt-subscription-rpc.test.ts +89 -89
- package/src/__tests__/chatgpt-subscription.test.ts +135 -135
- package/src/__tests__/chrome-devtools-guard.test.ts +165 -125
- package/src/__tests__/claude-adapter.test.ts +592 -592
- package/src/__tests__/codex-reset-actions.test.ts +146 -146
- package/src/__tests__/config-reload.test.ts +1 -0
- package/src/__tests__/config-sample.test.ts +11 -0
- package/src/__tests__/feishu-api.test.ts +60 -60
- package/src/__tests__/feishu-avatar.test.ts +180 -115
- package/src/__tests__/feishu-platform.test.ts +22 -22
- package/src/__tests__/format-message.test.ts +47 -47
- package/src/__tests__/orchestrator.test.ts +150 -2
- package/src/__tests__/privacy.test.ts +198 -198
- package/src/__tests__/raw-stream-log.test.ts +106 -106
- package/src/__tests__/session.test.ts +10 -0
- package/src/__tests__/shared-prefix.test.ts +36 -36
- package/src/__tests__/stream-state.test.ts +42 -42
- package/src/__tests__/web-ui.test.ts +209 -130
- package/src/adapters/claude-adapter.ts +566 -566
- package/src/adapters/claude-session-meta-store.ts +120 -120
- package/src/adapters/codex-adapter.ts +10 -6
- package/src/adapters/cursor-adapter.ts +46 -46
- package/src/adapters/raw-stream-log.ts +124 -124
- package/src/adapters/resource-monitor.ts +140 -140
- package/src/agent-delegate-task-rpc.ts +153 -153
- package/src/agent-delegate-task.ts +81 -81
- package/src/agent-stop-stuck.ts +129 -129
- package/src/builtin/cli.ts +189 -197
- package/src/builtin/index.ts +168 -167
- package/src/cards.ts +130 -89
- package/src/chatgpt-subscription-rpc.ts +27 -27
- package/src/chatgpt-subscription.ts +299 -299
- package/src/chrome-devtools-guard.ts +318 -242
- package/src/codex-reset-actions.ts +184 -184
- package/src/config.ts +38 -0
- package/src/feishu-api.ts +219 -190
- package/src/feishu-platform.ts +20 -20
- package/src/format-message.ts +293 -293
- package/src/index.ts +2 -2
- package/src/litellm-proxy.ts +374 -374
- package/src/orchestrator.ts +201 -9
- package/src/platform-adapter.ts +9 -1
- package/src/privacy.ts +118 -118
- package/src/session-chat-binding.ts +6 -6
- package/src/session-name.ts +8 -8
- package/src/session.ts +44 -16
- package/src/shared-prefix.ts +29 -29
- package/src/sim-platform.ts +20 -20
- package/src/turn-cards.ts +117 -117
- package/src/web-ui.ts +142 -24
|
@@ -80,6 +80,7 @@ import {
|
|
|
80
80
|
loadSessionRegistryForBinding,
|
|
81
81
|
recordSessionRegistry,
|
|
82
82
|
resetState,
|
|
83
|
+
sessionInfoMap,
|
|
83
84
|
} from "../session.ts";
|
|
84
85
|
import { activePrompts, resetBindingState } from "../session-chat-binding.ts";
|
|
85
86
|
import { ABD_APPEND_PROMPT } from "../shared-prefix.ts";
|
|
@@ -380,9 +381,105 @@ describe("handleCommand WeChat processing ack", () => {
|
|
|
380
381
|
expect(registry["feishu-p2p"]).toBeUndefined();
|
|
381
382
|
});
|
|
382
383
|
|
|
384
|
+
it("shows Claude effort switch card in an active Feishu session", async () => {
|
|
385
|
+
const platform = mockPlatform("feishu");
|
|
386
|
+
vi.mocked(platform.getChatInfo).mockResolvedValue({ name: "claude-session", description: "Claude Session: sid-claude-effort" });
|
|
387
|
+
vi.mocked(platform.extractSessionInfo).mockReturnValue({ sessionId: "sid-claude-effort", tool: "claude" });
|
|
388
|
+
await recordSessionRegistry({
|
|
389
|
+
chatId: "feishu-chat",
|
|
390
|
+
sessionId: "sid-claude-effort",
|
|
391
|
+
tool: "claude",
|
|
392
|
+
chatName: "claude-session",
|
|
393
|
+
running: false,
|
|
394
|
+
});
|
|
395
|
+
sessionInfoMap.set("feishu-chat", {
|
|
396
|
+
sessionId: "sid-claude-effort",
|
|
397
|
+
tool: "claude",
|
|
398
|
+
turnCount: 0,
|
|
399
|
+
lastContextTokens: 0,
|
|
400
|
+
startTime: Date.now(),
|
|
401
|
+
});
|
|
402
|
+
|
|
403
|
+
await handleCommand(platform, "/effort", "feishu-chat", "ou-user", Date.now(), "group");
|
|
404
|
+
|
|
405
|
+
expect(platform.sendRawCard).toHaveBeenCalled();
|
|
406
|
+
const card = JSON.parse(vi.mocked(platform.sendRawCard).mock.calls[0][1]);
|
|
407
|
+
const raw = JSON.stringify(card);
|
|
408
|
+
expect(raw).toContain("/effort low");
|
|
409
|
+
expect(raw).toContain("/effort xhigh");
|
|
410
|
+
expect(raw).toContain("/effort max");
|
|
411
|
+
});
|
|
412
|
+
|
|
413
|
+
it("switches Codex effort for the current session and reflects it in /state", async () => {
|
|
414
|
+
const platform = mockPlatform("feishu");
|
|
415
|
+
vi.mocked(platform.getChatInfo).mockResolvedValue({ name: "codex-session", description: "Codex Session: sid-codex-effort" });
|
|
416
|
+
vi.mocked(platform.extractSessionInfo).mockReturnValue({ sessionId: "sid-codex-effort", tool: "codex" });
|
|
417
|
+
_setAdapterForToolForTest("codex", mockAdapter("sid-codex-effort"));
|
|
418
|
+
await recordSessionRegistry({
|
|
419
|
+
chatId: "codex-chat",
|
|
420
|
+
sessionId: "sid-codex-effort",
|
|
421
|
+
tool: "codex",
|
|
422
|
+
chatName: "codex-session",
|
|
423
|
+
running: false,
|
|
424
|
+
});
|
|
425
|
+
sessionInfoMap.set("codex-chat", {
|
|
426
|
+
sessionId: "sid-codex-effort",
|
|
427
|
+
tool: "codex",
|
|
428
|
+
turnCount: 0,
|
|
429
|
+
lastContextTokens: 0,
|
|
430
|
+
startTime: Date.now(),
|
|
431
|
+
});
|
|
432
|
+
|
|
433
|
+
await handleCommand(platform, "/effort xhigh", "codex-chat", "ou-user", Date.now(), "group");
|
|
434
|
+
expect(platform.sendCard).toHaveBeenCalledWith(
|
|
435
|
+
"codex-chat",
|
|
436
|
+
"Effort 切换",
|
|
437
|
+
expect.stringContaining("xhigh"),
|
|
438
|
+
"green",
|
|
439
|
+
);
|
|
440
|
+
|
|
441
|
+
vi.mocked(platform.sendCard).mockClear();
|
|
442
|
+
vi.mocked(platform.sendRawCard).mockClear();
|
|
443
|
+
await handleCommand(platform, "/state", "codex-chat", "ou-user", Date.now() + 1, "group");
|
|
444
|
+
|
|
445
|
+
expect(platform.sendRawCard).toHaveBeenCalled();
|
|
446
|
+
const card = JSON.parse(vi.mocked(platform.sendRawCard).mock.calls[0][1]);
|
|
447
|
+
expect(JSON.stringify(card)).toContain("xhigh");
|
|
448
|
+
});
|
|
449
|
+
|
|
450
|
+
it("rejects /effort in Cursor sessions", async () => {
|
|
451
|
+
const platform = mockPlatform("feishu");
|
|
452
|
+
vi.mocked(platform.getChatInfo).mockResolvedValue({ name: "cursor-session", description: "Cursor Session: sid-cursor-effort" });
|
|
453
|
+
vi.mocked(platform.extractSessionInfo).mockReturnValue({ sessionId: "sid-cursor-effort", tool: "cursor" });
|
|
454
|
+
_setAdapterForToolForTest("cursor", mockAdapter("sid-cursor-effort"));
|
|
455
|
+
await recordSessionRegistry({
|
|
456
|
+
chatId: "cursor-chat",
|
|
457
|
+
sessionId: "sid-cursor-effort",
|
|
458
|
+
tool: "cursor",
|
|
459
|
+
chatName: "cursor-session",
|
|
460
|
+
running: false,
|
|
461
|
+
});
|
|
462
|
+
sessionInfoMap.set("cursor-chat", {
|
|
463
|
+
sessionId: "sid-cursor-effort",
|
|
464
|
+
tool: "cursor",
|
|
465
|
+
turnCount: 0,
|
|
466
|
+
lastContextTokens: 0,
|
|
467
|
+
startTime: Date.now(),
|
|
468
|
+
});
|
|
469
|
+
|
|
470
|
+
await handleCommand(platform, "/effort high", "cursor-chat", "ou-user", Date.now(), "group");
|
|
471
|
+
|
|
472
|
+
expect(platform.sendCard).toHaveBeenCalledWith(
|
|
473
|
+
"cursor-chat",
|
|
474
|
+
"Effort 切换",
|
|
475
|
+
expect.stringContaining("不支持 effort"),
|
|
476
|
+
"red",
|
|
477
|
+
);
|
|
478
|
+
});
|
|
479
|
+
|
|
383
480
|
it("handles /usage without creating a new Feishu group", async () => {
|
|
384
481
|
const platform = mockPlatform("feishu");
|
|
385
|
-
|
|
482
|
+
const usage = {
|
|
386
483
|
fiveHour: { usedPercent: 37, remainingPercent: 63, resetAtEpochSeconds: 1781528212, resetAfterSeconds: 10349 },
|
|
387
484
|
weekly: { usedPercent: 12, remainingPercent: 88, resetAtEpochSeconds: 1781842926, resetAfterSeconds: 325063 },
|
|
388
485
|
rateLimitResetCreditsAvailable: 2,
|
|
@@ -390,7 +487,8 @@ describe("handleCommand WeChat processing ack", () => {
|
|
|
390
487
|
{ grantedAt: "2026-06-12T04:01:47.770016Z", expiresAt: "2026-07-12T04:01:47.770016Z" },
|
|
391
488
|
{ grantedAt: "2026-06-18T00:44:23.904386Z", expiresAt: "2026-07-18T00:44:23.904386Z" },
|
|
392
489
|
],
|
|
393
|
-
}
|
|
490
|
+
};
|
|
491
|
+
mockGetCodexUsageSummary.mockResolvedValue(usage);
|
|
394
492
|
|
|
395
493
|
await handleCommand(platform, "/usage", "feishu-p2p", "ou-user", Date.now(), "p2p");
|
|
396
494
|
|
|
@@ -411,6 +509,7 @@ describe("handleCommand WeChat processing ack", () => {
|
|
|
411
509
|
expect(card.elements[0].text.content).toContain("[██░░░░░░░░░░░░░░░░░░]");
|
|
412
510
|
expect(card.elements[2].actions[0].text.content).toBe("发起重置");
|
|
413
511
|
expect(card.elements[2].actions[0].value).toEqual({ action: "codex_reset_request", availableCount: 2 });
|
|
512
|
+
expect(platform.setChatAvatar).toHaveBeenCalledWith("feishu-p2p", "codex", "idle", { codexUsage: usage });
|
|
414
513
|
});
|
|
415
514
|
|
|
416
515
|
it("adds ChatGPT subscription expiry to Codex /usage when CDP lookup succeeds", async () => {
|
|
@@ -445,6 +544,24 @@ describe("handleCommand WeChat processing ack", () => {
|
|
|
445
544
|
expect(card.elements[0].text.content).toContain("- 自动续费: 否");
|
|
446
545
|
});
|
|
447
546
|
|
|
547
|
+
it("shows an actionable ChatGPT subscription failure reason when Chrome CDP is enabled", async () => {
|
|
548
|
+
const platform = mockPlatform("feishu");
|
|
549
|
+
mockGetChatGptSubscriptionStatus.mockResolvedValue({
|
|
550
|
+
ok: false,
|
|
551
|
+
code: "chatgpt_session_missing",
|
|
552
|
+
reason: "ChatGPT browser session has no access token.",
|
|
553
|
+
chromeCdp: { enabled: true, port: 15166, status: "healthy" },
|
|
554
|
+
chatgpt: { sessionOk: true },
|
|
555
|
+
});
|
|
556
|
+
|
|
557
|
+
await handleCommand(platform, "/usage", "feishu-p2p", "ou-user", Date.now(), "p2p");
|
|
558
|
+
|
|
559
|
+
const card = JSON.parse(vi.mocked(platform.sendRawCard).mock.calls[0][1]);
|
|
560
|
+
expect(card.elements[0].text.content).toContain("**ChatGPT 订阅查询失败:**");
|
|
561
|
+
expect(card.elements[0].text.content).toContain("请在 15166 端口对应的 Chrome 浏览器中登录 ChatGPT");
|
|
562
|
+
expect(card.elements[0].text.content).toContain("ChatGPT browser session has no access token.");
|
|
563
|
+
});
|
|
564
|
+
|
|
448
565
|
it("handles /usage as Cursor usage in Cursor chats", async () => {
|
|
449
566
|
const platform = mockPlatform("feishu");
|
|
450
567
|
await recordSessionRegistry({
|
|
@@ -472,6 +589,37 @@ describe("handleCommand WeChat processing ack", () => {
|
|
|
472
589
|
expect.stringContaining("Pool remaining: $171417.76"),
|
|
473
590
|
"blue",
|
|
474
591
|
);
|
|
592
|
+
expect(platform.setChatAvatar).toHaveBeenCalledWith(
|
|
593
|
+
"cursor-chat",
|
|
594
|
+
"cursor",
|
|
595
|
+
"idle",
|
|
596
|
+
{ cursorUsage: expect.objectContaining({ displayMessage: "You've hit your usage limit" }) },
|
|
597
|
+
);
|
|
598
|
+
});
|
|
599
|
+
|
|
600
|
+
it("keeps the busy avatar status when /usage runs for an active Cursor session", async () => {
|
|
601
|
+
const platform = mockPlatform("feishu");
|
|
602
|
+
await recordSessionRegistry({
|
|
603
|
+
chatId: "cursor-chat",
|
|
604
|
+
sessionId: "sid-cursor",
|
|
605
|
+
tool: "cursor",
|
|
606
|
+
chatName: "cursor-session",
|
|
607
|
+
running: true,
|
|
608
|
+
});
|
|
609
|
+
activePrompts.set("sid-cursor", {
|
|
610
|
+
controller: new AbortController(),
|
|
611
|
+
stopped: false,
|
|
612
|
+
startTime: Date.now(),
|
|
613
|
+
});
|
|
614
|
+
|
|
615
|
+
await handleCommand(platform, "/usage", "cursor-chat", "ou-user", Date.now(), "group");
|
|
616
|
+
|
|
617
|
+
expect(platform.setChatAvatar).toHaveBeenCalledWith(
|
|
618
|
+
"cursor-chat",
|
|
619
|
+
"cursor",
|
|
620
|
+
"busy",
|
|
621
|
+
{ cursorUsage: expect.objectContaining({ displayMessage: "You've hit your usage limit" }) },
|
|
622
|
+
);
|
|
475
623
|
});
|
|
476
624
|
|
|
477
625
|
it("advertises /usage in new Codex and Cursor session ready messages", async () => {
|
|
@@ -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
|
+
});
|