File size: 5,156 Bytes
8ddfad6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Test script to validate the Document Classification Environment
"""

import sys
from environment import DocumentClassificationEnv
from grading import BaselineAgent, AgentGrader


def test_environment_creation():
    """Test that environments can be created for all difficulties"""
    print("Testing environment creation...")
    for difficulty in ["easy", "medium", "hard"]:
        try:
            env = DocumentClassificationEnv(task_difficulty=difficulty)
            obs, info = env.reset()
            print(f"βœ“ {difficulty} environment created successfully")
            assert "features" in obs, f"Missing features in {difficulty} observation"
            assert obs["features"].shape[0] > 0, f"Invalid feature shape in {difficulty}"
        except Exception as e:
            print(f"βœ— Failed to create {difficulty} environment: {e}")
            return False
    return True


def test_step_function():
    """Test that step function works correctly"""
    print("\nTesting step function...")
    env = DocumentClassificationEnv(task_difficulty="easy")
    obs, _ = env.reset()
    
    for i in range(5):
        action = env.action_space.sample()
        obs, reward, done, truncated, info = env.step(action)
        assert isinstance(reward, float), f"Reward should be float, got {type(reward)}"
        assert isinstance(done, bool), f"Done should be bool, got {type(done)}"
        print(f"βœ“ Step {i+1}: reward={reward:.3f}, accuracy={info['episode_accuracy']:.3f}")
        if done:
            break
    
    print(f"βœ“ Step function working correctly")
    return True


def test_state_function():
    """Test that state function works correctly"""
    print("\nTesting state function...")
    env = DocumentClassificationEnv(task_difficulty="easy")
    obs, _ = env.reset()
    
    for _ in range(3):
        action = env.action_space.sample()
        obs, reward, done, truncated, info = env.step(action)
    
    state = env.state()
    assert "current_observation" in state, "Missing current_observation in state"
    assert "current_document_index" in state, "Missing current_document_index in state"
    assert "episode_reward_total" in state, "Missing episode_reward_total in state"
    print(f"βœ“ State function working correctly")
    print(f"  State keys: {list(state.keys())}")
    return True


def test_baseline_agent():
    """Test baseline agent on each difficulty"""
    print("\nTesting baseline agent...")
    for difficulty in ["easy"]:  # Test only easy for speed
        try:
            env = DocumentClassificationEnv(task_difficulty=difficulty)
            agent = BaselineAgent(difficulty)
            obs, _ = env.reset()
            
            for _ in range(5):
                action = agent.decide(obs)
                assert 0 <= action < env.num_categories, f"Invalid action: {action}"
                obs, reward, done, truncated, info = env.step(action)
                if done:
                    break
            
            print(f"βœ“ Baseline agent working on {difficulty}")
        except Exception as e:
            print(f"βœ— Baseline agent failed on {difficulty}: {e}")
            return False
    return True


def test_grading():
    """Test the grading system"""
    print("\nTesting grading system...")
    try:
        grader = AgentGrader("easy")
        agent = BaselineAgent("easy")
        
        score, metrics = grader.grade_agent(agent.decide, verbose=False)
        
        assert 0.0 <= score <= 1.0, f"Score out of range: {score}"
        assert "accuracy" in metrics, "Missing accuracy in metrics"
        assert metrics["accuracy"] >= 0.0, "Negative accuracy"
        
        print(f"βœ“ Grading system working")
        print(f"  Score: {score:.4f}")
        print(f"  Accuracy: {metrics['accuracy']:.4f}")
        return True
    except Exception as e:
        print(f"βœ— Grading system failed: {e}")
        return False


def run_all_tests():
    """Run all tests"""
    print("="*60)
    print("Document Classification Environment - Test Suite")
    print("="*60)
    
    tests = [
        ("Environment Creation", test_environment_creation),
        ("Step Function", test_step_function),
        ("State Function", test_state_function),
        ("Baseline Agent", test_baseline_agent),
        ("Grading System", test_grading),
    ]
    
    results = []
    for test_name, test_func in tests:
        try:
            result = test_func()
            results.append((test_name, result))
        except Exception as e:
            print(f"βœ— {test_name} failed with exception: {e}")
            results.append((test_name, False))
    
    # Summary
    print("\n" + "="*60)
    print("TEST SUMMARY")
    print("="*60)
    
    passed = sum(1 for _, result in results if result)
    total = len(results)
    
    for test_name, result in results:
        status = "βœ“ PASS" if result else "βœ— FAIL"
        print(f"{status} - {test_name}")
    
    print(f"\nTotal: {passed}/{total} tests passed")
    print("="*60)
    
    return passed == total


if __name__ == "__main__":
    success = run_all_tests()
    sys.exit(0 if success else 1)