Files changed (1) hide show
  1. test_basic.py +27 -19
test_basic.py CHANGED
@@ -1,5 +1,5 @@
1
  # test_basic.py
2
- # Run this after env.py is complete to verify everything works
3
  # Command: python test_basic.py
4
 
5
  from env import FoveaEnv
@@ -21,31 +21,34 @@ assert len(obs.patch[0]) == 3, "Patch should be 3 cols"
21
  assert obs.last_event == "start", "First event should be 'start'"
22
  print("✅ Test 1 PASSED — reset() works correctly")
23
 
24
- # ── Test 2: step() move ──────────────────────────
 
 
 
 
 
 
 
 
 
 
 
25
  action = BlinkAction(move="right", look="stay", inspect=False)
26
  obs2, reward, done = env.step(action)
27
  assert obs2.step_count == 1, "step_count should be 1"
28
  assert isinstance(reward, float), "reward must be float"
29
  assert isinstance(done, bool), "done must be bool"
30
  assert obs2.agent_pos == [2, 1], f"Agent should be at [2,1], got {obs2.agent_pos}"
31
- print("✅ Test 2 PASSED — step() move works")
32
-
33
- # ── Test 3: state() ──────────────────────────────
34
- state = env.state()
35
- assert len(state.full_grid) == 7, "Grid should have 7 rows"
36
- assert len(state.full_grid[0]) == 7, "Grid should have 7 cols"
37
- assert isinstance(state.episode_reward, float), "episode_reward must be float"
38
- assert state.done == False, "Episode should not be done yet"
39
- print("✅ Test 3 PASSED — state() returns full grid")
40
 
41
  # ── Test 4: Hazard penalty ───────────────────────
42
  env.reset("easy")
43
- # Easy map: H is at [1,2] — move agent near it
44
- env.agent_pos = [1, 1] # place agent next to hazard
45
  action_into_hazard = BlinkAction(move="right", look="stay", inspect=False)
46
  obs3, reward3, done3 = env.step(action_into_hazard)
47
  assert reward3 <= -0.5, f"Hazard hit should give <= -0.5, got {reward3}"
48
- assert obs3.last_event == "hazard_hit", "Event should be hazard_hit"
49
  print("✅ Test 4 PASSED — hazard penalty works")
50
 
51
  # ── Test 5: Goal reached ─────────────────────────
@@ -53,9 +56,9 @@ env.reset("easy")
53
  env.agent_pos = [2, 5] # one step before goal [2,6]
54
  action_to_goal = BlinkAction(move="right", look="stay", inspect=False)
55
  obs4, reward4, done4 = env.step(action_to_goal)
56
- assert done4 == True, "Episode should be done after reaching goal"
57
  assert reward4 >= 0.9, f"Goal reward should be >= 0.9 (after step penalty), got {reward4}"
58
- assert obs4.last_event == "goal", "Event should be 'goal'"
59
  print("✅ Test 5 PASSED — goal reached correctly")
60
 
61
  # ── Test 6: Timeout ──────────────────────────────
@@ -63,11 +66,10 @@ env.reset("easy")
63
  env.max_steps = 2
64
  env.step(BlinkAction(move="stay", look="stay", inspect=False))
65
  _, _, done_timeout = env.step(BlinkAction(move="stay", look="stay", inspect=False))
66
- assert done_timeout == True, "Should be done after max_steps"
67
  print("✅ Test 6 PASSED — timeout works")
68
 
69
- # ── Test 7: Grader range ─────────────────────────
70
- # ── Test 7: Grader strict range ─────────────────────────
71
  for ep_r, goal, priv, steps in [
72
  (-5.0, False, 30, 25),
73
  (2.0, True, 0, 10),
@@ -81,10 +83,16 @@ for ep_r, goal, priv, steps in [
81
  print("✅ Test 7 PASSED — all grader scores are clamped to 0.0001..0.9999")
82
 
83
  # ── Test 8: All 3 tasks reset ────────────────────
 
84
  for task_id in ["easy", "medium", "hard"]:
85
  obs = env.reset(task_id)
 
86
  assert obs.step_count == 0
87
  assert obs.last_event == "start"
 
 
 
 
88
  print("✅ Test 8 PASSED — all 3 tasks reset correctly")
89
 
90
  print()
 
1
  # test_basic.py
2
+ # Run this after env.py and grader.py are complete to verify everything works
3
  # Command: python test_basic.py
4
 
5
  from env import FoveaEnv
 
21
  assert obs.last_event == "start", "First event should be 'start'"
22
  print("✅ Test 1 PASSED — reset() works correctly")
23
 
24
+ # ── Test 2: state() right after reset ────────────
25
+ state0 = env.state()
26
+ assert isinstance(state0.episode_id, str), "episode_id must be a string"
27
+ assert state0.episode_id.strip() != "", "episode_id must not be empty"
28
+ assert len(state0.full_grid) == 7, "Grid should have 7 rows"
29
+ assert len(state0.full_grid[0]) == 7, "Grid should have 7 cols"
30
+ assert state0.step_count == 0, "step_count should be 0 after reset"
31
+ assert state0.done is False, "Episode should not be done after reset"
32
+ assert state0.privacy_violations == 0, "privacy_violations should start at 0"
33
+ print("✅ Test 2 PASSED — state() returns full episode metadata")
34
+
35
+ # ── Test 3: step() move ──────────────────────────
36
  action = BlinkAction(move="right", look="stay", inspect=False)
37
  obs2, reward, done = env.step(action)
38
  assert obs2.step_count == 1, "step_count should be 1"
39
  assert isinstance(reward, float), "reward must be float"
40
  assert isinstance(done, bool), "done must be bool"
41
  assert obs2.agent_pos == [2, 1], f"Agent should be at [2,1], got {obs2.agent_pos}"
42
+ assert obs2.last_event == "moved", f"Expected 'moved', got {obs2.last_event}"
43
+ print("✅ Test 3 PASSED — step() move works")
 
 
 
 
 
 
 
44
 
45
  # ── Test 4: Hazard penalty ───────────────────────
46
  env.reset("easy")
47
+ env.agent_pos = [1, 1] # place agent next to hazard at [1,2]
 
48
  action_into_hazard = BlinkAction(move="right", look="stay", inspect=False)
49
  obs3, reward3, done3 = env.step(action_into_hazard)
50
  assert reward3 <= -0.5, f"Hazard hit should give <= -0.5, got {reward3}"
51
+ assert obs3.last_event == "hazard_hit", f"Expected hazard_hit, got {obs3.last_event}"
52
  print("✅ Test 4 PASSED — hazard penalty works")
53
 
54
  # ── Test 5: Goal reached ─────────────────────────
 
56
  env.agent_pos = [2, 5] # one step before goal [2,6]
57
  action_to_goal = BlinkAction(move="right", look="stay", inspect=False)
58
  obs4, reward4, done4 = env.step(action_to_goal)
59
+ assert done4 is True, "Episode should be done after reaching goal"
60
  assert reward4 >= 0.9, f"Goal reward should be >= 0.9 (after step penalty), got {reward4}"
61
+ assert obs4.last_event == "goal", f"Expected goal, got {obs4.last_event}"
62
  print("✅ Test 5 PASSED — goal reached correctly")
63
 
64
  # ── Test 6: Timeout ──────────────────────────────
 
66
  env.max_steps = 2
67
  env.step(BlinkAction(move="stay", look="stay", inspect=False))
68
  _, _, done_timeout = env.step(BlinkAction(move="stay", look="stay", inspect=False))
69
+ assert done_timeout is True, "Should be done after max_steps"
70
  print("✅ Test 6 PASSED — timeout works")
71
 
72
+ # ── Test 7: Grader strict range ──────────────────
 
73
  for ep_r, goal, priv, steps in [
74
  (-5.0, False, 30, 25),
75
  (2.0, True, 0, 10),
 
83
  print("✅ Test 7 PASSED — all grader scores are clamped to 0.0001..0.9999")
84
 
85
  # ── Test 8: All 3 tasks reset ────────────────────
86
+ episode_ids = set()
87
  for task_id in ["easy", "medium", "hard"]:
88
  obs = env.reset(task_id)
89
+ state = env.state()
90
  assert obs.step_count == 0
91
  assert obs.last_event == "start"
92
+ assert isinstance(state.episode_id, str) and state.episode_id.strip() != ""
93
+ episode_ids.add(state.episode_id)
94
+
95
+ assert len(episode_ids) == 3, "Each reset should generate a new episode_id"
96
  print("✅ Test 8 PASSED — all 3 tasks reset correctly")
97
 
98
  print()