Menu
popagent
publicLatest change 1a866d65ff74cc84d2d26f9e1c1d7a5d0302b45a - Make workspace contracts deployment-path independent by AkurAI Build
import { afterAll, beforeAll, describe, expect, test } from "bun:test";
import { resolve } from "node:path";
import { createWorkspaceTools, WORKSPACE_TOOLS } from "@mastra/core/workspace";
import { agentWorkspace } from "./workspace";
const testFile = `workspace-test-${crypto.randomUUID()}.txt`;
const workspaceRoot = resolve(process.env.POPAGENT_WORKSPACE_ROOT ?? "workspace");
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).toBe(workspaceRoot);
});
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()).toBe(workspaceRoot);
const escaped = await sandbox.executeCommand?.("sh", ["-c", "test -e ../package.json"]);
expect(escaped?.exitCode).not.toBe(0);
});
});