Spaces:
Sleeping
Sleeping
File size: 30,118 Bytes
57e0211 | 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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 | """Tests for the case data validator."""
import json
from pathlib import Path
import pytest
from turnabout.core.schema import CaseData
from turnabout.generation.validator import CaseValidator, ValidationReport
CASES_DIR = Path(__file__).parent.parent / "turnabout" / "cases"
# ------------------------------------------------------------------
# Fixtures
# ------------------------------------------------------------------
@pytest.fixture
def validator():
return CaseValidator()
@pytest.fixture
def stolen_prototype():
path = CASES_DIR / "stolen_prototype.json"
if not path.exists():
pytest.skip("stolen_prototype.json not found")
with open(path) as f:
data = json.load(f)
return CaseData(**data)
def _make_minimal_case(**overrides) -> dict:
"""Build a minimal valid case dict for mutation-based tests."""
base = {
"id": "test_case",
"title": "Test Case",
"difficulty": "easy",
"description": "A test case.",
"characters": [
{"id": "defendant", "name": "D", "description": "d", "role": "defendant"},
{"id": "witness1", "name": "W", "description": "w", "role": "witness"},
],
"evidence": [
{
"id": "ev1",
"name": "E1",
"item_type": "document",
"description": "desc",
"detail": "detail",
"source": "pre_given",
},
{
"id": "ev2",
"name": "E2",
"item_type": "document",
"description": "desc",
"detail": "detail",
"source": "pre_given",
},
],
"court": {
"penalty_limit": 5,
"rounds": [
{
"witness_id": "witness1",
"order": 0,
"initial_testimony_id": "t1",
"testimonies": [
{
"id": "t1",
"title": "Testimony",
"witness_id": "witness1",
"preamble": "The witness testifies.",
"statements": [
{
"id": "s1",
"text": "I saw something.",
"press_response": "Well...",
"contradiction": {
"evidence_id": "ev1",
"explanation": "This contradicts!",
"is_primary": True,
},
},
{
"id": "s2",
"text": "I was there.",
"press_response": "Yes...",
"contradiction": None,
},
],
}
],
}
],
"win_condition": {"required_contradiction_ids": ["s1"]},
},
}
base.update(overrides)
return base
def _make_minimal_case_data(**overrides) -> CaseData:
return CaseData(**_make_minimal_case(**overrides))
# ------------------------------------------------------------------
# Tests: ValidationReport
# ------------------------------------------------------------------
class TestValidationReport:
def test_default_is_valid(self):
report = ValidationReport()
assert report.is_valid is True
assert report.errors == []
assert report.warnings == []
def test_add_error_makes_invalid(self):
report = ValidationReport()
report.add_error("something broke")
assert report.is_valid is False
assert len(report.errors) == 1
def test_add_warning_stays_valid(self):
report = ValidationReport()
report.add_warning("minor issue")
assert report.is_valid is True
assert len(report.warnings) == 1
# ------------------------------------------------------------------
# Tests: Valid cases
# ------------------------------------------------------------------
class TestValidCases:
def test_stolen_prototype_is_valid(self, validator, stolen_prototype):
report = validator.validate(stolen_prototype)
assert report.is_valid, f"Errors: {report.errors}"
def test_minimal_case_is_valid(self, validator):
case = _make_minimal_case_data()
report = validator.validate(case)
assert report.is_valid, f"Errors: {report.errors}"
# ------------------------------------------------------------------
# Tests: Reference validation
# ------------------------------------------------------------------
class TestReferenceValidation:
def test_bad_location_connection(self, validator):
data = _make_minimal_case()
data["locations"] = [
{
"id": "loc1",
"name": "L1",
"description": "d",
"connected_to": ["nonexistent_loc"],
"available_from_start": True,
}
]
case = CaseData(**data)
report = validator.validate(case)
assert not report.is_valid
assert any("nonexistent_loc" in e for e in report.errors)
def test_bad_character_in_location(self, validator):
data = _make_minimal_case()
data["locations"] = [
{
"id": "loc1",
"name": "L1",
"description": "d",
"characters_present": ["ghost_character"],
"available_from_start": True,
}
]
case = CaseData(**data)
report = validator.validate(case)
assert not report.is_valid
assert any("ghost_character" in e for e in report.errors)
def test_bad_evidence_acquisition_location(self, validator):
data = _make_minimal_case()
data["locations"] = [
{
"id": "loc1",
"name": "L1",
"description": "d",
"examinables": [
{"id": "obj1", "name": "O1", "description": "d"}
],
"available_from_start": True,
}
]
data["evidence"].append(
{
"id": "ev3",
"name": "E3",
"item_type": "physical",
"description": "d",
"detail": "d",
"source": "investigation",
"acquisition": {
"method": "examine",
"location_id": "bad_loc",
"target_id": "obj1",
},
}
)
case = CaseData(**data)
report = validator.validate(case)
assert not report.is_valid
assert any("bad_loc" in e for e in report.errors)
def test_bad_evidence_acquisition_target(self, validator):
data = _make_minimal_case()
data["locations"] = [
{
"id": "loc1",
"name": "L1",
"description": "d",
"examinables": [],
"available_from_start": True,
}
]
data["evidence"].append(
{
"id": "ev3",
"name": "E3",
"item_type": "physical",
"description": "d",
"detail": "d",
"source": "investigation",
"acquisition": {
"method": "examine",
"location_id": "loc1",
"target_id": "bad_target",
},
}
)
case = CaseData(**data)
report = validator.validate(case)
assert not report.is_valid
assert any("bad_target" in e for e in report.errors)
def test_bad_win_condition_statement(self, validator):
data = _make_minimal_case()
data["court"]["win_condition"]["required_contradiction_ids"] = [
"nonexistent_statement"
]
case = CaseData(**data)
report = validator.validate(case)
assert not report.is_valid
assert any("nonexistent_statement" in e for e in report.errors)
def test_bad_dialogue_character(self, validator):
data = _make_minimal_case()
data["dialogues"] = [
{
"character_id": "nobody",
"lines": [],
"present_responses": [],
}
]
case = CaseData(**data)
report = validator.validate(case)
assert not report.is_valid
assert any("nobody" in e for e in report.errors)
def test_bad_court_round_witness(self, validator):
data = _make_minimal_case()
data["court"]["rounds"][0]["witness_id"] = "missing_witness"
case = CaseData(**data)
report = validator.validate(case)
assert not report.is_valid
assert any("missing_witness" in e for e in report.errors)
def test_bad_triggers_testimony(self, validator):
data = _make_minimal_case()
stmt = data["court"]["rounds"][0]["testimonies"][0]["statements"][0]
stmt["contradiction"]["triggers_testimony"] = "phantom_testimony"
case = CaseData(**data)
report = validator.validate(case)
assert not report.is_valid
assert any("phantom_testimony" in e for e in report.errors)
def test_bad_press_reveals_statement(self, validator):
data = _make_minimal_case()
stmt = data["court"]["rounds"][0]["testimonies"][0]["statements"][0]
stmt["press_reveals_statement"] = "ghost_statement"
case = CaseData(**data)
report = validator.validate(case)
assert not report.is_valid
assert any("ghost_statement" in e for e in report.errors)
def test_bad_final_evidence_id(self, validator):
data = _make_minimal_case()
data["court"]["win_condition"]["final_evidence_id"] = "missing_ev"
case = CaseData(**data)
report = validator.validate(case)
assert not report.is_valid
assert any("missing_ev" in e for e in report.errors)
def test_bad_investigation_gate_evidence(self, validator):
data = _make_minimal_case()
data["investigation_gate"] = {
"required_evidence": ["nonexistent_ev"],
"auto_advance": False,
}
case = CaseData(**data)
report = validator.validate(case)
assert not report.is_valid
assert any("nonexistent_ev" in e for e in report.errors)
def test_bad_location_unlock_prerequisite(self, validator):
data = _make_minimal_case()
data["locations"] = [
{
"id": "loc1",
"name": "L1",
"description": "d",
"available_from_start": True,
"connected_to": ["loc2"],
},
{
"id": "loc2",
"name": "L2",
"description": "d",
"available_from_start": False,
"unlock_prerequisites": ["fake_ev"],
"connected_to": ["loc1"],
},
]
case = CaseData(**data)
report = validator.validate(case)
assert not report.is_valid
assert any("fake_ev" in e for e in report.errors)
def test_bad_dialogue_line_grants_evidence(self, validator):
data = _make_minimal_case()
data["dialogues"] = [
{
"character_id": "witness1",
"lines": [
{
"id": "line1",
"text": "hi",
"grants_evidence": "missing_ev",
}
],
"present_responses": [],
}
]
case = CaseData(**data)
report = validator.validate(case)
assert not report.is_valid
assert any("missing_ev" in e for e in report.errors)
def test_bad_present_response_evidence(self, validator):
data = _make_minimal_case()
data["dialogues"] = [
{
"character_id": "witness1",
"lines": [],
"present_responses": [
{
"evidence_id": "ghost_ev",
"text": "hmm",
}
],
}
]
case = CaseData(**data)
report = validator.validate(case)
assert not report.is_valid
assert any("ghost_ev" in e for e in report.errors)
# ------------------------------------------------------------------
# Tests: Contradiction validation
# ------------------------------------------------------------------
class TestContradictionValidation:
def test_bad_contradiction_evidence(self, validator):
data = _make_minimal_case()
stmt = data["court"]["rounds"][0]["testimonies"][0]["statements"][0]
stmt["contradiction"]["evidence_id"] = "nonexistent_ev"
case = CaseData(**data)
report = validator.validate(case)
assert not report.is_valid
assert any("nonexistent_ev" in e for e in report.errors)
def test_valid_contradiction_evidence(self, validator):
case = _make_minimal_case_data()
errors = validator._validate_contradictions(case)
assert errors == []
# ------------------------------------------------------------------
# Tests: Solvability validation
# ------------------------------------------------------------------
class TestSolvabilityValidation:
def test_win_condition_statement_without_contradiction(self, validator):
data = _make_minimal_case()
# s2 has no contradiction but we require it in win condition
data["court"]["win_condition"]["required_contradiction_ids"] = ["s2"]
case = CaseData(**data)
report = validator.validate(case)
assert not report.is_valid
assert any("no contradiction defined" in e for e in report.errors)
def test_unreachable_testimony_for_win(self, validator):
data = _make_minimal_case()
# Add a second testimony that is never triggered
data["court"]["rounds"][0]["testimonies"].append(
{
"id": "t2",
"title": "Secret Testimony",
"witness_id": "witness1",
"preamble": "Hidden.",
"statements": [
{
"id": "s3",
"text": "Secret statement.",
"press_response": "...",
"contradiction": {
"evidence_id": "ev2",
"explanation": "Objection!",
"is_primary": True,
},
}
],
}
)
# Require s3 which is in unreachable t2
data["court"]["win_condition"]["required_contradiction_ids"] = ["s1", "s3"]
case = CaseData(**data)
report = validator.validate(case)
assert not report.is_valid
assert any("unreachable" in e.lower() for e in report.errors)
def test_reachable_testimony_via_trigger(self, validator):
"""Testimony reachable through triggers_testimony chain should pass."""
data = _make_minimal_case()
# s1 contradiction triggers t2
stmt = data["court"]["rounds"][0]["testimonies"][0]["statements"][0]
stmt["contradiction"]["triggers_testimony"] = "t2"
data["court"]["rounds"][0]["testimonies"].append(
{
"id": "t2",
"title": "Second Testimony",
"witness_id": "witness1",
"preamble": "More.",
"statements": [
{
"id": "s3",
"text": "Second statement.",
"press_response": "...",
"contradiction": {
"evidence_id": "ev2",
"explanation": "Objection!",
"is_primary": True,
},
}
],
}
)
data["court"]["win_condition"]["required_contradiction_ids"] = ["s1", "s3"]
case = CaseData(**data)
report = validator.validate(case)
assert report.is_valid, f"Errors: {report.errors}"
# ------------------------------------------------------------------
# Tests: Reachability validation
# ------------------------------------------------------------------
class TestReachabilityValidation:
def test_unreachable_investigation_evidence(self, validator):
data = _make_minimal_case()
data["locations"] = [
{
"id": "loc1",
"name": "L1",
"description": "d",
"examinables": [
{"id": "obj1", "name": "O1", "description": "d"}
],
"available_from_start": True,
"connected_to": ["loc2"],
},
{
"id": "loc2",
"name": "L2",
"description": "d",
"examinables": [
{"id": "obj2", "name": "O2", "description": "d"}
],
"available_from_start": False,
# Requires ev3 to unlock, but ev3 is IN loc2 -- catch-22
"unlock_prerequisites": ["ev3"],
"connected_to": ["loc1"],
},
]
data["evidence"].append(
{
"id": "ev3",
"name": "E3",
"item_type": "physical",
"description": "d",
"detail": "d",
"source": "investigation",
"acquisition": {
"method": "examine",
"location_id": "loc2",
"target_id": "obj2",
},
}
)
case = CaseData(**data)
report = validator.validate(case)
assert not report.is_valid
assert any("unreachable" in e.lower() for e in report.errors)
def test_reachable_evidence_chain(self, validator):
"""Evidence obtainable through a prerequisite chain should pass."""
data = _make_minimal_case()
data["locations"] = [
{
"id": "loc1",
"name": "L1",
"description": "d",
"examinables": [
{"id": "obj1", "name": "O1", "description": "d"},
{"id": "obj2", "name": "O2", "description": "d"},
],
"available_from_start": True,
}
]
data["evidence"].extend(
[
{
"id": "ev3",
"name": "E3",
"item_type": "physical",
"description": "d",
"detail": "d",
"source": "investigation",
"acquisition": {
"method": "examine",
"location_id": "loc1",
"target_id": "obj1",
"prerequisites": [],
},
},
{
"id": "ev4",
"name": "E4",
"item_type": "physical",
"description": "d",
"detail": "d",
"source": "investigation",
"acquisition": {
"method": "examine",
"location_id": "loc1",
"target_id": "obj2",
"prerequisites": ["ev3"],
},
},
]
)
case = CaseData(**data)
report = validator.validate(case)
assert report.is_valid, f"Errors: {report.errors}"
def test_stolen_prototype_reachable(self, validator, stolen_prototype):
"""All evidence in stolen_prototype should be reachable."""
errors = validator._validate_reachability(stolen_prototype)
assert errors == [], f"Reachability errors: {errors}"
def test_unreachable_gate_evidence(self, validator):
"""Investigation gate requiring unobtainable evidence should fail."""
data = _make_minimal_case()
data["locations"] = [
{
"id": "loc1",
"name": "L1",
"description": "d",
"examinables": [],
"available_from_start": True,
}
]
# ev3 is investigation evidence but has no way to be obtained
# (its location does not contain the right examinable)
data["evidence"].append(
{
"id": "ev3",
"name": "E3",
"item_type": "physical",
"description": "d",
"detail": "d",
"source": "investigation",
"acquisition": {
"method": "examine",
"location_id": "loc1",
"target_id": "missing_obj",
},
}
)
data["investigation_gate"] = {
"required_evidence": ["ev3"],
"auto_advance": False,
}
case = CaseData(**data)
report = validator.validate(case)
assert not report.is_valid
assert any("unreachable" in e.lower() for e in report.errors)
def test_evidence_from_dialogue(self, validator):
"""Evidence obtainable via dialogue should be reachable."""
data = _make_minimal_case()
data["locations"] = [
{
"id": "loc1",
"name": "L1",
"description": "d",
"characters_present": ["witness1"],
"available_from_start": True,
}
]
data["evidence"].append(
{
"id": "ev3",
"name": "E3",
"item_type": "testimony_record",
"description": "d",
"detail": "d",
"source": "investigation",
"acquisition": {
"method": "talk",
"location_id": "loc1",
"target_id": "witness1",
},
}
)
data["dialogues"] = [
{
"character_id": "witness1",
"lines": [
{
"id": "line1",
"text": "Here is some info.",
"grants_evidence": "ev3",
}
],
"present_responses": [],
}
]
case = CaseData(**data)
report = validator.validate(case)
assert report.is_valid, f"Errors: {report.errors}"
# ------------------------------------------------------------------
# Tests: Deadlock detection
# ------------------------------------------------------------------
class TestDeadlockValidation:
def test_circular_evidence_prerequisites(self, validator):
data = _make_minimal_case()
data["locations"] = [
{
"id": "loc1",
"name": "L1",
"description": "d",
"examinables": [
{"id": "obj1", "name": "O1", "description": "d"},
{"id": "obj2", "name": "O2", "description": "d"},
],
"available_from_start": True,
}
]
# ev3 requires ev4, ev4 requires ev3 -- circular
data["evidence"].extend(
[
{
"id": "ev3",
"name": "E3",
"item_type": "physical",
"description": "d",
"detail": "d",
"source": "investigation",
"acquisition": {
"method": "examine",
"location_id": "loc1",
"target_id": "obj1",
"prerequisites": ["ev4"],
},
},
{
"id": "ev4",
"name": "E4",
"item_type": "physical",
"description": "d",
"detail": "d",
"source": "investigation",
"acquisition": {
"method": "examine",
"location_id": "loc1",
"target_id": "obj2",
"prerequisites": ["ev3"],
},
},
]
)
case = CaseData(**data)
report = validator.validate(case)
assert not report.is_valid
assert any("circular" in e.lower() for e in report.errors)
def test_disconnected_location(self, validator):
data = _make_minimal_case()
data["locations"] = [
{
"id": "loc1",
"name": "L1",
"description": "d",
"connected_to": [],
"available_from_start": True,
},
{
"id": "loc2",
"name": "L2",
"description": "d",
"connected_to": [],
"available_from_start": False,
"unlock_prerequisites": [],
},
]
case = CaseData(**data)
report = validator.validate(case)
assert not report.is_valid
assert any("not reachable" in e.lower() for e in report.errors)
def test_no_deadlocks_in_stolen_prototype(self, validator, stolen_prototype):
errors = validator._validate_no_deadlocks(stolen_prototype)
assert errors == [], f"Deadlock errors: {errors}"
# ------------------------------------------------------------------
# Tests: Structural warnings
# ------------------------------------------------------------------
class TestStructuralWarnings:
def test_no_defendant_warning(self, validator):
data = _make_minimal_case()
data["characters"] = [
{"id": "npc1", "name": "N", "description": "n", "role": "npc"},
{"id": "witness1", "name": "W", "description": "w", "role": "witness"},
]
case = CaseData(**data)
report = validator.validate(case)
assert any("defendant" in w.lower() for w in report.warnings)
def test_hard_mode_no_locations_warning(self, validator):
data = _make_minimal_case(difficulty="hard")
case = CaseData(**data)
report = validator.validate(case)
assert any("locations" in w.lower() for w in report.warnings)
def test_hard_mode_no_dialogues_warning(self, validator):
data = _make_minimal_case(difficulty="hard")
data["locations"] = [
{
"id": "loc1",
"name": "L1",
"description": "d",
"available_from_start": True,
}
]
case = CaseData(**data)
report = validator.validate(case)
assert any("dialogues" in w.lower() for w in report.warnings)
def test_hard_mode_no_gate_warning(self, validator):
data = _make_minimal_case(difficulty="hard")
data["locations"] = [
{
"id": "loc1",
"name": "L1",
"description": "d",
"available_from_start": True,
}
]
data["dialogues"] = []
case = CaseData(**data)
report = validator.validate(case)
assert any("investigation gate" in w.lower() for w in report.warnings)
def test_isolated_location_warning(self, validator):
data = _make_minimal_case()
data["locations"] = [
{
"id": "loc1",
"name": "L1",
"description": "d",
"connected_to": [],
"available_from_start": True,
},
{
"id": "loc2",
"name": "L2",
"description": "d",
"connected_to": [],
"available_from_start": True,
},
]
case = CaseData(**data)
report = validator.validate(case)
assert any("no connections" in w.lower() for w in report.warnings)
# ------------------------------------------------------------------
# Tests: Full validate() integration
# ------------------------------------------------------------------
class TestFullValidation:
def test_stolen_prototype_full_validation(self, validator, stolen_prototype):
report = validator.validate(stolen_prototype)
assert report.is_valid, f"Errors: {report.errors}"
# Stolen prototype is a well-formed hard case; expect no warnings
# (or at most minor ones)
def test_multiple_errors_reported(self, validator):
"""Multiple issues should all be reported."""
data = _make_minimal_case()
data["court"]["win_condition"]["required_contradiction_ids"] = [
"bad_stmt_1",
"bad_stmt_2",
]
case = CaseData(**data)
report = validator.validate(case)
assert not report.is_valid
assert len(report.errors) >= 2
def test_clean_minimal_case(self, validator):
case = _make_minimal_case_data()
report = validator.validate(case)
assert report.is_valid
assert report.errors == []
|