import { assertEquals, assertStringIncludes } from "@std/assert"; import { BACKGROUND_CONTEXT as context } from "@earendil-works/chord/context"; import { getOrThrow } from "@earendil-works/pi-durable/env"; import { testExtension } from "@berry/api/testing.ts"; import { tempDir } from "@berry/util"; import code from "../../extensions/code/mod.ts"; import { skillsExtension } from "../../extensions/skills/mod.ts"; import { activeDirectory, hostEnvironment, writeSkill } from "../../extensions/skills/testing.ts"; import { BOOTSTRAP, Connection, RemoteEnvironment } from "./connection.ts"; Deno.test({ name: "host skills expose ordinary readable paths and executable scripts on a remote worker without running them at load time", ignore: Deno.build.os === "windows", }, async () => { await using home = await tempDir(); await using target = await tempDir(); const host = hostEnvironment(home.path); const main = await writeSkill( host, `${home.path}/.config/berry/skills`, "release", "Read references/checklist.txt and run scripts/check.sh when asked.", ); const source = main.slice(0, -"/SKILL.md".length); getOrThrow( await host.writeFile(`${source}/references/checklist.txt`, "Host-authored checklist.", context), ); getOrThrow( await host.writeFile( `${source}/scripts/check.sh`, '#!/bin/sh\nprintf "%s" "$BERRY_MACHINE" > executed\ncat executed\n', context, ), ); await Deno.chmod(`${source}/scripts/check.sh`, 0o755); const connection = await Connection.open("skills-test", [ "env", "BERRY_MACHINE=skills-test", `HOME=${target.path}`, `XDG_CONFIG_HOME=${target.path}/config`, `XDG_CACHE_HOME=${target.path}/cache`, Deno.execPath(), "eval", "--quiet", BOOTSTRAP, ]); const env = new RemoteEnvironment("skills-test", connection, target.path); const ext = testExtension(skillsExtension(host), { env }); const files = testExtension(code, { env }); try { assertStringIncludes(await ext.prompt(), '"name":"release"'); await ext.call("load_skill", { name: "release" }); const directory = await activeDirectory(env, await ext.prompt()); assertEquals(directory.startsWith(`${target.path}/`), true); assertStringIncludes( await files.call("read", { path: `${directory}/references/checklist.txt` }) as string, "Host-authored checklist.", ); assertEquals(await Deno.stat(`${home.path}/executed`).catch(() => null), null); assertEquals(getOrThrow(await env.exists("executed", context)), false); const result = await files.call("bash", { command: `${JSON.stringify(directory + "/scripts/check.sh")}`, timeout: 10, }) as { output: string; exitCode: number }; assertEquals(result.exitCode, 0); assertEquals(result.output, "skills-test"); assertEquals(getOrThrow(await env.readTextFile("executed", context)), "skills-test"); assertEquals(await Deno.stat(`${home.path}/executed`).catch(() => null), null); } finally { await ext.dispose(); await files.dispose(); await connection.close(); } });