forked from kindrat86/agentshield
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_emitter.py
More file actions
144 lines (121 loc) · 5.83 KB
/
Copy pathtest_emitter.py
File metadata and controls
144 lines (121 loc) · 5.83 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
"""
AgentShield → Agent-Devtools emitter tests
===========================================
Verifies the emitter produces the v1 event schema correctly and that the
per-rule trace is consistent with the engine's authoritative decision.
Run: python3.11 -m unittest tests.test_emitter -v
"""
import json
import os
import sys
import unittest
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
from agentshield.engine import SpendControlEngine
from agentshield.emitter import SpendEvaluationEmitter
def _txn(amount="500.00", merchant="openai-api", category="llm_inference", **kw):
txn = {"amount": amount, "merchant": merchant, "category": category}
txn.update(kw)
return txn
class TestEmitter(unittest.TestCase):
def setUp(self):
self.engine = SpendControlEngine()
self.emitter = SpendEvaluationEmitter(self.engine)
def test_blocked_event_matches_schema_and_decision(self):
rules = [
{"id": "r1", "type": "transaction_limit", "priority": 1,
"params": {"max_amount": 250}, "action": "BLOCK"},
{"id": "r2", "type": "velocity", "priority": 2,
"params": {"window_minutes": 60, "max_count": 10}, "action": "FLAG"},
]
txn = _txn(amount="500.00", agent_id="agent_7", session_id="sess_9",
timestamp="2026-08-13T10:29:59Z")
event = self.emitter.build_event(txn, rules, trace_id="trace_42")
# envelope
self.assertEqual(event["schema_version"], "1.0")
self.assertEqual(event["event_type"], "agentshield.spend.evaluation")
self.assertEqual(event["trace_id"], "trace_42")
self.assertEqual(event["agent_id"], "agent_7")
self.assertEqual(event["session_id"], "sess_9")
# transaction echo
self.assertEqual(event["transaction"]["amount"], "500.00")
self.assertEqual(event["transaction"]["merchant"], "openai-api")
# decision is identical to the engine's authoritative result
self.assertEqual(event["decision"], self.engine.evaluate(txn, rules, []))
# per-rule trace
evals = event["evaluation"]
self.assertEqual(evals[0]["rule_id"], "r1")
self.assertEqual(evals[0]["outcome"], "triggered")
self.assertEqual(evals[0]["detail"]["actual"], "500.00")
self.assertEqual(evals[0]["detail"]["limit"], "250.00")
self.assertEqual(evals[1]["outcome"], "not_reached")
def test_approved_event_all_rules_passed(self):
rules = [
{"id": "r1", "type": "transaction_limit", "priority": 1,
"params": {"max_amount": 1000}, "action": "BLOCK"},
{"id": "r2", "type": "merchant_allowlist", "priority": 2,
"params": {"allowed": ["openai-api"]}, "action": "BLOCK"},
]
event = self.emitter.build_event(_txn(amount="10.00"), rules)
self.assertEqual(event["decision"]["decision"], "APPROVED")
for e in event["evaluation"]:
self.assertEqual(e["outcome"], "passed")
def test_skipped_rule_missing_params(self):
rules = [
{"id": "r1", "type": "transaction_limit", "priority": 1,
"params": {}, "action": "BLOCK"},
{"id": "r2", "type": "merchant_allowlist", "priority": 2,
"params": {"allowed": ["openai-api"]}, "action": "BLOCK"},
]
event = self.emitter.build_event(_txn(amount="10.00"), rules)
evals = event["evaluation"]
self.assertEqual(evals[0]["outcome"], "skipped")
self.assertEqual(evals[1]["outcome"], "passed")
def test_ndjson_serializable_and_decodable(self):
rules = [
{"id": "r1", "type": "transaction_limit", "priority": 1,
"params": {"max_amount": 250}, "action": "BLOCK"},
]
event = self.emitter.build_event(_txn(amount="500.00"), rules)
line = self.emitter.to_ndjson(event)
decoded = json.loads(line)
self.assertEqual(decoded["decision"]["decision"], "BLOCKED")
self.assertEqual(decoded["evaluation"][0]["detail"]["limit"], "250.00")
def test_callback_emit(self):
rules = [
{"id": "r1", "type": "transaction_limit", "priority": 1,
"params": {"max_amount": 250}, "action": "BLOCK"},
]
captured = []
event = self.emitter.emit(_txn(amount="500.00"), rules, on_event=captured.append)
self.assertEqual(captured[0], event)
def test_daily_total_detail_matches_reason(self):
rules = [
{"id": "r1", "type": "daily_total", "priority": 1,
"params": {"max_daily": 2000}, "action": "BLOCK"},
]
prior = [
{"amount": "1900.00", "merchant": "openai-api", "category": "llm",
"timestamp": "2026-08-13T09:00:00Z", "agent_id": "a1"},
]
txn = _txn(amount="150.00", timestamp="2026-08-13T10:00:00Z", agent_id="a1")
event = self.emitter.build_event(txn, rules, prior)
self.assertEqual(event["decision"]["decision"], "BLOCKED")
detail = event["evaluation"][0]["detail"]
self.assertEqual(detail["daily_total"], "2050.00")
self.assertEqual(detail["max_daily"], "2000.00")
def test_invalid_transaction_empty_trace(self):
event = self.emitter.build_event({"amount": "abc"}, [])
self.assertEqual(event["decision"]["decision"], "FLAGGED")
self.assertEqual(event["evaluation"], [])
def test_parity_with_core_engine(self):
from core.engine import SpendControlEngine as CoreEngine
rules = [
{"id": "r1", "type": "transaction_limit", "priority": 1,
"params": {"max_amount": 250}, "action": "BLOCK"},
]
txn = _txn(amount="500.00")
pub = self.engine.evaluate_with_trace(txn, rules, [])
core = CoreEngine().evaluate_with_trace(txn, rules, [])
self.assertEqual(pub, core)
if __name__ == "__main__":
unittest.main()