File size: 1,843 Bytes
d416acc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# to run this file for checking please run this command :  py -m pytest tests/test_env.py -v 
# py -m pytest tests/test_env.py -v  in the terminal 
import pytest

from environment.api_triage_env import APITriageEnv
from environment.incident_generator import get_incident_by_type

def test_reset():
  """ reset should return the initial state of  the environment  and with step 0 """
  env = APITriageEnv(max_steps=10)
  state = env.reset()
  # btw here we used assert to check if the state is a string or not
  assert state.step == 0
  assert state.max_steps == 10
  assert state.incident_summary is not None 
  assert state.logs is not None 
  assert state.response_code in [400, 404, 401, 429, 500, 408]

def test_action_is_valid():
  """ valid action should return the float reward"""
  env = APITriageEnv()
  env.reset()
  test_action_is_valid = ["add_field", "wait_retry", "refresh_token", "change_endpoint", "escalate"]
  for action in test_action_is_valid:
    state , reward , done , info = env.step(action)
    # btw here we are checking if the reward is a float and if done is also a boolean ,if its not tthen test will catch it 
    assert isinstance(reward, float)
    assert isinstance(done, bool)

def test_action_is_invalid():
  """ invalid action should return a negative reward and done = true"""
  env = APITriageEnv()
  env.reset()
  state, reward, done, info = env.step("fake_action_123")
  assert reward == -2.0
  assert done is False

def test_episode_successful():
  env = APITriageEnv()
  env.incident = get_incident_by_type("auth_error")
  env.fix_applied = False
  env.done = False
  env.total_reward = 0.0
  state, reward, done, info = env.step("inspect_logs")
  state, reward, done, info = env.step("refresh_token")
  assert done is False

  state, reward, done, info = env.step("resolve")
  assert done is True