Skip to content

Commit 8c26e8d

Browse files
committed
Get stopAgent() to work as expected
1 parent a439407 commit 8c26e8d

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

tests/test1.mjs

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ async function test2() {
1616
}
1717

1818
try {
19-
const allTests = [test1]; // [test1, test2];
20-
allTests.forEach(runIntegrationTest);
19+
const allTests = [test1, test2];
20+
for (const test of allTests) await runIntegrationTest(test);
2121
} catch (error) {
2222
logError(error);
2323
process.exit(1);

tests/testLibrary.mjs

+15-10
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ async function writeEnvFile(entries) {
4747

4848
async function startAgent(character = DEFAULT_CHARACTER) {
4949
log(`Starting agent for character: ${character}`);
50-
const proc = spawn("pnpm", ["start", `--character=characters/${character}.character.json`, '--non-interactive'], {
51-
cwd: projectRoot(),
52-
shell: true,
50+
const proc = spawn("node", ["--loader", "ts-node/esm", "src/index.ts", "--isRoot", `--character=characters/${character}.character.json`, "--non-interactive"], {
51+
cwd: path.join(projectRoot(), "agent"),
52+
shell: false,
5353
stdio: "inherit"
5454
});
5555
log(`proc=${JSON.stringify(proc)}`);
@@ -62,19 +62,25 @@ async function startAgent(character = DEFAULT_CHARACTER) {
6262
if (Date.now() - startTime > 120000) {
6363
throw new Error("Timeout waiting for server to start");
6464
} else {
65-
log("Waiting for the server to be ready...");
6665
await sleep(1000);
6766
}
6867
}
69-
log("Server is ready");
7068
await sleep(1000);
7169
return proc;
7270
}
7371

7472
async function stopAgent(proc) {
75-
log("Stopping agent..." + JSON.stringify(proc));
76-
const q = proc.kill("SIGKILL");
77-
console.log(q);
73+
log("Stopping agent..." + JSON.stringify(proc.pid));
74+
proc.kill();
75+
const startTime = Date.now();
76+
while (true) {
77+
if (proc.killed) break;
78+
if (Date.now() - startTime > 60000) {
79+
throw new Error("Timeout waiting for the process to terminate");
80+
}
81+
await sleep(1000);
82+
}
83+
await sleep(1000);
7884
}
7985

8086
async function sleep(ms) {
@@ -110,12 +116,11 @@ async function send(message) {
110116
async function runIntegrationTest(fn) {
111117
const proc = await startAgent();
112118
try {
113-
fn();
119+
await fn();
114120
log("✓ Test passed");
115121
} catch (error) {
116122
log("✗ Test failed");
117123
logError(error);
118-
process.exit(1);
119124
} finally {
120125
await stopAgent(proc);
121126
}

0 commit comments

Comments
 (0)