chatccc 0.2.188 → 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 +14 -14
- 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 -33
- 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 -165
- 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 +4 -4
- package/src/__tests__/config-sample.test.ts +17 -17
- package/src/__tests__/feishu-api.test.ts +60 -60
- package/src/__tests__/feishu-platform.test.ts +22 -22
- package/src/__tests__/format-message.test.ts +47 -47
- package/src/__tests__/orchestrator.test.ts +112 -112
- package/src/__tests__/privacy.test.ts +198 -198
- package/src/__tests__/raw-stream-log.test.ts +106 -106
- package/src/__tests__/session.test.ts +17 -17
- 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 +27 -28
- 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 -189
- package/src/builtin/index.ts +168 -168
- package/src/cards.ts +137 -137
- package/src/chatgpt-subscription-rpc.ts +27 -27
- package/src/chatgpt-subscription.ts +299 -299
- package/src/chrome-devtools-guard.ts +318 -318
- package/src/codex-reset-actions.ts +184 -184
- package/src/config.ts +86 -86
- package/src/feishu-platform.ts +20 -20
- package/src/format-message.ts +293 -293
- package/src/litellm-proxy.ts +374 -374
- package/src/orchestrator.ts +143 -143
- 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 +98 -98
- 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
|
@@ -77,11 +77,11 @@ 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
86
|
import { ABD_APPEND_PROMPT } from "../shared-prefix.ts";
|
|
87
87
|
|
|
@@ -363,11 +363,11 @@ describe("handleCommand WeChat processing ack", () => {
|
|
|
363
363
|
expect(userText).not.toContain("/abd");
|
|
364
364
|
});
|
|
365
365
|
|
|
366
|
-
it("cleans stale Feishu p2p binding but keeps valid commands from auto-creating a group", async () => {
|
|
367
|
-
const platform = mockPlatform("feishu");
|
|
368
|
-
await recordSessionRegistry({
|
|
369
|
-
chatId: "feishu-p2p",
|
|
370
|
-
sessionId: "stale-sid",
|
|
366
|
+
it("cleans stale Feishu p2p binding but keeps valid commands from auto-creating a group", async () => {
|
|
367
|
+
const platform = mockPlatform("feishu");
|
|
368
|
+
await recordSessionRegistry({
|
|
369
|
+
chatId: "feishu-p2p",
|
|
370
|
+
sessionId: "stale-sid",
|
|
371
371
|
tool: "claude",
|
|
372
372
|
chatName: "旧私聊绑定",
|
|
373
373
|
running: false,
|
|
@@ -377,108 +377,108 @@ describe("handleCommand WeChat processing ack", () => {
|
|
|
377
377
|
|
|
378
378
|
expect(platform.createGroup).not.toHaveBeenCalled();
|
|
379
379
|
expect(platform.sendRawCard).toHaveBeenCalled();
|
|
380
|
-
const registry = await loadSessionRegistryForBinding();
|
|
381
|
-
expect(registry["feishu-p2p"]).toBeUndefined();
|
|
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
|
-
|
|
480
|
-
it("handles /usage without creating a new Feishu group", async () => {
|
|
481
|
-
const platform = mockPlatform("feishu");
|
|
380
|
+
const registry = await loadSessionRegistryForBinding();
|
|
381
|
+
expect(registry["feishu-p2p"]).toBeUndefined();
|
|
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
|
+
|
|
480
|
+
it("handles /usage without creating a new Feishu group", async () => {
|
|
481
|
+
const platform = mockPlatform("feishu");
|
|
482
482
|
const usage = {
|
|
483
483
|
fiveHour: { usedPercent: 37, remainingPercent: 63, resetAtEpochSeconds: 1781528212, resetAfterSeconds: 10349 },
|
|
484
484
|
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
|
+
});
|