-
Notifications
You must be signed in to change notification settings - Fork 339
Expand file tree
/
Copy pathtest-plugin.ts
More file actions
122 lines (101 loc) · 2.97 KB
/
Copy pathtest-plugin.ts
File metadata and controls
122 lines (101 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/**
* 插件本地测试脚本 - 不依赖 OpenClaw 运行时
*/
import { ProxyClient } from "./src/proxy-client.js";
import { startCallbackServer } from "./src/callback-server.js";
// ===== 测试配置 =====
const TEST_CONFIG = {
apiKey: "test_api_key_xxx",
accountId: "default",
proxyUrl: "http://localhost:3000/v1", // 你的代理服务地址
};
// ===== 测试 1: ProxyClient =====
async function testProxyClient() {
console.log("\n🧪 测试 ProxyClient...");
const client = new ProxyClient({
apiKey: TEST_CONFIG.apiKey,
accountId: TEST_CONFIG.accountId,
baseUrl: TEST_CONFIG.proxyUrl,
});
try {
// 测试获取状态
console.log(" - 测试 getStatus()");
const status = await client.getStatus();
console.log(" ✓ Status:", status);
} catch (err: any) {
console.log(" ✗ getStatus 失败:", err.message);
}
try {
// 测试获取二维码
console.log(" - 测试 getQRCode()");
const qr = await client.getQRCode("ipad", "2");
console.log(" ✓ QRCode:", qr);
} catch (err: any) {
console.log(" ✗ getQRCode 失败:", err.message);
}
}
// ===== 测试 2: Callback Server =====
async function testCallbackServer() {
console.log("\n🧪 测试 CallbackServer...");
try {
const { port, stop } = await startCallbackServer({
port: 18790,
apiKey: TEST_CONFIG.apiKey,
onMessage: (message) => {
console.log(" 📩 收到消息:", message);
},
});
console.log(` ✓ 服务器启动在端口 ${port}`);
// 5秒后停止
setTimeout(() => {
stop();
console.log(" ✓ 服务器已停止");
}, 5000);
} catch (err: any) {
console.log(" ✗ 启动失败:", err.message);
}
}
// ===== 测试 3: 模拟消息接收 =====
async function testWebhookReceive() {
console.log("\n🧪 测试 Webhook 接收...");
// 模拟发送一个 webhook 请求到本地服务器
const testPayload = {
messageType: "60001",
wcId: "wxid_test123",
timestamp: Date.now(),
data: {
newMsgId: 123456789,
fromUser: "wxid_fromuser",
content: "测试消息",
timestamp: Date.now(),
},
};
try {
const response = await fetch("http://localhost:18790/webhook/wechat", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(testPayload),
});
console.log(" ✓ Webhook 响应:", response.status);
} catch (err: any) {
console.log(" ✗ Webhook 请求失败:", err.message);
}
}
// ===== 主测试流程 =====
async function main() {
console.log("🚀 开始插件本地测试\n");
// 测试 ProxyClient
await testProxyClient();
// 测试 CallbackServer
await testCallbackServer();
// 等待服务器启动
await new Promise((r) => setTimeout(r, 1000));
// 测试 Webhook 接收
await testWebhookReceive();
console.log("\n✅ 测试完成");
process.exit(0);
}
main().catch((err) => {
console.error("测试失败:", err);
process.exit(1);
});