#!/usr/bin/env python3 """ Test script to verify fuzzy match confirmation modal functionality. """ import sys import os sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'code')) import requests import pytest import json def test_fuzzy_match_confirmation(): """Test that fuzzy matching returns confirmation_needed items for low confidence matches.""" print("๐Ÿ” Testing fuzzy match confirmation modal backend...") # Skip if local server isn't running try: requests.get('http://localhost:8080/', timeout=0.5) except Exception: pytest.skip('Local web server is not running on http://localhost:8080; skipping HTTP-based test') # Test with a typo that should trigger confirmation test_data = { 'include_cards': 'Lighning', # Worse typo to trigger confirmation 'exclude_cards': '', 'commander': 'Alesha, Who Smiles at Death', # Valid commander with red identity 'enforcement_mode': 'warn', 'allow_illegal': 'false', 'fuzzy_matching': 'true' } try: response = requests.post('http://localhost:8080/build/validate/include_exclude', data=test_data) if response.status_code != 200: print(f"โŒ Request failed with status {response.status_code}") assert False data = response.json() # Check if confirmation_needed is populated if 'confirmation_needed' not in data: print("โŒ No confirmation_needed field in response") assert False if not data['confirmation_needed']: print("โŒ confirmation_needed is empty") print(f"Response: {json.dumps(data, indent=2)}") assert False confirmation = data['confirmation_needed'][0] expected_fields = ['input', 'suggestions', 'confidence', 'type'] for field in expected_fields: if field not in confirmation: print(f"โŒ Missing field '{field}' in confirmation") assert False print("โœ… Fuzzy match confirmation working!") print(f" Input: {confirmation['input']}") print(f" Suggestions: {confirmation['suggestions']}") print(f" Confidence: {confirmation['confidence']:.2%}") print(f" Type: {confirmation['type']}") except Exception as e: print(f"โŒ Test failed with error: {e}") assert False def test_exact_match_no_confirmation(): """Test that exact matches don't trigger confirmation.""" print("\n๐ŸŽฏ Testing exact match (no confirmation)...") # Skip if local server isn't running try: requests.get('http://localhost:8080/', timeout=0.5) except Exception: pytest.skip('Local web server is not running on http://localhost:8080; skipping HTTP-based test') test_data = { 'include_cards': 'Lightning Bolt', # Exact match 'exclude_cards': '', 'commander': 'Alesha, Who Smiles at Death', # Valid commander with red identity 'enforcement_mode': 'warn', 'allow_illegal': 'false', 'fuzzy_matching': 'true' } try: response = requests.post('http://localhost:8080/build/validate/include_exclude', data=test_data) if response.status_code != 200: print(f"โŒ Request failed with status {response.status_code}") assert False data = response.json() # Should not have confirmation_needed for exact match if data.get('confirmation_needed'): print(f"โŒ Exact match should not trigger confirmation: {data['confirmation_needed']}") assert False # Should have legal includes if not data.get('includes', {}).get('legal'): print("โŒ Exact match should be in legal includes") print(f"Response: {json.dumps(data, indent=2)}") assert False print("โœ… Exact match correctly bypasses confirmation!") except Exception as e: print(f"โŒ Test failed with error: {e}") assert False if __name__ == "__main__": print("๐Ÿงช Testing Fuzzy Match Confirmation Modal") print("=" * 50) test1_pass = test_fuzzy_match_confirmation() test2_pass = test_exact_match_no_confirmation() print("\n๐Ÿ“‹ Test Summary:") print(f" Fuzzy confirmation: {'โœ… PASS' if test1_pass else 'โŒ FAIL'}") print(f" Exact match: {'โœ… PASS' if test2_pass else 'โŒ FAIL'}") if test1_pass and test2_pass: print("\n๐ŸŽ‰ All fuzzy match tests passed!") print("๐Ÿ’ก Modal functionality ready for user testing") else: print("\n๐Ÿ”ง Some tests failed - check implementation") exit(0 if test1_pass and test2_pass else 1)