File size: 2,039 Bytes
33dd3ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Tests for typed action models and discriminated union."""

import pytest
from pydantic import ValidationError

from models import (
    SentinelAction,
    QueryLogsAction,
    SubmitResolutionAction,
    SentinelObservation,
)


class TestDeserialization:
    def test_query_logs(self):
        raw = {"tool_name": "query_logs", "parameters": {"service": "auth", "query": "error"}}
        action = SentinelAction.model_validate(raw)
        assert action.tool_name == "query_logs"
        assert action.param_dict() == {"service": "auth", "query": "error", "severity": "all"}

    def test_submit_resolution(self):
        raw = {
            "tool_name": "submit_resolution",
            "parameters": {"root_cause": "x", "affected_service": "y", "recommendation": "z"},
        }
        action = SentinelAction.model_validate(raw)
        assert action.param_dict()["root_cause"] == "x"

    def test_get_dependency_map_defaults(self):
        raw = {"tool_name": "get_dependency_map", "parameters": {}}
        action = SentinelAction.model_validate(raw)
        assert action.param_dict() == {"service": ""}


class TestInvalidRejection:
    def test_unknown_tool_name(self):
        raw = {"tool_name": "hack_server", "parameters": {}}
        with pytest.raises(ValidationError):
            SentinelAction.model_validate(raw)

    def test_missing_required_param(self):
        raw = {"tool_name": "query_logs", "parameters": {}}
        with pytest.raises(ValidationError):
            SentinelAction.model_validate(raw)


class TestDiscriminator:
    def test_schema_has_discriminator(self):
        schema = SentinelAction.model_json_schema()
        assert "$defs" in schema


class TestObservation:
    def test_tool_descriptions_default_empty(self):
        obs = SentinelObservation()
        assert obs.tool_descriptions == {}

    def test_tool_descriptions_populated(self):
        obs = SentinelObservation(tool_descriptions={"query_logs": {"services": ["a"]}})
        assert "query_logs" in obs.tool_descriptions