From 82ac0f7ba65528537b15e44eefa62d3fca4a0e25 Mon Sep 17 00:00:00 2001 From: Labish Bardiya Date: Thu, 19 Nov 2026 10:48:49 +0530 Subject: [PATCH] test(opencode): cover subagent permission translation --- scripts/build-opencode-agents.js | 22 ++++++++++++------ scripts/build-opencode.js | 2 +- tests/opencode-agent-conversion.test.js | 31 +++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 8 deletions(-) create mode 100644 tests/opencode-agent-conversion.test.js diff --git a/scripts/build-opencode-agents.js b/scripts/build-opencode-agents.js index 5918a9634..a2f23b475 100644 --- a/scripts/build-opencode-agents.js +++ b/scripts/build-opencode-agents.js @@ -26,11 +26,7 @@ const agentNames = [ 'marketing-agent', ]; -fs.mkdirSync(outputDir, { recursive: true }); - -for (const name of agentNames) { - const sourcePath = path.join(repoRoot, 'agents', `${name}.md`); - const source = fs.readFileSync(sourcePath, 'utf8'); +function convertAgent(source, sourcePath) { const match = source.match(/^---\r?\n([\s\S]*?)\r?\n---\r?\n([\s\S]*)$/); if (!match) throw new Error(`Expected YAML frontmatter in ${sourcePath}`); @@ -49,6 +45,18 @@ for (const name of agentNames) { }, }; - const output = `---\n${yaml.dump(definition)}---\n${prompt}`; - fs.writeFileSync(path.join(outputDir, `${name}.md`), output); + return `---\n${yaml.dump(definition)}---\n${prompt}`; } + +function buildOpencodeAgents() { + fs.mkdirSync(outputDir, { recursive: true }); + + for (const name of agentNames) { + const sourcePath = path.join(repoRoot, 'agents', `${name}.md`); + const source = fs.readFileSync(sourcePath, 'utf8'); + const output = convertAgent(source, sourcePath); + fs.writeFileSync(path.join(outputDir, `${name}.md`), output); + } +} + +module.exports = { buildOpencodeAgents, convertAgent }; diff --git a/scripts/build-opencode.js b/scripts/build-opencode.js index 66286e9d2..50abcc21f 100644 --- a/scripts/build-opencode.js +++ b/scripts/build-opencode.js @@ -24,4 +24,4 @@ execFileSync(process.execPath, [tscEntrypoint, "-p", path.join(opencodeDir, "tsc cwd: rootDir, stdio: "inherit", }) -require("./build-opencode-agents") +require("./build-opencode-agents").buildOpencodeAgents() diff --git a/tests/opencode-agent-conversion.test.js b/tests/opencode-agent-conversion.test.js new file mode 100644 index 000000000..f2dde5fb5 --- /dev/null +++ b/tests/opencode-agent-conversion.test.js @@ -0,0 +1,31 @@ +const assert = require('node:assert/strict'); +const test = require('node:test'); +const yaml = require('js-yaml'); +const { convertAgent } = require('../scripts/build-opencode-agents'); + +const cases = [ + ['comma-separated read-only tools', 'Read, Grep, Glob', 'deny', 'deny'], + ['array with Edit', ['Read', 'Edit'], 'allow', 'deny'], + ['comma-separated Write and Bash', 'Read, Write, Bash', 'allow', 'ask'], + ['array with Bash and no edit tools', ['Read', 'Bash'], 'deny', 'ask'], +]; + +for (const [name, tools, edit, bash] of cases) { + test(`converts OpenCode agent permissions for ${name}`, () => { + const prompt = '## Review instructions\n\nKeep the original prompt.\n'; + const frontmatter = yaml.dump({ + description: 'Example reviewer', + model: 'sonnet', + tools, + }); + const output = convertAgent(`---\n${frontmatter}---\n${prompt}`, 'example.md'); + const [, generatedFrontmatter, generatedPrompt] = output.split('---\n'); + + assert.deepEqual(yaml.load(generatedFrontmatter), { + description: 'Example reviewer', + mode: 'subagent', + permission: { edit, bash }, + }); + assert.equal(generatedPrompt, prompt); + }); +}