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
|
@@ -1,146 +1,146 @@
|
|
|
1
|
-
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
-
|
|
3
|
-
import {
|
|
4
|
-
_resetCodexResetRequestsForTest,
|
|
5
|
-
handleCodexResetCardAction,
|
|
6
|
-
type CodexResetActionDeps,
|
|
7
|
-
} from "../codex-reset-actions.ts";
|
|
8
|
-
|
|
9
|
-
function deps(): CodexResetActionDeps & {
|
|
10
|
-
getTenantAccessToken: ReturnType<typeof vi.fn>;
|
|
11
|
-
sendRawCard: ReturnType<typeof vi.fn>;
|
|
12
|
-
sendTextReply: ReturnType<typeof vi.fn>;
|
|
13
|
-
sendCardReply: ReturnType<typeof vi.fn>;
|
|
14
|
-
updateCardMessage: ReturnType<typeof vi.fn>;
|
|
15
|
-
recallMessage: ReturnType<typeof vi.fn>;
|
|
16
|
-
consumeCodexRateLimitResetCredit: ReturnType<typeof vi.fn>;
|
|
17
|
-
createRequestId: ReturnType<typeof vi.fn>;
|
|
18
|
-
} {
|
|
19
|
-
return {
|
|
20
|
-
getTenantAccessToken: vi.fn(async () => "tenant-token"),
|
|
21
|
-
sendRawCard: vi.fn(async () => true),
|
|
22
|
-
sendTextReply: vi.fn(async () => true),
|
|
23
|
-
sendCardReply: vi.fn(async () => true),
|
|
24
|
-
updateCardMessage: vi.fn(async () => true),
|
|
25
|
-
recallMessage: vi.fn(async () => true),
|
|
26
|
-
consumeCodexRateLimitResetCredit: vi.fn(async () => ({ code: "reset" as const, windowsReset: 2 })),
|
|
27
|
-
createRequestId: vi.fn(() => "request-1"),
|
|
28
|
-
};
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
function event(value: Record<string, unknown>, messageId: string) {
|
|
32
|
-
return {
|
|
33
|
-
event: {
|
|
34
|
-
open_message_id: messageId,
|
|
35
|
-
open_chat_id: "chat-1",
|
|
36
|
-
operator: { open_id: "ou-user" },
|
|
37
|
-
action: { value },
|
|
38
|
-
},
|
|
39
|
-
};
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
describe("Codex reset card actions", () => {
|
|
43
|
-
beforeEach(() => {
|
|
44
|
-
_resetCodexResetRequestsForTest();
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
it("sends a confirmation card tied to the clicked usage card", async () => {
|
|
48
|
-
const d = deps();
|
|
49
|
-
|
|
50
|
-
await expect(handleCodexResetCardAction(
|
|
51
|
-
event({ action: "codex_reset_request" }, "usage-message"),
|
|
52
|
-
d,
|
|
53
|
-
)).resolves.toBe(true);
|
|
54
|
-
|
|
55
|
-
expect(d.sendRawCard).toHaveBeenCalledTimes(1);
|
|
56
|
-
expect(d.sendRawCard.mock.calls[0][1]).toBe("chat-1");
|
|
57
|
-
const confirmCard = JSON.parse(d.sendRawCard.mock.calls[0][2]);
|
|
58
|
-
const action = confirmCard.elements.find((element: any) => element.tag === "action");
|
|
59
|
-
expect(action.actions[0].value).toEqual({
|
|
60
|
-
action: "codex_reset_confirm",
|
|
61
|
-
decision: "yes",
|
|
62
|
-
parentMessageId: "usage-message",
|
|
63
|
-
requestId: "request-1",
|
|
64
|
-
});
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
it("consumes a reset credit, sends the result, and collapses the confirmation card when user confirms", async () => {
|
|
68
|
-
const d = deps();
|
|
69
|
-
await handleCodexResetCardAction(event({ action: "codex_reset_request" }, "usage-message"), d);
|
|
70
|
-
|
|
71
|
-
await expect(handleCodexResetCardAction(
|
|
72
|
-
event({
|
|
73
|
-
action: "codex_reset_confirm",
|
|
74
|
-
decision: "yes",
|
|
75
|
-
parentMessageId: "usage-message",
|
|
76
|
-
requestId: "request-1",
|
|
77
|
-
}, "confirm-message"),
|
|
78
|
-
d,
|
|
79
|
-
)).resolves.toBe(true);
|
|
80
|
-
|
|
81
|
-
expect(d.consumeCodexRateLimitResetCredit).toHaveBeenCalledWith("request-1");
|
|
82
|
-
expect(d.updateCardMessage).toHaveBeenCalledTimes(1);
|
|
83
|
-
expect(d.updateCardMessage.mock.calls[0][1]).toBe("confirm-message");
|
|
84
|
-
expect(JSON.parse(d.updateCardMessage.mock.calls[0][2]).elements).toEqual([
|
|
85
|
-
{ tag: "markdown", content: " " },
|
|
86
|
-
]);
|
|
87
|
-
expect(d.recallMessage).toHaveBeenCalledWith("tenant-token", "confirm-message");
|
|
88
|
-
expect(d.sendTextReply).toHaveBeenCalledWith(
|
|
89
|
-
"tenant-token",
|
|
90
|
-
"chat-1",
|
|
91
|
-
expect.stringContaining("重置成功"),
|
|
92
|
-
);
|
|
93
|
-
expect(d.sendCardReply).not.toHaveBeenCalled();
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
it("does not consume a reset credit and sends a cancellation message when user declines", async () => {
|
|
97
|
-
const d = deps();
|
|
98
|
-
await handleCodexResetCardAction(event({ action: "codex_reset_request" }, "usage-message"), d);
|
|
99
|
-
|
|
100
|
-
await expect(handleCodexResetCardAction(
|
|
101
|
-
event({
|
|
102
|
-
action: "codex_reset_confirm",
|
|
103
|
-
decision: "no",
|
|
104
|
-
parentMessageId: "usage-message",
|
|
105
|
-
requestId: "request-1",
|
|
106
|
-
}, "confirm-message"),
|
|
107
|
-
d,
|
|
108
|
-
)).resolves.toBe(true);
|
|
109
|
-
|
|
110
|
-
expect(d.consumeCodexRateLimitResetCredit).not.toHaveBeenCalled();
|
|
111
|
-
expect(d.updateCardMessage).toHaveBeenCalledTimes(1);
|
|
112
|
-
expect(d.recallMessage).toHaveBeenCalledWith("tenant-token", "confirm-message");
|
|
113
|
-
expect(d.sendTextReply).toHaveBeenCalledWith(
|
|
114
|
-
"tenant-token",
|
|
115
|
-
"chat-1",
|
|
116
|
-
"Codex 主动重置:用户取消了重置。",
|
|
117
|
-
);
|
|
118
|
-
expect(d.sendCardReply).not.toHaveBeenCalled();
|
|
119
|
-
});
|
|
120
|
-
|
|
121
|
-
it("can collapse a confirmation card using context.message_id fallback", async () => {
|
|
122
|
-
const d = deps();
|
|
123
|
-
await handleCodexResetCardAction(event({ action: "codex_reset_request" }, "usage-message"), d);
|
|
124
|
-
|
|
125
|
-
await expect(handleCodexResetCardAction({
|
|
126
|
-
event: {
|
|
127
|
-
context: {
|
|
128
|
-
message_id: "confirm-message-from-context",
|
|
129
|
-
open_chat_id: "chat-1",
|
|
130
|
-
},
|
|
131
|
-
action: {
|
|
132
|
-
value: {
|
|
133
|
-
action: "codex_reset_confirm",
|
|
134
|
-
decision: "no",
|
|
135
|
-
parentMessageId: "usage-message",
|
|
136
|
-
requestId: "request-1",
|
|
137
|
-
},
|
|
138
|
-
},
|
|
139
|
-
},
|
|
140
|
-
}, d)).resolves.toBe(true);
|
|
141
|
-
|
|
142
|
-
expect(d.updateCardMessage).toHaveBeenCalledTimes(1);
|
|
143
|
-
expect(d.updateCardMessage.mock.calls[0][1]).toBe("confirm-message-from-context");
|
|
144
|
-
expect(d.recallMessage).toHaveBeenCalledWith("tenant-token", "confirm-message-from-context");
|
|
145
|
-
});
|
|
146
|
-
});
|
|
1
|
+
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
_resetCodexResetRequestsForTest,
|
|
5
|
+
handleCodexResetCardAction,
|
|
6
|
+
type CodexResetActionDeps,
|
|
7
|
+
} from "../codex-reset-actions.ts";
|
|
8
|
+
|
|
9
|
+
function deps(): CodexResetActionDeps & {
|
|
10
|
+
getTenantAccessToken: ReturnType<typeof vi.fn>;
|
|
11
|
+
sendRawCard: ReturnType<typeof vi.fn>;
|
|
12
|
+
sendTextReply: ReturnType<typeof vi.fn>;
|
|
13
|
+
sendCardReply: ReturnType<typeof vi.fn>;
|
|
14
|
+
updateCardMessage: ReturnType<typeof vi.fn>;
|
|
15
|
+
recallMessage: ReturnType<typeof vi.fn>;
|
|
16
|
+
consumeCodexRateLimitResetCredit: ReturnType<typeof vi.fn>;
|
|
17
|
+
createRequestId: ReturnType<typeof vi.fn>;
|
|
18
|
+
} {
|
|
19
|
+
return {
|
|
20
|
+
getTenantAccessToken: vi.fn(async () => "tenant-token"),
|
|
21
|
+
sendRawCard: vi.fn(async () => true),
|
|
22
|
+
sendTextReply: vi.fn(async () => true),
|
|
23
|
+
sendCardReply: vi.fn(async () => true),
|
|
24
|
+
updateCardMessage: vi.fn(async () => true),
|
|
25
|
+
recallMessage: vi.fn(async () => true),
|
|
26
|
+
consumeCodexRateLimitResetCredit: vi.fn(async () => ({ code: "reset" as const, windowsReset: 2 })),
|
|
27
|
+
createRequestId: vi.fn(() => "request-1"),
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function event(value: Record<string, unknown>, messageId: string) {
|
|
32
|
+
return {
|
|
33
|
+
event: {
|
|
34
|
+
open_message_id: messageId,
|
|
35
|
+
open_chat_id: "chat-1",
|
|
36
|
+
operator: { open_id: "ou-user" },
|
|
37
|
+
action: { value },
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
describe("Codex reset card actions", () => {
|
|
43
|
+
beforeEach(() => {
|
|
44
|
+
_resetCodexResetRequestsForTest();
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("sends a confirmation card tied to the clicked usage card", async () => {
|
|
48
|
+
const d = deps();
|
|
49
|
+
|
|
50
|
+
await expect(handleCodexResetCardAction(
|
|
51
|
+
event({ action: "codex_reset_request" }, "usage-message"),
|
|
52
|
+
d,
|
|
53
|
+
)).resolves.toBe(true);
|
|
54
|
+
|
|
55
|
+
expect(d.sendRawCard).toHaveBeenCalledTimes(1);
|
|
56
|
+
expect(d.sendRawCard.mock.calls[0][1]).toBe("chat-1");
|
|
57
|
+
const confirmCard = JSON.parse(d.sendRawCard.mock.calls[0][2]);
|
|
58
|
+
const action = confirmCard.elements.find((element: any) => element.tag === "action");
|
|
59
|
+
expect(action.actions[0].value).toEqual({
|
|
60
|
+
action: "codex_reset_confirm",
|
|
61
|
+
decision: "yes",
|
|
62
|
+
parentMessageId: "usage-message",
|
|
63
|
+
requestId: "request-1",
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it("consumes a reset credit, sends the result, and collapses the confirmation card when user confirms", async () => {
|
|
68
|
+
const d = deps();
|
|
69
|
+
await handleCodexResetCardAction(event({ action: "codex_reset_request" }, "usage-message"), d);
|
|
70
|
+
|
|
71
|
+
await expect(handleCodexResetCardAction(
|
|
72
|
+
event({
|
|
73
|
+
action: "codex_reset_confirm",
|
|
74
|
+
decision: "yes",
|
|
75
|
+
parentMessageId: "usage-message",
|
|
76
|
+
requestId: "request-1",
|
|
77
|
+
}, "confirm-message"),
|
|
78
|
+
d,
|
|
79
|
+
)).resolves.toBe(true);
|
|
80
|
+
|
|
81
|
+
expect(d.consumeCodexRateLimitResetCredit).toHaveBeenCalledWith("request-1");
|
|
82
|
+
expect(d.updateCardMessage).toHaveBeenCalledTimes(1);
|
|
83
|
+
expect(d.updateCardMessage.mock.calls[0][1]).toBe("confirm-message");
|
|
84
|
+
expect(JSON.parse(d.updateCardMessage.mock.calls[0][2]).elements).toEqual([
|
|
85
|
+
{ tag: "markdown", content: " " },
|
|
86
|
+
]);
|
|
87
|
+
expect(d.recallMessage).toHaveBeenCalledWith("tenant-token", "confirm-message");
|
|
88
|
+
expect(d.sendTextReply).toHaveBeenCalledWith(
|
|
89
|
+
"tenant-token",
|
|
90
|
+
"chat-1",
|
|
91
|
+
expect.stringContaining("重置成功"),
|
|
92
|
+
);
|
|
93
|
+
expect(d.sendCardReply).not.toHaveBeenCalled();
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it("does not consume a reset credit and sends a cancellation message when user declines", async () => {
|
|
97
|
+
const d = deps();
|
|
98
|
+
await handleCodexResetCardAction(event({ action: "codex_reset_request" }, "usage-message"), d);
|
|
99
|
+
|
|
100
|
+
await expect(handleCodexResetCardAction(
|
|
101
|
+
event({
|
|
102
|
+
action: "codex_reset_confirm",
|
|
103
|
+
decision: "no",
|
|
104
|
+
parentMessageId: "usage-message",
|
|
105
|
+
requestId: "request-1",
|
|
106
|
+
}, "confirm-message"),
|
|
107
|
+
d,
|
|
108
|
+
)).resolves.toBe(true);
|
|
109
|
+
|
|
110
|
+
expect(d.consumeCodexRateLimitResetCredit).not.toHaveBeenCalled();
|
|
111
|
+
expect(d.updateCardMessage).toHaveBeenCalledTimes(1);
|
|
112
|
+
expect(d.recallMessage).toHaveBeenCalledWith("tenant-token", "confirm-message");
|
|
113
|
+
expect(d.sendTextReply).toHaveBeenCalledWith(
|
|
114
|
+
"tenant-token",
|
|
115
|
+
"chat-1",
|
|
116
|
+
"Codex 主动重置:用户取消了重置。",
|
|
117
|
+
);
|
|
118
|
+
expect(d.sendCardReply).not.toHaveBeenCalled();
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it("can collapse a confirmation card using context.message_id fallback", async () => {
|
|
122
|
+
const d = deps();
|
|
123
|
+
await handleCodexResetCardAction(event({ action: "codex_reset_request" }, "usage-message"), d);
|
|
124
|
+
|
|
125
|
+
await expect(handleCodexResetCardAction({
|
|
126
|
+
event: {
|
|
127
|
+
context: {
|
|
128
|
+
message_id: "confirm-message-from-context",
|
|
129
|
+
open_chat_id: "chat-1",
|
|
130
|
+
},
|
|
131
|
+
action: {
|
|
132
|
+
value: {
|
|
133
|
+
action: "codex_reset_confirm",
|
|
134
|
+
decision: "no",
|
|
135
|
+
parentMessageId: "usage-message",
|
|
136
|
+
requestId: "request-1",
|
|
137
|
+
},
|
|
138
|
+
},
|
|
139
|
+
},
|
|
140
|
+
}, d)).resolves.toBe(true);
|
|
141
|
+
|
|
142
|
+
expect(d.updateCardMessage).toHaveBeenCalledTimes(1);
|
|
143
|
+
expect(d.updateCardMessage.mock.calls[0][1]).toBe("confirm-message-from-context");
|
|
144
|
+
expect(d.recallMessage).toHaveBeenCalledWith("tenant-token", "confirm-message-from-context");
|
|
145
|
+
});
|
|
146
|
+
});
|
|
@@ -70,6 +70,7 @@ const baseAppConfig: AppConfig = {
|
|
|
70
70
|
onDemandMonthlyBudget: 1000,
|
|
71
71
|
},
|
|
72
72
|
codex: { enabled: true, defaultAgent: false, path: "/initial/codex", model: "initial-codex-model", alternativeModel: "initial-codex-alt-model", effort: "initial-codex-effort" },
|
|
73
|
+
ccc: { DEEPSEEK_API_KEY: "initial-ccc-key", DEEPSEEK_BASE_URL: "https://initial.deepseek.test/v1", model: "initial-ccc-model" },
|
|
73
74
|
};
|
|
74
75
|
|
|
75
76
|
// 把 module 状态抢救快照:每个 it 跑前重置回这个状态,避免污染相邻测试。
|
|
@@ -39,6 +39,17 @@ describe("config.sample.json", () => {
|
|
|
39
39
|
expect(sample.codex?.alternativeModel).toBe("");
|
|
40
40
|
});
|
|
41
41
|
|
|
42
|
+
it("sets ccc agent DeepSeek defaults in the sample config", () => {
|
|
43
|
+
const configSamplePath = join(process.cwd(), "config.sample.json");
|
|
44
|
+
const sample = JSON.parse(readFileSync(configSamplePath, "utf8")) as {
|
|
45
|
+
ccc?: { DEEPSEEK_API_KEY?: unknown; DEEPSEEK_BASE_URL?: unknown; model?: unknown };
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
expect(sample.ccc?.DEEPSEEK_API_KEY).toBe("");
|
|
49
|
+
expect(sample.ccc?.DEEPSEEK_BASE_URL).toBe("https://api.deepseek.com/v1");
|
|
50
|
+
expect(sample.ccc?.model).toBe("deepseek-v4-pro[1m]");
|
|
51
|
+
});
|
|
52
|
+
|
|
42
53
|
it("keeps Chrome CDP guard disabled by default with port 15166", () => {
|
|
43
54
|
const configSamplePath = join(process.cwd(), "config.sample.json");
|
|
44
55
|
const sample = JSON.parse(readFileSync(configSamplePath, "utf8")) as {
|
|
@@ -1,60 +1,60 @@
|
|
|
1
|
-
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
2
|
-
|
|
3
|
-
import { sendTextReply } from "../feishu-api.ts";
|
|
4
|
-
|
|
5
|
-
function abortError(): Error {
|
|
6
|
-
const err = new Error("aborted");
|
|
7
|
-
err.name = "AbortError";
|
|
8
|
-
return err;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
describe("sendTextReply timeout", () => {
|
|
12
|
-
afterEach(() => {
|
|
13
|
-
vi.useRealTimers();
|
|
14
|
-
vi.restoreAllMocks();
|
|
15
|
-
vi.unstubAllGlobals();
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
it("aborts a hung text send request", async () => {
|
|
19
|
-
vi.useFakeTimers();
|
|
20
|
-
vi.spyOn(console, "error").mockImplementation(() => {});
|
|
21
|
-
|
|
22
|
-
const fetchMock = vi.fn((_url: Parameters<typeof fetch>[0], init?: Parameters<typeof fetch>[1]) => (
|
|
23
|
-
new Promise<Response>((_resolve, reject) => {
|
|
24
|
-
init?.signal?.addEventListener("abort", () => reject(abortError()));
|
|
25
|
-
})
|
|
26
|
-
));
|
|
27
|
-
vi.stubGlobal("fetch", fetchMock);
|
|
28
|
-
|
|
29
|
-
const send = expect(sendTextReply("token", "chat-1", "hello"))
|
|
30
|
-
.resolves.toBe(false);
|
|
31
|
-
await vi.advanceTimersByTimeAsync(15_000);
|
|
32
|
-
|
|
33
|
-
await send;
|
|
34
|
-
expect(fetchMock).toHaveBeenCalledWith(
|
|
35
|
-
expect.stringContaining("/im/v1/messages?receive_id_type=chat_id"),
|
|
36
|
-
expect.objectContaining({ signal: expect.any(AbortSignal) }),
|
|
37
|
-
);
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
it("also aborts when response body reading hangs", async () => {
|
|
41
|
-
vi.useFakeTimers();
|
|
42
|
-
vi.spyOn(console, "error").mockImplementation(() => {});
|
|
43
|
-
|
|
44
|
-
const fetchMock = vi.fn((_url: Parameters<typeof fetch>[0], init?: Parameters<typeof fetch>[1]) => (
|
|
45
|
-
Promise.resolve({
|
|
46
|
-
status: 200,
|
|
47
|
-
text: () => new Promise<string>((_resolve, reject) => {
|
|
48
|
-
init?.signal?.addEventListener("abort", () => reject(abortError()));
|
|
49
|
-
}),
|
|
50
|
-
} as Response)
|
|
51
|
-
));
|
|
52
|
-
vi.stubGlobal("fetch", fetchMock);
|
|
53
|
-
|
|
54
|
-
const send = expect(sendTextReply("token", "chat-2", "hello"))
|
|
55
|
-
.resolves.toBe(false);
|
|
56
|
-
await vi.advanceTimersByTimeAsync(15_000);
|
|
57
|
-
|
|
58
|
-
await send;
|
|
59
|
-
});
|
|
60
|
-
});
|
|
1
|
+
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
|
|
3
|
+
import { sendTextReply } from "../feishu-api.ts";
|
|
4
|
+
|
|
5
|
+
function abortError(): Error {
|
|
6
|
+
const err = new Error("aborted");
|
|
7
|
+
err.name = "AbortError";
|
|
8
|
+
return err;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
describe("sendTextReply timeout", () => {
|
|
12
|
+
afterEach(() => {
|
|
13
|
+
vi.useRealTimers();
|
|
14
|
+
vi.restoreAllMocks();
|
|
15
|
+
vi.unstubAllGlobals();
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it("aborts a hung text send request", async () => {
|
|
19
|
+
vi.useFakeTimers();
|
|
20
|
+
vi.spyOn(console, "error").mockImplementation(() => {});
|
|
21
|
+
|
|
22
|
+
const fetchMock = vi.fn((_url: Parameters<typeof fetch>[0], init?: Parameters<typeof fetch>[1]) => (
|
|
23
|
+
new Promise<Response>((_resolve, reject) => {
|
|
24
|
+
init?.signal?.addEventListener("abort", () => reject(abortError()));
|
|
25
|
+
})
|
|
26
|
+
));
|
|
27
|
+
vi.stubGlobal("fetch", fetchMock);
|
|
28
|
+
|
|
29
|
+
const send = expect(sendTextReply("token", "chat-1", "hello"))
|
|
30
|
+
.resolves.toBe(false);
|
|
31
|
+
await vi.advanceTimersByTimeAsync(15_000);
|
|
32
|
+
|
|
33
|
+
await send;
|
|
34
|
+
expect(fetchMock).toHaveBeenCalledWith(
|
|
35
|
+
expect.stringContaining("/im/v1/messages?receive_id_type=chat_id"),
|
|
36
|
+
expect.objectContaining({ signal: expect.any(AbortSignal) }),
|
|
37
|
+
);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it("also aborts when response body reading hangs", async () => {
|
|
41
|
+
vi.useFakeTimers();
|
|
42
|
+
vi.spyOn(console, "error").mockImplementation(() => {});
|
|
43
|
+
|
|
44
|
+
const fetchMock = vi.fn((_url: Parameters<typeof fetch>[0], init?: Parameters<typeof fetch>[1]) => (
|
|
45
|
+
Promise.resolve({
|
|
46
|
+
status: 200,
|
|
47
|
+
text: () => new Promise<string>((_resolve, reject) => {
|
|
48
|
+
init?.signal?.addEventListener("abort", () => reject(abortError()));
|
|
49
|
+
}),
|
|
50
|
+
} as Response)
|
|
51
|
+
));
|
|
52
|
+
vi.stubGlobal("fetch", fetchMock);
|
|
53
|
+
|
|
54
|
+
const send = expect(sendTextReply("token", "chat-2", "hello"))
|
|
55
|
+
.resolves.toBe(false);
|
|
56
|
+
await vi.advanceTimersByTimeAsync(15_000);
|
|
57
|
+
|
|
58
|
+
await send;
|
|
59
|
+
});
|
|
60
|
+
});
|