#!/usr/bin/env python3
"""Test script for LLMScorer to verify it works correctly."""

import os
import sys

# Add the rubric module to the path
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'rubric'))

from rubric.core.scorer import LLMScorer, SCORER_REGISTRY


def test_llm_scorer_registration():
    """Test that LLMScorer is properly registered."""
    print("Testing LLMScorer registration...")
    assert "llm" in SCORER_REGISTRY
    assert SCORER_REGISTRY["llm"] == LLMScorer
    print("✓ LLMScorer is properly registered")


def test_llm_scorer_creation():
    """Test that LLMScorer can be created and serialized."""
    print("Testing LLMScorer creation and serialization...")
    
    system_prompt = "You are an expert code reviewer."
    user_prompt = "Evaluate the code quality. Respond with JSON in a ```json code block:\n```json\n{\"reason\": \"detailed explanation\", \"score\": 0.XX}\n```"
    
    scorer = LLMScorer(system_prompt=system_prompt, user_prompt=user_prompt)
    
    # Test to_dict
    scorer_dict = scorer.to_dict()
    expected_dict = {
        "type": "llm",
        "system_prompt": system_prompt,
        "user_prompt": user_prompt,
    }
    assert scorer_dict == expected_dict
    print("✓ LLMScorer to_dict works correctly")
    
    # Test from_dict
    recreated_scorer = LLMScorer.from_dict(scorer_dict)
    assert recreated_scorer.system_prompt == system_prompt
    assert recreated_scorer.user_prompt == user_prompt
    print("✓ LLMScorer from_dict works correctly")
    
    # Test JSON description
    json_desc = LLMScorer.get_json_description()
    assert "llm" in json_desc
    assert "system_prompt" in json_desc
    assert "user_prompt" in json_desc
    print("✓ LLMScorer get_json_description works correctly")


def test_llm_scorer_prompt_formatting():
    """Test that LLMScorer can format prompts with context."""
    print("Testing LLMScorer prompt formatting...")
    
    system_prompt = "You are evaluating {subject}."
    user_prompt = "Score the {aspect} with value {value}. Respond with JSON in a ```json code block:\n```json\n{{\"reason\": \"explanation\", \"score\": 0.XX}}\n```"
    
    scorer = LLMScorer(system_prompt=system_prompt, user_prompt=user_prompt)
    
    # We can't actually test the LLM call without proper credentials,
    # but we can verify the scorer was created correctly
    assert scorer.system_prompt == system_prompt
    assert scorer.user_prompt == user_prompt
    print("✓ LLMScorer prompt formatting setup works correctly")


def main():
    """Run all tests."""
    print("Running LLMScorer tests...\n")
    
    try:
        test_llm_scorer_registration()
        test_llm_scorer_creation()
        test_llm_scorer_prompt_formatting()
        
        print("\n🎉 All tests passed! LLMScorer is working correctly.")
        
    except Exception as e:
        print(f"\n❌ Test failed: {e}")
        sys.exit(1)


if __name__ == "__main__":
    main()
