vault-sync: 2026-09-16 00:00 (jimmy)

This commit is contained in:
jimmy
2026-09-16 00:00:30 -07:00
parent cf3c882c94
commit 00719428b2
194 changed files with 42289 additions and 0 deletions
@@ -0,0 +1,223 @@
#!/usr/bin/env bash
# Tests for adapters/claude-code/adapter.sh
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../../.." && pwd)"
source "$ROOT/adapters/lib.sh"
source "$ROOT/adapters/claude-code/adapter.sh"
test_finalize_writes_plugin_json() {
local src; src="$(mktemp -d)"
local dst; dst="$(mktemp -d)"
mkdir -p "$src/.claude-plugin"
echo '{"name": "test"}' > "$src/.claude-plugin/plugin.json"
adapter_finalize "$src" "$dst"
local result=0
[[ -f "$dst/.claude-plugin/plugin.json" ]] || { echo "plugin.json missing"; result=1; }
rm -rf "$src" "$dst"
return $result
}
test_translate_mcp_basic() {
local src; src="$(mktemp -d)"
local dst; dst="$(mktemp -d)"
mkdir -p "$src/mcp"
cat > "$src/mcp/servers.yaml" <<'EOF'
servers:
- name: gmail
type: local
command: [npx, -y, "@anthropic-ai/gmail-mcp"]
env: {}
EOF
adapter_translate_mcp "$src/mcp" "$dst"
local out="$dst/.mcp.json"
local result=0
[[ -f "$out" ]] || { echo ".mcp.json missing"; result=1; }
jq -e '.mcpServers.gmail.command == "npx"' "$out" >/dev/null || { echo "command field wrong"; cat "$out"; result=1; }
jq -e '.mcpServers.gmail.args == ["-y", "@anthropic-ai/gmail-mcp"]' "$out" >/dev/null || { echo "args field wrong"; cat "$out"; result=1; }
rm -rf "$src" "$dst"
return $result
}
test_translate_hooks_creates_files() {
local src; src="$(mktemp -d)"
local dst; dst="$(mktemp -d)"
mkdir -p "$src/hooks"
cat > "$src/hooks/protect.hook.yaml" <<'EOF'
name: protect
script: protect.sh
triggers:
- event: before-tool-use
match-tool: [edit, write]
EOF
cat > "$src/hooks/protect.sh" <<'EOF'
#!/usr/bin/env bash
exit 0
EOF
adapter_translate_hooks "$src/hooks" "$dst"
local result=0
[[ -f "$dst/.claude/hooks/protect.sh" ]] || { echo "protect.sh missing"; result=1; }
[[ -f "$dst/.claude/hooks/protect-wrapper.sh" ]] || { echo "wrapper missing"; result=1; }
[[ -f "$dst/.claude/settings.json" ]] || { echo "settings.json missing"; result=1; }
grep -q "PreToolUse" "$dst/.claude/settings.json" || { echo "PreToolUse not in settings"; result=1; }
grep -q "Edit|Write" "$dst/.claude/settings.json" || { echo "matcher missing"; result=1; }
rm -rf "$src" "$dst"
return $result
}
test_translate_agents_basic() {
local src; src="$(mktemp -d)"
local dst; dst="$(mktemp -d)"
mkdir -p "$src/agents"
cat > "$src/agents/scribe.md" <<'EOF'
---
name: scribe
description: Test scribe
model: mid
mode: subagent
capabilities: [read, write, edit]
---
You are the Scribe.
EOF
adapter_translate_agents "$src/agents" "$dst"
local out="$dst/.claude/agents/scribe.md"
local result=0
[[ -f "$out" ]] || { echo "agent file missing"; result=1; }
grep -q "^name: scribe" "$out" || { echo "name missing"; result=1; }
grep -q "^tools: Read, Glob, Grep, Write, Edit" "$out" || { echo "tools incorrect: $(grep '^tools:' "$out")"; result=1; }
grep -q "^You are the Scribe" "$out" || { echo "body missing"; result=1; }
grep -q "capabilities:" "$out" && { echo "capabilities should be removed"; result=1; }
rm -rf "$src" "$dst"
return $result
}
test_translate_agents_supports_crlf_frontmatter() {
local src; src="$(mktemp -d)"
local dst; dst="$(mktemp -d)"
mkdir -p "$src/agents"
printf '%s\r\n' \
'---' \
'name: scribe' \
'description: >' \
' Test scribe' \
' across line endings' \
'model: mid' \
'mode: subagent' \
'capabilities: [read, write, edit]' \
'---' \
'' \
'You are the Scribe.' > "$src/agents/scribe.md"
adapter_translate_agents "$src/agents" "$dst"
local out="$dst/.claude/agents/scribe.md"
local result=0
grep -q '^name: scribe' "$out" || { echo "name missing"; result=1; }
grep -q '^description: >' "$out" || { echo "description header missing"; result=1; }
grep -q '^ Test scribe' "$out" || { echo "description continuation missing"; result=1; }
grep -q '^model: sonnet' "$out" || { echo "model not mapped from CRLF frontmatter"; result=1; }
grep -q '^tools: Read, Glob, Grep, Write, Edit' "$out" || { echo "tools not mapped from CRLF capabilities"; result=1; }
grep -q '^You are the Scribe' "$out" || { echo "body missing"; result=1; }
rm -rf "$src" "$dst"
return $result
}
test_translate_agents_bash_capability() {
local src; src="$(mktemp -d)"
local dst; dst="$(mktemp -d)"
mkdir -p "$src/agents"
cat > "$src/agents/architect.md" <<'EOF'
---
name: architect
description: Test
model: high
capabilities: [read, write, edit, bash]
---
body
EOF
adapter_translate_agents "$src/agents" "$dst"
local out="$dst/.claude/agents/architect.md"
grep -q "^tools: Read, Glob, Grep, Write, Edit, Bash" "$out" || { echo "tools incorrect: $(grep '^tools:' "$out")"; rm -rf "$src" "$dst"; return 1; }
rm -rf "$src" "$dst"
return 0
}
test_translate_skills_copies_skill_md() {
local src; src="$(mktemp -d)"
local dst; dst="$(mktemp -d)"
mkdir -p "$src/skills/foo" "$src/skills/bar"
cat > "$src/skills/foo/SKILL.md" <<'EOF'
---
name: foo
description: Foo skill
---
body
EOF
cat > "$src/skills/bar/SKILL.md" <<'EOF'
---
name: bar
description: Bar skill
---
body
EOF
adapter_translate_skills "$src/skills" "$dst"
local result=0
[[ -f "$dst/.claude/skills/foo/SKILL.md" ]] || { echo "foo missing"; result=1; }
[[ -f "$dst/.claude/skills/bar/SKILL.md" ]] || { echo "bar missing"; result=1; }
rm -rf "$src" "$dst"
return $result
}
test_translate_skills_excludes() {
local src; src="$(mktemp -d)"
local dst; dst="$(mktemp -d)"
mkdir -p "$src/skills/foo"
cat > "$src/skills/foo/SKILL.md" <<'EOF'
---
name: foo
description: Foo
exclude: [claude-code]
---
EOF
adapter_translate_skills "$src/skills" "$dst"
local result=0
[[ ! -f "$dst/.claude/skills/foo/SKILL.md" ]] || { echo "foo should be excluded"; result=1; }
rm -rf "$src" "$dst"
return $result
}
test_translate_references_copies_md_files() {
local src; src="$(mktemp -d)"
local dst; dst="$(mktemp -d)"
mkdir -p "$src/references"
echo "ref1" > "$src/references/one.md"
echo "ref2" > "$src/references/two.md"
adapter_translate_references "$src/references" "$dst"
local result=0
[[ -f "$dst/.claude/references/one.md" ]] || { echo "one.md missing"; result=1; }
[[ -f "$dst/.claude/references/two.md" ]] || { echo "two.md missing"; result=1; }
rm -rf "$src" "$dst"
return $result
}
test_translate_dispatcher_renames_to_claude_md() {
local src; src="$(mktemp -d)"
local dst; dst="$(mktemp -d)"
cat > "$src/DISPATCHER.md" <<'EOF'
# Dispatcher
Some content
EOF
adapter_translate_dispatcher "$src/DISPATCHER.md" "$dst"
local result=0
[[ -f "$dst/CLAUDE.md" ]] || { echo "CLAUDE.md not created"; result=1; }
[[ "$(cat "$dst/CLAUDE.md")" == "$(cat "$src/DISPATCHER.md")" ]] || { echo "content mismatch"; result=1; }
rm -rf "$src" "$dst"
return $result
}
test_cc_model_to_native_maps_tiers() {
local result=0
[[ "$(cc_model_to_native "low")" == "haiku" ]] || { echo "low→haiku failed"; result=1; }
[[ "$(cc_model_to_native "mid")" == "sonnet" ]] || { echo "mid→sonnet failed"; result=1; }
[[ "$(cc_model_to_native "high")" == "opus" ]] || { echo "high→opus failed"; result=1; }
[[ "$(cc_model_to_native "anthropic/custom")" == "anthropic/custom" ]] || { echo "passthrough failed"; result=1; }
return $result
}
@@ -0,0 +1,333 @@
#!/usr/bin/env bash
# Tests for adapters/gemini-cli/adapter.sh
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../../.." && pwd)"
source "$ROOT/adapters/lib.sh"
source "$ROOT/adapters/gemini-cli/adapter.sh"
test_gemini_translate_dispatcher_renames_to_gemini_md() {
local src; src="$(mktemp -d)"
local dst; dst="$(mktemp -d)"
echo "# Dispatcher content" > "$src/DISPATCHER.md"
adapter_translate_dispatcher "$src/DISPATCHER.md" "$dst"
local result=0
[[ -f "$dst/GEMINI.md" ]] || { echo "GEMINI.md not created"; result=1; }
[[ ! -f "$dst/CLAUDE.md" ]] || { echo "CLAUDE.md should not exist"; result=1; }
[[ ! -f "$dst/AGENTS.md" ]] || { echo "AGENTS.md should not exist"; result=1; }
[[ "$(cat "$dst/GEMINI.md")" == "$(cat "$src/DISPATCHER.md")" ]] || { echo "content mismatch"; result=1; }
rm -rf "$src" "$dst"
return $result
}
test_gemini_translate_references_copies_md_files() {
local src; src="$(mktemp -d)"
local dst; dst="$(mktemp -d)"
mkdir -p "$src/references"
echo "ref1" > "$src/references/one.md"
echo "ref2" > "$src/references/two.md"
adapter_translate_references "$src/references" "$dst"
local result=0
[[ -f "$dst/.gemini/references/one.md" ]] || { echo "one.md missing"; result=1; }
[[ -f "$dst/.gemini/references/two.md" ]] || { echo "two.md missing"; result=1; }
rm -rf "$src" "$dst"
return $result
}
test_gemini_translate_skills_copies_skill_md() {
local src; src="$(mktemp -d)"
local dst; dst="$(mktemp -d)"
mkdir -p "$src/skills/foo" "$src/skills/bar"
cat > "$src/skills/foo/SKILL.md" <<'HEREDOC'
---
name: foo
description: Foo skill
---
body
HEREDOC
cat > "$src/skills/bar/SKILL.md" <<'HEREDOC'
---
name: bar
description: Bar skill
---
body
HEREDOC
adapter_translate_skills "$src/skills" "$dst"
local result=0
[[ -f "$dst/.gemini/skills/foo/SKILL.md" ]] || { echo "foo missing"; result=1; }
[[ -f "$dst/.gemini/skills/bar/SKILL.md" ]] || { echo "bar missing"; result=1; }
rm -rf "$src" "$dst"
return $result
}
test_gemini_translate_skills_honors_exclude() {
local src; src="$(mktemp -d)"
local dst; dst="$(mktemp -d)"
mkdir -p "$src/skills/foo"
cat > "$src/skills/foo/SKILL.md" <<'HEREDOC'
---
name: foo
description: Foo
exclude: [gemini-cli]
---
HEREDOC
adapter_translate_skills "$src/skills" "$dst"
local result=0
[[ ! -f "$dst/.gemini/skills/foo/SKILL.md" ]] || { echo "foo should be excluded"; result=1; }
rm -rf "$src" "$dst"
return $result
}
test_gemini_translate_agents_basic() {
local src; src="$(mktemp -d)"
local dst; dst="$(mktemp -d)"
mkdir -p "$src/agents"
cat > "$src/agents/scribe.md" <<'HEREDOC'
---
name: scribe
description: Test scribe
model: mid
capabilities: [read, write, edit]
---
You are the Scribe.
HEREDOC
adapter_translate_agents "$src/agents" "$dst"
local out="$dst/.gemini/agents/scribe.md"
local result=0
[[ -f "$out" ]] || { echo "agent file missing"; result=1; }
grep -q '^name: scribe' "$out" || { echo "name missing"; result=1; }
grep -q '^model: gemini-2.5-flash' "$out" || { echo "model not mapped"; cat "$out"; result=1; }
grep -q '^ *- read_file' "$out" || { echo "read_file tool missing"; cat "$out"; result=1; }
grep -q '^ *- write_file' "$out" || { echo "write_file tool missing"; result=1; }
grep -q '^ *- replace' "$out" || { echo "replace tool missing"; result=1; }
grep -q '^ *- grep_search' "$out" || { echo "grep_search tool missing"; result=1; }
grep -q '^You are the Scribe' "$out" || { echo "body missing"; result=1; }
grep -q '^capabilities:' "$out" && { echo "capabilities should be dropped"; result=1; }
rm -rf "$src" "$dst"
return $result
}
test_gemini_translate_agents_supports_crlf_frontmatter() {
local src; src="$(mktemp -d)"
local dst; dst="$(mktemp -d)"
mkdir -p "$src/agents"
printf '%s\r\n' \
'---' \
'name: scribe' \
'description: >' \
' Test scribe' \
' across line endings' \
'model: mid' \
'capabilities: [read, write, edit]' \
'---' \
'' \
'You are the Scribe.' > "$src/agents/scribe.md"
adapter_translate_agents "$src/agents" "$dst"
local out="$dst/.gemini/agents/scribe.md"
local result=0
grep -q '^name: scribe' "$out" || { echo "name missing"; result=1; }
grep -q '^description: >' "$out" || { echo "description header missing"; result=1; }
grep -q '^ Test scribe' "$out" || { echo "description continuation missing"; result=1; }
grep -q '^model: gemini-2.5-flash' "$out" || { echo "model not mapped from CRLF frontmatter"; result=1; }
grep -q '^ *- read_file' "$out" || { echo "tools not mapped from CRLF capabilities"; result=1; }
grep -q '^You are the Scribe' "$out" || { echo "body missing"; result=1; }
rm -rf "$src" "$dst"
return $result
}
test_gemini_translate_agents_bash_capability() {
local src; src="$(mktemp -d)"
local dst; dst="$(mktemp -d)"
mkdir -p "$src/agents"
cat > "$src/agents/architect.md" <<'HEREDOC'
---
name: architect
description: Test arch
model: high
capabilities: [read, write, edit, bash]
---
body
HEREDOC
adapter_translate_agents "$src/agents" "$dst"
local out="$dst/.gemini/agents/architect.md"
local result=0
grep -q '^ *- run_shell_command' "$out" || { echo "run_shell_command missing"; result=1; }
grep -q '^model: gemini-2.5-pro' "$out" || { echo "model not mapped to pro"; result=1; }
rm -rf "$src" "$dst"
return $result
}
test_gemini_translate_agents_dedupes_tools() {
local src; src="$(mktemp -d)"
local dst; dst="$(mktemp -d)"
mkdir -p "$src/agents"
cat > "$src/agents/seeker.md" <<'HEREDOC'
---
name: seeker
description: Search
model: mid
capabilities: [read]
---
body
HEREDOC
adapter_translate_agents "$src/agents" "$dst"
local out="$dst/.gemini/agents/seeker.md"
local count; count="$(grep -c '^ *- read_file' "$out")"
rm -rf "$src" "$dst"
[[ "$count" == "1" ]] || { echo "expected 1 read_file, got $count"; return 1; }
}
test_gemini_translate_hooks_creates_files() {
local src; src="$(mktemp -d)"
local dst; dst="$(mktemp -d)"
mkdir -p "$src/hooks"
cat > "$src/hooks/protect.hook.yaml" <<'HEREDOC'
name: protect
script: protect.sh
triggers:
- event: before-tool-use
match-tool: [edit, write]
HEREDOC
echo '#!/usr/bin/env bash' > "$src/hooks/protect.sh"
adapter_translate_hooks "$src/hooks" "$dst"
local result=0
[[ -f "$dst/.gemini/hooks/protect.sh" ]] || { echo "protect.sh not copied"; result=1; }
[[ -f "$dst/.gemini/hooks/protect-wrapper.sh" ]] || { echo "wrapper not created"; result=1; }
[[ -f "$dst/.gemini/_hooks.json" ]] || { echo "_hooks.json not created"; result=1; }
rm -rf "$src" "$dst"
return $result
}
test_gemini_translate_hooks_json_has_entries() {
local src; src="$(mktemp -d)"
local dst; dst="$(mktemp -d)"
mkdir -p "$src/hooks"
cat > "$src/hooks/protect.hook.yaml" <<'HEREDOC'
name: protect
script: protect.sh
triggers:
- event: before-tool-use
match-tool: [edit, write]
HEREDOC
echo '#!/usr/bin/env bash' > "$src/hooks/protect.sh"
adapter_translate_hooks "$src/hooks" "$dst"
local json="$dst/.gemini/_hooks.json"
local result=0
jq -e '.hooks.BeforeTool' "$json" >/dev/null || { echo "BeforeTool event missing"; result=1; }
jq -e '.hooks.BeforeTool[0].matcher' "$json" >/dev/null || { echo "matcher missing"; result=1; }
local matcher; matcher="$(jq -r '.hooks.BeforeTool[0].matcher' "$json")"
[[ "$matcher" == *"replace"* ]] || { echo "replace not in matcher: $matcher"; result=1; }
[[ "$matcher" == *"write_file"* ]] || { echo "write_file not in matcher: $matcher"; result=1; }
rm -rf "$src" "$dst"
return $result
}
test_gemini_translate_hooks_no_hooks_is_noop() {
local src; src="$(mktemp -d)"
local dst; dst="$(mktemp -d)"
adapter_translate_hooks "$src/hooks" "$dst"
local result=0
[[ ! -f "$dst/.gemini/_hooks.json" ]] || { echo "should not create hooks json when no hooks dir"; result=1; }
rm -rf "$src" "$dst"
return $result
}
test_gemini_translate_mcp_remote() {
local src; src="$(mktemp -d)"
local dst; dst="$(mktemp -d)"
mkdir -p "$src/mcp" "$dst/.gemini"
cat > "$src/mcp/servers.yaml" <<'HEREDOC'
servers:
- name: Gmail
type: http
url: "https://gmail.mcp.claude.com/mcp"
env: {}
HEREDOC
adapter_translate_mcp "$src/mcp" "$dst"
local json="$dst/.gemini/_mcp.json"
local result=0
[[ -f "$json" ]] || { echo "_mcp.json missing"; result=1; }
jq -e '.mcpServers.Gmail.url == "https://gmail.mcp.claude.com/mcp"' "$json" >/dev/null || { echo "url wrong"; cat "$json"; result=1; }
rm -rf "$src" "$dst"
return $result
}
test_gemini_translate_mcp_local() {
local src; src="$(mktemp -d)"
local dst; dst="$(mktemp -d)"
mkdir -p "$src/mcp" "$dst/.gemini"
cat > "$src/mcp/servers.yaml" <<'HEREDOC'
servers:
- name: gmail
type: local
command: [npx, -y, "@anthropic-ai/gmail-mcp"]
env: {}
HEREDOC
adapter_translate_mcp "$src/mcp" "$dst"
local json="$dst/.gemini/_mcp.json"
local result=0
jq -e '.mcpServers.gmail.command == "npx"' "$json" >/dev/null || { echo "command wrong"; cat "$json"; result=1; }
jq -e '.mcpServers.gmail.args | length > 0' "$json" >/dev/null || { echo "args missing"; cat "$json"; result=1; }
rm -rf "$src" "$dst"
return $result
}
test_gemini_adapter_build_end_to_end() {
local src; src="$(mktemp -d)"
local dst; dst="$(mktemp -d)"
echo "# Dispatcher" > "$src/DISPATCHER.md"
mkdir -p "$src/agents" "$src/hooks" "$src/skills/onboarding" "$src/references" "$src/mcp"
cat > "$src/agents/scribe.md" <<'HEREDOC'
---
name: scribe
description: Test scribe
model: mid
capabilities: [read, write, edit]
---
body
HEREDOC
cat > "$src/hooks/protect.hook.yaml" <<'HEREDOC'
name: protect
script: protect.sh
triggers:
- event: before-tool-use
match-tool: [edit]
HEREDOC
echo "#!/usr/bin/env bash" > "$src/hooks/protect.sh"
cat > "$src/skills/onboarding/SKILL.md" <<'HEREDOC'
---
name: onboarding
description: Onboarding skill
---
body
HEREDOC
echo "reference content" > "$src/references/policy.md"
cat > "$src/mcp/servers.yaml" <<'HEREDOC'
servers:
- name: Gmail
type: http
url: "https://gmail.mcp.claude.com/mcp"
env: {}
HEREDOC
adapter_build "$src" "$dst"
local result=0
[[ -f "$dst/GEMINI.md" ]] || { echo "GEMINI.md missing"; result=1; }
[[ -f "$dst/.gemini/agents/scribe.md" ]] || { echo "agent missing"; result=1; }
[[ -f "$dst/.gemini/skills/onboarding/SKILL.md" ]] || { echo "skill missing"; result=1; }
[[ -f "$dst/.gemini/references/policy.md" ]] || { echo "reference missing"; result=1; }
[[ -f "$dst/.gemini/hooks/protect.sh" ]] || { echo "hook script missing"; result=1; }
[[ -f "$dst/.gemini/hooks/protect-wrapper.sh" ]] || { echo "wrapper missing"; result=1; }
[[ -f "$dst/.gemini/settings.json" ]] || { echo "settings.json missing"; result=1; }
jq -e '.hooks' "$dst/.gemini/settings.json" >/dev/null || { echo "hooks missing from settings.json"; result=1; }
jq -e '.mcpServers' "$dst/.gemini/settings.json" >/dev/null || { echo "mcpServers missing from settings.json"; result=1; }
[[ ! -f "$dst/.gemini/_hooks.json" ]] || { echo "_hooks.json should be cleaned up"; result=1; }
[[ ! -f "$dst/.gemini/_mcp.json" ]] || { echo "_mcp.json should be cleaned up"; result=1; }
rm -rf "$src" "$dst"
return $result
}
@@ -0,0 +1,270 @@
#!/usr/bin/env bash
# Tests for adapters/lib.sh
# Source the lib under test
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
source "$ROOT/adapters/lib.sh"
# Test functions will be added in subsequent tasks.
# Each function must be named test_* to be auto-discovered by tests/run.sh.
test_parse_frontmatter_scalar() {
local fixture; fixture="$(mktemp)"
cat > "$fixture" <<'EOF'
---
name: scribe
description: Text Capture
model: sonnet
---
body content
EOF
local result; result="$(parse_frontmatter "$fixture" name)"
rm "$fixture"
[[ "$result" == "scribe" ]] || { echo "expected 'scribe', got '$result'"; return 1; }
}
test_parse_frontmatter_and_body_support_crlf() {
local fixture; fixture="$(mktemp)"
printf '%s\r\n' \
'---' \
'name: architect' \
'model: high' \
'capabilities: [read, write]' \
'---' \
'You are the Architect.' > "$fixture"
local name model capabilities body
name="$(parse_frontmatter "$fixture" name)"
model="$(parse_frontmatter "$fixture" model)"
capabilities="$(parse_capabilities "$fixture")"
body="$(agent_body "$fixture")"
rm "$fixture"
local result=0
[[ "$name" == "architect" ]] || { echo "expected name architect, got '$name'"; result=1; }
[[ "$model" == "high" ]] || { echo "expected model high, got '$model'"; result=1; }
[[ "$capabilities" == "read write" ]] || { echo "expected capabilities 'read write', got '$capabilities'"; result=1; }
[[ "$body" == "You are the Architect." ]] || { echo "expected CRLF body, got '$body'"; result=1; }
return $result
}
test_parse_frontmatter_list() {
local fixture; fixture="$(mktemp)"
cat > "$fixture" <<'EOF'
---
name: scribe
capabilities: [read, write, edit]
---
EOF
local result; result="$(parse_frontmatter "$fixture" capabilities)"
rm "$fixture"
[[ "$result" == "[read, write, edit]" ]] || { echo "expected '[read, write, edit]', got '$result'"; return 1; }
}
test_parse_frontmatter_missing_key() {
local fixture; fixture="$(mktemp)"
cat > "$fixture" <<'EOF'
---
name: scribe
---
EOF
local result; result="$(parse_frontmatter "$fixture" nonexistent)"
rm "$fixture"
[[ -z "$result" ]] || { echo "expected empty, got '$result'"; return 1; }
}
test_parse_capabilities_normal() {
local fixture; fixture="$(mktemp)"
cat > "$fixture" <<'EOF'
---
name: scribe
capabilities: [read, write, edit]
---
EOF
local result; result="$(parse_capabilities "$fixture")"
rm "$fixture"
[[ "$result" == "read write edit" ]] || { echo "expected 'read write edit', got '$result'"; return 1; }
}
test_parse_capabilities_single() {
local fixture; fixture="$(mktemp)"
cat > "$fixture" <<'EOF'
---
capabilities: [read]
---
EOF
local result; result="$(parse_capabilities "$fixture")"
rm "$fixture"
[[ "$result" == "read" ]] || { echo "expected 'read', got '$result'"; return 1; }
}
test_parse_capabilities_empty() {
local fixture; fixture="$(mktemp)"
cat > "$fixture" <<'EOF'
---
capabilities: []
---
EOF
local result; result="$(parse_capabilities "$fixture")"
rm "$fixture"
[[ -z "$result" ]] || { echo "expected empty, got '$result'"; return 1; }
}
test_should_include_no_exclude() {
local fixture; fixture="$(mktemp)"
cat > "$fixture" <<'EOF'
---
name: scribe
---
EOF
if should_include "$fixture" claude-code; then
rm "$fixture"; return 0
else
rm "$fixture"; echo "expected 0, got 1"; return 1
fi
}
test_should_include_empty_exclude() {
local fixture; fixture="$(mktemp)"
cat > "$fixture" <<'EOF'
---
name: scribe
exclude: []
---
EOF
if should_include "$fixture" claude-code; then
rm "$fixture"; return 0
else
rm "$fixture"; echo "expected 0, got 1"; return 1
fi
}
test_should_include_excluded() {
local fixture; fixture="$(mktemp)"
cat > "$fixture" <<'EOF'
---
name: scribe
exclude: [opencode]
---
EOF
if should_include "$fixture" opencode; then
rm "$fixture"; echo "expected 1, got 0"; return 1
else
rm "$fixture"; return 0
fi
}
test_should_include_excluded_other_fw() {
local fixture; fixture="$(mktemp)"
cat > "$fixture" <<'EOF'
---
exclude: [opencode]
---
EOF
if should_include "$fixture" claude-code; then
rm "$fixture"; return 0
else
rm "$fixture"; echo "expected 0, got 1"; return 1
fi
}
test_parse_hook_yaml_simple() {
local fixture; fixture="$(mktemp)"
cat > "$fixture" <<'EOF'
name: notify
script: notify.sh
triggers:
- event: on-notification
exclude: []
EOF
local result; result="$(parse_hook_yaml "$fixture")"
rm "$fixture"
[[ "$result" == *"name=notify"* ]] || { echo "missing name=notify in: $result"; return 1; }
[[ "$result" == *"script=notify.sh"* ]] || { echo "missing script="; return 1; }
[[ "$result" == *"event=on-notification"* ]] || { echo "missing event="; return 1; }
}
test_parse_hook_yaml_with_match() {
local fixture; fixture="$(mktemp)"
cat > "$fixture" <<'EOF'
name: protect-system-files
script: protect-system-files.sh
triggers:
- event: before-tool-use
match-tool: [edit, write]
EOF
local result; result="$(parse_hook_yaml "$fixture")"
rm "$fixture"
[[ "$result" == *"event=before-tool-use"* ]] || { echo "missing event"; return 1; }
[[ "$result" == *"match-tool=edit write"* ]] || { echo "missing match-tool: $result"; return 1; }
}
test_agent_body() {
local fixture; fixture="$(mktemp)"
cat > "$fixture" <<'EOF'
---
name: scribe
---
You are the Scribe.
You write notes.
EOF
local result; result="$(agent_body "$fixture")"
rm "$fixture"
[[ "$result" == *"You are the Scribe."* ]] || { echo "missing body line 1"; return 1; }
[[ "$result" == *"You write notes."* ]] || { echo "missing body line 2"; return 1; }
[[ "$result" != *"name: scribe"* ]] || { echo "frontmatter leaked into body"; return 1; }
}
test_enumerate_agents() {
local dir; dir="$(mktemp -d)"
touch "$dir/foo.md" "$dir/bar.md" "$dir/not-an-agent.txt"
local count; count="$(enumerate_agents "$dir" | wc -l | xargs)"
rm -rf "$dir"
[[ "$count" == "2" ]] || { echo "expected 2, got $count"; return 1; }
}
test_enumerate_hooks() {
local dir; dir="$(mktemp -d)"
touch "$dir/foo.hook.yaml" "$dir/bar.hook.yaml" "$dir/foo.sh"
local count; count="$(enumerate_hooks "$dir" | wc -l | xargs)"
rm -rf "$dir"
[[ "$count" == "2" ]] || { echo "expected 2, got $count"; return 1; }
}
test_rewrite_platform_paths_replaces_both() {
local tmp; tmp="$(mktemp)"
cat > "$tmp" <<'HEREDOC'
See .platform/references/agent-orchestration.md for details.
The dispatcher (DISPATCHER.md) handles routing.
Files live in .platform/agents/ directory.
HEREDOC
rewrite_platform_paths "$tmp" "claude" "CLAUDE.md"
local result=0
grep -q '\.claude/references/agent-orchestration.md' "$tmp" || { echo ".platform/ not rewritten"; result=1; }
grep -q 'CLAUDE.md' "$tmp" || { echo "DISPATCHER.md not rewritten"; result=1; }
grep -q '\.claude/agents/' "$tmp" || { echo "second .platform/ not rewritten"; result=1; }
grep -q '\.platform/' "$tmp" && { echo ".platform/ still present"; result=1; }
grep -q 'DISPATCHER\.md' "$tmp" && { echo "DISPATCHER.md still present"; result=1; }
rm -f "$tmp"
return $result
}
test_rewrite_platform_paths_opencode() {
local tmp; tmp="$(mktemp)"
echo 'See .platform/references/agents.md and DISPATCHER.md' > "$tmp"
rewrite_platform_paths "$tmp" "opencode" "AGENTS.md"
local result=0
grep -q '\.opencode/references/agents.md' "$tmp" || { echo "not rewritten to .opencode/"; result=1; }
grep -q 'AGENTS.md' "$tmp" || { echo "not rewritten to AGENTS.md"; result=1; }
rm -f "$tmp"
return $result
}
test_rewrite_platform_paths_gemini() {
local tmp; tmp="$(mktemp)"
echo 'See .platform/agents/scribe.md and DISPATCHER.md' > "$tmp"
rewrite_platform_paths "$tmp" "gemini" "GEMINI.md"
local result=0
grep -q '\.gemini/agents/scribe.md' "$tmp" || { echo "not rewritten to .gemini/"; result=1; }
grep -q 'GEMINI.md' "$tmp" || { echo "not rewritten to GEMINI.md"; result=1; }
rm -f "$tmp"
return $result
}
@@ -0,0 +1,435 @@
#!/usr/bin/env bash
# Tests for adapters/opencode/adapter.sh
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../../.." && pwd)"
source "$ROOT/adapters/lib.sh"
source "$ROOT/adapters/opencode/adapter.sh"
test_oc_translate_dispatcher_renames_to_agents_md() {
local src; src="$(mktemp -d)"
local dst; dst="$(mktemp -d)"
cat > "$src/DISPATCHER.md" <<'EOF'
# Dispatcher
See .platform/agents/ for agents. Consult DISPATCHER.md for rules.
EOF
adapter_translate_dispatcher "$src/DISPATCHER.md" "$dst"
local result=0
[[ -f "$dst/AGENTS.md" ]] || { echo "AGENTS.md not created"; result=1; }
[[ ! -f "$dst/CLAUDE.md" ]] || { echo "CLAUDE.md should not exist"; result=1; }
local content; content="$(cat "$dst/AGENTS.md")"
[[ "$content" == *".opencode/agents/"* ]] || { echo ".opencode/agents/ not found: $content"; result=1; }
[[ "$content" == *"AGENTS.md"* ]] || { echo "AGENTS.md ref not rewritten: $content"; result=1; }
[[ "$content" != *".claude/"* ]] || { echo ".claude/ still present: $content"; result=1; }
[[ "$content" != *"CLAUDE.md"* ]] || { echo "CLAUDE.md still present: $content"; result=1; }
rm -rf "$src" "$dst"
return $result
}
test_oc_translate_references_copies_md_files() {
local src; src="$(mktemp -d)"
local dst; dst="$(mktemp -d)"
mkdir -p "$src/references"
echo "ref1" > "$src/references/one.md"
echo "ref2" > "$src/references/two.md"
adapter_translate_references "$src/references" "$dst"
local result=0
[[ -f "$dst/.opencode/references/one.md" ]] || { echo "one.md missing"; result=1; }
[[ -f "$dst/.opencode/references/two.md" ]] || { echo "two.md missing"; result=1; }
rm -rf "$src" "$dst"
return $result
}
test_oc_translate_references_rewrites_paths() {
local src; src="$(mktemp -d)"
local dst; dst="$(mktemp -d)"
mkdir -p "$src/references"
printf 'See .platform/agents/ and DISPATCHER.md for details.\n' > "$src/references/guide.md"
adapter_translate_references "$src/references" "$dst"
local content; content="$(cat "$dst/.opencode/references/guide.md")"
local result=0
[[ "$content" == *".opencode/agents/"* ]] || { echo ".opencode/agents/ not found: $content"; result=1; }
[[ "$content" == *"AGENTS.md"* ]] || { echo "AGENTS.md not found: $content"; result=1; }
[[ "$content" != *".claude/"* ]] || { echo ".claude/ still present: $content"; result=1; }
rm -rf "$src" "$dst"
return $result
}
test_oc_translate_skills_copies_skill_md() {
local src; src="$(mktemp -d)"
local dst; dst="$(mktemp -d)"
mkdir -p "$src/skills/foo" "$src/skills/bar"
cat > "$src/skills/foo/SKILL.md" <<'SKILLEOF'
---
name: foo
description: Foo skill
---
body
SKILLEOF
cat > "$src/skills/bar/SKILL.md" <<'SKILLEOF'
---
name: bar
description: Bar skill
---
body
SKILLEOF
adapter_translate_skills "$src/skills" "$dst"
local result=0
[[ -f "$dst/.opencode/skills/foo/SKILL.md" ]] || { echo "foo missing"; result=1; }
[[ -f "$dst/.opencode/skills/bar/SKILL.md" ]] || { echo "bar missing"; result=1; }
rm -rf "$src" "$dst"
return $result
}
test_oc_translate_skills_rewrites_paths() {
local src; src="$(mktemp -d)"
local dst; dst="$(mktemp -d)"
mkdir -p "$src/skills/create-agent"
cat > "$src/skills/create-agent/SKILL.md" <<'SKILLEOF'
---
name: create-agent
description: Create a new agent
---
Save to .platform/agents/ and update DISPATCHER.md.
SKILLEOF
adapter_translate_skills "$src/skills" "$dst"
local content; content="$(cat "$dst/.opencode/skills/create-agent/SKILL.md")"
local result=0
[[ "$content" == *".opencode/agents/"* ]] || { echo ".opencode/agents/ not found: $content"; result=1; }
[[ "$content" == *"AGENTS.md"* ]] || { echo "AGENTS.md not found: $content"; result=1; }
[[ "$content" != *".claude/"* ]] || { echo ".claude/ still present: $content"; result=1; }
rm -rf "$src" "$dst"
return $result
}
test_oc_translate_skills_honors_exclude() {
local src; src="$(mktemp -d)"
local dst; dst="$(mktemp -d)"
mkdir -p "$src/skills/foo"
cat > "$src/skills/foo/SKILL.md" <<'SKILLEOF'
---
name: foo
description: Foo
exclude: [opencode]
---
SKILLEOF
adapter_translate_skills "$src/skills" "$dst"
local result=0
[[ ! -f "$dst/.opencode/skills/foo/SKILL.md" ]] || { echo "foo should be excluded"; result=1; }
rm -rf "$src" "$dst"
return $result
}
test_oc_translate_agents_basic() {
local src; src="$(mktemp -d)"
local dst; dst="$(mktemp -d)"
mkdir -p "$src/agents"
cat > "$src/agents/scribe.md" <<'EOF'
---
name: scribe
description: Test scribe
model: mid
mode: subagent
capabilities: [read, write, edit]
---
You are the Scribe.
EOF
adapter_translate_agents "$src/agents" "$dst"
local out="$dst/.opencode/agents/scribe.md"
local result=0
[[ -f "$out" ]] || { echo "agent file missing"; result=1; }
grep -q '^description: Test scribe' "$out" || { echo "description missing or wrong format"; cat "$out"; result=1; }
grep -q '^mode: subagent' "$out" || { echo "mode missing"; result=1; }
grep -q '^model: anthropic/claude-sonnet-4-5' "$out" || { echo "model not mapped"; result=1; }
grep -q '^permission:' "$out" || { echo "permission block missing"; result=1; }
grep -q '^ edit: allow' "$out" || { echo "edit permission missing"; result=1; }
grep -q '^You are the Scribe' "$out" || { echo "body missing"; result=1; }
grep -q '^name:' "$out" && { echo "name: should be dropped"; result=1; }
grep -q '^capabilities:' "$out" && { echo "capabilities: should be dropped"; result=1; }
rm -rf "$src" "$dst"
return $result
}
test_oc_translate_agents_supports_crlf_frontmatter() {
local src; src="$(mktemp -d)"
local dst; dst="$(mktemp -d)"
mkdir -p "$src/agents"
printf '%s\r\n' \
'---' \
'name: scribe' \
'description: >' \
' Test scribe' \
' across line endings' \
'model: mid' \
'mode: subagent' \
'capabilities: [read, write, edit]' \
'---' \
'' \
'You are the Scribe.' > "$src/agents/scribe.md"
adapter_translate_agents "$src/agents" "$dst"
local out="$dst/.opencode/agents/scribe.md"
local result=0
grep -q '^description: >' "$out" || { echo "description header missing"; result=1; }
grep -q '^ Test scribe' "$out" || { echo "description continuation missing"; result=1; }
grep -q '^mode: subagent' "$out" || { echo "mode not parsed from CRLF frontmatter"; result=1; }
grep -q '^model: anthropic/claude-sonnet-4-5' "$out" || { echo "model not mapped from CRLF frontmatter"; result=1; }
grep -q '^ edit: allow' "$out" || { echo "permissions not mapped from CRLF capabilities"; result=1; }
grep -q '^You are the Scribe' "$out" || { echo "body missing"; result=1; }
rm -rf "$src" "$dst"
return $result
}
test_oc_translate_agents_rewrites_body_paths() {
local src; src="$(mktemp -d)"
local dst; dst="$(mktemp -d)"
mkdir -p "$src/agents"
cat > "$src/agents/scribe.md" <<'EOF'
---
name: scribe
description: Test scribe
model: sonnet
capabilities: [read, write]
---
See .platform/references/agents.md and DISPATCHER.md for context.
EOF
adapter_translate_agents "$src/agents" "$dst"
local out="$dst/.opencode/agents/scribe.md"
local result=0
local content; content="$(cat "$out")"
[[ "$content" == *".opencode/references/agents.md"* ]] || { echo ".opencode/references/ not found: $content"; result=1; }
[[ "$content" == *"AGENTS.md"* ]] || { echo "AGENTS.md not found: $content"; result=1; }
[[ "$content" != *".claude/"* ]] || { echo ".claude/ still present: $content"; result=1; }
rm -rf "$src" "$dst"
return $result
}
test_oc_translate_agents_bash_capability() {
local src; src="$(mktemp -d)"
local dst; dst="$(mktemp -d)"
mkdir -p "$src/agents"
cat > "$src/agents/architect.md" <<'EOF'
---
name: architect
description: Test arch
model: high
capabilities: [read, write, edit, bash]
---
body
EOF
adapter_translate_agents "$src/agents" "$dst"
local out="$dst/.opencode/agents/architect.md"
local result=0
grep -q '^ edit: allow' "$out" || { echo "edit missing"; result=1; }
grep -q '^ bash: allow' "$out" || { echo "bash missing"; result=1; }
grep -q '^model: anthropic/claude-opus-4-5' "$out" || { echo "model not mapped"; result=1; }
rm -rf "$src" "$dst"
return $result
}
test_oc_translate_agents_read_only_emits_empty_permission() {
local src; src="$(mktemp -d)"
local dst; dst="$(mktemp -d)"
mkdir -p "$src/agents"
cat > "$src/agents/seeker.md" <<'EOF'
---
name: seeker
description: Search only
model: mid
capabilities: [read]
---
body
EOF
adapter_translate_agents "$src/agents" "$dst"
local out="$dst/.opencode/agents/seeker.md"
local result=0
grep -q '^permission: {}' "$out" || { echo "expected 'permission: {}'"; cat "$out"; result=1; }
rm -rf "$src" "$dst"
return $result
}
test_oc_translate_agents_dedupes_edit() {
# write+edit both map to "edit: allow" — must appear once, not twice.
local src; src="$(mktemp -d)"
local dst; dst="$(mktemp -d)"
mkdir -p "$src/agents"
cat > "$src/agents/scribe.md" <<'EOF'
---
name: scribe
description: Dedupe test
model: mid
capabilities: [read, write, edit]
---
body
EOF
adapter_translate_agents "$src/agents" "$dst"
local out="$dst/.opencode/agents/scribe.md"
local count; count="$(grep -c '^ edit: allow' "$out")"
rm -rf "$src" "$dst"
[[ "$count" == "1" ]] || { echo "expected 1 edit line, got $count"; return 1; }
}
test_oc_translate_hooks_copies_scripts() {
local src; src="$(mktemp -d)"
local dst; dst="$(mktemp -d)"
mkdir -p "$src/hooks"
cat > "$src/hooks/protect.hook.yaml" <<'EOF'
name: protect
script: protect.sh
triggers:
- event: before-tool-use
match-tool: [edit, write]
EOF
cat > "$src/hooks/protect.sh" <<'EOF'
#!/usr/bin/env bash
exit 0
EOF
adapter_translate_hooks "$src/hooks" "$dst"
local result=0
[[ -f "$dst/.opencode/hooks/protect.sh" ]] || { echo "protect.sh not copied"; result=1; }
[[ -f "$dst/.opencode/plugins/mbifc-hooks.js" ]] || { echo "mbifc-hooks.js not generated"; result=1; }
rm -rf "$src" "$dst"
return $result
}
test_oc_translate_hooks_registry_has_entries() {
local src; src="$(mktemp -d)"
local dst; dst="$(mktemp -d)"
mkdir -p "$src/hooks"
cat > "$src/hooks/protect.hook.yaml" <<'EOF'
name: protect
script: protect.sh
triggers:
- event: before-tool-use
match-tool: [edit, write]
EOF
cat > "$src/hooks/notify.hook.yaml" <<'EOF'
name: notify
script: notify.sh
triggers:
- event: on-notification
EOF
touch "$src/hooks/protect.sh" "$src/hooks/notify.sh"
adapter_translate_hooks "$src/hooks" "$dst"
local plugin="$dst/.opencode/plugins/mbifc-hooks.js"
local result=0
grep -q '"name": "protect"' "$plugin" || { echo "protect not in registry"; result=1; }
grep -q '"name": "notify"' "$plugin" || { echo "notify not in registry"; result=1; }
grep -q '"event": "tool.execute.before"' "$plugin" || { echo "tool.execute.before event missing"; result=1; }
grep -q '"event": "session.idle"' "$plugin" || { echo "session.idle event missing"; result=1; }
grep -q '"matchTool":' "$plugin" || { echo "matchTool field missing"; result=1; }
grep -q 'spawn("bash"' "$plugin" || { echo "bash-executor not inlined"; result=1; }
rm -rf "$src" "$dst"
return $result
}
test_oc_translate_hooks_no_hooks_dir_is_noop() {
local src; src="$(mktemp -d)"
local dst; dst="$(mktemp -d)"
# src has no hooks/ — should not error, should not create plugin file
adapter_translate_hooks "$src/hooks" "$dst"
local result=0
[[ ! -f "$dst/.opencode/plugins/mbifc-hooks.js" ]] || { echo "plugin should not exist when no hooks"; result=1; }
rm -rf "$src" "$dst"
return $result
}
test_oc_translate_mcp_local() {
local src; src="$(mktemp -d)"
local dst; dst="$(mktemp -d)"
mkdir -p "$src/mcp"
cat > "$src/mcp/servers.yaml" <<'EOF'
servers:
- name: gmail
type: local
command: [npx, -y, "@anthropic-ai/gmail-mcp"]
env: {}
EOF
adapter_translate_mcp "$src/mcp" "$dst"
local out="$dst/opencode.json"
local result=0
[[ -f "$out" ]] || { echo "opencode.json missing"; result=1; }
jq -e '.mcp.gmail.type == "local"' "$out" >/dev/null || { echo "type wrong"; cat "$out"; result=1; }
jq -e '.mcp.gmail.command | startswith("npx")' "$out" >/dev/null || { echo "command wrong"; cat "$out"; result=1; }
jq -e '.mcp.gmail.environment == {}' "$out" >/dev/null || { echo "environment key missing"; cat "$out"; result=1; }
rm -rf "$src" "$dst"
return $result
}
test_oc_translate_mcp_remote() {
local src; src="$(mktemp -d)"
local dst; dst="$(mktemp -d)"
mkdir -p "$src/mcp"
cat > "$src/mcp/servers.yaml" <<'EOF'
servers:
- name: Gmail
type: http
url: "https://gmail.mcp.claude.com/mcp"
env: {}
EOF
adapter_translate_mcp "$src/mcp" "$dst"
local out="$dst/opencode.json"
local result=0
jq -e '.mcp.Gmail.type == "remote"' "$out" >/dev/null || { echo "type should be remote"; cat "$out"; result=1; }
jq -e '.mcp.Gmail.url == "https://gmail.mcp.claude.com/mcp"' "$out" >/dev/null || { echo "url wrong"; cat "$out"; result=1; }
rm -rf "$src" "$dst"
return $result
}
test_oc_adapter_build_end_to_end() {
local src; src="$(mktemp -d)"
local dst; dst="$(mktemp -d)"
# Minimal source tree
echo "# Dispatcher" > "$src/DISPATCHER.md"
mkdir -p "$src/agents" "$src/hooks" "$src/skills/onboarding" "$src/references" "$src/mcp"
cat > "$src/agents/scribe.md" <<'EOF'
---
name: scribe
description: Test scribe
model: mid
capabilities: [read, write, edit]
---
body
EOF
cat > "$src/hooks/protect.hook.yaml" <<'EOF'
name: protect
script: protect.sh
triggers:
- event: before-tool-use
match-tool: [edit]
EOF
echo "#!/usr/bin/env bash" > "$src/hooks/protect.sh"
cat > "$src/skills/onboarding/SKILL.md" <<'EOF'
---
name: onboarding
description: Onboarding skill
---
body
EOF
echo "reference content" > "$src/references/policy.md"
cat > "$src/mcp/servers.yaml" <<'EOF'
servers:
- name: gmail
type: local
command: [npx, -y, "@anthropic-ai/gmail-mcp"]
env: {}
EOF
adapter_build "$src" "$dst"
local result=0
[[ -f "$dst/AGENTS.md" ]] || { echo "AGENTS.md missing"; result=1; }
[[ -f "$dst/.opencode/agents/scribe.md" ]] || { echo "agent missing"; result=1; }
[[ -f "$dst/.opencode/skills/onboarding/SKILL.md" ]] || { echo "skill missing"; result=1; }
[[ -f "$dst/.opencode/references/policy.md" ]] || { echo "reference missing"; result=1; }
[[ -f "$dst/.opencode/hooks/protect.sh" ]] || { echo "hook script missing"; result=1; }
[[ -f "$dst/.opencode/plugins/mbifc-hooks.js" ]] || { echo "plugin missing"; result=1; }
[[ -f "$dst/opencode.json" ]] || { echo "opencode.json missing"; result=1; }
rm -rf "$src" "$dst"
return $result
}
@@ -0,0 +1,172 @@
#!/usr/bin/env bash
# Tests for adapters/opencode/config-merge.sh
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../../.." && pwd)"
source "$ROOT/adapters/lib.sh"
source "$ROOT/adapters/opencode/config-merge.sh"
test_oc_merge_fresh_install() {
local built; built="$(mktemp)"
local existing; existing="$(mktemp)"
local output; output="$(mktemp)"
rm "$existing"
cat > "$built" <<'HEREDOC'
{
"mcp": {
"Gmail": {
"type": "remote",
"url": "https://gmail.mcp.claude.com/mcp"
}
}
}
HEREDOC
oc_config_merge "$built" "$existing" "$output"
local result=0
[[ -f "$output" ]] || { echo "output not created"; result=1; }
jq -e '.mcp.Gmail.url' "$output" >/dev/null || { echo "Gmail not in output"; result=1; }
rm -f "$built" "$output"
return $result
}
test_oc_merge_preserves_user_keys() {
local built; built="$(mktemp)"
local existing; existing="$(mktemp)"
local output; output="$(mktemp)"
cat > "$built" <<'HEREDOC'
{
"mcp": {
"Gmail": {
"type": "remote",
"url": "https://gmail.mcp.claude.com/mcp"
}
}
}
HEREDOC
cat > "$existing" <<'HEREDOC'
{
"model": "anthropic/claude-sonnet-4-5",
"small_model": "anthropic/claude-haiku-4-5",
"mcp": {
"MyCustomServer": {
"type": "local",
"command": "my-server"
}
}
}
HEREDOC
oc_config_merge "$built" "$existing" "$output"
local result=0
jq -e '.mcp.Gmail.url' "$output" >/dev/null || { echo "Gmail missing"; result=1; }
jq -e '.model == "anthropic/claude-sonnet-4-5"' "$output" >/dev/null || { echo "model key lost"; result=1; }
jq -e '.small_model == "anthropic/claude-haiku-4-5"' "$output" >/dev/null || { echo "small_model key lost"; result=1; }
jq -e '.mcp.MyCustomServer.command == "my-server"' "$output" >/dev/null || { echo "user MCP server lost"; result=1; }
rm -f "$built" "$existing" "$output"
return $result
}
test_oc_merge_our_mcp_overwrites_same_name() {
local built; built="$(mktemp)"
local existing; existing="$(mktemp)"
local output; output="$(mktemp)"
cat > "$built" <<'HEREDOC'
{
"mcp": {
"Gmail": {
"type": "remote",
"url": "https://gmail.mcp.claude.com/mcp"
}
}
}
HEREDOC
cat > "$existing" <<'HEREDOC'
{
"mcp": {
"Gmail": {
"type": "remote",
"url": "https://old-url.example.com"
}
}
}
HEREDOC
oc_config_merge "$built" "$existing" "$output"
local result=0
local url; url="$(jq -r '.mcp.Gmail.url' "$output")"
[[ "$url" == "https://gmail.mcp.claude.com/mcp" ]] || { echo "our url should win: got $url"; result=1; }
rm -f "$built" "$existing" "$output"
return $result
}
test_oc_merge_detects_indentation() {
local built; built="$(mktemp)"
local existing; existing="$(mktemp)"
local output; output="$(mktemp)"
cat > "$built" <<'HEREDOC'
{
"mcp": {
"Gmail": {
"type": "remote",
"url": "https://gmail.mcp.claude.com/mcp"
}
}
}
HEREDOC
cat > "$existing" <<'HEREDOC'
{
"model": "anthropic/claude-sonnet-4-5",
"mcp": {}
}
HEREDOC
oc_config_merge "$built" "$existing" "$output"
local result=0
grep -q '^ "model"' "$output" || { echo "expected 4-space indent"; cat "$output"; result=1; }
rm -f "$built" "$existing" "$output"
return $result
}
test_oc_merge_malformed_existing_falls_back() {
local built; built="$(mktemp)"
local existing; existing="$(mktemp)"
local output; output="$(mktemp)"
cat > "$built" <<'HEREDOC'
{
"mcp": {
"Gmail": {
"type": "remote",
"url": "https://gmail.mcp.claude.com/mcp"
}
}
}
HEREDOC
echo "this is not json" > "$existing"
oc_config_merge "$built" "$existing" "$output" 2>/dev/null
local result=0
jq -e '.mcp.Gmail.url' "$output" >/dev/null || { echo "fallback failed"; result=1; }
rm -f "$built" "$existing" "$output"
return $result
}
test_oc_merge_no_mcp_in_existing() {
local built; built="$(mktemp)"
local existing; existing="$(mktemp)"
local output; output="$(mktemp)"
cat > "$built" <<'HEREDOC'
{
"mcp": {
"Gmail": {
"type": "remote",
"url": "https://gmail.mcp.claude.com/mcp"
}
}
}
HEREDOC
cat > "$existing" <<'HEREDOC'
{
"model": "anthropic/claude-sonnet-4-5"
}
HEREDOC
oc_config_merge "$built" "$existing" "$output"
local result=0
jq -e '.model == "anthropic/claude-sonnet-4-5"' "$output" >/dev/null || { echo "model key lost"; result=1; }
jq -e '.mcp.Gmail.url' "$output" >/dev/null || { echo "Gmail not added"; result=1; }
rm -f "$built" "$existing" "$output"
return $result
}