|
| 1 | +import { describe, expect, test } from "bun:test" |
| 2 | +import { convertClaudeToQwen } from "../src/converters/claude-to-qwen" |
| 3 | +import { parseFrontmatter } from "../src/utils/frontmatter" |
| 4 | +import type { ClaudePlugin } from "../src/types/claude" |
| 5 | + |
| 6 | +const fixturePlugin: ClaudePlugin = { |
| 7 | + root: "/tmp/plugin", |
| 8 | + manifest: { name: "compound-engineering", version: "1.2.0", description: "A plugin for engineers" }, |
| 9 | + agents: [ |
| 10 | + { |
| 11 | + name: "security-sentinel", |
| 12 | + description: "Security-focused agent", |
| 13 | + capabilities: ["Threat modeling", "OWASP"], |
| 14 | + model: "claude-sonnet-4-20250514", |
| 15 | + body: "Focus on vulnerabilities in ~/.claude/settings.", |
| 16 | + sourcePath: "/tmp/plugin/agents/security-sentinel.md", |
| 17 | + }, |
| 18 | + { |
| 19 | + name: "brainstorm-agent", |
| 20 | + description: "Creative brainstormer", |
| 21 | + model: "inherit", |
| 22 | + body: "Generate ideas.", |
| 23 | + sourcePath: "/tmp/plugin/agents/brainstorm-agent.md", |
| 24 | + }, |
| 25 | + ], |
| 26 | + commands: [ |
| 27 | + { |
| 28 | + name: "workflows:plan", |
| 29 | + description: "Planning command", |
| 30 | + argumentHint: "[FOCUS]", |
| 31 | + model: "inherit", |
| 32 | + allowedTools: ["Read"], |
| 33 | + body: "Plan the work. Config at ~/.claude/settings.", |
| 34 | + sourcePath: "/tmp/plugin/commands/workflows/plan.md", |
| 35 | + }, |
| 36 | + { |
| 37 | + name: "disabled-cmd", |
| 38 | + description: "Disabled", |
| 39 | + model: "inherit", |
| 40 | + allowedTools: [], |
| 41 | + body: "Should be excluded.", |
| 42 | + disableModelInvocation: true, |
| 43 | + sourcePath: "/tmp/plugin/commands/disabled-cmd.md", |
| 44 | + }, |
| 45 | + ], |
| 46 | + skills: [ |
| 47 | + { |
| 48 | + name: "existing-skill", |
| 49 | + description: "Existing skill", |
| 50 | + sourceDir: "/tmp/plugin/skills/existing-skill", |
| 51 | + skillPath: "/tmp/plugin/skills/existing-skill/SKILL.md", |
| 52 | + }, |
| 53 | + ], |
| 54 | + hooks: undefined, |
| 55 | + mcpServers: { |
| 56 | + local: { command: "npx", args: ["-y", "some-mcp"], env: { API_KEY: "${YOUR_API_KEY}" } }, |
| 57 | + remote: { url: "https://mcp.example.com/api", headers: { Authorization: "Bearer token" } }, |
| 58 | + }, |
| 59 | +} |
| 60 | + |
| 61 | +const defaultOptions = { |
| 62 | + agentMode: "subagent" as const, |
| 63 | + inferTemperature: false, |
| 64 | +} |
| 65 | + |
| 66 | +describe("convertClaudeToQwen", () => { |
| 67 | + test("converts agents to yaml format with frontmatter", () => { |
| 68 | + const bundle = convertClaudeToQwen(fixturePlugin, defaultOptions) |
| 69 | + |
| 70 | + const agent = bundle.agents.find((a) => a.name === "security-sentinel") |
| 71 | + expect(agent).toBeDefined() |
| 72 | + expect(agent!.format).toBe("yaml") |
| 73 | + const parsed = parseFrontmatter(agent!.content) |
| 74 | + expect(parsed.data.name).toBe("security-sentinel") |
| 75 | + expect(parsed.data.description).toBe("Security-focused agent") |
| 76 | + expect(parsed.data.model).toBe("anthropic/claude-sonnet-4-20250514") |
| 77 | + expect(parsed.body).toContain("Focus on vulnerabilities") |
| 78 | + }) |
| 79 | + |
| 80 | + test("agent with inherit model has no model field in frontmatter", () => { |
| 81 | + const bundle = convertClaudeToQwen(fixturePlugin, defaultOptions) |
| 82 | + const agent = bundle.agents.find((a) => a.name === "brainstorm-agent") |
| 83 | + expect(agent).toBeDefined() |
| 84 | + const parsed = parseFrontmatter(agent!.content) |
| 85 | + expect(parsed.data.model).toBeUndefined() |
| 86 | + }) |
| 87 | + |
| 88 | + test("inferTemperature injects temperature based on agent name/description", () => { |
| 89 | + const bundle = convertClaudeToQwen(fixturePlugin, { ...defaultOptions, inferTemperature: true }) |
| 90 | + |
| 91 | + const sentinel = bundle.agents.find((a) => a.name === "security-sentinel") |
| 92 | + const parsed = parseFrontmatter(sentinel!.content) |
| 93 | + expect(parsed.data.temperature).toBe(0.1) // review/security → 0.1 |
| 94 | + |
| 95 | + const brainstorm = bundle.agents.find((a) => a.name === "brainstorm-agent") |
| 96 | + const bParsed = parseFrontmatter(brainstorm!.content) |
| 97 | + expect(bParsed.data.temperature).toBe(0.6) // brainstorm → 0.6 |
| 98 | + }) |
| 99 | + |
| 100 | + test("inferTemperature returns undefined for unrecognized agents (no temperature set)", () => { |
| 101 | + const plugin: ClaudePlugin = { |
| 102 | + ...fixturePlugin, |
| 103 | + agents: [{ name: "my-helper", description: "Generic helper", model: "inherit", body: "help", sourcePath: "/tmp/a.md" }], |
| 104 | + } |
| 105 | + const bundle = convertClaudeToQwen(plugin, { ...defaultOptions, inferTemperature: true }) |
| 106 | + const agent = bundle.agents[0] |
| 107 | + const parsed = parseFrontmatter(agent.content) |
| 108 | + expect(parsed.data.temperature).toBeUndefined() |
| 109 | + }) |
| 110 | + |
| 111 | + test("converts commands to command files excluding disableModelInvocation", () => { |
| 112 | + const bundle = convertClaudeToQwen(fixturePlugin, defaultOptions) |
| 113 | + |
| 114 | + const planCmd = bundle.commandFiles.find((c) => c.name === "workflows:plan") |
| 115 | + expect(planCmd).toBeDefined() |
| 116 | + const parsed = parseFrontmatter(planCmd!.content) |
| 117 | + expect(parsed.data.description).toBe("Planning command") |
| 118 | + expect(parsed.data.allowedTools).toEqual(["Read"]) |
| 119 | + |
| 120 | + const disabled = bundle.commandFiles.find((c) => c.name === "disabled-cmd") |
| 121 | + expect(disabled).toBeUndefined() |
| 122 | + }) |
| 123 | + |
| 124 | + test("config uses plugin manifest name and version", () => { |
| 125 | + const bundle = convertClaudeToQwen(fixturePlugin, defaultOptions) |
| 126 | + expect(bundle.config.name).toBe("compound-engineering") |
| 127 | + expect(bundle.config.version).toBe("1.2.0") |
| 128 | + expect(bundle.config.commands).toBe("commands") |
| 129 | + expect(bundle.config.skills).toBe("skills") |
| 130 | + expect(bundle.config.agents).toBe("agents") |
| 131 | + }) |
| 132 | + |
| 133 | + test("stdio MCP servers are included in config", () => { |
| 134 | + const bundle = convertClaudeToQwen(fixturePlugin, defaultOptions) |
| 135 | + expect(bundle.config.mcpServers).toBeDefined() |
| 136 | + const local = bundle.config.mcpServers!.local |
| 137 | + expect(local.command).toBe("npx") |
| 138 | + expect(local.args).toEqual(["-y", "some-mcp"]) |
| 139 | + // No cwd field |
| 140 | + expect((local as any).cwd).toBeUndefined() |
| 141 | + }) |
| 142 | + |
| 143 | + test("remote MCP servers are skipped with a warning (not converted to curl)", () => { |
| 144 | + const bundle = convertClaudeToQwen(fixturePlugin, defaultOptions) |
| 145 | + // Only local (stdio) server should be present |
| 146 | + expect(bundle.config.mcpServers).toBeDefined() |
| 147 | + expect(bundle.config.mcpServers!.remote).toBeUndefined() |
| 148 | + expect(bundle.config.mcpServers!.local).toBeDefined() |
| 149 | + }) |
| 150 | + |
| 151 | + test("placeholder env vars are extracted as settings", () => { |
| 152 | + const bundle = convertClaudeToQwen(fixturePlugin, defaultOptions) |
| 153 | + expect(bundle.config.settings).toBeDefined() |
| 154 | + const apiKeySetting = bundle.config.settings!.find((s) => s.envVar === "API_KEY") |
| 155 | + expect(apiKeySetting).toBeDefined() |
| 156 | + expect(apiKeySetting!.sensitive).toBe(true) |
| 157 | + expect(apiKeySetting!.name).toBe("Api Key") |
| 158 | + }) |
| 159 | + |
| 160 | + test("plugin with no MCP servers has no mcpServers in config", () => { |
| 161 | + const plugin: ClaudePlugin = { ...fixturePlugin, mcpServers: undefined } |
| 162 | + const bundle = convertClaudeToQwen(plugin, defaultOptions) |
| 163 | + expect(bundle.config.mcpServers).toBeUndefined() |
| 164 | + }) |
| 165 | + |
| 166 | + test("context file uses plugin.manifest.name and manifest.description", () => { |
| 167 | + const bundle = convertClaudeToQwen(fixturePlugin, defaultOptions) |
| 168 | + expect(bundle.contextFile).toContain("# compound-engineering") |
| 169 | + expect(bundle.contextFile).toContain("A plugin for engineers") |
| 170 | + expect(bundle.contextFile).toContain("## Agents") |
| 171 | + expect(bundle.contextFile).toContain("security-sentinel") |
| 172 | + expect(bundle.contextFile).toContain("## Commands") |
| 173 | + expect(bundle.contextFile).toContain("/workflows:plan") |
| 174 | + // Disabled commands excluded |
| 175 | + expect(bundle.contextFile).not.toContain("disabled-cmd") |
| 176 | + expect(bundle.contextFile).toContain("## Skills") |
| 177 | + expect(bundle.contextFile).toContain("existing-skill") |
| 178 | + }) |
| 179 | + |
| 180 | + test("paths are rewritten from .claude/ to .qwen/ in agent and command content", () => { |
| 181 | + const bundle = convertClaudeToQwen(fixturePlugin, defaultOptions) |
| 182 | + |
| 183 | + const agent = bundle.agents.find((a) => a.name === "security-sentinel") |
| 184 | + expect(agent!.content).toContain("~/.qwen/settings") |
| 185 | + expect(agent!.content).not.toContain("~/.claude/settings") |
| 186 | + |
| 187 | + const cmd = bundle.commandFiles.find((c) => c.name === "workflows:plan") |
| 188 | + expect(cmd!.content).toContain("~/.qwen/settings") |
| 189 | + expect(cmd!.content).not.toContain("~/.claude/settings") |
| 190 | + }) |
| 191 | + |
| 192 | + test("opencode paths are NOT rewritten (only claude paths)", () => { |
| 193 | + const plugin: ClaudePlugin = { |
| 194 | + ...fixturePlugin, |
| 195 | + agents: [ |
| 196 | + { |
| 197 | + name: "test-agent", |
| 198 | + description: "test", |
| 199 | + model: "inherit", |
| 200 | + body: "See .opencode/config and ~/.config/opencode/settings", |
| 201 | + sourcePath: "/tmp/a.md", |
| 202 | + }, |
| 203 | + ], |
| 204 | + } |
| 205 | + const bundle = convertClaudeToQwen(plugin, defaultOptions) |
| 206 | + const agent = bundle.agents[0] |
| 207 | + // opencode paths should NOT be rewritten |
| 208 | + expect(agent.content).toContain(".opencode/config") |
| 209 | + expect(agent.content).not.toContain(".qwen/config") |
| 210 | + }) |
| 211 | + |
| 212 | + test("skillDirs passes through original skills", () => { |
| 213 | + const bundle = convertClaudeToQwen(fixturePlugin, defaultOptions) |
| 214 | + const skill = bundle.skillDirs.find((s) => s.name === "existing-skill") |
| 215 | + expect(skill).toBeDefined() |
| 216 | + expect(skill!.sourceDir).toBe("/tmp/plugin/skills/existing-skill") |
| 217 | + }) |
| 218 | + |
| 219 | + test("normalizeModel prefixes claude models with anthropic/", () => { |
| 220 | + const plugin: ClaudePlugin = { |
| 221 | + ...fixturePlugin, |
| 222 | + agents: [{ name: "a", description: "d", model: "claude-opus-4-5", body: "b", sourcePath: "/tmp/a.md" }], |
| 223 | + } |
| 224 | + const bundle = convertClaudeToQwen(plugin, defaultOptions) |
| 225 | + const parsed = parseFrontmatter(bundle.agents[0].content) |
| 226 | + expect(parsed.data.model).toBe("anthropic/claude-opus-4-5") |
| 227 | + }) |
| 228 | + |
| 229 | + test("normalizeModel passes through already-namespaced models unchanged", () => { |
| 230 | + const plugin: ClaudePlugin = { |
| 231 | + ...fixturePlugin, |
| 232 | + agents: [{ name: "a", description: "d", model: "google/gemini-2.0", body: "b", sourcePath: "/tmp/a.md" }], |
| 233 | + } |
| 234 | + const bundle = convertClaudeToQwen(plugin, defaultOptions) |
| 235 | + const parsed = parseFrontmatter(bundle.agents[0].content) |
| 236 | + expect(parsed.data.model).toBe("google/gemini-2.0") |
| 237 | + }) |
| 238 | +}) |
0 commit comments