Files
next-tjwater-agent/tests/runtime/opencodeConfig.test.ts
T
jiang 874bfba907
Generic Container CI/CD / test-build-publish (push) Successful in 56s
Agent CI/CD / build-test-publish-and-deploy (push) Successful in 56s
fix: harden report tools and guide investigation presentation
2026-11-02 15:23:34 +08:00

54 lines
3.1 KiB
TypeScript

import { describe, expect, it } from "bun:test";
import { readFile } from "node:fs/promises";
import { serviceOpencodeConfig } from "../../src/runtime/opencode2-config.js";
import { TASK_READER_AGENT } from "../../src/runtime/task-agent-policy.js";
describe("OpenCode 2 permissions", () => {
it("keeps one-shot recommendations separate from the business chat protocol and denies all tools", async () => {
const config = serviceOpencodeConfig({});
const generator = config.agents["prompt-suggestions"];
const source = await readFile(".opencode/agents/prompt-suggestions.md", "utf8");
expect(generator.system).toBe(source.split("\n---\n")[1].trimStart());
expect(generator.system).not.toBe(config.agents.instruction.system);
expect(generator.model).toBe(config.agents.instruction.model);
expect(generator.permissions).toEqual([{ action: "*", resource: "*", effect: "deny" }]);
});
it("loads complete prompt bodies while keeping version metadata out of every model instruction", async () => {
const bodies: Record<string, string> = {};
for (const name of ["instruction", "investigation", "prompt-suggestions"]) {
const source = await readFile(`.opencode/agents/${name}.md`, "utf8");
const [header, body] = source.split("\n---\n");
expect(header).toContain(`name: ${name}`);
expect(header).toMatch(/metadata:\n version: "\d+\.\d+\.\d+"/);
bodies[name] = body.trimStart();
}
const config = serviceOpencodeConfig({});
expect(config.agents.instruction.system).toBe(bodies.instruction);
expect(config.agents.investigation.system).toBe(`${bodies.instruction}\n\n${bodies.investigation}`);
expect(config.agents["prompt-suggestions"].system).toBe(bodies["prompt-suggestions"]);
});
it("keeps protected paths denied, shell/edit gated and subagents disabled", async () => {
const config = JSON.parse(await readFile("opencode.json", "utf8"));
const rules = config.permissions as { action: string; resource: string; effect: string }[];
const effect = (action: string, resource: string) => rules.find((rule) => rule.action === action && rule.resource === resource)?.effect;
expect(effect("*", "*")).toBe("ask");
expect(effect("external_directory", "*")).toBe("deny");
expect(effect("subagent", "*")).toBe("deny");
expect(effect("question", "*")).toBe("allow");
expect(effect("ask_user", "*")).toBe("allow");
expect(effect("todowrite", "*")).toBe("allow");
for (const action of ["read", "edit"]) {
for (const path of [".env", "*.env", "data/**", "**/logs/**"]) expect(effect(action, path)).toBe("deny");
}
expect(effect("edit", "*")).toBe("ask");
expect(effect("shell", "*")).toBe("ask");
for (const pattern of ["*.env*", "*data/*", "*logs/*"]) expect(effect("shell", pattern)).toBe("deny");
expect(config.permission).toBeUndefined();
expect(config.experimental?.continue_loop_on_deny).toBeUndefined();
});
it("lets each parent subagent call provide the reader prompt", () => {
const reader = serviceOpencodeConfig({}).agents[TASK_READER_AGENT];
expect(reader).not.toHaveProperty("system");
});
});