mirror of
https://github.com/K-Dense-AI/scientific-agent-skills.git
synced 2026-12-04 01:24:00 +08:00
AGENTS.md has always said that a skill shipping scripts/ puts its tests in
tests/<name>/, but nothing checked it: 54 of the 100 such skills had no suite
at all, including docx, pptx, xlsx, pdf and scanpy. All 100 now do.
tests/_meta is the guard. It runs the shared structural contract across every
skill in a single process -- safe because it parses scripts with ast and never
imports them -- and fails when a skill ships scripts/ without a suite or
without a [skills.<name>] entry in skill-requirements.toml. It needs no
scientific packages and finishes in seconds, so skill-tests.yml blocks every
pull request on it, plus the packages=[] suites.
tests/_contract holds what the per-skill suites were each reimplementing:
frontmatter conformance, the 500-line limit, no tests or bytecode under
skills/, local links resolving, scripts parsing, no eval/exec/os.system, no
standard-library shadowing, no hardcoded local paths, valid shell scripts. Also
the --help contract, which skips when a skill's packages are absent and runs
for real under --isolated, and shared behaviour for the files docx/pptx/xlsx
and five schematic-shipping skills carry byte-identical copies of, with drift
detection so they cannot diverge silently.
67 new suites, 3325 test functions. The existing suites were retrofitted: 18
no longer pin an exact skill version, so a version bump no longer breaks a
test; 11 duplicated structural methods removed; 19 wired to the --help
contract; and 5 that failed collection without their packages now skip
cleanly. run_all.py in the bare project environment goes from 6 failures to 0.
Writing the tests surfaced 19 defects in the skills, fixed here with version
bumps. The ones that changed scientific output:
- openpiv reported vorticity 0 for a rotating flow, from a sign error in
openpiv's y-up coordinate relabelling; solid-body rotation now gives 2w
exactly, on grids of either orientation
- deepchem returned solubility predictions in z-scored space while labelling
them log(mol/L), because it transformed a y-less dataset instead of
untransforming the output
- neuropixels-analysis had the Allen and IBL ISI thresholds swapped,
contradicting its own references/QUALITY_METRICS.md and inverting the two
standards' relative strictness
- scanpy's summarize() raised TypeError on every AnnData under anndata 0.13,
which reports an unnamed None key on .layers; scanpy convert was broken
- experimental-design's Latin hypercube was never reproducible: pyDOE3 draws
from its own default_rng and ignores numpy's global seed
- primekg shipped a hardcoded path naming a person, which is why
no_personal_paths is now a contract rule
The remainder is upstream API drift, each verified against the installed
package: retired symbols in bioservices 1.16, gget helpers that returned lists
where a string was written, ArviZ 1.x kwargs in pymc, a positional-only
factory in pymoo, ReduceLROnPlateau(verbose=) in torch 2.13, a removed scvelo
parameter, and PyPDF2 in scientific-slides.
Two manifest environments could not build and are pinned: gget, where an
unpinned scanpy walked back to 1.9.8 and pulled llvmlite 0.36 which does not
compile on 3.13, and pymatgen, pinned to the snapshot its own _common.py
enforces rather than loosening that check. deepchem gains torch, without which
no model class exists.
python tests/run_all.py --isolated: 101 passed, 0 failed.
244 lines
9.1 KiB
Python
244 lines
9.1 KiB
Python
"""Dependency-free, network-free tests for the generate-image helper CLI."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import base64
|
|
import importlib.util
|
|
import os
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
SKILL_ROOT = Path(__file__).resolve().parents[2] / "skills" / "generate-image"
|
|
SCRIPT = SKILL_ROOT / "scripts" / "generate_image.py"
|
|
|
|
|
|
def load_module():
|
|
"""Import the CLI under a unique name so it cannot collide with other skills.
|
|
|
|
Bytecode writing is suppressed: a __pycache__ left inside the skill directory
|
|
would ship as an artifact the agent never loads.
|
|
"""
|
|
spec = importlib.util.spec_from_file_location("generate_image_skill_cli", SCRIPT)
|
|
module = importlib.util.module_from_spec(spec)
|
|
previous = sys.dont_write_bytecode
|
|
sys.dont_write_bytecode = True
|
|
try:
|
|
spec.loader.exec_module(module)
|
|
finally:
|
|
sys.dont_write_bytecode = previous
|
|
return module
|
|
|
|
|
|
gi = load_module()
|
|
|
|
|
|
def make_args(**overrides) -> argparse.Namespace:
|
|
defaults = dict(
|
|
prompt="a prompt",
|
|
model=gi.DEFAULT_MODEL,
|
|
output=None,
|
|
input=None,
|
|
n=None,
|
|
aspect_ratio=None,
|
|
resolution=None,
|
|
size=None,
|
|
quality=None,
|
|
output_format=None,
|
|
background=None,
|
|
output_compression=None,
|
|
seed=None,
|
|
)
|
|
defaults.update(overrides)
|
|
return argparse.Namespace(**defaults)
|
|
|
|
|
|
class CliSurfaceTests(unittest.TestCase):
|
|
def test_help_exits_zero(self):
|
|
completed = subprocess.run(
|
|
[sys.executable, "-B", str(SCRIPT), "--help"],
|
|
text=True, capture_output=True, timeout=20, check=False,
|
|
)
|
|
self.assertEqual(completed.returncode, 0, completed.stderr)
|
|
self.assertIn("usage:", completed.stdout.lower())
|
|
|
|
def test_missing_prompt_is_an_error(self):
|
|
completed = subprocess.run(
|
|
[sys.executable, "-B", str(SCRIPT)],
|
|
text=True, capture_output=True, timeout=20, check=False,
|
|
)
|
|
self.assertNotEqual(completed.returncode, 0)
|
|
self.assertIn("prompt is required", completed.stderr)
|
|
|
|
def test_targets_the_images_endpoint_not_chat_completions(self):
|
|
self.assertEqual(gi.IMAGES_URL, "https://openrouter.ai/api/v1/images")
|
|
self.assertEqual(gi.MODELS_URL, "https://openrouter.ai/api/v1/images/models")
|
|
source = SCRIPT.read_text(encoding="utf-8")
|
|
self.assertNotIn("chat/completions", source)
|
|
self.assertNotIn("modalities", source)
|
|
|
|
def test_uses_only_the_standard_library(self):
|
|
source = SCRIPT.read_text(encoding="utf-8")
|
|
for third_party in ("import requests", "import httpx", "from requests"):
|
|
self.assertNotIn(third_party, source)
|
|
|
|
|
|
class ApiKeyResolutionTests(unittest.TestCase):
|
|
def test_explicit_key_wins(self):
|
|
self.assertEqual(gi.find_api_key("explicit-key"), "explicit-key")
|
|
|
|
def test_environment_variable_is_read(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
previous = os.getcwd()
|
|
os.chdir(tmp)
|
|
try:
|
|
os.environ["OPENROUTER_API_KEY"] = "env-key"
|
|
self.assertEqual(gi.find_api_key(None), "env-key")
|
|
finally:
|
|
os.environ.pop("OPENROUTER_API_KEY", None)
|
|
os.chdir(previous)
|
|
|
|
def test_dotenv_is_read_when_environment_is_empty(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
(Path(tmp) / ".env").write_text(
|
|
"# comment\nOTHER=1\nOPENROUTER_API_KEY=\"dotenv-key\"\n", encoding="utf-8"
|
|
)
|
|
previous = os.getcwd()
|
|
os.chdir(tmp)
|
|
try:
|
|
os.environ.pop("OPENROUTER_API_KEY", None)
|
|
self.assertEqual(gi.find_api_key(None), "dotenv-key")
|
|
finally:
|
|
os.chdir(previous)
|
|
|
|
def test_absent_key_raises_with_guidance(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
previous = os.getcwd()
|
|
os.chdir(tmp)
|
|
try:
|
|
os.environ.pop("OPENROUTER_API_KEY", None)
|
|
with self.assertRaises(gi.ApiError) as caught:
|
|
gi.find_api_key(None)
|
|
self.assertIn("OPENROUTER_API_KEY", str(caught.exception))
|
|
finally:
|
|
os.chdir(previous)
|
|
|
|
|
|
class PayloadTests(unittest.TestCase):
|
|
def test_unset_parameters_are_omitted(self):
|
|
payload = gi.build_payload(make_args())
|
|
self.assertEqual(set(payload), {"model", "prompt"})
|
|
|
|
def test_set_parameters_are_included(self):
|
|
payload = gi.build_payload(make_args(aspect_ratio="16:9", seed=42, n=3))
|
|
self.assertEqual(payload["aspect_ratio"], "16:9")
|
|
self.assertEqual(payload["seed"], 42)
|
|
self.assertEqual(payload["n"], 3)
|
|
self.assertNotIn("quality", payload)
|
|
|
|
def test_zero_seed_survives_omission_filtering(self):
|
|
payload = gi.build_payload(make_args(seed=0))
|
|
self.assertEqual(payload["seed"], 0)
|
|
|
|
def test_references_use_the_input_references_shape(self):
|
|
payload = gi.build_payload(make_args(input=["https://example.com/a.png"]))
|
|
self.assertEqual(
|
|
payload["input_references"],
|
|
[{"type": "image_url", "image_url": {"url": "https://example.com/a.png"}}],
|
|
)
|
|
|
|
|
|
class ReferenceEncodingTests(unittest.TestCase):
|
|
def test_urls_and_data_urls_pass_through(self):
|
|
for value in ("https://e.com/a.png", "http://e.com/a.png", "data:image/png;base64,AAA"):
|
|
self.assertEqual(gi.encode_reference(value), value)
|
|
|
|
def test_local_file_becomes_a_data_url(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
path = Path(tmp) / "pic.png"
|
|
path.write_bytes(b"\x89PNG\r\n\x1a\nfake")
|
|
encoded = gi.encode_reference(str(path))
|
|
self.assertTrue(encoded.startswith("data:image/png;base64,"))
|
|
self.assertEqual(
|
|
base64.b64decode(encoded.split(",", 1)[1]), b"\x89PNG\r\n\x1a\nfake"
|
|
)
|
|
|
|
def test_jpeg_extension_maps_to_jpeg_mime(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
path = Path(tmp) / "pic.jpg"
|
|
path.write_bytes(b"jpegbytes")
|
|
self.assertTrue(gi.encode_reference(str(path)).startswith("data:image/jpeg;base64,"))
|
|
|
|
def test_missing_file_raises(self):
|
|
with self.assertRaises(gi.ApiError):
|
|
gi.encode_reference("/nonexistent/nope.png")
|
|
|
|
def test_unsupported_extension_raises(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
path = Path(tmp) / "pic.tiff"
|
|
path.write_bytes(b"x")
|
|
with self.assertRaises(gi.ApiError):
|
|
gi.encode_reference(str(path))
|
|
|
|
|
|
class OutputPathTests(unittest.TestCase):
|
|
def test_default_name_follows_media_type(self):
|
|
self.assertEqual(gi.output_paths(None, "image/png", 1), [Path("generated_image.png")])
|
|
self.assertEqual(gi.output_paths(None, "image/svg+xml", 1), [Path("generated_image.svg")])
|
|
self.assertEqual(gi.output_paths(None, "image/jpeg", 1), [Path("generated_image.jpg")])
|
|
|
|
def test_unknown_media_type_falls_back_to_png(self):
|
|
self.assertEqual(gi.output_paths(None, "image/unheard-of", 1), [Path("generated_image.png")])
|
|
|
|
def test_explicit_output_is_respected(self):
|
|
self.assertEqual(gi.output_paths("art.png", "image/png", 1), [Path("art.png")])
|
|
|
|
def test_multiple_images_are_numbered(self):
|
|
self.assertEqual(
|
|
gi.output_paths("out.png", "image/png", 3),
|
|
[Path("out_1.png"), Path("out_2.png"), Path("out_3.png")],
|
|
)
|
|
|
|
|
|
class SaveImageTests(unittest.TestCase):
|
|
def test_saves_base64_payload(self):
|
|
payload = base64.b64encode(b"imagebytes").decode("ascii")
|
|
response = {"data": [{"b64_json": payload, "media_type": "image/png"}]}
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
target = Path(tmp) / "out.png"
|
|
written = gi.save_images(response, str(target))
|
|
self.assertEqual(written, [target])
|
|
self.assertEqual(target.read_bytes(), b"imagebytes")
|
|
|
|
def test_creates_missing_parent_directories(self):
|
|
payload = base64.b64encode(b"x").decode("ascii")
|
|
response = {"data": [{"b64_json": payload, "media_type": "image/png"}]}
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
target = Path(tmp) / "nested" / "dir" / "out.png"
|
|
gi.save_images(response, str(target))
|
|
self.assertTrue(target.is_file())
|
|
|
|
def test_empty_data_raises(self):
|
|
with self.assertRaises(gi.ApiError):
|
|
gi.save_images({"data": []}, None)
|
|
|
|
|
|
class SkillDocumentTests(unittest.TestCase):
|
|
def test_frontmatter_version_matches_expected(self):
|
|
text = (SKILL_ROOT / "SKILL.md").read_text(encoding="utf-8")
|
|
self.assertRegex(text, r'\n version: "\d+\.\d+"\n')
|
|
|
|
def test_documented_default_model_matches_the_script(self):
|
|
text = (SKILL_ROOT / "SKILL.md").read_text(encoding="utf-8")
|
|
self.assertIn(gi.DEFAULT_MODEL, text)
|
|
|
|
def test_reference_file_is_present(self):
|
|
self.assertTrue((SKILL_ROOT / "references" / "models.md").is_file())
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|