Skip to content

Commit

Permalink
Wait for server to start
Browse files Browse the repository at this point in the history
  • Loading branch information
jzvikart committed Dec 16, 2024
1 parent 6424c39 commit 9b0302c
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 31 deletions.
11 changes: 7 additions & 4 deletions tests/test1.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import assert from 'assert';
import {
send
send,
log,
logError,
runIntegrationTest
} from "./testLibrary.mjs";

async function test1() {
Expand All @@ -13,10 +16,10 @@ async function test2() {
}

try {
const allTests = [test1, test2];
// const allTests = [test1, test2];
const allTests = [test2];
allTests.forEach(runIntegrationTest);
} catch (error) {
console.error(`Error: ${error.message}`);
console.log(error);
logError(error);
process.exit(1);
}
78 changes: 51 additions & 27 deletions tests/testLibrary.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ function projectRoot() {
return path.join(import.meta.dirname, "..");
}

function log(message) {
console.log(message);
}

function logError(error) {
log("ERROR: " + error.message);
log(error); // Print stack trace
}

async function runProcess(command, args = [], directory = projectRoot()) {
try {
throw new Exception("Not implemented yet"); // TODO
Expand All @@ -19,12 +28,12 @@ async function runProcess(command, args = [], directory = projectRoot()) {
}

async function installProjectDependencies() {
console.log('Installing dependencies...');
log('Installing dependencies...');
return await runProcess('pnpm', ['install', '-r']);
}

async function buildProject() {
console.log('Building project...');
log('Building project...');
return await runProcess('pnpm', ['build']);
}

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

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

sleep(60000); // Wait for server to be ready
const startTime = Date.now();
const url = "http://127.0.0.1:3000/";
while (true) {
try {
const response = await fetch(url, {method: "GET"});
if (response.ok) break;
} catch (error) {}
if (Date.now() - startTime > 120000) {
throw new Error("Timeout 120s waiting for server to start");
} else {
log("Waiting for the server to be ready...");
await sleep(1000);
}
}
log("Server is ready");
await sleep(1000);
return proc;
}

async function stopAgent(proc) {
console.log('Stopping agent...');
proc.kill('SIGTERM');
log("Stopping agent..." + JSON.stringify(proc));
const q = proc.kill("SIGKILL");
console.log(q);
}

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

async function send(message) {
const endpoint = `http://127.0.0.1:3000/${DEFAULT_AGENT_ID}/message`;
const payload = {
text: message,
userId: "user",
userName: "User"
};

async function sendPostRequest(url, method, payload) {
try {
const response = await fetch(endpoint, {
method: 'POST',
const response = await fetch(url, {
method: method,
headers: {
'Content-Type': 'application/json'
"Content-Type": "application/json"
},
body: JSON.stringify(payload)
});

if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}

if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
const data = await response.json();
return data[0].text;
} catch (error) {
throw new Error(`Failed to send message: ${error.message}`);
}
}

async function send(message) {
const url = `http://127.0.0.1:3000/${DEFAULT_AGENT_ID}/message`;
return await sendPostRequest(url, "POST", {
text: message,
userId: "user",
userName: "User"
});
}

async function runIntegrationTest(fn) {
const proc = await startAgent();
try {
fn();
console.log('✓ Test passed');
log("✓ Test passed");
} catch (error) {
console.error(`✗ Test failed: ${error.message}`);
console.log(error);
logError(`✗ Test failed: ${error.message}`);
logError(error);
process.exit(1);
} finally {
await stopAgent(proc);
Expand All @@ -105,5 +128,6 @@ export {
stopAgent,
send,
runIntegrationTest,
log
log,
logError
}

0 comments on commit 9b0302c

Please sign in to comment.