Merge branch 'main' into feat/copilot-converter-target

This commit is contained in:
Brayan Jules
2026-02-15 09:25:05 -03:00
committed by GitHub
16 changed files with 1369 additions and 7 deletions

View File

@@ -0,0 +1,373 @@
import { describe, expect, test } from "bun:test"
import { convertClaudeToGemini, toToml, transformContentForGemini } from "../src/converters/claude-to-gemini"
import { parseFrontmatter } from "../src/utils/frontmatter"
import type { ClaudePlugin } from "../src/types/claude"
const fixturePlugin: ClaudePlugin = {
root: "/tmp/plugin",
manifest: { name: "fixture", version: "1.0.0" },
agents: [
{
name: "Security Reviewer",
description: "Security-focused agent",
capabilities: ["Threat modeling", "OWASP"],
model: "claude-sonnet-4-20250514",
body: "Focus on vulnerabilities.",
sourcePath: "/tmp/plugin/agents/security-reviewer.md",
},
],
commands: [
{
name: "workflows:plan",
description: "Planning command",
argumentHint: "[FOCUS]",
model: "inherit",
allowedTools: ["Read"],
body: "Plan the work.",
sourcePath: "/tmp/plugin/commands/workflows/plan.md",
},
],
skills: [
{
name: "existing-skill",
description: "Existing skill",
sourceDir: "/tmp/plugin/skills/existing-skill",
skillPath: "/tmp/plugin/skills/existing-skill/SKILL.md",
},
],
hooks: undefined,
mcpServers: {
local: { command: "echo", args: ["hello"] },
},
}
describe("convertClaudeToGemini", () => {
test("converts agents to skills with SKILL.md frontmatter", () => {
const bundle = convertClaudeToGemini(fixturePlugin, {
agentMode: "subagent",
inferTemperature: false,
permissions: "none",
})
const skill = bundle.generatedSkills.find((s) => s.name === "security-reviewer")
expect(skill).toBeDefined()
const parsed = parseFrontmatter(skill!.content)
expect(parsed.data.name).toBe("security-reviewer")
expect(parsed.data.description).toBe("Security-focused agent")
expect(parsed.body).toContain("Focus on vulnerabilities.")
})
test("agent with capabilities prepended to body", () => {
const bundle = convertClaudeToGemini(fixturePlugin, {
agentMode: "subagent",
inferTemperature: false,
permissions: "none",
})
const skill = bundle.generatedSkills.find((s) => s.name === "security-reviewer")
expect(skill).toBeDefined()
const parsed = parseFrontmatter(skill!.content)
expect(parsed.body).toContain("## Capabilities")
expect(parsed.body).toContain("- Threat modeling")
expect(parsed.body).toContain("- OWASP")
})
test("agent with empty description gets default description", () => {
const plugin: ClaudePlugin = {
...fixturePlugin,
agents: [
{
name: "my-agent",
body: "Do things.",
sourcePath: "/tmp/plugin/agents/my-agent.md",
},
],
commands: [],
skills: [],
}
const bundle = convertClaudeToGemini(plugin, {
agentMode: "subagent",
inferTemperature: false,
permissions: "none",
})
const parsed = parseFrontmatter(bundle.generatedSkills[0].content)
expect(parsed.data.description).toBe("Use this skill for my-agent tasks")
})
test("agent model field silently dropped", () => {
const bundle = convertClaudeToGemini(fixturePlugin, {
agentMode: "subagent",
inferTemperature: false,
permissions: "none",
})
const skill = bundle.generatedSkills.find((s) => s.name === "security-reviewer")
const parsed = parseFrontmatter(skill!.content)
expect(parsed.data.model).toBeUndefined()
})
test("agent with empty body gets default body text", () => {
const plugin: ClaudePlugin = {
...fixturePlugin,
agents: [
{
name: "Empty Agent",
description: "An empty agent",
body: "",
sourcePath: "/tmp/plugin/agents/empty.md",
},
],
commands: [],
skills: [],
}
const bundle = convertClaudeToGemini(plugin, {
agentMode: "subagent",
inferTemperature: false,
permissions: "none",
})
const parsed = parseFrontmatter(bundle.generatedSkills[0].content)
expect(parsed.body).toContain("Instructions converted from the Empty Agent agent.")
})
test("converts commands to TOML with prompt and description", () => {
const bundle = convertClaudeToGemini(fixturePlugin, {
agentMode: "subagent",
inferTemperature: false,
permissions: "none",
})
expect(bundle.commands).toHaveLength(1)
const command = bundle.commands[0]
expect(command.name).toBe("workflows/plan")
expect(command.content).toContain('description = "Planning command"')
expect(command.content).toContain('prompt = """')
expect(command.content).toContain("Plan the work.")
})
test("namespaced command creates correct path", () => {
const bundle = convertClaudeToGemini(fixturePlugin, {
agentMode: "subagent",
inferTemperature: false,
permissions: "none",
})
const command = bundle.commands.find((c) => c.name === "workflows/plan")
expect(command).toBeDefined()
})
test("command with argument-hint gets {{args}} placeholder", () => {
const bundle = convertClaudeToGemini(fixturePlugin, {
agentMode: "subagent",
inferTemperature: false,
permissions: "none",
})
const command = bundle.commands[0]
expect(command.content).toContain("{{args}}")
})
test("command with disable-model-invocation is still included", () => {
const plugin: ClaudePlugin = {
...fixturePlugin,
commands: [
{
name: "disabled-command",
description: "Disabled command",
disableModelInvocation: true,
body: "Disabled body.",
sourcePath: "/tmp/plugin/commands/disabled.md",
},
],
agents: [],
skills: [],
}
const bundle = convertClaudeToGemini(plugin, {
agentMode: "subagent",
inferTemperature: false,
permissions: "none",
})
// Gemini TOML commands are prompts, not code — always include
expect(bundle.commands).toHaveLength(1)
expect(bundle.commands[0].name).toBe("disabled-command")
})
test("command allowedTools silently dropped", () => {
const bundle = convertClaudeToGemini(fixturePlugin, {
agentMode: "subagent",
inferTemperature: false,
permissions: "none",
})
const command = bundle.commands[0]
expect(command.content).not.toContain("allowedTools")
expect(command.content).not.toContain("Read")
})
test("skills pass through as directory references", () => {
const bundle = convertClaudeToGemini(fixturePlugin, {
agentMode: "subagent",
inferTemperature: false,
permissions: "none",
})
expect(bundle.skillDirs).toHaveLength(1)
expect(bundle.skillDirs[0].name).toBe("existing-skill")
expect(bundle.skillDirs[0].sourceDir).toBe("/tmp/plugin/skills/existing-skill")
})
test("MCP servers convert to settings.json-compatible config", () => {
const bundle = convertClaudeToGemini(fixturePlugin, {
agentMode: "subagent",
inferTemperature: false,
permissions: "none",
})
expect(bundle.mcpServers?.local?.command).toBe("echo")
expect(bundle.mcpServers?.local?.args).toEqual(["hello"])
})
test("plugin with zero agents produces empty generatedSkills", () => {
const plugin: ClaudePlugin = {
...fixturePlugin,
agents: [],
commands: [],
skills: [],
}
const bundle = convertClaudeToGemini(plugin, {
agentMode: "subagent",
inferTemperature: false,
permissions: "none",
})
expect(bundle.generatedSkills).toHaveLength(0)
})
test("plugin with only skills works correctly", () => {
const plugin: ClaudePlugin = {
...fixturePlugin,
agents: [],
commands: [],
}
const bundle = convertClaudeToGemini(plugin, {
agentMode: "subagent",
inferTemperature: false,
permissions: "none",
})
expect(bundle.generatedSkills).toHaveLength(0)
expect(bundle.skillDirs).toHaveLength(1)
expect(bundle.commands).toHaveLength(0)
})
test("agent name colliding with skill name gets deduplicated", () => {
const plugin: ClaudePlugin = {
...fixturePlugin,
skills: [{ name: "security-reviewer", description: "Existing skill", sourceDir: "/tmp/skill", skillPath: "/tmp/skill/SKILL.md" }],
agents: [{ name: "Security Reviewer", description: "Agent version", body: "Body.", sourcePath: "/tmp/agents/sr.md" }],
commands: [],
}
const bundle = convertClaudeToGemini(plugin, {
agentMode: "subagent",
inferTemperature: false,
permissions: "none",
})
// Agent should be deduplicated since skill already has "security-reviewer"
expect(bundle.generatedSkills[0].name).toBe("security-reviewer-2")
expect(bundle.skillDirs[0].name).toBe("security-reviewer")
})
test("hooks present emits console.warn", () => {
const warnings: string[] = []
const originalWarn = console.warn
console.warn = (msg: string) => warnings.push(msg)
const plugin: ClaudePlugin = {
...fixturePlugin,
hooks: { hooks: { PreToolUse: [{ matcher: "*", body: "hook body" }] } },
agents: [],
commands: [],
skills: [],
}
convertClaudeToGemini(plugin, {
agentMode: "subagent",
inferTemperature: false,
permissions: "none",
})
console.warn = originalWarn
expect(warnings.some((w) => w.includes("Gemini"))).toBe(true)
})
})
describe("transformContentForGemini", () => {
test("transforms .claude/ paths to .gemini/", () => {
const result = transformContentForGemini("Read .claude/settings.json for config.")
expect(result).toContain(".gemini/settings.json")
expect(result).not.toContain(".claude/")
})
test("transforms ~/.claude/ paths to ~/.gemini/", () => {
const result = transformContentForGemini("Check ~/.claude/config for settings.")
expect(result).toContain("~/.gemini/config")
expect(result).not.toContain("~/.claude/")
})
test("transforms Task agent(args) to natural language skill reference", () => {
const input = `Run these:
- Task repo-research-analyst(feature_description)
- Task learnings-researcher(feature_description)
Task best-practices-researcher(topic)`
const result = transformContentForGemini(input)
expect(result).toContain("Use the repo-research-analyst skill to: feature_description")
expect(result).toContain("Use the learnings-researcher skill to: feature_description")
expect(result).toContain("Use the best-practices-researcher skill to: topic")
expect(result).not.toContain("Task repo-research-analyst")
})
test("transforms @agent references to skill references", () => {
const result = transformContentForGemini("Ask @security-sentinel for a review.")
expect(result).toContain("the security-sentinel skill")
expect(result).not.toContain("@security-sentinel")
})
})
describe("toToml", () => {
test("produces valid TOML with description and prompt", () => {
const result = toToml("A description", "The prompt content")
expect(result).toContain('description = "A description"')
expect(result).toContain('prompt = """')
expect(result).toContain("The prompt content")
expect(result).toContain('"""')
})
test("escapes quotes in description", () => {
const result = toToml('Say "hello"', "Prompt")
expect(result).toContain('description = "Say \\"hello\\""')
})
test("escapes triple quotes in prompt", () => {
const result = toToml("A command", 'Content with """ inside it')
// Should not contain an unescaped """ that would close the TOML multi-line string prematurely
// The prompt section should have the escaped version
expect(result).toContain('description = "A command"')
expect(result).toContain('prompt = """')
// The inner """ should be escaped
expect(result).not.toMatch(/""".*""".*"""/s) // Should not have 3 separate triple-quote sequences (open, content, close would make 3)
// Verify it contains the escaped form
expect(result).toContain('\\"\\"\\"')
})
})

181
tests/gemini-writer.test.ts Normal file
View File

@@ -0,0 +1,181 @@
import { describe, expect, test } from "bun:test"
import { promises as fs } from "fs"
import path from "path"
import os from "os"
import { writeGeminiBundle } from "../src/targets/gemini"
import type { GeminiBundle } from "../src/types/gemini"
async function exists(filePath: string): Promise<boolean> {
try {
await fs.access(filePath)
return true
} catch {
return false
}
}
describe("writeGeminiBundle", () => {
test("writes skills, commands, and settings.json", async () => {
const tempRoot = await fs.mkdtemp(path.join(os.tmpdir(), "gemini-test-"))
const bundle: GeminiBundle = {
generatedSkills: [
{
name: "security-reviewer",
content: "---\nname: security-reviewer\ndescription: Security\n---\n\nReview code.",
},
],
skillDirs: [
{
name: "skill-one",
sourceDir: path.join(import.meta.dir, "fixtures", "sample-plugin", "skills", "skill-one"),
},
],
commands: [
{
name: "plan",
content: 'description = "Plan"\nprompt = """\nPlan the work.\n"""',
},
],
mcpServers: {
playwright: { command: "npx", args: ["-y", "@anthropic/mcp-playwright"] },
},
}
await writeGeminiBundle(tempRoot, bundle)
expect(await exists(path.join(tempRoot, ".gemini", "skills", "security-reviewer", "SKILL.md"))).toBe(true)
expect(await exists(path.join(tempRoot, ".gemini", "skills", "skill-one", "SKILL.md"))).toBe(true)
expect(await exists(path.join(tempRoot, ".gemini", "commands", "plan.toml"))).toBe(true)
expect(await exists(path.join(tempRoot, ".gemini", "settings.json"))).toBe(true)
const skillContent = await fs.readFile(
path.join(tempRoot, ".gemini", "skills", "security-reviewer", "SKILL.md"),
"utf8",
)
expect(skillContent).toContain("Review code.")
const commandContent = await fs.readFile(
path.join(tempRoot, ".gemini", "commands", "plan.toml"),
"utf8",
)
expect(commandContent).toContain("Plan the work.")
const settingsContent = JSON.parse(
await fs.readFile(path.join(tempRoot, ".gemini", "settings.json"), "utf8"),
)
expect(settingsContent.mcpServers.playwright.command).toBe("npx")
})
test("namespaced commands create subdirectories", async () => {
const tempRoot = await fs.mkdtemp(path.join(os.tmpdir(), "gemini-ns-"))
const bundle: GeminiBundle = {
generatedSkills: [],
skillDirs: [],
commands: [
{
name: "workflows/plan",
content: 'description = "Plan"\nprompt = """\nPlan.\n"""',
},
],
}
await writeGeminiBundle(tempRoot, bundle)
expect(await exists(path.join(tempRoot, ".gemini", "commands", "workflows", "plan.toml"))).toBe(true)
})
test("does not double-nest when output root is .gemini", async () => {
const tempRoot = await fs.mkdtemp(path.join(os.tmpdir(), "gemini-home-"))
const geminiRoot = path.join(tempRoot, ".gemini")
const bundle: GeminiBundle = {
generatedSkills: [
{ name: "reviewer", content: "Reviewer skill content" },
],
skillDirs: [],
commands: [
{ name: "plan", content: "Plan content" },
],
}
await writeGeminiBundle(geminiRoot, bundle)
expect(await exists(path.join(geminiRoot, "skills", "reviewer", "SKILL.md"))).toBe(true)
expect(await exists(path.join(geminiRoot, "commands", "plan.toml"))).toBe(true)
// Should NOT double-nest under .gemini/.gemini
expect(await exists(path.join(geminiRoot, ".gemini"))).toBe(false)
})
test("handles empty bundles gracefully", async () => {
const tempRoot = await fs.mkdtemp(path.join(os.tmpdir(), "gemini-empty-"))
const bundle: GeminiBundle = {
generatedSkills: [],
skillDirs: [],
commands: [],
}
await writeGeminiBundle(tempRoot, bundle)
expect(await exists(tempRoot)).toBe(true)
})
test("backs up existing settings.json before overwrite", async () => {
const tempRoot = await fs.mkdtemp(path.join(os.tmpdir(), "gemini-backup-"))
const geminiRoot = path.join(tempRoot, ".gemini")
await fs.mkdir(geminiRoot, { recursive: true })
// Write existing settings.json
const settingsPath = path.join(geminiRoot, "settings.json")
await fs.writeFile(settingsPath, JSON.stringify({ mcpServers: { old: { command: "old-cmd" } } }))
const bundle: GeminiBundle = {
generatedSkills: [],
skillDirs: [],
commands: [],
mcpServers: {
newServer: { command: "new-cmd" },
},
}
await writeGeminiBundle(geminiRoot, bundle)
// New settings.json should have the new content
const newContent = JSON.parse(await fs.readFile(settingsPath, "utf8"))
expect(newContent.mcpServers.newServer.command).toBe("new-cmd")
// A backup file should exist
const files = await fs.readdir(geminiRoot)
const backupFiles = files.filter((f) => f.startsWith("settings.json.bak."))
expect(backupFiles.length).toBeGreaterThanOrEqual(1)
})
test("merges mcpServers into existing settings.json without clobbering other keys", async () => {
const tempRoot = await fs.mkdtemp(path.join(os.tmpdir(), "gemini-merge-"))
const geminiRoot = path.join(tempRoot, ".gemini")
await fs.mkdir(geminiRoot, { recursive: true })
// Write existing settings.json with other keys
const settingsPath = path.join(geminiRoot, "settings.json")
await fs.writeFile(settingsPath, JSON.stringify({
model: "gemini-2.5-pro",
mcpServers: { old: { command: "old-cmd" } },
}))
const bundle: GeminiBundle = {
generatedSkills: [],
skillDirs: [],
commands: [],
mcpServers: {
newServer: { command: "new-cmd" },
},
}
await writeGeminiBundle(geminiRoot, bundle)
const content = JSON.parse(await fs.readFile(settingsPath, "utf8"))
// Should preserve existing model key
expect(content.model).toBe("gemini-2.5-pro")
// Should preserve existing MCP server
expect(content.mcpServers.old.command).toBe("old-cmd")
// Should add new MCP server
expect(content.mcpServers.newServer.command).toBe("new-cmd")
})
})