From e69c6bd90aa3954b9fdadfa059b135732a292dba Mon Sep 17 00:00:00 2001 From: Alexander Whitestone Date: Thu, 9 Apr 2026 14:52:11 +0000 Subject: [PATCH] test: add tests for visual state verifier module Relates to #1482 --- tests/test_visual_state_verifier.py | 113 ++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 tests/test_visual_state_verifier.py diff --git a/tests/test_visual_state_verifier.py b/tests/test_visual_state_verifier.py new file mode 100644 index 0000000..d32b93a --- /dev/null +++ b/tests/test_visual_state_verifier.py @@ -0,0 +1,113 @@ +"""Tests for visual state verification module.""" +import json +import tempfile +from pathlib import Path +import pytest + +# Add parent to path for import +import sys +sys.path.insert(0, str(Path(__file__).parent.parent)) + +from scripts.visual_state_verifier import ( + VisualStateVerifier, + VerificationResult, + VerificationStatus, +) + + +class TestVisualStateVerifier: + """Test the visual state verifier.""" + + def test_missing_screenshot_returns_error(self): + verifier = VisualStateVerifier() + result = verifier.verify_state( + screenshot_path="/nonexistent/screenshot.png", + expected_state={"location": "Balmora"}, + game="morrowind" + ) + assert result.status == VerificationStatus.ERROR + assert not result.verified + assert "not found" in result.details.lower() + + def test_morrowind_state_builder(self): + state = VisualStateVerifier.morrowind_state( + location="Balmora", + health_min=50, + has_weapon=True, + nearby_npcs=["Caius Cosades"] + ) + assert state["location"] == "Balmora" + assert state["health_above"] == 50 + assert state["has_weapon"] is True + assert state["nearby_npcs"] == ["Caius Cosades"] + + def test_morrowind_state_minimal(self): + state = VisualStateVerifier.morrowind_state(location="Vivec") + assert state == {"location": "Vivec"} + + def test_morrowind_state_with_extras(self): + state = VisualStateVerifier.morrowind_state( + location="Balmora", + quest_complete=True, + gold_min=1000 + ) + assert state["quest_complete"] is True + assert state["gold_min"] == 1000 + + def test_prompt_includes_conditions(self): + verifier = VisualStateVerifier() + expected = {"location": "Balmora", "health_above": 50} + prompt = verifier._build_prompt(expected, "Test context", "morrowind") + assert "Balmora" in prompt + assert "50" in prompt + assert "Test context" in prompt + assert "morrowind" in prompt + + def test_parse_analysis_returns_pending_for_raw(self): + verifier = VisualStateVerifier() + raw_analysis = json.dumps({ + "prompt": "test", + "screenshot_path": "/tmp/test.png", + "instruction": "Use vision_analyze" + }) + result = verifier._parse_analysis(raw_analysis, {}, "/tmp/test.png") + assert result.status == VerificationStatus.UNCERTAIN + assert not result.verified + + def test_parse_analysis_extracts_json(self): + verifier = VisualStateVerifier() + analysis = """ + The player appears to be in Balmora. + Health looks good. + + ```json + { + "verified": true, + "confidence": 0.85, + "details": "Player is in Balmora with weapon equipped", + "mismatches": [] + } + ``` + """ + result = verifier._parse_analysis(analysis, {"location": "Balmora"}, "/tmp/test.png") + assert result.status == VerificationStatus.VERIFIED + assert result.verified + assert result.confidence == 0.85 + assert result.mismatches == [] + + def test_parse_analysis_handles_failures(self): + verifier = VisualStateVerifier() + analysis = """ + ```json + { + "verified": false, + "confidence": 0.9, + "details": "Player is not in Balmora", + "mismatches": ["location"] + } + ``` + """ + result = verifier._parse_analysis(analysis, {"location": "Balmora"}, "/tmp/test.png") + assert result.status == VerificationStatus.FAILED + assert not result.verified + assert "location" in result.mismatches