File size: 1,973 Bytes
b6d1ff0
 
 
 
 
eefb7bf
b6d1ff0
 
 
eefb7bf
 
 
 
 
 
 
 
 
 
b6d1ff0
 
af2ccc5
b6d1ff0
 
 
 
 
 
 
 
 
 
af2ccc5
 
b6d1ff0
 
 
 
af2ccc5
b6d1ff0
 
 
 
 
 
 
 
 
 
 
 
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
import json
import tempfile
import unittest
from pathlib import Path

from inference import normalize_action, write_results


class InferenceOutputTests(unittest.TestCase):
    def test_normalize_action_uppercases_model_outputs(self) -> None:
        normalized = normalize_action(
            {"action": "failover"},
            {"incident_id": "INC-014", "task_type": "task3"},
        )

        self.assertEqual(normalized["action"], "FAILOVER")
        self.assertIsNone(normalized["severity"])
        self.assertIsNone(normalized["root_cause"])

    def test_write_results_writes_summary_to_configured_path(self) -> None:
        results = [
            {"incident_id": "INC-001", "task_type": "task1", "score": 0.99, "success": True},
            {"incident_id": "INC-002", "task_type": "task2", "score": 0.5, "success": False},
        ]

        with tempfile.TemporaryDirectory() as temp_dir:
            output_path = Path(temp_dir) / "nested" / "baseline_scores.json"
            write_results(results, output_path=output_path)

            self.assertTrue(output_path.exists())
            payload = json.loads(output_path.read_text())
            self.assertEqual(payload["episodes"], 2)
            self.assertAlmostEqual(payload["average_score"], 0.745)
            self.assertEqual(payload["by_task"]["task1"]["average_score"], 0.99)
            self.assertEqual(payload["by_task"]["task2"]["average_score"], 0.5)

    def test_write_results_tolerates_unwritable_path(self) -> None:
        results = [
            {"incident_id": "INC-001", "task_type": "task1", "score": 0.99, "success": True},
        ]

        with tempfile.TemporaryDirectory() as temp_dir:
            blocked_parent = Path(temp_dir) / "blocked"
            blocked_parent.write_text("not-a-directory")
            blocked_path = blocked_parent / "baseline_scores.json"

            write_results(results, output_path=blocked_path)


if __name__ == "__main__":
    unittest.main()