Open History to see the last commit
1import * as NodeAssert from "node:assert/strict";
2
3import * as RegExpUtils from "effect/RegExp";
4import { describe, it } from "vite-plus/test";
5
6import * as OpenCodeRuntime from "./OpenCodeRuntime.ts";
7
8function actionFor(
9 runtimeMode: Parameters<typeof OpenCodeRuntime.buildOpenCodePermissionRules>[0],
10 permission: string,
11 target = "*",
12) {
13 // OpenCode uses the last matching rule. Its wildcards match directory separators.
14 return OpenCodeRuntime.buildOpenCodePermissionRules(runtimeMode).findLast(
15 (rule) =>
16 (rule.permission === "*" || rule.permission === permission) &&
17 new RegExp(`^${RegExpUtils.escape(rule.pattern).replaceAll("\\*", ".*")}$`, "s").test(target),
18 )?.action;
19}
20
21describe("buildOpenCodePermissionRules", () => {
22 it("pre-approves edits once the user has chosen to auto-accept them", () => {
23 NodeAssert.equal(actionFor("auto-accept-edits", "edit"), "allow");
24 });
25
26 it("still asks before editing when approval is required", () => {
27 NodeAssert.equal(actionFor("approval-required", "edit"), "ask");
28 });
29
30 it("leaves auto asking, as the docs say it does without a reviewer", () => {
31 NodeAssert.equal(actionFor("auto", "edit"), "ask");
32 });
33
34 it("allows workspace reads and task updates without asking in supervised modes", () => {
35 for (const runtimeMode of ["approval-required", "auto-accept-edits", "auto"] as const) {
36 for (const permission of ["read", "glob", "grep", "lsp", "skill", "todowrite"]) {
37 NodeAssert.equal(actionFor(runtimeMode, permission, "src/index.ts"), "allow");
38 }
39 }
40 });
41
42 it("preserves OpenCode's environment-file approval rules", () => {
43 for (const runtimeMode of ["approval-required", "auto-accept-edits", "auto"] as const) {
44 for (const target of [
45 ".env",
46 ".env.local",
47 "config/service.env",
48 "config/service.env.local",
49 ]) {
50 NodeAssert.equal(actionFor(runtimeMode, "read", target), "ask");
51 }
52 for (const target of [".env.example", "config/service.env.example"]) {
53 NodeAssert.equal(actionFor(runtimeMode, "read", target), "allow");
54 }
55 }
56 });
57
58 it("still asks before commands, network access, external directories and unknown tools", () => {
59 for (const runtimeMode of ["approval-required", "auto-accept-edits", "auto"] as const) {
60 NodeAssert.equal(actionFor(runtimeMode, "bash"), "ask");
61 NodeAssert.equal(actionFor(runtimeMode, "webfetch"), "ask");
62 NodeAssert.equal(actionFor(runtimeMode, "websearch"), "ask");
63 NodeAssert.equal(actionFor(runtimeMode, "external_directory"), "ask");
64 NodeAssert.equal(actionFor(runtimeMode, "doom_loop"), "ask");
65 NodeAssert.equal(actionFor(runtimeMode, "custom_tool"), "ask");
66 }
67 });
68
69 it("allows everything only under full access", () => {
70 NodeAssert.deepEqual(OpenCodeRuntime.buildOpenCodePermissionRules("full-access"), [
71 { permission: "*", pattern: "*", action: "allow" },
72 { permission: "external_directory", pattern: "*", action: "allow" },
73 ]);
74 });
75});
76
77describe("toOpenCodePermissionReply", () => {
78 it.each([
79 ["accept", "once"],
80 ["acceptForSession", "always"],
81 ["acceptAlways", "always"],
82 ["decline", "reject"],
83 ["cancel", "reject"],
84 ] as const)("maps %s to %s", (decision, reply) => {
85 NodeAssert.equal(OpenCodeRuntime.toOpenCodePermissionReply(decision), reply);
86 });
87});