Menu
popagent
publicLatest change 6edc7e928c57dab23c0973ed3f7ee70b3ebb72d5 - Initial popagent baseline by AkurAI Build
import { afterAll, beforeAll, describe, expect, test } from "bun:test";
import { createWorkspaceTools, WORKSPACE_TOOLS } from "@mastra/core/workspace";
import { agentWorkspace } from "./workspace";
const testFile = `workspace-test-${crypto.randomUUID()}.txt`;
const filesystem = agentWorkspace.filesystem!;
const sandbox = agentWorkspace.sandbox!;
beforeAll(() => agentWorkspace.init());
afterAll(async () => {
await filesystem.deleteFile(testFile, { force: true });
await agentWorkspace.destroy();
});
describe("agentWorkspace", () => {
test("reads and writes files inside the contained workspace", async () => {
await filesystem.writeFile(testFile, "workspace file access");
expect(await filesystem.readFile(testFile, { encoding: "utf8" })).toBe("workspace file access");
expect(filesystem.basePath).toEndWith("/popagent/workspace");
});
test("requires a fresh read before an agent overwrites a file", async () => {
await filesystem.writeFile(testFile, "original");
const tools = await createWorkspaceTools(agentWorkspace);
const readFile = tools[WORKSPACE_TOOLS.FILESYSTEM.READ_FILE]!;
const writeFile = tools[WORKSPACE_TOOLS.FILESYSTEM.WRITE_FILE]!;
await expect(writeFile.execute!({
path: testFile,
content: "unreviewed overwrite",
overwrite: true,
}, {} as never)).rejects.toThrow("must read a file before writing");
await readFile.execute!({ path: testFile }, {} as never);
await writeFile.execute!({
path: testFile,
content: "reviewed overwrite",
overwrite: true,
}, {} as never);
expect(await filesystem.readFile(testFile, { encoding: "utf8" }))
.toBe("reviewed overwrite");
await expect(writeFile.execute!({
path: testFile,
content: "stale second overwrite",
overwrite: true,
}, {} as never)).rejects.toThrow("must read a file before writing");
});
test("rejects filesystem paths outside the workspace", async () => {
await expect(filesystem.readFile("../package.json")).rejects.toThrow();
});
test("executes shell commands from the workspace", async () => {
const result = await sandbox.executeCommand?.("pwd");
expect(result?.success).toBe(true);
expect(result?.stdout.trim()).toEndWith("/popagent/workspace");
const escaped = await sandbox.executeCommand?.("sh", ["-c", "test -e ../package.json"]);
expect(escaped?.exitCode).not.toBe(0);
});
});