Files
mechanical-compiler/tests/test_oracle.py
T
civicus-build c7e32d8e07 Seed repository: rev-8.0.0 reference, frozen oracle, toolchain, test harness
Reference implementation of the strap-beam generators at revision 8.0.0, kept
so the acceptance oracle can be regenerated. Not a live target; the running
application has no OpenSCAD dependency.

The oracle holds 123 frozen cases, 113 accepted and 10 rejected, produced by
OpenSCAD 2021.01 with BOSL2 at 92d697c2. The ten rejections are part of the
contract: a port that accepts them is wrong.

tests/test_oracle.py specifies the port API and was written before the port,
so the interface follows from what must be verified rather than what is
convenient to implement. Proven by adversarial stub: a build() that rejects
everything passes all 10 rejection tests and fails all 226 acceptance tests.
2026-08-18 07:30:17 -05:00

158 lines
5.9 KiB
Python

"""
Acceptance of any reimplementation against the frozen rev-8.0.0 oracle.
This file is the specification of the port's public API. It was written before
the port existed, deliberately: the shape of the interface should be decided by
what has to be verified, not by what happens to be convenient to implement.
Until `mechcomp.profiles.build` exists, everything except the integrity checks
skips with a clear reason. Those integrity checks always run -- an oracle that
has been edited is worse than no oracle, and that should fail loudly on any
machine, at any time, with no dependencies.
pytest -n auto # all of it
pytest -m oracle # acceptance only
pytest -k integrity # oracle checks alone, always runnable
"""
from __future__ import annotations
import pytest
pytestmark = pytest.mark.oracle
# ---------------------------------------------------------------------------
# Integrity - no dependency on the port
# ---------------------------------------------------------------------------
def test_integrity_hash(oracle_raw):
"""The committed oracle has not been modified."""
import hashlib
import json
doc = dict(oracle_raw)
recorded = doc.pop("fixtures_sha256")
actual = hashlib.sha256(json.dumps(doc, indent=2, sort_keys=True).encode()).hexdigest()
assert actual == recorded, (
"The oracle has been edited. Its hash covers the document without the "
"hash field, serialised with indent=2 and sort_keys=True. If this was "
"intentional, regenerate it inside the reference toolchain image and "
"record why in FAILURES.md."
)
def test_integrity_shape(oracle_raw):
"""The case matrix is the one the documents describe."""
assert oracle_raw["generator_revision"] == "8.0.0"
assert oracle_raw["toolchain"]["openscad"] == "2021.01"
assert oracle_raw["toolchain"]["bosl2_commit"].startswith("92d697c2")
summary = oracle_raw["summary"]
assert summary["cases"] == 123
assert summary["accepted"] == 113
assert summary["rejected"] == 10
assert len(oracle_raw["cases"]) == summary["cases"]
def test_integrity_invariants_hold_in_the_oracle(accepted_cases):
"""
Every accepted case in the oracle satisfies the invariants.
This validates the oracle itself rather than the port. If it ever fails,
the fixture set is describing geometry that should never have been
accepted, and no port should be measured against it.
"""
for case in accepted_cases:
r = case["report"]
where = f"{case['profile']}/{case['label']}"
expected_members = 3 if "3x" in case["generator"] else 4
assert r["SECTION_PARTS"] == 1, f"{where}: not one connected solid"
assert r["STRAP_CHANNELS"] == expected_members, f"{where}: channels merged"
assert r["MIN_WALL_ACTUAL_MM"] >= r["MIN_WALL_SPEC_MM"] - 1e-4, \
f"{where}: wall below its declared minimum"
# ---------------------------------------------------------------------------
# Acceptance - requires the port
# ---------------------------------------------------------------------------
def test_accepted_case_matches_oracle(port, accepted_case, tolerance):
"""
A case the reference accepted must be accepted, with matching geometry.
Both halves matter. Reproducing the measured values while accepting a case
the reference rejected is not a passing port.
"""
expected = accepted_case["report"]
where = f"{accepted_case['profile']}/{accepted_case['label']}"
result = port.build(
family=accepted_case["family"],
profile=accepted_case["profile"],
params=accepted_case["params"],
)
got = result.report
# Counts are exact.
for key in ("SECTION_PARTS", "STRAP_CHANNELS", "BUNDLE_COUNT"):
assert got[key] == expected[key], f"{where}: {key}"
# Lengths and areas carry the tolerance the oracle declares.
for key, want in expected.items():
if not isinstance(want, float) or key in ("SECTION_PARTS", "STRAP_CHANNELS"):
continue
tol = tolerance["areas_mm2"] if key.endswith("_MM2") else tolerance["lengths_mm"]
assert key in got, f"{where}: port did not report {key}"
assert abs(got[key] - want) <= tol, (
f"{where}: {key} is {got[key]}, oracle says {want}"
)
def test_rejected_case_is_rejected(port, rejected_case):
"""
A case the reference rejected must be rejected.
These ten are the part of the contract a naive reimplementation loses: it
is easy to reproduce the geometry and quietly drop the constraint that made
it trustworthy. Accepting any of them is a failure, however good the
numbers look elsewhere.
"""
where = f"{rejected_case['profile']}/{rejected_case['label']}"
with pytest.raises(port.ProfileRejected) as excinfo:
port.build(
family=rejected_case["family"],
profile=rejected_case["profile"],
params=rejected_case["params"],
)
assert str(excinfo.value).strip(), (
f"{where}: rejected without a message. A rejection must name the "
f"parameter and the limit, as the reference does:\n"
f" {rejected_case['rejection']}"
)
def test_no_cad_dependency_on_the_2d_path(port, accepted_case):
"""
Building a cross-section must not import the 3D kernel.
ENVIRONMENT.md section 1.1: a slow OCCT import must never land in the
request path for a page that only draws a cross-section. This test also
runs in the CI job where requirements-cad.txt is absent, where an accidental
import fails outright rather than merely being slow.
"""
import sys
for module in ("cadquery", "OCP", "build123d"):
sys.modules.pop(module, None)
port.build(
family=accepted_case["family"],
profile=accepted_case["profile"],
params=accepted_case["params"],
)
leaked = [m for m in ("cadquery", "OCP", "build123d") if m in sys.modules]
assert not leaked, f"the 2D path imported {leaked}"