Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
jzvikart committed Dec 16, 2024
1 parent 8824041 commit 6424c39
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 35 deletions.
31 changes: 10 additions & 21 deletions tests/test1.mjs
Original file line number Diff line number Diff line change
@@ -1,33 +1,22 @@
import { $, chalk } from 'zx';
import assert from 'assert';
import {
startAgent,
stopAgent,
send
} from "./testLibrary.mjs";
import { stringToUuid } from '../packages/core/dist/index.js'

export const DEFAULT_CHARACTER = "trump"
export const DEFAULT_AGENT_ID = stringToUuid(DEFAULT_CHARACTER ?? uuidv4());

async function test1() {
const proc = await startAgent();
try {
const reply = await send("Hi");
assert(reply.length > 10);
}

const reply = await send("Hi");
assert(reply.length > 10);
console.log(chalk.green('✓ Test 1 passed'));
} catch (error) {
console.error(chalk.red(`✗ Test 1 failed: ${error.message}`));
process.exit(1);
} finally {
await stopAgent(proc);
}
async function test2() {
// TODO
}

try {
await test1();
const allTests = [test1, test2];
allTests.forEach(runIntegrationTest);
} catch (error) {
console.error(chalk.red(`Error: ${error.message}`));
console.error(`Error: ${error.message}`);
console.log(error);
process.exit(1);
}
}
44 changes: 30 additions & 14 deletions tests/testLibrary.mjs
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
import { $, fs, path, chalk } from 'zx';
import { DEFAULT_AGENT_ID, DEFAULT_CHARACTER } from './test1.mjs';
import { spawn } from 'node:child_process';
$.verbose = false; // Suppress command output unless there's an error
import { stringToUuid } from '../packages/core/dist/index.js';

export const DEFAULT_CHARACTER = "trump"
export const DEFAULT_AGENT_ID = stringToUuid(DEFAULT_CHARACTER ?? uuidv4());

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

async function runProcess(command, args = [], directory = projectRoot()) {
try {
const result = await $`cd ${directory} && ${command} ${args}`;
throw new Exception("Not implemented yet"); // TODO
// const result = await $`cd ${directory} && ${command} ${args}`;
return result.stdout.trim();
} catch (error) {
throw new Error(`Command failed: ${error.message}`);
}
}

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

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

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

async function startAgent(character = DEFAULT_CHARACTER) {
console.log(chalk.blue(`Starting agent for character: ${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(`proc=${JSON.stringify(proc)}`);

// Wait for server to be ready
await new Promise(resolve => setTimeout(resolve, 60000));
sleep(60000); // Wait for server to be ready
return proc;
}

async function stopAgent(proc) {
console.log(chalk.blue('Stopping agent...'));
proc.kill('SIGTERM')
console.log('Stopping agent...');
proc.kill('SIGTERM');
}

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

async function send(message) {
Expand Down Expand Up @@ -76,8 +81,18 @@ async function send(message) {
}
}

function log(message) {
console.log(message);
async function runIntegrationTest(fn) {
const proc = await startAgent();
try {
fn();
console.log('✓ Test passed');
} catch (error) {
console.error(`✗ Test failed: ${error.message}`);
console.log(error);
process.exit(1);
} finally {
await stopAgent(proc);
}
}

export {
Expand All @@ -89,5 +104,6 @@ export {
startAgent,
stopAgent,
send,
runIntegrationTest,
log
}
}

0 comments on commit 6424c39

Please sign in to comment.