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.
288 lines
11 KiB
Python
288 lines
11 KiB
Python
"""Tests for the pdf skill's form tooling.
|
|
|
|
The coordinate transforms are the part worth pinning. PDF puts the origin at
|
|
the bottom-left and images put it at the top-left, so filling a form from
|
|
image-space boxes means flipping the y axis and scaling both axes. Getting the
|
|
flip backwards places every field on the wrong half of the page, and nothing
|
|
raises -- the output just looks wrong. Both transforms are pure arithmetic, so
|
|
they are tested directly with hand-computed expectations.
|
|
|
|
The remaining tests build real PDFs with reportlab where a fixture is needed,
|
|
and skip when the reader libraries are absent.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import io
|
|
import json
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
import skill_contract
|
|
|
|
SKILL_ROOT = Path(__file__).resolve().parents[2] / "skills" / "pdf"
|
|
SCRIPTS = SKILL_ROOT / "scripts"
|
|
sys.path.insert(0, str(SCRIPTS))
|
|
|
|
# These scripts take positional paths via `sys.argv` rather than argparse, so
|
|
# there is no `--help` contract to apply here.
|
|
|
|
|
|
def coords():
|
|
pytest.importorskip("pypdf", reason="pdf scripts need pypdf")
|
|
import fill_pdf_form_with_annotations
|
|
|
|
return fill_pdf_form_with_annotations
|
|
|
|
|
|
class ImageToPdfTransformTests(unittest.TestCase):
|
|
"""Image space: origin top-left, y grows down. PDF: origin bottom-left."""
|
|
|
|
def test_a_box_at_the_image_origin_lands_at_the_pdf_top(self) -> None:
|
|
# Image (0,0)-(100,50) on a 1000x500 image over a 200x100 page:
|
|
# x scales by 0.2, y flips -> top of the page.
|
|
transformed = coords().transform_from_image_coords(
|
|
(0, 0, 100, 50), 1000, 500, 200, 100
|
|
)
|
|
left, bottom, right, top = transformed
|
|
self.assertAlmostEqual(left, 0.0)
|
|
self.assertAlmostEqual(right, 20.0)
|
|
self.assertAlmostEqual(top, 100.0)
|
|
self.assertAlmostEqual(bottom, 90.0)
|
|
|
|
def test_a_box_at_the_image_bottom_lands_at_the_pdf_origin(self) -> None:
|
|
transformed = coords().transform_from_image_coords(
|
|
(0, 450, 100, 500), 1000, 500, 200, 100
|
|
)
|
|
_, bottom, _, top = transformed
|
|
self.assertAlmostEqual(bottom, 0.0)
|
|
self.assertAlmostEqual(top, 10.0)
|
|
|
|
def test_identity_scaling_only_flips_the_y_axis(self) -> None:
|
|
transformed = coords().transform_from_image_coords(
|
|
(10, 20, 30, 40), 100, 100, 100, 100
|
|
)
|
|
left, bottom, right, top = transformed
|
|
self.assertAlmostEqual(left, 10.0)
|
|
self.assertAlmostEqual(right, 30.0)
|
|
self.assertAlmostEqual(bottom, 60.0) # 100 - 40
|
|
self.assertAlmostEqual(top, 80.0) # 100 - 20
|
|
|
|
def test_the_box_keeps_its_size_under_a_pure_flip(self) -> None:
|
|
left, bottom, right, top = coords().transform_from_image_coords(
|
|
(10, 20, 30, 50), 100, 100, 100, 100
|
|
)
|
|
self.assertAlmostEqual(right - left, 20.0)
|
|
self.assertAlmostEqual(top - bottom, 30.0)
|
|
|
|
def test_the_transform_is_invertible_within_rounding(self) -> None:
|
|
original = (12.0, 34.0, 56.0, 78.0)
|
|
once = coords().transform_from_image_coords(original, 200, 200, 200, 200)
|
|
twice = coords().transform_from_image_coords(once, 200, 200, 200, 200)
|
|
for expected, actual in zip(original, twice):
|
|
self.assertAlmostEqual(expected, actual, places=6)
|
|
|
|
|
|
class PdfCoordinateTests(unittest.TestCase):
|
|
def test_the_y_axis_is_flipped_about_the_page_height(self) -> None:
|
|
left, bottom, right, top = coords().transform_from_pdf_coords(
|
|
(10, 20, 30, 40), 100
|
|
)
|
|
self.assertAlmostEqual(left, 10.0)
|
|
self.assertAlmostEqual(right, 30.0)
|
|
self.assertAlmostEqual(bottom, 60.0)
|
|
self.assertAlmostEqual(top, 80.0)
|
|
|
|
def test_flipping_twice_returns_the_original(self) -> None:
|
|
original = (5.0, 15.0, 25.0, 35.0)
|
|
once = coords().transform_from_pdf_coords(original, 100)
|
|
twice = coords().transform_from_pdf_coords(once, 100)
|
|
for expected, actual in zip(original, twice):
|
|
self.assertAlmostEqual(expected, actual, places=9)
|
|
|
|
def test_a_box_spanning_the_full_page_is_unchanged_vertically(self) -> None:
|
|
_, bottom, _, top = coords().transform_from_pdf_coords((0, 0, 10, 100), 100)
|
|
self.assertAlmostEqual(bottom, 0.0)
|
|
self.assertAlmostEqual(top, 100.0)
|
|
|
|
|
|
def field(description: str, label: list, entry: list, page: int = 1) -> dict:
|
|
return {
|
|
"description": description,
|
|
"label_bounding_box": label,
|
|
"entry_bounding_box": entry,
|
|
"page_number": page,
|
|
}
|
|
|
|
|
|
class BoundingBoxCheckTests(unittest.TestCase):
|
|
"""Overlapping boxes put text on top of text in the filled PDF."""
|
|
|
|
def _messages(self, *fields) -> list[str]:
|
|
import check_bounding_boxes
|
|
|
|
return check_bounding_boxes.get_bounding_box_messages(
|
|
io.StringIO(json.dumps({"form_fields": list(fields)}))
|
|
)
|
|
|
|
def _failures(self, *fields) -> list[str]:
|
|
return [m for m in self._messages(*fields) if m.startswith("FAILURE")]
|
|
|
|
def test_a_clean_layout_reports_only_the_field_count(self) -> None:
|
|
messages = self._messages(
|
|
field("Full name", [10, 10, 100, 30], [110, 10, 300, 30]),
|
|
field("Email", [10, 40, 100, 60], [110, 40, 300, 60]),
|
|
)
|
|
self.assertEqual(
|
|
messages, ["Read 2 fields", "SUCCESS: All bounding boxes are valid"]
|
|
)
|
|
|
|
def test_an_empty_form_reports_zero_fields(self) -> None:
|
|
self.assertEqual(
|
|
self._messages(), ["Read 0 fields", "SUCCESS: All bounding boxes are valid"]
|
|
)
|
|
|
|
def test_a_label_overlapping_its_own_entry_is_reported(self) -> None:
|
|
failures = self._failures(
|
|
field("Full name", [10, 10, 100, 30], [50, 10, 300, 30])
|
|
)
|
|
self.assertTrue(failures)
|
|
self.assertIn("label and entry", failures[0])
|
|
self.assertIn("Full name", failures[0])
|
|
|
|
def test_two_fields_overlapping_each_other_are_reported(self) -> None:
|
|
failures = self._failures(
|
|
field("Full name", [10, 10, 100, 30], [110, 10, 300, 30]),
|
|
field("Email", [90, 15, 200, 25], [310, 15, 400, 25]),
|
|
)
|
|
self.assertTrue(failures)
|
|
joined = " ".join(failures)
|
|
self.assertIn("Full name", joined)
|
|
self.assertIn("Email", joined)
|
|
|
|
def test_boxes_on_different_pages_never_collide(self) -> None:
|
|
self.assertEqual(
|
|
self._failures(
|
|
field("A", [10, 10, 100, 30], [110, 10, 300, 30], page=1),
|
|
field("B", [10, 10, 100, 30], [110, 10, 300, 30], page=2),
|
|
),
|
|
[],
|
|
)
|
|
|
|
def test_boxes_that_merely_touch_do_not_count_as_intersecting(self) -> None:
|
|
# Edge-sharing is how adjacent fields are normally laid out.
|
|
self.assertEqual(
|
|
self._failures(field("A", [10, 10, 100, 30], [100, 10, 200, 30])), []
|
|
)
|
|
|
|
def test_the_report_stops_after_twenty_messages(self) -> None:
|
|
# A form where everything overlaps would otherwise produce thousands.
|
|
overlapping = [
|
|
field(f"Field {index}", [0, 0, 100, 100], [0, 0, 100, 100])
|
|
for index in range(30)
|
|
]
|
|
messages = self._messages(*overlapping)
|
|
self.assertLessEqual(len(messages), 21)
|
|
self.assertIn("Aborting further checks", messages[-1])
|
|
|
|
|
|
class FieldDescriptorTests(unittest.TestCase):
|
|
"""`make_field_dict` turns raw AcroForm entries into the filler's schema."""
|
|
|
|
def _descriptor(self, raw: dict, field_id: str = "full_name") -> dict:
|
|
pytest.importorskip("pypdf", reason="pdf scripts need pypdf")
|
|
import extract_form_field_info
|
|
|
|
return extract_form_field_info.make_field_dict(raw, field_id)
|
|
|
|
def test_a_text_field_is_typed_as_text(self) -> None:
|
|
descriptor = self._descriptor({"/FT": "/Tx"})
|
|
self.assertEqual(descriptor["type"], "text")
|
|
self.assertEqual(descriptor["field_id"], "full_name")
|
|
|
|
def test_a_checkbox_learns_its_checked_and_unchecked_values(self) -> None:
|
|
descriptor = self._descriptor(
|
|
{"/FT": "/Btn", "/_States_": ["/Off", "/Yes"]}, "subscribe"
|
|
)
|
|
self.assertEqual(descriptor["type"], "checkbox")
|
|
self.assertEqual(descriptor["checked_value"], "/Yes")
|
|
self.assertEqual(descriptor["unchecked_value"], "/Off")
|
|
|
|
def test_the_state_order_does_not_decide_which_value_means_checked(self) -> None:
|
|
# "/Off" is the unchecked state wherever it appears in the list.
|
|
descriptor = self._descriptor({"/FT": "/Btn", "/_States_": ["/Yes", "/Off"]})
|
|
self.assertEqual(descriptor["checked_value"], "/Yes")
|
|
self.assertEqual(descriptor["unchecked_value"], "/Off")
|
|
|
|
def test_a_choice_field_carries_its_options(self) -> None:
|
|
descriptor = self._descriptor(
|
|
{"/FT": "/Ch", "/_States_": [("a", "Alpha"), ("b", "Beta")]}, "pick"
|
|
)
|
|
self.assertEqual(descriptor["type"], "choice")
|
|
self.assertEqual(
|
|
descriptor["choice_options"],
|
|
[{"value": "a", "text": "Alpha"}, {"value": "b", "text": "Beta"}],
|
|
)
|
|
|
|
def test_an_unrecognised_field_type_is_labelled_rather_than_dropped(self) -> None:
|
|
descriptor = self._descriptor({"/FT": "/Sig"})
|
|
self.assertIn("unknown", descriptor["type"])
|
|
self.assertIn("/Sig", descriptor["type"])
|
|
|
|
|
|
class ValueValidationTests(unittest.TestCase):
|
|
def _error(self, field_info, value):
|
|
pytest.importorskip("pypdf", reason="pdf scripts need pypdf")
|
|
import fill_fillable_fields
|
|
|
|
return fill_fillable_fields.validation_error_for_field_value(field_info, value)
|
|
|
|
CHECKBOX = {
|
|
"field_id": "subscribe",
|
|
"type": "checkbox",
|
|
"checked_value": "/Yes",
|
|
"unchecked_value": "/Off",
|
|
}
|
|
|
|
def test_a_checkbox_accepts_only_its_two_declared_states(self) -> None:
|
|
self.assertIsNone(self._error(self.CHECKBOX, "/Yes"))
|
|
self.assertIsNone(self._error(self.CHECKBOX, "/Off"))
|
|
|
|
error = self._error(self.CHECKBOX, "/Maybe")
|
|
self.assertIsNotNone(error)
|
|
self.assertIn("subscribe", error)
|
|
self.assertIn("/Yes", error)
|
|
|
|
def test_a_choice_field_accepts_only_its_option_values(self) -> None:
|
|
choice = {
|
|
"field_id": "country",
|
|
"type": "choice",
|
|
"choice_options": [{"value": "uk", "text": "United Kingdom"}],
|
|
}
|
|
self.assertIsNone(self._error(choice, "uk"))
|
|
# The display text is not a valid value -- only the option value is.
|
|
self.assertIsNotNone(self._error(choice, "United Kingdom"))
|
|
|
|
def test_a_radio_group_accepts_only_its_option_values(self) -> None:
|
|
radio = {
|
|
"field_id": "size",
|
|
"type": "radio_group",
|
|
"radio_options": [{"value": "/S"}, {"value": "/M"}],
|
|
}
|
|
self.assertIsNone(self._error(radio, "/M"))
|
|
self.assertIsNotNone(self._error(radio, "/XL"))
|
|
|
|
def test_a_text_field_accepts_any_string(self) -> None:
|
|
text_field = {"field_id": "full_name", "type": "text"}
|
|
for value in ("Ada Lovelace", "", "12345"):
|
|
with self.subTest(value=value):
|
|
self.assertIsNone(self._error(text_field, value))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|