214 lines
6.2 KiB
Python
214 lines
6.2 KiB
Python
from __future__ import annotations
|
|
|
|
import json
|
|
import subprocess
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
from PIL import Image, ImageDraw
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0, str(ROOT / "scripts"))
|
|
|
|
import showcase_schema # noqa: E402
|
|
from test_contracts import minimal_spec # noqa: E402
|
|
|
|
|
|
def run(*args: str) -> subprocess.CompletedProcess[str]:
|
|
return subprocess.run(list(args), cwd=ROOT, text=True, capture_output=True)
|
|
|
|
|
|
def build_fixture(tmp_path: Path) -> tuple[Path, Path, Path]:
|
|
spec = tmp_path / "spec.json"
|
|
glb = tmp_path / "model.glb"
|
|
metadata = tmp_path / "model_metadata.json"
|
|
spec.write_text(json.dumps(minimal_spec()), encoding="utf-8")
|
|
proc = run(
|
|
sys.executable,
|
|
"scripts/spec_to_glb.py",
|
|
"--spec",
|
|
str(spec),
|
|
"--out",
|
|
str(glb),
|
|
"--metadata-out",
|
|
str(metadata),
|
|
)
|
|
assert proc.returncode == 0, proc.stderr
|
|
return spec, glb, metadata
|
|
|
|
|
|
def render_fixture(path: Path) -> None:
|
|
image = Image.new("RGB", (640, 480), "white")
|
|
draw = ImageDraw.Draw(image)
|
|
draw.rectangle((100, 120, 540, 400), fill="#4477aa", outline="black", width=4)
|
|
image.save(path)
|
|
|
|
|
|
def source_report_fixture(tmp_path: Path) -> Path:
|
|
path = tmp_path / "cad_read_report.json"
|
|
path.write_text(
|
|
json.dumps(
|
|
{
|
|
"report_version": 1,
|
|
"dxf_sanitized": str(tmp_path),
|
|
"gate": {"name": "all_sources_converted_and_readable", "status": "PASS"},
|
|
"files": [{"file": "fixture.dxf", "status": "ok", "modelspace_entities": 1}],
|
|
}
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
return path
|
|
|
|
|
|
def test_strict_delivery_passes_for_glb_render_and_offline_viewer(tmp_path: Path) -> None:
|
|
spec, glb, _metadata = build_fixture(tmp_path)
|
|
render = tmp_path / "render.png"
|
|
render_fixture(render)
|
|
offline = tmp_path / "viewer.html"
|
|
proc = run(
|
|
sys.executable,
|
|
"scripts/write_offline_viewer.py",
|
|
"--spec",
|
|
str(spec),
|
|
"--glb",
|
|
str(glb),
|
|
"--out",
|
|
str(offline),
|
|
)
|
|
assert proc.returncode == 0, proc.stderr
|
|
evidence = tmp_path / "evidence.json"
|
|
source_report = source_report_fixture(tmp_path)
|
|
proc = run(
|
|
sys.executable,
|
|
"scripts/verify_delivery.py",
|
|
"--spec",
|
|
str(spec),
|
|
"--source-report",
|
|
str(source_report),
|
|
"--glb",
|
|
str(glb),
|
|
"--render",
|
|
str(render),
|
|
"--offline-viewer",
|
|
str(offline),
|
|
"--evidence-out",
|
|
str(evidence),
|
|
)
|
|
assert proc.returncode == 0, proc.stderr
|
|
report = json.loads(evidence.read_text(encoding="utf-8"))
|
|
assert report["status"] == "PASS"
|
|
assert any(item["name"] == "offline:payload" and item["status"] == "PASS" for item in report["checks"])
|
|
|
|
|
|
def test_strict_delivery_requires_a_render(tmp_path: Path) -> None:
|
|
spec, glb, _metadata = build_fixture(tmp_path)
|
|
source_report = source_report_fixture(tmp_path)
|
|
proc = run(
|
|
sys.executable,
|
|
"scripts/verify_delivery.py",
|
|
"--spec",
|
|
str(spec),
|
|
"--source-report",
|
|
str(source_report),
|
|
"--glb",
|
|
str(glb),
|
|
"--evidence-out",
|
|
str(tmp_path / "evidence.json"),
|
|
)
|
|
assert proc.returncode == 3
|
|
assert "At least one inspected render is required" in proc.stderr
|
|
|
|
|
|
def test_web_writer_and_static_delivery_checks(tmp_path: Path) -> None:
|
|
spec, glb, metadata = build_fixture(tmp_path)
|
|
web = tmp_path / "web"
|
|
proc = run(
|
|
sys.executable,
|
|
"scripts/write_web_showcase.py",
|
|
"--glb",
|
|
str(glb),
|
|
"--metadata",
|
|
str(metadata),
|
|
"--out-dir",
|
|
str(web),
|
|
)
|
|
assert proc.returncode == 0, proc.stderr
|
|
for relative in ("node_modules/three/build/three.module.js", "node_modules/lucide/dist/umd/lucide.js"):
|
|
target = web / relative
|
|
target.parent.mkdir(parents=True, exist_ok=True)
|
|
target.write_text("// test dependency", encoding="utf-8")
|
|
render = tmp_path / "render.png"
|
|
render_fixture(render)
|
|
source_report = source_report_fixture(tmp_path)
|
|
proc = run(
|
|
sys.executable,
|
|
"scripts/verify_delivery.py",
|
|
"--spec",
|
|
str(spec),
|
|
"--source-report",
|
|
str(source_report),
|
|
"--glb",
|
|
str(glb),
|
|
"--render",
|
|
str(render),
|
|
"--web-dir",
|
|
str(web),
|
|
"--evidence-out",
|
|
str(tmp_path / "web_evidence.json"),
|
|
)
|
|
assert proc.returncode == 0, proc.stderr
|
|
|
|
|
|
def test_variants_manifest_requires_metadata_artifact(tmp_path: Path) -> None:
|
|
spec, glb, _metadata = build_fixture(tmp_path)
|
|
render = tmp_path / "render.png"
|
|
render_fixture(render)
|
|
source_report = source_report_fixture(tmp_path)
|
|
manifest = tmp_path / "variants_manifest.json"
|
|
manifest.write_text(
|
|
json.dumps(
|
|
{
|
|
"manifest_version": 1,
|
|
"name": "missing-metadata",
|
|
"default_key": "standard",
|
|
"variants": [
|
|
{
|
|
"key": "standard",
|
|
"label": "standard",
|
|
"file": glb.name,
|
|
"source_glb": glb.name,
|
|
"metadata": "missing_metadata.json",
|
|
"spec": spec.name,
|
|
}
|
|
],
|
|
}
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
proc = run(
|
|
sys.executable,
|
|
"scripts/verify_delivery.py",
|
|
"--source-report",
|
|
str(source_report),
|
|
"--spec",
|
|
str(spec),
|
|
"--variants-manifest",
|
|
str(manifest),
|
|
"--render",
|
|
str(render),
|
|
"--evidence-out",
|
|
str(tmp_path / "evidence.json"),
|
|
)
|
|
assert proc.returncode == 3
|
|
assert "missing_metadata.json" in proc.stderr
|
|
|
|
|
|
def test_schema_validator_rejects_legacy_spec_in_strict_mode() -> None:
|
|
legacy = minimal_spec()
|
|
legacy.pop("schema_version")
|
|
try:
|
|
showcase_schema.validate_spec(legacy)
|
|
except showcase_schema.ValidationFailure:
|
|
return
|
|
raise AssertionError("strict validator accepted a spec without schema_version")
|