Skip to content

Commit 08199d0

Browse files
committed
agentapi module
1 parent bd5ad3b commit 08199d0

File tree

9 files changed

+645
-0
lines changed

9 files changed

+645
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
For internal use
2+
3+
TODO: fill this out
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
import {
2+
test,
3+
afterEach,
4+
expect,
5+
describe,
6+
setDefaultTimeout,
7+
beforeAll,
8+
} from "bun:test";
9+
import { execContainer, readFileContainer, runTerraformInit } from "~test";
10+
import {
11+
loadTestFile,
12+
writeExecutable,
13+
setup as setupUtil,
14+
execModuleScript,
15+
expectAgentAPIStarted,
16+
} from "./test-util";
17+
18+
let cleanupFunctions: (() => Promise<void>)[] = [];
19+
20+
const registerCleanup = (cleanup: () => Promise<void>) => {
21+
cleanupFunctions.push(cleanup);
22+
};
23+
24+
// Cleanup logic depends on the fact that bun's built-in test runner
25+
// runs tests sequentially.
26+
// https://bun.sh/docs/test/discovery#execution-order
27+
// Weird things would happen if tried to run tests in parallel.
28+
// One test could clean up resources that another test was still using.
29+
afterEach(async () => {
30+
// reverse the cleanup functions so that they are run in the correct order
31+
const cleanupFnsCopy = cleanupFunctions.slice().reverse();
32+
cleanupFunctions = [];
33+
for (const cleanup of cleanupFnsCopy) {
34+
try {
35+
await cleanup();
36+
} catch (error) {
37+
console.error("Error during cleanup:", error);
38+
}
39+
}
40+
});
41+
42+
interface SetupProps {
43+
skipAgentAPIMock?: boolean;
44+
moduleVariables?: Record<string, string>;
45+
}
46+
47+
const moduleDirName = ".agentapi-module";
48+
49+
const setup = async (props?: SetupProps): Promise<{ id: string }> => {
50+
const projectDir = "/home/coder/project";
51+
const { id } = await setupUtil({
52+
moduleVariables: {
53+
experiment_report_tasks: "true",
54+
install_agentapi: props?.skipAgentAPIMock ? "true" : "false",
55+
web_app_display_name: "AgentAPI Web",
56+
web_app_slug: "agentapi-web",
57+
cli_app_display_name: "AgentAPI CLI",
58+
cli_app_slug: "agentapi-cli",
59+
agentapi_version: "preview",
60+
module_dir_name: moduleDirName,
61+
start_script: await loadTestFile(import.meta.dir, "agentapi-start.sh"),
62+
folder: projectDir,
63+
...props?.moduleVariables,
64+
},
65+
registerCleanup,
66+
projectDir,
67+
skipAgentAPIMock: props?.skipAgentAPIMock,
68+
moduleDir: import.meta.dir,
69+
});
70+
await writeExecutable({
71+
containerId: id,
72+
filePath: "/usr/bin/aiagent",
73+
content: await loadTestFile(import.meta.dir, "ai-agent-mock.js"),
74+
});
75+
return { id };
76+
};
77+
78+
// increase the default timeout to 60 seconds
79+
setDefaultTimeout(60 * 1000);
80+
81+
// we don't run these tests in CI because they take too long and make network
82+
// calls. they are dedicated for local development.
83+
describe("agentapi", async () => {
84+
beforeAll(async () => {
85+
await runTerraformInit(import.meta.dir);
86+
});
87+
88+
test("happy-path", async () => {
89+
const { id } = await setup();
90+
91+
await execModuleScript(id);
92+
93+
await expectAgentAPIStarted(id);
94+
});
95+
96+
test("custom-port", async () => {
97+
const { id } = await setup({
98+
moduleVariables: {
99+
agentapi_port: "3827",
100+
},
101+
});
102+
await execModuleScript(id);
103+
await expectAgentAPIStarted(id, 3827);
104+
});
105+
106+
test("pre-post-install-scripts", async () => {
107+
const { id } = await setup({
108+
moduleVariables: {
109+
pre_install_script: `#!/bin/bash\necho "pre-install"`,
110+
install_script: `#!/bin/bash\necho "install"`,
111+
post_install_script: `#!/bin/bash\necho "post-install"`,
112+
},
113+
});
114+
115+
await execModuleScript(id);
116+
await expectAgentAPIStarted(id);
117+
118+
const preInstallLog = await readFileContainer(
119+
id,
120+
`/home/coder/${moduleDirName}/pre_install.log`,
121+
);
122+
const installLog = await readFileContainer(
123+
id,
124+
`/home/coder/${moduleDirName}/install.log`,
125+
);
126+
const postInstallLog = await readFileContainer(
127+
id,
128+
`/home/coder/${moduleDirName}/post_install.log`,
129+
);
130+
131+
expect(preInstallLog).toContain("pre-install");
132+
expect(installLog).toContain("install");
133+
expect(postInstallLog).toContain("post-install");
134+
});
135+
136+
test("install-agentapi", async () => {
137+
const { id } = await setup({ skipAgentAPIMock: true });
138+
139+
const respModuleScript = await execModuleScript(id);
140+
expect(respModuleScript.exitCode).toBe(0);
141+
142+
await expectAgentAPIStarted(id);
143+
const respAgentAPI = await execContainer(id, [
144+
"bash",
145+
"-c",
146+
"agentapi --version",
147+
]);
148+
expect(respAgentAPI.exitCode).toBe(0);
149+
});
150+
});

0 commit comments

Comments
 (0)