#!/usr/bin/env python3 """Test script for role detection service.""" import urllib.request import json def test_role_detection(text, expected_roles, description): """Test role detection with given text.""" print(f"\nTesting: {description}") print(f"Input text: {text[:100]}..." if len(text) > 100 else f"Input text: {text}") data = json.dumps({"text": text}).encode('utf-8') req = urllib.request.Request( 'http://localhost:8001/api/tts/roles', data=data, headers={'Content-Type': 'application/json'} ) try: response = urllib.request.urlopen(req) result = json.loads(response.read().decode()) roles = [role['label'] for role in result['roles']] print(f"Detected roles: {roles}") print(f"Expected roles: {expected_roles}") if set(roles) == set(expected_roles): print("✓ PASS") return True else: print("✗ FAIL: Roles don't match") return False except Exception as e: print(f"✗ FAIL: {str(e)}") return False def main(): """Run all role detection tests.""" print("Testing Role Detection Service...") tests = [ # Test colon pattern ( "Alice: Hello there!\nBob: Hi Alice!", ["Alice", "Bob"], "Colon pattern extraction" ), # Test bracket pattern ( "[Narrator] The story begins.\n[Alice] She said hello.", ["Alice", "Narrator"], "Bracket pattern extraction" ), # Test mixed patterns ( "Alice: Hello!\n[Narrator] She waved.\nBob: Hey!", ["Alice", "Bob", "Narrator"], "Mixed colon and bracket patterns" ), # Test deduplication ( "Alice: First line.\nAlice: Second line.\nAlice: Third line.", ["Alice"], "Role deduplication" ), # Test empty text ( "", ["Narrator"], "Empty text returns default Narrator" ), # Test no role markers ( "This is just plain text without any role markers.", ["Narrator"], "No role markers returns default Narrator" ), # Test whitespace only ( " \n\n ", ["Narrator"], "Whitespace only returns default Narrator" ), ] passed = 0 failed = 0 for text, expected, description in tests: if test_role_detection(text, expected, description): passed += 1 else: failed += 1 print(f"\n{'='*50}") print(f"Results: {passed} passed, {failed} failed") print(f"{'='*50}") return failed == 0 if __name__ == '__main__': success = main() exit(0 if success else 1)