Skip to content

Commit 6424c39

Browse files
committed
Refactoring
1 parent 8824041 commit 6424c39

File tree

2 files changed

+40
-35
lines changed

2 files changed

+40
-35
lines changed

tests/test1.mjs

+10-21
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,22 @@
1-
import { $, chalk } from 'zx';
21
import assert from 'assert';
32
import {
4-
startAgent,
5-
stopAgent,
63
send
74
} from "./testLibrary.mjs";
8-
import { stringToUuid } from '../packages/core/dist/index.js'
9-
10-
export const DEFAULT_CHARACTER = "trump"
11-
export const DEFAULT_AGENT_ID = stringToUuid(DEFAULT_CHARACTER ?? uuidv4());
125

136
async function test1() {
14-
const proc = await startAgent();
15-
try {
7+
const reply = await send("Hi");
8+
assert(reply.length > 10);
9+
}
1610

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-
}
11+
async function test2() {
12+
// TODO
2613
}
2714

2815
try {
29-
await test1();
16+
const allTests = [test1, test2];
17+
allTests.forEach(runIntegrationTest);
3018
} catch (error) {
31-
console.error(chalk.red(`Error: ${error.message}`));
19+
console.error(`Error: ${error.message}`);
20+
console.log(error);
3221
process.exit(1);
33-
}
22+
}

tests/testLibrary.mjs

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

67
function projectRoot() {
78
return path.join(import.meta.dirname, "..");
89
}
910

1011
async function runProcess(command, args = [], directory = projectRoot()) {
1112
try {
12-
const result = await $`cd ${directory} && ${command} ${args}`;
13+
throw new Exception("Not implemented yet"); // TODO
14+
// const result = await $`cd ${directory} && ${command} ${args}`;
1315
return result.stdout.trim();
1416
} catch (error) {
1517
throw new Error(`Command failed: ${error.message}`);
1618
}
1719
}
1820

1921
async function installProjectDependencies() {
20-
console.log(chalk.blue('Installing dependencies...'));
22+
console.log('Installing dependencies...');
2123
return await runProcess('pnpm', ['install', '-r']);
2224
}
2325

2426
async function buildProject() {
25-
console.log(chalk.blue('Building project...'));
27+
console.log('Building project...');
2628
return await runProcess('pnpm', ['build']);
2729
}
2830

@@ -34,18 +36,21 @@ async function writeEnvFile(entries) {
3436
}
3537

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

41-
// Wait for server to be ready
42-
await new Promise(resolve => setTimeout(resolve, 60000));
43+
sleep(60000); // Wait for server to be ready
4344
return proc;
4445
}
4546

4647
async function stopAgent(proc) {
47-
console.log(chalk.blue('Stopping agent...'));
48-
proc.kill('SIGTERM')
48+
console.log('Stopping agent...');
49+
proc.kill('SIGTERM');
50+
}
51+
52+
async function sleep(ms) {
53+
await new Promise(resolve => setTimeout(resolve, ms));
4954
}
5055

5156
async function send(message) {
@@ -76,8 +81,18 @@ async function send(message) {
7681
}
7782
}
7883

79-
function log(message) {
80-
console.log(message);
84+
async function runIntegrationTest(fn) {
85+
const proc = await startAgent();
86+
try {
87+
fn();
88+
console.log('✓ Test passed');
89+
} catch (error) {
90+
console.error(`✗ Test failed: ${error.message}`);
91+
console.log(error);
92+
process.exit(1);
93+
} finally {
94+
await stopAgent(proc);
95+
}
8196
}
8297

8398
export {
@@ -89,5 +104,6 @@ export {
89104
startAgent,
90105
stopAgent,
91106
send,
107+
runIntegrationTest,
92108
log
93-
}
109+
}

0 commit comments

Comments
 (0)