|
| 1 | +import fs from "node:fs/promises"; |
| 2 | +import os from "node:os"; |
| 3 | +import path from "node:path"; |
| 4 | +import { afterEach, describe, expect, it } from "vitest"; |
| 5 | +import { DEFAULT_AGENTS_FILENAME } from "../workspace.js"; |
| 6 | +import { ensureSandboxWorkspace } from "./workspace.js"; |
| 7 | + |
| 8 | +const tempRoots: string[] = []; |
| 9 | + |
| 10 | +async function makeTempRoot(): Promise<string> { |
| 11 | + const root = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-sandbox-workspace-")); |
| 12 | + tempRoots.push(root); |
| 13 | + return root; |
| 14 | +} |
| 15 | + |
| 16 | +afterEach(async () => { |
| 17 | + await Promise.all( |
| 18 | + tempRoots.splice(0).map((root) => fs.rm(root, { recursive: true, force: true })), |
| 19 | + ); |
| 20 | +}); |
| 21 | + |
| 22 | +describe("ensureSandboxWorkspace", () => { |
| 23 | + it("seeds regular bootstrap files from the source workspace", async () => { |
| 24 | + const root = await makeTempRoot(); |
| 25 | + const seed = path.join(root, "seed"); |
| 26 | + const sandbox = path.join(root, "sandbox"); |
| 27 | + await fs.mkdir(seed, { recursive: true }); |
| 28 | + await fs.writeFile(path.join(seed, DEFAULT_AGENTS_FILENAME), "seeded-agents", "utf-8"); |
| 29 | + |
| 30 | + await ensureSandboxWorkspace(sandbox, seed, true); |
| 31 | + |
| 32 | + await expect(fs.readFile(path.join(sandbox, DEFAULT_AGENTS_FILENAME), "utf-8")).resolves.toBe( |
| 33 | + "seeded-agents", |
| 34 | + ); |
| 35 | + }); |
| 36 | + |
| 37 | + it.runIf(process.platform !== "win32")("skips symlinked bootstrap seed files", async () => { |
| 38 | + const root = await makeTempRoot(); |
| 39 | + const seed = path.join(root, "seed"); |
| 40 | + const sandbox = path.join(root, "sandbox"); |
| 41 | + const outside = path.join(root, "outside-secret.txt"); |
| 42 | + await fs.mkdir(seed, { recursive: true }); |
| 43 | + await fs.writeFile(outside, "secret", "utf-8"); |
| 44 | + await fs.symlink(outside, path.join(seed, DEFAULT_AGENTS_FILENAME)); |
| 45 | + |
| 46 | + await ensureSandboxWorkspace(sandbox, seed, true); |
| 47 | + |
| 48 | + await expect( |
| 49 | + fs.readFile(path.join(sandbox, DEFAULT_AGENTS_FILENAME), "utf-8"), |
| 50 | + ).rejects.toBeDefined(); |
| 51 | + }); |
| 52 | + |
| 53 | + it.runIf(process.platform !== "win32")("skips hardlinked bootstrap seed files", async () => { |
| 54 | + const root = await makeTempRoot(); |
| 55 | + const seed = path.join(root, "seed"); |
| 56 | + const sandbox = path.join(root, "sandbox"); |
| 57 | + const outside = path.join(root, "outside-agents.txt"); |
| 58 | + const linkedSeed = path.join(seed, DEFAULT_AGENTS_FILENAME); |
| 59 | + await fs.mkdir(seed, { recursive: true }); |
| 60 | + await fs.writeFile(outside, "outside", "utf-8"); |
| 61 | + try { |
| 62 | + await fs.link(outside, linkedSeed); |
| 63 | + } catch (error) { |
| 64 | + if ((error as NodeJS.ErrnoException).code === "EXDEV") { |
| 65 | + return; |
| 66 | + } |
| 67 | + throw error; |
| 68 | + } |
| 69 | + |
| 70 | + await ensureSandboxWorkspace(sandbox, seed, true); |
| 71 | + |
| 72 | + await expect( |
| 73 | + fs.readFile(path.join(sandbox, DEFAULT_AGENTS_FILENAME), "utf-8"), |
| 74 | + ).rejects.toBeDefined(); |
| 75 | + }); |
| 76 | +}); |
0 commit comments