Skip to content

Commit 9b0302c

Browse files
committed
Wait for server to start
1 parent 6424c39 commit 9b0302c

File tree

2 files changed

+58
-31
lines changed

2 files changed

+58
-31
lines changed

tests/test1.mjs

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import assert from 'assert';
22
import {
3-
send
3+
send,
4+
log,
5+
logError,
6+
runIntegrationTest
47
} from "./testLibrary.mjs";
58

69
async function test1() {
@@ -13,10 +16,10 @@ async function test2() {
1316
}
1417

1518
try {
16-
const allTests = [test1, test2];
19+
// const allTests = [test1, test2];
20+
const allTests = [test2];
1721
allTests.forEach(runIntegrationTest);
1822
} catch (error) {
19-
console.error(`Error: ${error.message}`);
20-
console.log(error);
23+
logError(error);
2124
process.exit(1);
2225
}

tests/testLibrary.mjs

+51-27
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@ function projectRoot() {
88
return path.join(import.meta.dirname, "..");
99
}
1010

11+
function log(message) {
12+
console.log(message);
13+
}
14+
15+
function logError(error) {
16+
log("ERROR: " + error.message);
17+
log(error); // Print stack trace
18+
}
19+
1120
async function runProcess(command, args = [], directory = projectRoot()) {
1221
try {
1322
throw new Exception("Not implemented yet"); // TODO
@@ -19,12 +28,12 @@ async function runProcess(command, args = [], directory = projectRoot()) {
1928
}
2029

2130
async function installProjectDependencies() {
22-
console.log('Installing dependencies...');
31+
log('Installing dependencies...');
2332
return await runProcess('pnpm', ['install', '-r']);
2433
}
2534

2635
async function buildProject() {
27-
console.log('Building project...');
36+
log('Building project...');
2837
return await runProcess('pnpm', ['build']);
2938
}
3039

@@ -36,59 +45,73 @@ async function writeEnvFile(entries) {
3645
}
3746

3847
async function startAgent(character = DEFAULT_CHARACTER) {
39-
console.log(`Starting agent for character: ${character}`);
40-
const proc = spawn('pnpm', ['start', `--character=characters/${character}.character.json`, '--non-interactive'], { shell: true, "stdio": "inherit" });
48+
log(`Starting agent for character: ${character}`);
49+
const proc = spawn("pnpm", ["start", `--character=characters/${character}.character.json`, '--non-interactive'], { shell: true, "stdio": "inherit" });
4150
log(`proc=${JSON.stringify(proc)}`);
4251

43-
sleep(60000); // Wait for server to be ready
52+
const startTime = Date.now();
53+
const url = "http://127.0.0.1:3000/";
54+
while (true) {
55+
try {
56+
const response = await fetch(url, {method: "GET"});
57+
if (response.ok) break;
58+
} catch (error) {}
59+
if (Date.now() - startTime > 120000) {
60+
throw new Error("Timeout 120s waiting for server to start");
61+
} else {
62+
log("Waiting for the server to be ready...");
63+
await sleep(1000);
64+
}
65+
}
66+
log("Server is ready");
67+
await sleep(1000);
4468
return proc;
4569
}
4670

4771
async function stopAgent(proc) {
48-
console.log('Stopping agent...');
49-
proc.kill('SIGTERM');
72+
log("Stopping agent..." + JSON.stringify(proc));
73+
const q = proc.kill("SIGKILL");
74+
console.log(q);
5075
}
5176

5277
async function sleep(ms) {
5378
await new Promise(resolve => setTimeout(resolve, ms));
5479
}
5580

56-
async function send(message) {
57-
const endpoint = `http://127.0.0.1:3000/${DEFAULT_AGENT_ID}/message`;
58-
const payload = {
59-
text: message,
60-
userId: "user",
61-
userName: "User"
62-
};
63-
81+
async function sendPostRequest(url, method, payload) {
6482
try {
65-
const response = await fetch(endpoint, {
66-
method: 'POST',
83+
const response = await fetch(url, {
84+
method: method,
6785
headers: {
68-
'Content-Type': 'application/json'
86+
"Content-Type": "application/json"
6987
},
7088
body: JSON.stringify(payload)
7189
});
72-
73-
if (!response.ok) {
74-
throw new Error(`HTTP error! status: ${response.status}`);
75-
}
76-
90+
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
7791
const data = await response.json();
7892
return data[0].text;
7993
} catch (error) {
8094
throw new Error(`Failed to send message: ${error.message}`);
8195
}
8296
}
8397

98+
async function send(message) {
99+
const url = `http://127.0.0.1:3000/${DEFAULT_AGENT_ID}/message`;
100+
return await sendPostRequest(url, "POST", {
101+
text: message,
102+
userId: "user",
103+
userName: "User"
104+
});
105+
}
106+
84107
async function runIntegrationTest(fn) {
85108
const proc = await startAgent();
86109
try {
87110
fn();
88-
console.log('✓ Test passed');
111+
log("✓ Test passed");
89112
} catch (error) {
90-
console.error(`✗ Test failed: ${error.message}`);
91-
console.log(error);
113+
logError(`✗ Test failed: ${error.message}`);
114+
logError(error);
92115
process.exit(1);
93116
} finally {
94117
await stopAgent(proc);
@@ -105,5 +128,6 @@ export {
105128
stopAgent,
106129
send,
107130
runIntegrationTest,
108-
log
131+
log,
132+
logError
109133
}

0 commit comments

Comments
 (0)