File size: 3,450 Bytes
8ddfad6
49234f8
8ddfad6
49234f8
 
 
8ddfad6
 
 
 
 
 
 
49234f8
8ddfad6
49234f8
8ddfad6
49234f8
8ddfad6
 
 
 
 
 
 
49234f8
8ddfad6
49234f8
8ddfad6
 
49234f8
 
 
8ddfad6
 
49234f8
 
 
8ddfad6
 
49234f8
 
 
 
8ddfad6
 
49234f8
 
 
 
8ddfad6
49234f8
 
 
8ddfad6
49234f8
 
 
 
 
8ddfad6
 
49234f8
 
 
 
8ddfad6
 
 
49234f8
 
8ddfad6
49234f8
8ddfad6
49234f8
8ddfad6
49234f8
 
8ddfad6
49234f8
 
 
 
8ddfad6
49234f8
 
 
 
 
 
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
import sys
import numpy as np
from environment import DocumentClassificationEnv
from grading import AgentGrader
from models import Observation, Action, Reward, EnvironmentState
from agent import TicketAgent

def test_environment_creation():
    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 "content" in obs, f"Missing content in {difficulty} observation"
        except Exception as e:
            print(f"  βœ— Failed to create {difficulty} environment: {e}")
            return False
    return True

def test_step_function():
    print("\nTesting step function...")
    env = DocumentClassificationEnv(task_difficulty="easy")
    obs, _ = env.reset()

    for i in range(5):
        action = i % env.num_categories
        obs, reward, done, truncated, info = env.step(action)
        assert isinstance(reward, float), f"Reward should be float, got {type(reward)}"
        assert "is_correct" in info, "Missing is_correct in info"
        print(f"  βœ“ Step {i+1}: reward={reward:.3f}, accuracy={info.get('episode_accuracy', 0):.3f}")
        if done: break
    return True

def test_ticket_agent_loop():
    print("\nTesting TicketAgent interaction loop...")
    agent = TicketAgent(model_dir=".")
    env = DocumentClassificationEnv(task_difficulty="easy")
    
    state = agent.run_episode(env)
    print(f"  βœ“ Agent finished episode. Accuracy: {state['accuracy']:.2%}, Total Reward: {state['total_reward']:.2f}")
    assert state['total_documents'] > 0
    assert 'accuracy' in state
    return True

def test_pydantic_models():
    print("\nTesting Pydantic typed models...")
    env = DocumentClassificationEnv(task_difficulty="easy")
    obs_raw, _ = env.reset()

    obs_typed = Observation.from_gym_obs(obs_raw)
    assert obs_typed.document_id != ""
    print(f"  βœ“ Observation model: doc_id={obs_typed.document_id}")

    state_raw = env.state()
    state_typed = EnvironmentState.from_state_dict(state_raw)
    assert state_typed.difficulty == "easy"
    print(f"  βœ“ EnvironmentState model: difficulty={state_typed.difficulty}, accuracy={state_typed.accuracy:.3f}")
    return True

def run_all_tests():
    print("=" * 60)
    print("Document Classification Environment - Fixed Test Suite")
    print("=" * 60)

    tests = [
        ("Environment Creation", test_environment_creation),
        ("Step Function", test_step_function),
        ("TicketAgent Loop", test_ticket_agent_loop),
        ("Pydantic Models", test_pydantic_models),
    ]

    results = []
    for name, func in tests:
        try:
            res = func()
            results.append((name, res))
        except Exception as e:
            print(f"  βœ— {name} failed: {e}")
            import traceback
            traceback.print_exc()
            results.append((name, False))

    print("\nSUMMARY")
    passed = sum(1 for _, r in results if r)
    for name, r in results:
        print(f"  [{'PASS' if r else 'FAIL'}] {name}")
    print(f"\nTotal: {passed}/{len(tests)} passed")
    return passed == len(tests)

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