Files
claude-engineering-plugin/tests/opencode-writer.test.ts
2026-02-20 13:28:25 -05:00

176 lines
6.7 KiB
TypeScript

import { describe, expect, test } from "bun:test"
import { promises as fs } from "fs"
import path from "path"
import os from "os"
import { writeOpenCodeBundle } from "../src/targets/opencode"
import type { OpenCodeBundle } from "../src/types/opencode"
async function exists(filePath: string): Promise<boolean> {
try {
await fs.access(filePath)
return true
} catch {
return false
}
}
describe("writeOpenCodeBundle", () => {
test("writes config, agents, plugins, and skills", async () => {
const tempRoot = await fs.mkdtemp(path.join(os.tmpdir(), "opencode-test-"))
const bundle: OpenCodeBundle = {
config: { $schema: "https://opencode.ai/config.json" },
agents: [{ name: "agent-one", content: "Agent content" }],
plugins: [{ name: "hook.ts", content: "export {}" }],
commandFiles: [],
skillDirs: [
{
name: "skill-one",
sourceDir: path.join(import.meta.dir, "fixtures", "sample-plugin", "skills", "skill-one"),
},
],
}
await writeOpenCodeBundle(tempRoot, bundle)
expect(await exists(path.join(tempRoot, "opencode.json"))).toBe(true)
expect(await exists(path.join(tempRoot, ".opencode", "agents", "agent-one.md"))).toBe(true)
expect(await exists(path.join(tempRoot, ".opencode", "plugins", "hook.ts"))).toBe(true)
expect(await exists(path.join(tempRoot, ".opencode", "skills", "skill-one", "SKILL.md"))).toBe(true)
})
test("writes directly into a .opencode output root", async () => {
const tempRoot = await fs.mkdtemp(path.join(os.tmpdir(), "opencode-root-"))
const outputRoot = path.join(tempRoot, ".opencode")
const bundle: OpenCodeBundle = {
config: { $schema: "https://opencode.ai/config.json" },
agents: [{ name: "agent-one", content: "Agent content" }],
plugins: [],
commandFiles: [],
skillDirs: [
{
name: "skill-one",
sourceDir: path.join(import.meta.dir, "fixtures", "sample-plugin", "skills", "skill-one"),
},
],
}
await writeOpenCodeBundle(outputRoot, bundle)
expect(await exists(path.join(outputRoot, "opencode.json"))).toBe(true)
expect(await exists(path.join(outputRoot, "agents", "agent-one.md"))).toBe(true)
expect(await exists(path.join(outputRoot, "skills", "skill-one", "SKILL.md"))).toBe(true)
expect(await exists(path.join(outputRoot, ".opencode"))).toBe(false)
})
test("writes directly into ~/.config/opencode style output root", async () => {
// Simulates the global install path: ~/.config/opencode
const tempRoot = await fs.mkdtemp(path.join(os.tmpdir(), "config-opencode-"))
const outputRoot = path.join(tempRoot, ".config", "opencode")
const bundle: OpenCodeBundle = {
config: { $schema: "https://opencode.ai/config.json" },
agents: [{ name: "agent-one", content: "Agent content" }],
plugins: [],
commandFiles: [],
skillDirs: [
{
name: "skill-one",
sourceDir: path.join(import.meta.dir, "fixtures", "sample-plugin", "skills", "skill-one"),
},
],
}
await writeOpenCodeBundle(outputRoot, bundle)
// Should write directly, not nested under .opencode
expect(await exists(path.join(outputRoot, "opencode.json"))).toBe(true)
expect(await exists(path.join(outputRoot, "agents", "agent-one.md"))).toBe(true)
expect(await exists(path.join(outputRoot, "skills", "skill-one", "SKILL.md"))).toBe(true)
expect(await exists(path.join(outputRoot, ".opencode"))).toBe(false)
})
test("backs up existing opencode.json before overwriting", async () => {
const tempRoot = await fs.mkdtemp(path.join(os.tmpdir(), "opencode-backup-"))
const outputRoot = path.join(tempRoot, ".opencode")
const configPath = path.join(outputRoot, "opencode.json")
// Create existing config
await fs.mkdir(outputRoot, { recursive: true })
const originalConfig = { $schema: "https://opencode.ai/config.json", custom: "value" }
await fs.writeFile(configPath, JSON.stringify(originalConfig, null, 2))
const bundle: OpenCodeBundle = {
config: { $schema: "https://opencode.ai/config.json", new: "config" },
agents: [],
plugins: [],
commandFiles: [],
skillDirs: [],
}
await writeOpenCodeBundle(outputRoot, bundle)
// New config should be written
const newConfig = JSON.parse(await fs.readFile(configPath, "utf8"))
expect(newConfig.new).toBe("config")
// Backup should exist with original content
const files = await fs.readdir(outputRoot)
const backupFileName = files.find((f) => f.startsWith("opencode.json.bak."))
expect(backupFileName).toBeDefined()
const backupContent = JSON.parse(await fs.readFile(path.join(outputRoot, backupFileName!), "utf8"))
expect(backupContent.custom).toBe("value")
})
test("writes command files as .md in commands/ directory", async () => {
const tempRoot = await fs.mkdtemp(path.join(os.tmpdir(), "opencode-cmd-"))
const outputRoot = path.join(tempRoot, ".config", "opencode")
const bundle: OpenCodeBundle = {
config: { $schema: "https://opencode.ai/config.json" },
agents: [],
plugins: [],
commandFiles: [{ name: "my-cmd", content: "---\ndescription: Test\n---\n\nDo something." }],
skillDirs: [],
}
await writeOpenCodeBundle(outputRoot, bundle)
const cmdPath = path.join(outputRoot, "commands", "my-cmd.md")
expect(await exists(cmdPath)).toBe(true)
const content = await fs.readFile(cmdPath, "utf8")
expect(content).toBe("---\ndescription: Test\n---\n\nDo something.\n")
})
test("backs up existing command .md file before overwriting", async () => {
const tempRoot = await fs.mkdtemp(path.join(os.tmpdir(), "opencode-cmd-backup-"))
const outputRoot = path.join(tempRoot, ".opencode")
const commandsDir = path.join(outputRoot, "commands")
await fs.mkdir(commandsDir, { recursive: true })
const cmdPath = path.join(commandsDir, "my-cmd.md")
await fs.writeFile(cmdPath, "old content\n")
const bundle: OpenCodeBundle = {
config: { $schema: "https://opencode.ai/config.json" },
agents: [],
plugins: [],
commandFiles: [{ name: "my-cmd", content: "---\ndescription: New\n---\n\nNew content." }],
skillDirs: [],
}
await writeOpenCodeBundle(outputRoot, bundle)
// New content should be written
const content = await fs.readFile(cmdPath, "utf8")
expect(content).toBe("---\ndescription: New\n---\n\nNew content.\n")
// Backup should exist
const files = await fs.readdir(commandsDir)
const backupFileName = files.find((f) => f.startsWith("my-cmd.md.bak."))
expect(backupFileName).toBeDefined()
const backupContent = await fs.readFile(path.join(commandsDir, backupFileName!), "utf8")
expect(backupContent).toBe("old content\n")
})
})