Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: external test #2205

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
180 changes: 180 additions & 0 deletions testLibrary.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
import { spawn } from "node:child_process";
import { stringToUuid } from "../packages/core/dist/index.js";
import path from "path";
import { v4 as uuidv4 } from "uuid";
import fs from "fs/promises";

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 {
throw new Error("Not implemented yet");
// TODO
// const result = await $`cd ${directory} && ${command} ${args}`;
return result.stdout.trim();
} catch (error) {
throw new Error(`Command failed: ${error.message}`);
}
}
Comment on lines +24 to +33
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The runProcess function currently throws immediately and contains unreachable code. Consider implementing process execution using either spawn() or exec() from the child_process module instead of the commented template literal syntax. Here's an example:

const result = await spawn(command, args, { cwd: directory });

This would provide proper process handling and output capture.

Spotted by Graphite Reviewer

Is this helpful? React 👍 or 👎 to let us know.


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,
};
Loading