mirror of
https://github.com/K-Dense-AI/scientific-agent-skills.git
synced 2026-12-06 11:33:58 +08:00
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.
This commit is contained in:
1 parent
d33b2671cf
commit
4fb7e0bc29
155 files changed
+24403
-431
No files matched your search
@@ -0,0 +1,226 @@
|
||||
"""Tests for the docx skill's OOXML editors.
|
||||
|
||||
The shared `office/` tree -- zip safety, relationship resolution, repacking --
|
||||
is covered by the contract, since pptx and xlsx ship byte-identical copies.
|
||||
What is specific to docx is the run merger and the comment writer, and both
|
||||
edit `word/document.xml` in place, so the tests build a real unpacked package
|
||||
in a temporary directory and assert on the XML that comes back out.
|
||||
|
||||
`merge_runs` is the one with a genuine correctness risk: Word splits a single
|
||||
sentence across many `<w:r>` runs, and merging them must preserve the visible
|
||||
text exactly, including whitespace held by `xml:space="preserve"`.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
import skill_contract
|
||||
|
||||
SKILL_ROOT = Path(__file__).resolve().parents[2] / "skills" / "docx"
|
||||
SCRIPTS = SKILL_ROOT / "scripts"
|
||||
sys.path.insert(0, str(SCRIPTS))
|
||||
|
||||
pytest.importorskip("defusedxml", reason="docx scripts need defusedxml")
|
||||
|
||||
import merge_runs # noqa: E402
|
||||
|
||||
OfficeTests = skill_contract.office.office_test_case(SKILL_ROOT)
|
||||
CliHelpTests = skill_contract.cli.help_test_case(SKILL_ROOT)
|
||||
|
||||
WORDML = "http://schemas.openxmlformats.org/wordprocessingml/2006/main"
|
||||
|
||||
|
||||
def document(body: str) -> str:
|
||||
return (
|
||||
'<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\n'
|
||||
f'<w:document xmlns:w="{WORDML}"><w:body>{body}</w:body></w:document>'
|
||||
)
|
||||
|
||||
|
||||
def run(text: str, *, preserve: bool = False, properties: str = "") -> str:
|
||||
space = ' xml:space="preserve"' if preserve else ""
|
||||
return f"<w:r>{properties}<w:t{space}>{text}</w:t></w:r>"
|
||||
|
||||
|
||||
class UnpackedPackage:
|
||||
"""A minimal unpacked .docx directory."""
|
||||
|
||||
def __init__(self, root: Path, body: str) -> None:
|
||||
self.root = root
|
||||
(root / "word").mkdir(parents=True, exist_ok=True)
|
||||
(root / "word" / "document.xml").write_text(document(body), encoding="utf-8")
|
||||
(root / "[Content_Types].xml").write_text("<Types/>", encoding="utf-8")
|
||||
|
||||
@property
|
||||
def xml(self) -> str:
|
||||
return (self.root / "word" / "document.xml").read_text(encoding="utf-8")
|
||||
|
||||
def visible_text(self) -> str:
|
||||
import re
|
||||
|
||||
return "".join(re.findall(r"<w:t[^>]*>(.*?)</w:t>", self.xml, re.DOTALL))
|
||||
|
||||
|
||||
class MergeRunsTestCase(unittest.TestCase):
|
||||
def setUp(self) -> None:
|
||||
self._temporary = tempfile.TemporaryDirectory()
|
||||
self.addCleanup(self._temporary.cleanup)
|
||||
self.root = Path(self._temporary.name)
|
||||
|
||||
def package(self, body: str) -> UnpackedPackage:
|
||||
return UnpackedPackage(self.root, body)
|
||||
|
||||
|
||||
class TextPreservationTests(MergeRunsTestCase):
|
||||
def test_adjacent_identical_runs_merge_without_changing_the_text(self) -> None:
|
||||
# The trailing space needs xml:space="preserve" to be significant --
|
||||
# without it OOXML says the space is not part of the text, which is
|
||||
# what `office/helpers.rendered_text` implements.
|
||||
package = self.package(
|
||||
"<w:p>" + run("Hello ", preserve=True) + run("world") + run("!") + "</w:p>"
|
||||
)
|
||||
before = package.visible_text()
|
||||
merge_runs.merge_runs(str(self.root))
|
||||
self.assertEqual(package.visible_text(), before)
|
||||
self.assertEqual(package.visible_text(), "Hello world!")
|
||||
|
||||
def test_unpreserved_trailing_whitespace_is_insignificant(self) -> None:
|
||||
# The counterpart: without xml:space="preserve" the space is dropped,
|
||||
# matching how Word itself reads the part.
|
||||
package = self.package("<w:p>" + run("Hello ") + run("world") + "</w:p>")
|
||||
merge_runs.merge_runs(str(self.root))
|
||||
self.assertEqual(package.visible_text(), "Helloworld")
|
||||
|
||||
def test_merging_reduces_the_run_count(self) -> None:
|
||||
package = self.package(
|
||||
"<w:p>" + run("a") + run("b") + run("c") + "</w:p>"
|
||||
)
|
||||
merged, _ = merge_runs.merge_runs(str(self.root))
|
||||
self.assertGreater(merged, 0)
|
||||
self.assertEqual(package.xml.count("<w:r>"), 1)
|
||||
|
||||
def test_runs_with_different_formatting_are_not_merged(self) -> None:
|
||||
bold = "<w:rPr><w:b/></w:rPr>"
|
||||
package = self.package(
|
||||
"<w:p>" + run("plain") + run("bold", properties=bold) + "</w:p>"
|
||||
)
|
||||
merge_runs.merge_runs(str(self.root))
|
||||
self.assertEqual(package.xml.count("<w:r>"), 2)
|
||||
self.assertEqual(package.visible_text(), "plainbold")
|
||||
|
||||
def test_preserved_whitespace_survives_a_merge(self) -> None:
|
||||
# Losing xml:space="preserve" silently deletes the space between words.
|
||||
package = self.package(
|
||||
"<w:p>" + run("one ", preserve=True) + run("two", preserve=True) + "</w:p>"
|
||||
)
|
||||
merge_runs.merge_runs(str(self.root))
|
||||
self.assertEqual(package.visible_text(), "one two")
|
||||
|
||||
def test_runs_in_different_paragraphs_are_not_merged_across(self) -> None:
|
||||
package = self.package(
|
||||
"<w:p>" + run("first") + "</w:p><w:p>" + run("second") + "</w:p>"
|
||||
)
|
||||
merge_runs.merge_runs(str(self.root))
|
||||
self.assertEqual(package.xml.count("<w:p>"), 2)
|
||||
self.assertEqual(package.visible_text(), "firstsecond")
|
||||
|
||||
def test_a_document_with_nothing_to_merge_keeps_its_content(self) -> None:
|
||||
# The XML declaration is rewritten by minidom's serializer, so compare
|
||||
# the document element rather than the whole byte stream.
|
||||
package = self.package("<w:p>" + run("only one run") + "</w:p>")
|
||||
merged, _ = merge_runs.merge_runs(str(self.root))
|
||||
self.assertEqual(merged, 0)
|
||||
self.assertEqual(package.visible_text(), "only one run")
|
||||
self.assertEqual(package.xml.count("<w:r>"), 1)
|
||||
|
||||
def test_an_empty_document_does_not_raise(self) -> None:
|
||||
self.package("")
|
||||
merged, _ = merge_runs.merge_runs(str(self.root))
|
||||
self.assertEqual(merged, 0)
|
||||
|
||||
def test_the_result_is_still_well_formed_xml(self) -> None:
|
||||
import defusedxml.ElementTree as ElementTree
|
||||
|
||||
package = self.package(
|
||||
"<w:p>" + "".join(run(str(index)) for index in range(20)) + "</w:p>"
|
||||
)
|
||||
merge_runs.merge_runs(str(self.root))
|
||||
ElementTree.fromstring(package.xml)
|
||||
self.assertEqual(package.visible_text(), "".join(str(i) for i in range(20)))
|
||||
|
||||
def test_merging_is_idempotent(self) -> None:
|
||||
package = self.package("<w:p>" + run("a") + run("b") + "</w:p>")
|
||||
merge_runs.merge_runs(str(self.root))
|
||||
once = package.xml
|
||||
merged, _ = merge_runs.merge_runs(str(self.root))
|
||||
self.assertEqual(merged, 0)
|
||||
self.assertEqual(package.xml, once)
|
||||
|
||||
|
||||
class HelperTests(unittest.TestCase):
|
||||
"""`merge_runs` walks a minidom tree, so the helpers take minidom nodes."""
|
||||
|
||||
def _runs(self, body: str) -> list:
|
||||
import defusedxml.minidom
|
||||
|
||||
dom = defusedxml.minidom.parseString(document(body))
|
||||
root = dom.documentElement
|
||||
return merge_runs._find_runs(root, merge_runs._run_tag_names(root))
|
||||
|
||||
def test_adjacency_requires_the_elements_to_be_siblings_in_order(self) -> None:
|
||||
runs = self._runs("<w:p>" + run("a") + run("b") + "</w:p>")
|
||||
self.assertEqual(len(runs), 2)
|
||||
self.assertTrue(merge_runs._is_adjacent(runs[0], runs[1]))
|
||||
self.assertFalse(merge_runs._is_adjacent(runs[1], runs[0]))
|
||||
|
||||
def test_an_intervening_element_breaks_adjacency(self) -> None:
|
||||
runs = self._runs(
|
||||
"<w:p>" + run("a") + "<w:bookmarkStart/>" + run("b") + "</w:p>"
|
||||
)
|
||||
self.assertFalse(merge_runs._is_adjacent(runs[0], runs[1]))
|
||||
|
||||
def test_element_matching_is_namespace_aware(self) -> None:
|
||||
runs = self._runs("<w:p>" + run("a") + "</w:p>")
|
||||
self.assertTrue(merge_runs._is_element(runs[0], "r"))
|
||||
self.assertFalse(merge_runs._is_element(runs[0], "p"))
|
||||
|
||||
def test_run_tag_names_are_discovered_from_the_document(self) -> None:
|
||||
names = merge_runs._run_tag_names(
|
||||
__import__("defusedxml.minidom", fromlist=["parseString"])
|
||||
.parseString(document("<w:p>" + run("a") + "</w:p>"))
|
||||
.documentElement
|
||||
)
|
||||
self.assertTrue(names)
|
||||
|
||||
|
||||
class TemplateTests(unittest.TestCase):
|
||||
def test_the_comment_templates_are_shipped_and_parse(self) -> None:
|
||||
import defusedxml.ElementTree as ElementTree
|
||||
|
||||
templates = sorted((SCRIPTS / "templates").glob("*.xml"))
|
||||
self.assertTrue(templates, "no comment templates shipped")
|
||||
for template in templates:
|
||||
with self.subTest(template=template.name):
|
||||
ElementTree.fromstring(template.read_text(encoding="utf-8"))
|
||||
|
||||
def test_the_documented_comment_parts_are_all_present(self) -> None:
|
||||
# Word needs every one of these to open a commented document.
|
||||
shipped = {path.name for path in (SCRIPTS / "templates").glob("*.xml")}
|
||||
for required in (
|
||||
"comments.xml",
|
||||
"commentsExtended.xml",
|
||||
"commentsIds.xml",
|
||||
"people.xml",
|
||||
):
|
||||
with self.subTest(part=required):
|
||||
self.assertIn(required, shipped)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user