Files
scientific-agent-skills/tests/xlsx/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

186 lines
6.8 KiB
Python

"""Tests for the xlsx skill's recalculation helper.
The shared `office/` tree is covered by the contract. What is specific here is
`recalc`, which drives LibreOffice to recompute formulas -- and the safety
check in front of it.
That check is the interesting part. Recalculating a workbook whose formulas
point at *other* workbooks, when those workbooks are unavailable, replaces
cached values with errors: the recalculation destroys the only copy of the
numbers. `external_links_at_risk` is what stops that, so the tests build real
workbooks with openpyxl and assert on exactly which cells it names.
"""
from __future__ import annotations
import sys
import tempfile
import unittest
import zipfile
from pathlib import Path
import pytest
import skill_contract
SKILL_ROOT = Path(__file__).resolve().parents[2] / "skills" / "xlsx"
SCRIPTS = SKILL_ROOT / "scripts"
sys.path.insert(0, str(SCRIPTS))
openpyxl = pytest.importorskip("openpyxl", reason="xlsx scripts need openpyxl")
import recalc # noqa: E402
OfficeTests = skill_contract.office.office_test_case(SKILL_ROOT)
class CommandLineTests(unittest.TestCase):
"""`recalc.py` reads `sys.argv` directly, so it has no argparse `--help`."""
def _run(self, *args: str):
import os
import subprocess
return subprocess.run(
[sys.executable, str(SCRIPTS / "recalc.py"), *args],
capture_output=True,
text=True,
timeout=120,
env={**os.environ, "PYTHONDONTWRITEBYTECODE": "1"},
)
def test_no_arguments_prints_usage(self) -> None:
result = self._run()
self.assertIn("Usage: python recalc.py", result.stdout)
self.assertNotIn("Traceback", result.stderr)
def test_the_usage_text_documents_the_force_flag(self) -> None:
# --force is the escape hatch past the external-link guard; it has to
# be discoverable or people will not know the guard can be overridden.
self.assertIn("--force", self._run().stdout)
class ExternalReferencePatternTests(unittest.TestCase):
"""`EXTERNAL_REF_RE` decides which formulas count as reaching outside."""
def test_bracketed_workbook_references_match(self) -> None:
for formula in (
"=[1]Sheet1!A1",
"='[1]Some Sheet'!A1",
"=SUM([2]Data!A1:A9)",
):
with self.subTest(formula=formula):
self.assertTrue(recalc.EXTERNAL_REF_RE.search(formula), formula)
def test_ordinary_local_formulas_do_not_match(self) -> None:
for formula in (
"=A1+B2",
"=SUM(Sheet2!A1:A9)",
"=IF(A1>0,1,0)",
'=CONCATENATE("[1]",A1)',
"=INDEX(Table1[Column],1)",
):
with self.subTest(formula=formula):
self.assertIsNone(recalc.EXTERNAL_REF_RE.search(formula), formula)
def test_the_reported_location_cap_is_positive(self) -> None:
self.assertGreater(recalc.MAX_LOCATIONS, 0)
class WorkbookTestCase(unittest.TestCase):
def setUp(self) -> None:
self._temporary = tempfile.TemporaryDirectory()
self.addCleanup(self._temporary.cleanup)
self.root = Path(self._temporary.name)
def workbook(self, name: str = "book.xlsx", **cells) -> Path:
book = openpyxl.Workbook()
sheet = book.active
sheet.title = "Data"
for coordinate, value in cells.items():
sheet[coordinate] = value
path = self.root / name
book.save(path)
book.close()
return path
class ExternalLinkRiskTests(WorkbookTestCase):
def test_a_workbook_with_no_external_links_is_never_at_risk(self) -> None:
path = self.workbook(A1=1, A2=2, A3="=SUM(A1:A2)")
self.assertEqual(recalc.external_links_at_risk(path), [])
def test_a_non_workbook_file_is_reported_as_no_risk_rather_than_raising(self) -> None:
path = self.root / "not-a-workbook.xlsx"
path.write_bytes(b"definitely not a zip")
self.assertEqual(recalc.external_links_at_risk(path), [])
def test_a_missing_file_is_reported_as_no_risk(self) -> None:
self.assertEqual(
recalc.external_links_at_risk(self.root / "absent.xlsx"), []
)
def test_the_check_reads_the_package_without_modifying_it(self) -> None:
path = self.workbook(A1=1, A2="=A1*2")
before = path.read_bytes()
recalc.external_links_at_risk(path)
self.assertEqual(path.read_bytes(), before)
def test_a_workbook_without_the_external_links_part_short_circuits(self) -> None:
# The function returns early unless xl/externalLinks/ exists, so the
# common case never pays for two full workbook loads.
path = self.workbook(A1=1)
with zipfile.ZipFile(path) as archive:
self.assertFalse(
any(name.startswith("xl/externalLinks/") for name in archive.namelist())
)
self.assertEqual(recalc.external_links_at_risk(path), [])
class MacroSetupTests(WorkbookTestCase):
def test_the_macro_module_is_written_into_the_profile(self) -> None:
profile = self.root / "profile"
recalc.setup_libreoffice_macro(profile)
module = profile / "user" / "basic" / "Standard" / recalc.MACRO_FILENAME
self.assertTrue(module.is_file(), "macro module was not written")
self.assertIn("Recalculate", module.read_text(encoding="utf-8"))
def test_the_macro_xml_is_well_formed(self) -> None:
import defusedxml.ElementTree as ElementTree
ElementTree.fromstring(recalc.RECALCULATE_MACRO)
def test_setup_is_idempotent(self) -> None:
profile = self.root / "profile"
recalc.setup_libreoffice_macro(profile)
first = (profile / "user" / "basic" / "Standard" / recalc.MACRO_FILENAME).read_bytes()
recalc.setup_libreoffice_macro(profile)
second = (profile / "user" / "basic" / "Standard" / recalc.MACRO_FILENAME).read_bytes()
self.assertEqual(first, second)
class RecalcGuardTests(WorkbookTestCase):
def test_a_missing_soffice_is_reported_clearly(self) -> None:
import shutil
if shutil.which("soffice"):
self.skipTest("LibreOffice is installed; this asserts the absent path")
path = self.workbook(A1=1, A2="=A1*2")
result = recalc.recalc(path)
self.assertIn("soffice", str(result).lower())
def test_the_missing_soffice_message_names_libreoffice(self) -> None:
self.assertIn("LibreOffice", recalc.SOFFICE_MISSING)
def test_the_timestamp_helper_reports_a_files_mtime(self) -> None:
path = self.workbook(A1=1)
self.assertEqual(recalc._stamp(path), recalc._stamp(path))
self.assertIsNotNone(recalc._stamp(path))
def test_the_timeout_helper_answers_without_raising(self) -> None:
self.assertIn(recalc.has_gtimeout(), (True, False))
if __name__ == "__main__":
unittest.main()