-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Expand file tree
/
Copy pathtest-client.ts
More file actions
323 lines (277 loc) Β· 7.8 KB
/
Copy pathtest-client.ts
File metadata and controls
323 lines (277 loc) Β· 7.8 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#!/usr/bin/env bun
/**
* Test client for elizaOS Vercel Edge Functions
*
* Usage:
* bun run test-client.ts # Test local dev server
* bun run test-client.ts --endpoint https://your-app.vercel.app # Test deployed
*
* Environment:
* VERCEL_URL - Base URL for the Vercel deployment
*/
import * as readline from "node:readline";
import { parseArgs } from "node:util";
// Types
interface ChatResponse {
response: string;
conversationId: string;
timestamp: string;
}
interface HealthResponse {
status: string;
runtime: string;
version: string;
}
interface ErrorResponse {
error: string;
code: string;
}
// Parse command line arguments
const { values: args } = parseArgs({
options: {
endpoint: { type: "string", short: "e" },
help: { type: "boolean", short: "h" },
interactive: { type: "boolean", short: "i" },
},
});
const DEFAULT_ENDPOINT = "http://localhost:3000";
function showHelp(): void {
console.log(`
elizaOS Vercel Edge Function Test Client
Usage:
bun run test-client.ts [options]
Options:
-e, --endpoint <url> API endpoint URL (default: ${DEFAULT_ENDPOINT})
-i, --interactive Start interactive chat mode
-h, --help Show this help message
Examples:
bun run test-client.ts # Run tests against local
bun run test-client.ts -e https://your-app.vercel.app # Test deployed app
bun run test-client.ts -i # Interactive chat mode
`);
}
if (args.help) {
showHelp();
process.exit(0);
}
const baseUrl = args.endpoint ?? process.env.VERCEL_URL ?? DEFAULT_ENDPOINT;
console.log(`π Using endpoint: ${baseUrl}\n`);
/**
* Make an HTTP request to the API
*/
async function apiRequest<T>(
path: string,
method: string = "GET",
body?: Record<string, unknown>,
): Promise<{ status: number; data: T }> {
const url = `${baseUrl}${path}`;
const options: RequestInit = {
method,
headers: {
"Content-Type": "application/json",
},
};
if (body) {
options.body = JSON.stringify(body);
}
const response = await fetch(url, options);
const data = (await response.json()) as T;
return { status: response.status, data };
}
/**
* Check if the server is available
*/
async function isServerAvailable(): Promise<boolean> {
try {
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), 2000);
await fetch(`${baseUrl}/api/health`, { signal: controller.signal });
clearTimeout(timeoutId);
return true;
} catch {
return false;
}
}
/**
* Run automated tests
*/
async function runTests(): Promise<void> {
console.log("π§ͺ Testing elizaOS Vercel Edge Functions\n");
// Check if server is available
const serverAvailable = await isServerAvailable();
if (!serverAvailable) {
console.log(`β οΈ Server not available at ${baseUrl}`);
console.log(" Skipping integration tests (server must be running)");
console.log("\n To run these tests:");
console.log(" 1. Start the server: bun run dev");
console.log(" 2. Run tests: bun run test\n");
console.log("β
Tests skipped (no server running)\n");
process.exit(0);
}
let passed = 0;
let failed = 0;
// Test 1: Health check
console.log("1οΈβ£ Testing health check...");
try {
const { status, data } = await apiRequest<HealthResponse>("/api/health");
console.log(` Status: ${status}`);
console.log(` Runtime: ${data.runtime}`);
console.log(` Version: ${data.version}`);
if (status === 200 && data.status === "healthy") {
console.log(" β
Health check passed\n");
passed++;
} else {
console.log(" β Health check failed\n");
failed++;
}
} catch (error) {
console.log(
` β Error: ${error instanceof Error ? error.message : "Unknown"}\n`,
);
failed++;
}
// Test 2: Chat endpoint
console.log("2οΈβ£ Testing chat endpoint...");
try {
const start = Date.now();
const { status, data } = await apiRequest<ChatResponse>(
"/api/chat",
"POST",
{
message: "Hello! What's 2 + 2?",
},
);
const duration = Date.now() - start;
console.log(` Status: ${status}`);
console.log(` Duration: ${duration}ms`);
console.log(` Conversation ID: ${data.conversationId}`);
console.log(
` Response: ${data.response.slice(0, 100)}${data.response.length > 100 ? "..." : ""}`,
);
if (status === 200 && data.response) {
console.log(" β
Chat endpoint passed\n");
passed++;
} else {
console.log(" β Chat endpoint failed\n");
failed++;
}
} catch (error) {
console.log(
` β Error: ${error instanceof Error ? error.message : "Unknown"}\n`,
);
failed++;
}
// Test 3: Validation (empty message)
console.log("3οΈβ£ Testing validation (empty message)...");
try {
const { status, data } = await apiRequest<ErrorResponse>(
"/api/chat",
"POST",
{
message: "",
},
);
console.log(` Status: ${status}`);
console.log(` Error: ${data.error}`);
if (status === 400 && data.code === "BAD_REQUEST") {
console.log(" β
Validation passed\n");
passed++;
} else {
console.log(" β Validation failed\n");
failed++;
}
} catch (error) {
console.log(
` β Error: ${error instanceof Error ? error.message : "Unknown"}\n`,
);
failed++;
}
// Test 4: 404 handling
console.log("4οΈβ£ Testing 404 response...");
try {
const { status, data } = await apiRequest<ErrorResponse>("/api/unknown");
console.log(` Status: ${status}`);
if (status === 404 && data.code === "NOT_FOUND") {
console.log(" β
404 handling passed\n");
passed++;
} else {
console.log(" β 404 handling failed\n");
failed++;
}
} catch (error) {
console.log(
` β Error: ${error instanceof Error ? error.message : "Unknown"}\n`,
);
failed++;
}
// Test 5: Method not allowed
console.log("5οΈβ£ Testing method not allowed...");
try {
const { status, data } = await apiRequest<ErrorResponse>(
"/api/chat",
"GET",
);
console.log(` Status: ${status}`);
if (status === 405 && data.code === "METHOD_NOT_ALLOWED") {
console.log(" β
Method handling passed\n");
passed++;
} else {
console.log(" β Method handling failed\n");
failed++;
}
} catch (error) {
console.log(
` β Error: ${error instanceof Error ? error.message : "Unknown"}\n`,
);
failed++;
}
// Summary
console.log("β".repeat(40));
console.log(`Results: ${passed} passed, ${failed} failed`);
if (failed > 0) {
console.log("\nβ Some tests failed!");
process.exit(1);
} else {
console.log("\nπ All tests passed!");
}
}
/**
* Interactive chat mode
*/
async function interactiveMode(): Promise<void> {
console.log("π¬ Interactive Chat Mode");
console.log(' Type your message and press Enter. Type "exit" to quit.\n');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
let conversationId: string | undefined;
const prompt = (): void => {
rl.question("You: ", async (input: string) => {
const message = input.trim();
if (message.toLowerCase() === "exit") {
console.log("\nπ Goodbye!");
rl.close();
return;
}
if (!message) {
prompt();
return;
}
const { data } = await apiRequest<ChatResponse>("/api/chat", "POST", {
message,
conversationId,
});
conversationId = data.conversationId;
console.log(`\nEliza: ${data.response}\n`);
prompt();
});
};
prompt();
}
// Main
if (args.interactive) {
interactiveMode();
} else {
runTests();
}