Skip to content

Commit 1781ae1

Browse files
authored
Merge pull request #1177 from Sifchain/jure/integration-tests
feat: integration tests fixes + library improvements
2 parents 855c029 + be74341 commit 1781ae1

File tree

7 files changed

+22712
-17784
lines changed

7 files changed

+22712
-17784
lines changed

.github/workflows/integrationTests.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ on:
33
push:
44
branches:
55
- "*"
6-
pull_request_target:
6+
pull_request:
77
branches:
88
- "*"
99
jobs:

agent/src/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ export const wait = (minTime: number = 1000, maxTime: number = 3000) => {
7070

7171
const logFetch = async (url: string, options: any) => {
7272
elizaLogger.debug(`Fetching ${url}`);
73-
elizaLogger.debug(JSON.stringify(options, null, 2));
73+
// Disabled to avoid disclosure of sensitive information such as API keys
74+
// elizaLogger.debug(JSON.stringify(options, null, 2));
7475
return fetch(url, options);
7576
};
7677

package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@
4040
"typedoc": "0.26.11",
4141
"typescript": "5.6.3",
4242
"vite": "5.4.11",
43-
"vitest": "2.1.5",
44-
"zx": "^8.2.4"
43+
"vitest": "2.1.5"
4544
},
4645
"pnpm": {
4746
"overrides": {

packages/core/src/logger.ts

-1
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,6 @@ class ElizaLogger {
265265
}
266266

267267
export const elizaLogger = new ElizaLogger();
268-
elizaLogger.clear();
269268
elizaLogger.closeByNewLine = true;
270269
elizaLogger.useIcons = true;
271270

pnpm-lock.yaml

+22,613-17,720
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/test1.mjs

+14-23
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,24 @@
1-
import { $, chalk } from 'zx';
21
import assert from 'assert';
32
import {
4-
startAgent,
5-
stopAgent,
6-
send
3+
send,
4+
log,
5+
logError,
6+
runIntegrationTest
77
} from "./testLibrary.mjs";
8-
import { stringToUuid } from '../packages/core/dist/index.js'
98

10-
export const DEFAULT_CHARACTER = "trump"
11-
export const DEFAULT_AGENT_ID = stringToUuid(DEFAULT_CHARACTER ?? uuidv4());
12-
13-
async function test1() {
14-
const proc = await startAgent();
15-
try {
9+
async function helloTrump() {
10+
const reply = await send("Hi");
11+
assert(reply.length > 10);
12+
}
1613

17-
const reply = await send("Hi");
18-
assert(reply.length > 10);
19-
console.log(chalk.green('✓ Test 1 passed'));
20-
} catch (error) {
21-
console.error(chalk.red(`✗ Test 1 failed: ${error.message}`));
22-
process.exit(1);
23-
} finally {
24-
await stopAgent(proc);
25-
}
14+
async function coinbaseTest() {
15+
// TODO
2616
}
2717

18+
const testSuite = [helloTrump]; // Add tests here
2819
try {
29-
await test1();
20+
for (const test of testSuite) await runIntegrationTest(test);
3021
} catch (error) {
31-
console.error(chalk.red(`Error: ${error.message}`));
22+
logError(error);
3223
process.exit(1);
33-
}
24+
}

tests/testLibrary.mjs

+81-36
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,40 @@
1-
import { $, fs, path, chalk } from 'zx';
2-
import { DEFAULT_AGENT_ID, DEFAULT_CHARACTER } from './test1.mjs';
3-
import { spawn } from 'node:child_process';
4-
$.verbose = false; // Suppress command output unless there's an error
1+
import { spawn } from "node:child_process";
2+
import { stringToUuid } from "../packages/core/dist/index.js";
3+
import path from "path";
4+
5+
export const DEFAULT_CHARACTER = "trump"
6+
export const DEFAULT_AGENT_ID = stringToUuid(DEFAULT_CHARACTER ?? uuidv4());
57

68
function projectRoot() {
79
return path.join(import.meta.dirname, "..");
810
}
911

12+
function log(message) {
13+
console.log(message);
14+
}
15+
16+
function logError(error) {
17+
log("ERROR: " + error.message);
18+
log(error); // Print stack trace
19+
}
20+
1021
async function runProcess(command, args = [], directory = projectRoot()) {
1122
try {
12-
const result = await $`cd ${directory} && ${command} ${args}`;
23+
throw new Exception("Not implemented yet"); // TODO
24+
// const result = await $`cd ${directory} && ${command} ${args}`;
1325
return result.stdout.trim();
1426
} catch (error) {
1527
throw new Error(`Command failed: ${error.message}`);
1628
}
1729
}
1830

1931
async function installProjectDependencies() {
20-
console.log(chalk.blue('Installing dependencies...'));
32+
log('Installing dependencies...');
2133
return await runProcess('pnpm', ['install', '-r']);
2234
}
2335

2436
async function buildProject() {
25-
console.log(chalk.blue('Building project...'));
37+
log('Building project...');
2638
return await runProcess('pnpm', ['build']);
2739
}
2840

@@ -34,50 +46,81 @@ async function writeEnvFile(entries) {
3446
}
3547

3648
async function startAgent(character = DEFAULT_CHARACTER) {
37-
console.log(chalk.blue(`Starting agent for character: ${character}`));
38-
const proc = spawn('pnpm', ['start', `--character=characters/${character}.character.json`, '--non-interactive'], { shell: true, "stdio": "inherit" });
39-
log(`proc=${JSON.stringify(proc)}`);
40-
41-
// Wait for server to be ready
42-
await new Promise(resolve => setTimeout(resolve, 20000));
49+
log(`Starting agent for character: ${character}`);
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,
53+
stdio: "inherit"
54+
});
55+
const startTime = Date.now();
56+
while (true) {
57+
try {
58+
const response = await fetch("http://127.0.0.1:3000/", {method: "GET"});
59+
if (response.ok) break;
60+
} catch (error) {}
61+
if (Date.now() - startTime > 120000) {
62+
throw new Error("Timeout waiting for process to start");
63+
} else {
64+
await sleep(1000);
65+
}
66+
}
67+
await sleep(1000);
4368
return proc;
4469
}
4570

4671
async function stopAgent(proc) {
47-
console.log(chalk.blue('Stopping agent...'));
48-
proc.kill('SIGTERM')
72+
log("Stopping agent..." + JSON.stringify(proc.pid));
73+
proc.kill();
74+
const startTime = Date.now();
75+
while (true) {
76+
if (proc.killed) break;
77+
if (Date.now() - startTime > 60000) {
78+
throw new Error("Timeout waiting for the process to terminate");
79+
}
80+
await sleep(1000);
81+
}
82+
await sleep(1000);
4983
}
5084

51-
async function send(message) {
52-
const endpoint = `http://127.0.0.1:3000/${DEFAULT_AGENT_ID}/message`;
53-
const payload = {
54-
text: message,
55-
userId: "user",
56-
userName: "User"
57-
};
85+
async function sleep(ms) {
86+
await new Promise(resolve => setTimeout(resolve, ms));
87+
}
5888

89+
async function sendPostRequest(url, method, payload) {
5990
try {
60-
const response = await fetch(endpoint, {
61-
method: 'POST',
62-
headers: {
63-
'Content-Type': 'application/json'
64-
},
91+
const response = await fetch(url, {
92+
method: method,
93+
headers: {"Content-Type": "application/json"},
6594
body: JSON.stringify(payload)
6695
});
67-
68-
if (!response.ok) {
69-
throw new Error(`HTTP error! status: ${response.status}`);
70-
}
71-
96+
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
7297
const data = await response.json();
7398
return data[0].text;
7499
} catch (error) {
75100
throw new Error(`Failed to send message: ${error.message}`);
76101
}
77102
}
78103

79-
function log(message) {
80-
console.log(message);
104+
async function send(message) {
105+
const url = `http://127.0.0.1:3000/${DEFAULT_AGENT_ID}/message`;
106+
return await sendPostRequest(url, "POST", {
107+
text: message,
108+
userId: "user",
109+
userName: "User"
110+
});
111+
}
112+
113+
async function runIntegrationTest(fn) {
114+
const proc = await startAgent();
115+
try {
116+
await fn();
117+
log("✓ Test passed");
118+
} catch (error) {
119+
log("✗ Test failed");
120+
logError(error);
121+
} finally {
122+
await stopAgent(proc);
123+
}
81124
}
82125

83126
export {
@@ -89,5 +132,7 @@ export {
89132
startAgent,
90133
stopAgent,
91134
send,
92-
log
93-
}
135+
runIntegrationTest,
136+
log,
137+
logError
138+
}

0 commit comments

Comments
 (0)