from __future__ import annotations
import json, sys
sys.path.insert(0, sys.argv[1])
from ai import brain_server as brain

assert brain.ACADEMY_OUTPUT_CONTRACT == "academy_round_json_v6"
assert hasattr(brain, "_academy_round1_correction_prompt")

valid = {
    "round": 1,
    "principles": [
        "Define a closed profile before creating an extrusion.",
        "Use explicit dimensions and units for every controlling value.",
        "Validate the resulting solid instead of assuming success.",
    ],
    "repeatable_method_steps": [
        "Create and constrain one closed two-dimensional profile.",
        "Apply a positive extrusion distance along the intended axis.",
        "Inspect the resulting solid and record measurable acceptance evidence.",
    ],
    "acceptance_criteria": [
        "The result contains exactly one valid solid body.",
        "The measured height matches the requested extrusion distance.",
    ],
    "required_inputs": ["Closed profile dimensions", "Extrusion distance"],
    "safe_knowledge_gate": (
        "This is knowledge-only guidance and does not claim tool execution "
        "or physical validation."
    ),
    "limitations": [
        "No CadQuery command was executed by this contract test.",
        "No physical print or professional review was completed.",
    ],
    "forge_execution_allowed": False,
}

payload = json.loads(
    brain.canonicalise_academy_answer(json.dumps(valid), 1)["canonical"]
)
assert len(payload["repeatable_method_steps"]) == 3
assert payload["forge_execution_allowed"] is False

invalid = dict(valid)
invalid["repeatable_method_steps"] = ["Create a profile.", "Extrude the profile."]

try:
    brain.canonicalise_academy_answer(json.dumps(invalid), 1)
except brain.AcademyStructuredOutputError:
    pass
else:
    raise AssertionError("Invalid two-step Round 1 payload was accepted.")

prompt = brain._academy_round1_correction_prompt(
    "Learn safe extrusion fundamentals.",
    json.dumps(invalid),
    "round 1 needs three method steps",
)
assert "repeatable_method_steps" in prompt
assert "exactly three" in prompt
assert "forge_execution_allowed must be false" in prompt
assert "no markdown" in prompt.lower()

print("PASS: Live Academy contract is v6.")
print("PASS: Valid three-step Round 1 payload is accepted.")
print("PASS: Invalid two-step Round 1 payload is rejected.")
print("PASS: Corrective prompt requires exactly three steps.")
print("PASS: Forge remains explicitly prohibited.")
