Repository navigation
Expand file tree
/
Copy pathtest_opencode_generation.py
More file actions
239 lines (192 loc) · 9.54 KB
/
Copy pathtest_opencode_generation.py
File metadata and controls
239 lines (192 loc) · 9.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
"""Contract tests for the generated OpenCode target.
The canonical skills, agents, and hooks stay Claude-compatible. The OpenCode
target is generated into `.opencode/` so OpenCode sees native frontmatter,
native tool calls, slash commands, and the local plugin. These tests fail when
the generated layer drifts from the canonical source or regresses to Claude-only
syntax.
"""
from __future__ import annotations
import json
import re
import subprocess
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
OPENCODE = ROOT / ".opencode"
SKILLS = ROOT / "skills"
AGENTS = ROOT / "agents"
def frontmatter(text: str) -> str:
match = re.match(r"^---\r?\n(.*?)\r?\n---", text, re.DOTALL)
assert match, "missing frontmatter"
return match.group(1)
def test_generated_target_is_current() -> None:
result = subprocess.run(
[sys.executable, str(ROOT / "scripts" / "build_opencode.py"), "--check"],
cwd=ROOT,
capture_output=True,
text=True,
check=False,
)
assert result.returncode == 0, result.stdout + result.stderr
def test_skill_and_agent_counts_match_canonical_sources() -> None:
expected_skills = {path.parent.name for path in SKILLS.glob("*/SKILL.md")}
expected_agents = {path.stem for path in AGENTS.glob("*.md")}
actual_skills = {path.parent.name for path in (OPENCODE / "skills").glob("*/SKILL.md")}
actual_agents = {path.stem for path in (OPENCODE / "agents").glob("*.md")}
assert actual_skills == expected_skills
assert actual_agents == expected_agents
def test_generated_markdown_has_no_claude_tokens() -> None:
forbidden = (
"${CLAUDE_PLUGIN_ROOT}",
"mcp__",
"maxTurns",
"seo run run",
"~/.config/claude-seo",
"~/.cache/claude-seo",
)
hits: list[str] = []
for path in sorted((OPENCODE / "skills").rglob("*.md")) + sorted((OPENCODE / "agents").glob("*.md")):
text = path.read_text(encoding="utf-8")
for token in forbidden:
if token in text:
hits.append(f"{path.relative_to(ROOT).as_posix()}: {token}")
assert not hits, "\n".join(hits)
def test_vendor_matrix_keeps_the_anthropic_product_name() -> None:
text = (
OPENCODE / "skills" / "seo-agentic" / "references" / "vendor-matrix.md"
).read_text(encoding="utf-8")
assert "Claude in Chrome (Claude Code, Cowork, Desktop)" in text
assert "Claude in Chrome (OpenCode, Cowork, Desktop)" not in text
def test_cross_skill_reference_exists() -> None:
text = (
OPENCODE / "skills" / "seo-agentic" / "references" / "discovery-and-markdown.md"
).read_text(encoding="utf-8")
assert "../seo-geo/references/llmstxt-evidence.md" in text
assert (
OPENCODE / "skills" / "seo-geo" / "references" / "llmstxt-evidence.md"
).is_file()
def test_extension_commands_guard_missing_skills() -> None:
for name, extension in (
("ahrefs", "ahrefs"),
("firecrawl", "firecrawl"),
("matomo", "matomo"),
("profound", "profound"),
("seranking", "seranking"),
("bing", "bing-webmaster"),
("unlighthouse", "unlighthouse"),
):
text = (OPENCODE / "commands" / "seo" / f"{name}.md").read_text(encoding="utf-8")
assert "install-opencode" in text, name
assert extension in text, name
def test_generated_skills_do_not_declare_claude_only_frontmatter() -> None:
hits: list[str] = []
for path in sorted((OPENCODE / "skills").glob("*/SKILL.md")):
value = frontmatter(path.read_text(encoding="utf-8"))
for key in ("user-invocable", "argument-hint"):
if re.search(rf"(?m)^{key}:", value):
hits.append(f"{path.relative_to(ROOT).as_posix()}: {key}")
assert not hits, "\n".join(hits)
def test_generated_agents_use_opencode_fields() -> None:
for path in sorted((OPENCODE / "agents").glob("*.md")):
value = frontmatter(path.read_text(encoding="utf-8"))
assert re.search(r"(?m)^mode:\s*subagent$", value), path
assert re.search(r"(?m)^steps:\s*\d+$", value), path
assert re.search(r"(?m)^permissions:$", value), path
assert not re.search(r"(?m)^tools:", value), path
assert not re.search(r"(?m)^maxTurns:", value), path
assert not re.search(r"(?m)^model:\s*(sonnet|opus)$", value), path
assert 'action: "seo"' in value, path
def test_agent_permissions_keep_seo_tool_and_env_safety() -> None:
for path in sorted((OPENCODE / "agents").glob("*.md")):
value = frontmatter(path.read_text(encoding="utf-8"))
assert 'action: "*"' in value
assert "effect: deny" in value
assert 'action: "read"' in value
assert 'resource: "*.env"' in value
assert "effect: ask" in value
def test_commands_exist() -> None:
assert (OPENCODE / "commands" / "seo.md").is_file()
for name in ("audit", "page", "technical", "schema", "sitemap", "geo"):
assert (OPENCODE / "commands" / "seo" / f"{name}.md").is_file(), name
def test_plugin_and_root_config_exist() -> None:
assert (OPENCODE / "plugins" / "opencode-seo" / "index.ts").is_file()
assert (OPENCODE / "plugins" / "opencode-seo" / "package.json").is_file()
config = json.loads((ROOT / "opencode.json").read_text(encoding="utf-8"))
assert "skills" not in config, "explicit skills source would override .opencode/skills"
assert "instructions" not in config, "AGENTS.md is discovered automatically"
assert config["experimental"]["subagent_depth"] == 1
assert config["experimental"]["todo"] is True
def test_package_manifest_exposes_the_plugin() -> None:
root = json.loads((ROOT / "package.json").read_text(encoding="utf-8"))
plugin = json.loads(
(ROOT / "opencode" / "plugin" / "opencode-seo" / "package.json").read_text(encoding="utf-8")
)
assert plugin["type"] == "module"
assert plugin["exports"]["."] == "./index.ts"
assert plugin["version"] == root["version"]
def test_plugin_exposes_runtime_bridge_and_schema_hook() -> None:
plugin = (OPENCODE / "plugins" / "opencode-seo" / "index.ts").read_text(encoding="utf-8")
assert "ctx.tool.transform" in plugin
assert "ctx.tool.hook(\"execute.after\"" in plugin
assert "ctx.permission.assert" in plugin
assert "ctx.location.directory" in plugin
assert "cwd: workspace" in plugin
assert "runtime.py" in plugin
for action in ("run", "setup", "doctor", "path", "read"):
assert f'"{action}"' in plugin
assert "shell: true" not in plugin
def test_plugin_reads_prefer_the_generated_opencode_assets() -> None:
plugin = (OPENCODE / "plugins" / "opencode-seo" / "index.ts").read_text(encoding="utf-8")
assert 'path.join(runtimeRoot, ".opencode")' in plugin, (
"the seo read action must serve the generated OpenCode-native assets, not the "
"canonical Claude-compatible copies"
)
def test_plugin_unwraps_schema_hook_context_and_filters_extensions() -> None:
plugin = (OPENCODE / "plugins" / "opencode-seo" / "index.ts").read_text(encoding="utf-8")
assert "hookSpecificOutput" in plugin, "warning output must be unwrapped for OpenCode"
assert "schemaHookText(result)" in plugin
assert r"\.(html?|jsx|tsx|vue|svelte|php|ejs)$" in plugin, (
"only HTML-like edits can carry JSON-LD; other extensions should not spawn Python"
)
def test_plugin_caches_runtime_probes_and_routes_seo_prompts() -> None:
plugin = (OPENCODE / "plugins" / "opencode-seo" / "index.ts").read_text(encoding="utf-8")
assert "PYTHON_STORAGE_KEY" in plugin
assert "DOCTOR_STORAGE_KEY" in plugin
assert "ctx.storage" in plugin
assert "DOCTOR_TTL_MS" in plugin
assert 'ctx.session.hook("prompt"' in plugin
assert "seoPromptHint" in plugin
assert "refresh?: boolean" in plugin
def test_image_gen_offers_native_generation_and_keeps_banana() -> None:
text = (OPENCODE / "skills" / "seo-image-gen" / "SKILL.md").read_text(encoding="utf-8")
assert "image_generate" in text
assert "experimental.image_generate.model" in text
assert "banana" in text.lower(), "banana must remain a supported alternative"
assert "gemini_generate_image" in text
def test_generated_templates_referenced_by_skills_are_present() -> None:
assert (OPENCODE / "skills" / "seo-cluster" / "templates" / "cluster-map.html").is_file()
assert (
OPENCODE / "skills" / "seo-google" / "assets" / "templates" / "cwv-audit-report.md"
).is_file()
def test_generated_target_drops_claude_branding() -> None:
text_suffixes = {".md", ".ts", ".json", ".txt", ".html", ".css", ".js", ".svg", ".yml", ".yaml"}
offences: list[str] = []
for path in sorted(OPENCODE.rglob("*")):
if not path.is_file() or path.name == "LICENSE.txt":
continue
if path.suffix.lower() not in text_suffixes:
continue
if "claude-seo" in path.read_text(encoding="utf-8", errors="ignore"):
offences.append(path.relative_to(OPENCODE).as_posix())
assert not offences, f"unbranded OpenCode files still mention claude-seo: {offences}"
def test_generated_target_has_no_claude_plugin_tokens() -> None:
tokens = ("${CLAUDE_PLUGIN_ROOT}", "maxTurns", "Task(", "~/.claude", "allowed-tools")
offences: list[str] = []
for path in sorted(OPENCODE.rglob("*")):
if not path.is_file() or path.suffix.lower() not in {".md", ".ts", ".json"}:
continue
text = path.read_text(encoding="utf-8", errors="ignore")
for token in tokens:
if token in text:
offences.append(f"{path.relative_to(OPENCODE).as_posix()}: {token}")
assert not offences, offences