-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtestLibrary.mjs
204 lines (184 loc) · 5.37 KB
/
testLibrary.mjs
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
import { spawn } from "node:child_process";
import { stringToUuid } from "../packages/core/dist/index.js";
import path from "path";
export const DEFAULT_CHARACTER = "trump";
export const DEFAULT_AGENT_ID = stringToUuid(DEFAULT_CHARACTER ?? uuidv4());
function projectRoot() {
return path.join(import.meta.dirname, "..");
// return "/Users/piotr/Documents/GitHub/Sifchain/eliza"
}
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 {
return new Promise((resolve, reject) => {
const process = spawn(command, args, {
cwd: directory,
shell: true,
stdio: ['inherit', 'pipe', 'pipe']
});
let stdout = '';
let stderr = '';
process.stdout.on('data', (data) => {
stdout += data.toString();
});
process.stderr.on('data', (data) => {
stderr += data.toString();
});
process.on('close', (code) => {
if (code === 0) {
resolve(stdout.trim());
} else {
reject(new Error(`Command failed with code ${code}: ${stderr}`));
}
});
process.on('error', (error) => {
reject(new Error(`Failed to start command: ${error.message}`));
});
});
} catch (error) {
throw new Error(`Command failed: ${error.message}`);
}
}
async function installProjectDependencies() {
log("Installing dependencies...");
return await runProcess("pnpm", ["install", "-r"]);
}
async function buildProject() {
log("Building project...");
return await runProcess("pnpm", ["build"]);
}
async function writeEnvFile(entries) {
const envContent = Object.entries(entries)
.map(([key, value]) => `${key}=${value}`)
.join("\n");
await fs.writeFile(".env", envContent);
}
async function startAgent(character = DEFAULT_CHARACTER) {
log(`Starting agent for character: ${character}`);
const proc = spawn(
"node",
[
"--loader",
"ts-node/esm",
"src/index.ts",
"--isRoot",
`--character=characters/${character}.character.json`,
"--non-interactive",
],
{
cwd: path.join(projectRoot(), "agent"),
shell: false,
stdio: "inherit",
}
);
const startTime = Date.now();
while (true) {
try {
const response = await fetch("http://127.0.0.1:3000/", {
method: "GET",
});
if (response.ok) break;
} catch (error) {}
if (Date.now() - startTime > 120000) {
throw new Error("Timeout waiting for process to start");
} else {
await sleep(1000);
}
}
await sleep(1000);
return proc;
}
async function stopAgent(proc) {
log("Stopping agent..." + JSON.stringify(proc.pid));
proc.kill();
const startTime = Date.now();
while (true) {
if (proc.killed) break;
if (Date.now() - startTime > 60000) {
throw new Error("Timeout waiting for the process to terminate");
}
await sleep(1000);
}
await sleep(1000);
}
async function sleep(ms) {
await new Promise((resolve) => setTimeout(resolve, ms));
}
async function sendPostRequest(url, method, payload) {
try {
const response = await fetch(url, {
method: method,
headers: { "Content-Type": "application/json" },
body: JSON.stringify(payload),
});
if (!response.ok)
throw new Error(`HTTP error! status: ${response.status}`);
const data = await response.json();
return data;
} 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) {
log(fn);
const skip = fn.hasOwnProperty("skipIf") ? fn.skipIf : false;
if (skip) {
log(
fn.description
? `Skipping test ${fn.description}...`
: "Skipping test..."
);
} else {
log(
fn.description
? `Running test ${fn.description}...`
: "Running test..."
);
const proc = await startAgent();
try {
await fn();
log(
fn.description
? `✓ Test ${fn.description} passed`
: "✓ Test passed"
);
} catch (error) {
log(
fn.description
? `✗ Test ${fn.description} failed`
: "✗ Test failed"
);
logError(error);
} finally {
await stopAgent(proc);
}
}
}
export {
projectRoot,
runProcess,
installProjectDependencies,
buildProject,
writeEnvFile,
startAgent,
stopAgent,
send,
runIntegrationTest,
log,
logError,
sleep,
};