Files
scientific-agent-skills/tests/liteparse/test_scripts.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

220 lines
7.9 KiB
Python

"""Tests for the liteparse batch directory parser.
Parsing is liteparse's job; the script owns file discovery, serialisation, and
error containment. All three are testable with a stub parser -- and the third
matters most: a batch run over a hundred documents must not abort because one
of them is corrupt.
`parse_one` names its output from the source *stem*, so two sources whose stems
collide overwrite each other. That is pinned below as current behaviour rather
than asserted to be safe.
"""
from __future__ import annotations
import json
import sys
import tempfile
import unittest
from pathlib import Path
from types import SimpleNamespace
import pytest
import skill_contract
SKILL_ROOT = Path(__file__).resolve().parents[2] / "skills" / "liteparse"
SCRIPTS = SKILL_ROOT / "scripts"
sys.path.insert(0, str(SCRIPTS))
pytest.importorskip("liteparse", reason="liteparse scripts import liteparse")
import batch_parse_dir # noqa: E402
CliHelpTests = skill_contract.cli.help_test_case(SKILL_ROOT)
def text_item(text: str = "hello"):
return SimpleNamespace(
text=text,
x=1.0,
y=2.0,
width=3.0,
height=4.0,
font_name="Helvetica",
font_size=12.0,
confidence=0.99,
)
def parse_result(text: str = "hello"):
page = SimpleNamespace(
page_num=1, width=612.0, height=792.0, text=text, text_items=[text_item(text)]
)
return SimpleNamespace(text=text, pages=[page])
class StubParser:
"""Stands in for LiteParse; raises for any source named `broken*`."""
def __init__(self) -> None:
self.seen: list[Path] = []
def parse(self, path: Path):
self.seen.append(path)
if path.stem.startswith("broken"):
raise RuntimeError("unreadable document")
return parse_result(f"contents of {path.name}")
class ExtensionTests(unittest.TestCase):
def test_the_default_set_is_lowercase_and_dotted(self) -> None:
self.assertTrue(batch_parse_dir.DEFAULT_EXTENSIONS)
for extension in batch_parse_dir.DEFAULT_EXTENSIONS:
with self.subTest(extension=extension):
self.assertTrue(extension.startswith("."))
self.assertEqual(extension, extension.lower())
def test_the_documented_document_and_image_families_are_covered(self) -> None:
defaults = batch_parse_dir.DEFAULT_EXTENSIONS
for extension in (".pdf", ".docx", ".xlsx", ".pptx", ".csv", ".png", ".tiff"):
with self.subTest(extension=extension):
self.assertIn(extension, defaults)
class DiscoveryTests(unittest.TestCase):
def setUp(self) -> None:
self._temporary = tempfile.TemporaryDirectory()
self.addCleanup(self._temporary.cleanup)
self.root = Path(self._temporary.name)
def touch(self, relative: str) -> Path:
path = self.root / relative
path.parent.mkdir(parents=True, exist_ok=True)
path.write_bytes(b"x")
return path
def _found(self, **kwargs) -> list[str]:
kwargs.setdefault("recursive", False)
kwargs.setdefault("extension", None)
return [path.name for path in batch_parse_dir.iter_files(self.root, **kwargs)]
def test_only_supported_extensions_are_yielded(self) -> None:
self.touch("a.pdf")
self.touch("b.docx")
self.touch("c.exe")
self.assertEqual(self._found(), ["a.pdf", "b.docx"])
def test_matching_is_case_insensitive(self) -> None:
self.touch("SCAN.PDF")
self.assertEqual(self._found(), ["SCAN.PDF"])
def test_an_explicit_extension_narrows_the_selection(self) -> None:
self.touch("a.pdf")
self.touch("b.docx")
self.assertEqual(self._found(extension=".pdf"), ["a.pdf"])
def test_an_explicit_extension_is_matched_case_insensitively(self) -> None:
self.touch("a.PDF")
self.assertEqual(self._found(extension=".PdF"), ["a.PDF"])
def test_recursion_is_opt_in(self) -> None:
self.touch("top.pdf")
self.touch("nested/deep.pdf")
self.assertEqual(self._found(), ["top.pdf"])
self.assertEqual(sorted(self._found(recursive=True)), ["deep.pdf", "top.pdf"])
def test_directories_are_skipped(self) -> None:
(self.root / "folder.pdf").mkdir()
self.assertEqual(self._found(), [])
def test_the_order_is_deterministic(self) -> None:
for name in ("c.pdf", "a.pdf", "b.pdf"):
self.touch(name)
self.assertEqual(self._found(), ["a.pdf", "b.pdf", "c.pdf"])
class SerialisationTests(unittest.TestCase):
def test_a_result_becomes_a_json_serialisable_dictionary(self) -> None:
payload = batch_parse_dir._result_to_dict(parse_result("body text"))
json.dumps(payload)
self.assertEqual(payload["text"], "body text")
self.assertEqual(len(payload["pages"]), 1)
def test_page_geometry_and_text_items_survive(self) -> None:
page = batch_parse_dir._result_to_dict(parse_result())["pages"][0]
self.assertEqual(page["page_num"], 1)
self.assertEqual(page["width"], 612.0)
self.assertEqual(len(page["text_items"]), 1)
def test_a_text_item_keeps_its_position_font_and_confidence(self) -> None:
item = batch_parse_dir._text_item_dict(text_item("word"))
self.assertEqual(
set(item),
{"text", "x", "y", "width", "height", "font_name", "font_size", "confidence"},
)
self.assertEqual(item["text"], "word")
self.assertEqual(item["font_name"], "Helvetica")
self.assertEqual(item["confidence"], 0.99)
class ParseOneTests(unittest.TestCase):
def setUp(self) -> None:
self._temporary = tempfile.TemporaryDirectory()
self.addCleanup(self._temporary.cleanup)
self.root = Path(self._temporary.name)
self.output = self.root / "out"
self.output.mkdir()
self.parser = StubParser()
def test_text_output_writes_the_plain_text(self) -> None:
ok, source, message = batch_parse_dir.parse_one(
self.parser, Path("paper.pdf"), self.output, "text"
)
self.assertTrue(ok)
self.assertIn("paper.txt", message)
self.assertEqual(
(self.output / "paper.txt").read_text(encoding="utf-8"),
"contents of paper.pdf",
)
def test_json_output_writes_the_structured_result(self) -> None:
ok, _, message = batch_parse_dir.parse_one(
self.parser, Path("paper.pdf"), self.output, "json"
)
self.assertTrue(ok)
self.assertIn("paper.json", message)
payload = json.loads((self.output / "paper.json").read_text(encoding="utf-8"))
self.assertEqual(payload["text"], "contents of paper.pdf")
self.assertIn("pages", payload)
def test_a_failing_document_is_reported_not_raised(self) -> None:
# One corrupt file must not abort a hundred-document batch.
ok, source, message = batch_parse_dir.parse_one(
self.parser, Path("broken.pdf"), self.output, "text"
)
self.assertFalse(ok)
self.assertEqual(source, "broken.pdf")
self.assertIn("unreadable document", message)
self.assertEqual(list(self.output.iterdir()), [])
def test_the_output_name_comes_from_the_source_stem(self) -> None:
# Consequence: report.pdf and report.docx both write report.txt.
batch_parse_dir.parse_one(self.parser, Path("report.pdf"), self.output, "text")
batch_parse_dir.parse_one(self.parser, Path("report.docx"), self.output, "text")
self.assertEqual(
[path.name for path in self.output.iterdir()], ["report.txt"]
)
self.assertEqual(
(self.output / "report.txt").read_text(encoding="utf-8"),
"contents of report.docx",
)
def test_an_unknown_format_falls_back_to_text(self) -> None:
batch_parse_dir.parse_one(self.parser, Path("a.pdf"), self.output, "yaml")
self.assertTrue((self.output / "a.txt").is_file())
if __name__ == "__main__":
unittest.main()