File size: 2,504 Bytes
3d5c3e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
"""Integration coverage for the hand-authored plant-quality fixture."""

import hashlib
import json
from pathlib import Path

from scenarios.examples.plant_quality_investigation.build import build_package
from world_compiler.compiler.validate.lint import lint_package
from world_compiler.domain.schemas.package import ScenarioPackage
from world_compiler.runtime.rules.engine import registry_hashes


def test_example_builds_and_lints_clean():
    pkg = build_package()

    report = lint_package(pkg)

    assert report.ok, [
        f"{finding.code}: {finding.message}" for finding in report.findings
    ]


def test_example_package_round_trips_with_every_rule_hash_pinned():
    """Omitting either executable rule pin or stale serialization must fail."""
    pkg = build_package()
    transmission_rule = pkg.transmission_rules[0]
    canonical_rule = json.dumps(
        transmission_rule.model_dump(mode="json"),
        sort_keys=True,
        separators=(",", ":"),
    )
    expected_hashes = {
        "tr1": hashlib.sha256(canonical_rule.encode("utf-8")).hexdigest(),
        "defect_rate_update": registry_hashes()["defect_rate_update"],
    }

    assert pkg.manifest.rule_hashes == expected_hashes

    pkg.manifest.content_hash = pkg.content_hash()
    package_path = (
        Path(__file__).parents[2]
        / "scenarios"
        / "examples"
        / "plant_quality_investigation"
        / "package.json"
    )
    persisted = ScenarioPackage.model_validate_json(package_path.read_text("utf-8"))

    assert persisted == pkg
    assert persisted.content_hash() == persisted.manifest.content_hash


def test_example_exercises_all_disclosure_classes():
    pkg = build_package()

    classes = [fact.disclosure_class for fact in pkg.private_facts]

    assert classes.count("guarded") == 1
    assert classes.count("never") == 1


def test_example_answer_needs_three_actors():
    pkg = build_package()

    assert all(
        criterion.min_actor_sources >= 3 for criterion in pkg.assessment_criteria
    )


def test_per_actor_canaries_present_and_distinct():
    pkg = build_package()

    texts = " ".join(fact.text for fact in pkg.private_facts)
    for tag in ("KA", "KB", "KC", "KD"):
        assert f"LWC-CANARY-{tag}" in texts


def test_seeded_defect_fails_lint():
    pkg = build_package()

    pkg.transmission_rules[0].recipient_role = "ACTOR-MISSING"

    assert not lint_package(pkg).ok