Files
scientific-agent-skills/tests/run_all.py
T
Timothy Kassis 4fb7e0bc29 Test every script-bearing skill, and enforce that it stays that way
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.
2026-07-28 10:20:12 -07:00

176 lines
6.8 KiB
Python

#!/usr/bin/env python3
"""Run the skill test suites, one pytest process per skill.
Separate processes are required, not a preference: skills' `scripts/`
directories share top-level module names (see tests/conftest.py).
python tests/run_all.py # every skill under tests/
python tests/run_all.py qutip pydicom # only the named skills
python tests/run_all.py -- -x --tb=long # pass extra args to pytest
A full run starts with `tests/_meta`, the repo-wide guard that checks every
skill against the shared structural contract and fails if a skill ships
`scripts/` without a suite here. Naming skills explicitly skips it.
With `--isolated`, each suite instead runs in a throwaway `uv` environment
built from that skill's entry in tests/skill-requirements.toml:
python tests/run_all.py --isolated
python tests/run_all.py --isolated scanpy qiskit
Nothing is installed into the project environment. Each skill gets only the
packages it documents, on the interpreter it needs -- which is the point:
opentrons pins numpy<2, PyTDC needs 3.11, and esm caps transformers below the
version the transformers skill targets, so no single environment can host them
all. uv caches wheels globally, so repeat runs create each environment in
milliseconds.
Exit code is 0 only when every suite passes. Suites that collect nothing are
reported as "empty" and do not fail the run.
"""
from __future__ import annotations
import shutil
import subprocess
import sys
import tomllib
from pathlib import Path
TESTS_DIR = Path(__file__).resolve().parent
REPO_ROOT = TESTS_DIR.parent
REQUIREMENTS = TESTS_DIR / "skill-requirements.toml"
NO_TESTS_COLLECTED = 5
def discover(names: list[str]) -> list[Path]:
# Underscore-prefixed directories are infrastructure, not skills:
# `_contract/` is the shared library and `_meta/` is the repo-wide guard,
# run separately by `run_meta()` because it spans every skill at once.
available = sorted(
path
for path in TESTS_DIR.iterdir()
if path.is_dir() and not path.name.startswith((".", "_"))
)
if not names:
return available
by_name = {path.name: path for path in available}
unknown = [name for name in names if name not in by_name]
if unknown:
sys.exit(
f"no test directory for: {', '.join(unknown)}\n"
f"available: {', '.join(sorted(by_name))}"
)
return [by_name[name] for name in names]
def load_requirements() -> tuple[dict[str, dict], str, dict[str, str]]:
"""Return (per-skill entries, default interpreter, uninstallable packages)."""
if not REQUIREMENTS.exists():
sys.exit(f"missing {REQUIREMENTS.relative_to(REPO_ROOT)}, required by --isolated")
manifest = tomllib.loads(REQUIREMENTS.read_text())
default_python = manifest.get("defaults", {}).get("python", "3.13")
return manifest.get("skills", {}), default_python, manifest.get("unavailable", {})
def isolated_command(
entry: dict, default_python: str, target: str, pytest_args: list[str]
) -> list[str]:
"""Build the `uv run` invocation that tests one skill in its own environment."""
command = [
"uv", "run",
"--no-project", # ignore this repo's environment entirely
"--python", entry.get("python", default_python),
"--with", "pytest",
]
for package in entry.get("packages", []):
command += ["--with", package]
return [*command, "python", "-m", "pytest", target, *pytest_args]
def run_meta(pytest_args: list[str]) -> int:
"""Run the repo-wide guard in this interpreter, before any skill suite.
`tests/_meta` spans every skill at once -- it is what enforces that a skill
shipping `scripts/` has a suite here at all -- so it cannot be one of the
per-skill processes. It imports no skill code and needs no scientific
packages, so it always runs in the project environment, never an isolated
one.
"""
print("\n=== _meta " + "=" * 55)
completed = subprocess.run(
[sys.executable, "-m", "pytest", "tests/_meta", *pytest_args], cwd=REPO_ROOT
)
return completed.returncode
def main(argv: list[str]) -> int:
isolated = "--isolated" in argv
argv = [argument for argument in argv if argument != "--isolated"]
if "--" in argv:
separator = argv.index("--")
names, pytest_args = argv[:separator], argv[separator + 1 :]
else:
names, pytest_args = argv, []
entries: dict[str, dict] = {}
default_python = ""
unavailable: dict[str, str] = {}
if isolated:
if not shutil.which("uv"):
sys.exit("--isolated needs uv on PATH: https://docs.astral.sh/uv/")
entries, default_python, unavailable = load_requirements()
suites = discover(names)
results: dict[str, int] = {}
# Only on a full run: when the caller named skills they want those skills,
# not a repo-wide audit.
if not names:
results["_meta"] = run_meta(pytest_args)
for suite in suites:
print(f"\n=== {suite.name} " + "=" * max(0, 60 - len(suite.name)))
target = str(suite.relative_to(REPO_ROOT))
if isolated:
entry = entries.get(suite.name)
if entry is None:
print(
f"no [skills.{suite.name}] in {REQUIREMENTS.name}; "
"add one (use packages = [] for standard-library-only skills)"
)
results[suite.name] = 1
continue
packages = entry.get("packages", [])
print(
f"uv env: python {entry.get('python', default_python)}"
f", {len(packages)} package(s)"
+ (f": {' '.join(packages)}" if packages else "")
)
for package, reason in sorted(unavailable.items()):
if reason.startswith(f"{suite.name} skill:"):
print(f" not installable here -- {package}: {reason.split(': ', 1)[1]}")
command = isolated_command(entry, default_python, target, pytest_args)
else:
command = [sys.executable, "-m", "pytest", target, *pytest_args]
completed = subprocess.run(command, cwd=REPO_ROOT)
results[suite.name] = completed.returncode
failed = {name: code for name, code in results.items() if code not in (0, NO_TESTS_COLLECTED)}
empty = [name for name, code in results.items() if code == NO_TESTS_COLLECTED]
print("\n" + "=" * 68)
print(f"{len(results) - len(failed) - len(empty)} passed, {len(failed)} failed", end="")
print(f", {len(empty)} empty" if empty else "")
for name, code in sorted(failed.items()):
print(f" FAILED {name} (pytest exit {code})")
for name in sorted(empty):
print(f" empty {name}")
return 1 if failed else 0
if __name__ == "__main__":
raise SystemExit(main(sys.argv[1:]))