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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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
"""Tests for PolicyEngine in gateway.engines.policy."""

import pytest
from datetime import datetime

from backend.application.engines.policy import (
    PolicyEngine,
    PolicyRule,
    PolicyDecision,
)
from backend.core.contracts import UsageContract, Environment, ActionType
from backend.core.auth import AppCredentials


@pytest.fixture
def mock_contract():
    """Create a mock UsageContract for testing."""
    return UsageContract(
        app_id="test-app",
        environment=Environment.PRODUCTION,
        feature="chat",
        action=ActionType.GENERATE,
    )


@pytest.fixture
def mock_credentials():
    """Create mock AppCredentials for testing."""
    return AppCredentials(
        app_id="test-app",
        api_key_id=1,
        api_key_prefix="gw_test_key",
        owner="test-team",
        environment="production",
        created_at=datetime.now(),
        allowed_models=[],  # Empty = all allowed
    )


class TestPolicyEngine:
    """Tests for PolicyEngine class."""

    def test_init_empty(self):
        """Engine should start with no rules."""
        engine = PolicyEngine()
        assert engine.rules == []
        assert engine.is_loaded is False

    def test_init_with_rules(self):
        """Engine can be initialized with rules."""
        rules = [
            PolicyRule(name="rule1"),
            PolicyRule(name="rule2"),
        ]
        engine = PolicyEngine(rules=rules)

        assert len(engine.rules) == 2
        assert engine.is_loaded is True

    def test_rules_property_setter(self):
        """Rules can be set via property."""
        engine = PolicyEngine()
        rules = [PolicyRule(name="new-rule")]

        engine.rules = rules

        assert engine.rules == rules
        assert engine.is_loaded is True


class TestPolicyEngineEvaluate:
    """Tests for PolicyEngine.evaluate method."""

    def test_evaluate_no_rules_allows(self, mock_contract, mock_credentials):
        """No rules should allow the request."""
        engine = PolicyEngine(rules=[])

        result = engine.evaluate(mock_contract, mock_credentials, "gpt-4o")

        assert result.decision == PolicyDecision.ALLOW
        assert result.reason is None
        assert result.warnings == []

    def test_evaluate_disabled_rule_ignored(self, mock_contract, mock_credentials):
        """Disabled rules should be ignored."""
        rules = [
            PolicyRule(
                name="disabled-rule",
                enabled=False,
                action=PolicyDecision.DENY,
            )
        ]
        engine = PolicyEngine(rules=rules)

        result = engine.evaluate(mock_contract, mock_credentials, "gpt-4o")

        assert result.decision == PolicyDecision.ALLOW
        assert "disabled-rule" not in result.matched_rules

    def test_evaluate_rule_environment_filter(self, mock_contract, mock_credentials):
        """Rule should only match specified environments."""
        rules = [
            PolicyRule(
                name="staging-only",
                environments=[Environment.STAGING],
                action=PolicyDecision.DENY,
            )
        ]
        engine = PolicyEngine(rules=rules)

        # Production contract should not match staging-only rule
        result = engine.evaluate(mock_contract, mock_credentials, "gpt-4o")

        assert result.decision == PolicyDecision.ALLOW
        assert "staging-only" not in result.matched_rules

    def test_evaluate_rule_environment_matches(self, mock_credentials):
        """Rule should match when environment matches."""
        rules = [
            PolicyRule(
                name="prod-limit",
                environments=[Environment.PRODUCTION],
                max_tokens=100,
                action=PolicyDecision.DENY,
            )
        ]
        engine = PolicyEngine(rules=rules)
        contract = UsageContract(
            app_id="test-app",
            environment=Environment.PRODUCTION,
            feature="chat",
            action=ActionType.GENERATE,
        )

        # Request exceeds max_tokens
        result = engine.evaluate(contract, mock_credentials, "gpt-4o", max_tokens=200)

        assert result.decision == PolicyDecision.DENY
        assert "prod-limit" in result.matched_rules

    def test_evaluate_token_limit_deny(self, mock_contract, mock_credentials):
        """Should deny when token limit exceeded with DENY action."""
        rules = [
            PolicyRule(
                name="token-limit",
                max_tokens=1000,
                action=PolicyDecision.DENY,
            )
        ]
        engine = PolicyEngine(rules=rules)

        result = engine.evaluate(
            mock_contract, mock_credentials, "gpt-4o", max_tokens=2000
        )

        assert result.decision == PolicyDecision.DENY
        assert "token-limit" in result.matched_rules
        assert "2000" in result.reason
        assert "1000" in result.reason

    def test_evaluate_token_limit_warn(self, mock_contract, mock_credentials):
        """Should warn when token limit exceeded with WARN action."""
        rules = [
            PolicyRule(
                name="soft-limit",
                max_tokens=1000,
                action=PolicyDecision.WARN,
            )
        ]
        engine = PolicyEngine(rules=rules)

        result = engine.evaluate(
            mock_contract, mock_credentials, "gpt-4o", max_tokens=2000
        )

        assert result.decision == PolicyDecision.ALLOW
        assert len(result.warnings) > 0
        assert "2000" in result.warnings[0]

    def test_evaluate_token_limit_under_limit(self, mock_contract, mock_credentials):
        """Should allow when under token limit."""
        rules = [
            PolicyRule(
                name="token-limit",
                max_tokens=1000,
                action=PolicyDecision.DENY,
            )
        ]
        engine = PolicyEngine(rules=rules)

        result = engine.evaluate(
            mock_contract, mock_credentials, "gpt-4o", max_tokens=500
        )

        assert result.decision == PolicyDecision.ALLOW

    def test_evaluate_model_whitelist_allowed(self, mock_contract, mock_credentials):
        """Should allow model in whitelist."""
        rules = [
            PolicyRule(
                name="model-whitelist",
                models=["gpt-4o", "gpt-4o-mini"],
                action=PolicyDecision.DENY,
            )
        ]
        engine = PolicyEngine(rules=rules)

        result = engine.evaluate(mock_contract, mock_credentials, "gpt-4o")

        assert result.decision == PolicyDecision.ALLOW

    def test_evaluate_model_whitelist_denied(self, mock_contract, mock_credentials):
        """Should deny model not in whitelist."""
        rules = [
            PolicyRule(
                name="model-whitelist",
                models=["gpt-4o-mini"],
                action=PolicyDecision.DENY,
            )
        ]
        engine = PolicyEngine(rules=rules)

        result = engine.evaluate(mock_contract, mock_credentials, "claude-3-opus")

        assert result.decision == PolicyDecision.DENY
        assert "not in allowed" in result.reason

    def test_evaluate_model_pattern_matching(self, mock_contract, mock_credentials):
        """Should support pattern matching for models."""
        rules = [
            PolicyRule(
                name="gpt-only",
                models=["gpt-*"],
                action=PolicyDecision.DENY,
            )
        ]
        engine = PolicyEngine(rules=rules)

        # gpt-4o should match gpt-*
        result = engine.evaluate(mock_contract, mock_credentials, "gpt-4o")
        assert result.decision == PolicyDecision.ALLOW

        # claude should not match gpt-*
        result = engine.evaluate(mock_contract, mock_credentials, "claude-3-opus")
        assert result.decision == PolicyDecision.DENY

    def test_evaluate_feature_filter(self, mock_credentials):
        """Rule should only match specified features."""
        rules = [
            PolicyRule(
                name="chat-only",
                features=["chat"],
                max_tokens=100,
                action=PolicyDecision.DENY,
            )
        ]
        engine = PolicyEngine(rules=rules)

        # Chat feature should match
        chat_contract = UsageContract(
            app_id="test-app",
            environment=Environment.PRODUCTION,
            feature="chat",
            action=ActionType.GENERATE,
        )
        result = engine.evaluate(
            chat_contract, mock_credentials, "gpt-4o", max_tokens=200
        )
        assert result.decision == PolicyDecision.DENY

        # Embedding feature should not match
        embed_contract = UsageContract(
            app_id="test-app",
            environment=Environment.PRODUCTION,
            feature="embedding",
            action=ActionType.EMBED,
        )
        result = engine.evaluate(
            embed_contract, mock_credentials, "gpt-4o", max_tokens=200
        )
        assert result.decision == PolicyDecision.ALLOW

    def test_evaluate_app_filter(self, mock_credentials):
        """Rule should only match specified apps."""
        rules = [
            PolicyRule(
                name="app-specific",
                apps=["other-app"],
                action=PolicyDecision.DENY,
            )
        ]
        engine = PolicyEngine(rules=rules)
        contract = UsageContract(
            app_id="test-app",
            environment=Environment.PRODUCTION,
            feature="chat",
            action=ActionType.GENERATE,
        )

        result = engine.evaluate(contract, mock_credentials, "gpt-4o")

        # Should not match because app_id doesn't match
        assert result.decision == PolicyDecision.ALLOW

    def test_evaluate_credentials_model_restriction(self, mock_contract):
        """Should enforce model restrictions from credentials."""
        engine = PolicyEngine(rules=[])
        credentials = AppCredentials(
            app_id="test-app",
            api_key_id=1,
            api_key_prefix="gw_test_key",
            owner="test-team",
            environment="production",
            created_at=datetime.now(),
            allowed_models=["gpt-4o-mini"],  # Only mini allowed
        )

        result = engine.evaluate(mock_contract, credentials, "gpt-4o")

        assert result.decision == PolicyDecision.DENY
        assert "not allowed" in result.reason
        assert "test-app" in result.reason

    def test_evaluate_multiple_rules_first_deny_wins(
        self, mock_contract, mock_credentials
    ):
        """First matching DENY rule should terminate evaluation."""
        rules = [
            PolicyRule(
                name="deny-all",
                action=PolicyDecision.DENY,
                max_tokens=100,
            ),
            PolicyRule(
                name="would-allow",
                action=PolicyDecision.ALLOW,
            ),
        ]
        engine = PolicyEngine(rules=rules)

        result = engine.evaluate(
            mock_contract, mock_credentials, "gpt-4o", max_tokens=200
        )

        assert result.decision == PolicyDecision.DENY
        assert "deny-all" in result.matched_rules


class TestPolicyEngineRuleMatches:
    """Tests for PolicyEngine._rule_matches method."""

    def test_rule_matches_no_conditions(self):
        """Rule with no conditions should always match."""
        engine = PolicyEngine()
        rule = PolicyRule(name="catch-all")

        from backend.core.base import ConditionContext

        context = ConditionContext(
            model="gpt-4o",
            environment="production",
            app_id="test-app",
        )

        assert engine._rule_matches(rule, context) is True

    def test_rule_matches_environment_condition(self):
        """Rule should match only specified environments."""
        engine = PolicyEngine()
        rule = PolicyRule(name="staging-rule", environments=[Environment.STAGING])

        from backend.core.base import ConditionContext

        staging_ctx = ConditionContext(environment="staging")
        assert engine._rule_matches(rule, staging_ctx) is True

        prod_ctx = ConditionContext(environment="production")
        assert engine._rule_matches(rule, prod_ctx) is False

    def test_rule_matches_app_condition(self):
        """Rule should match only specified apps."""
        engine = PolicyEngine()
        rule = PolicyRule(name="app-rule", apps=["my-app", "other-app"])

        from backend.core.base import ConditionContext

        my_app_ctx = ConditionContext(app_id="my-app")
        assert engine._rule_matches(rule, my_app_ctx) is True

        unknown_ctx = ConditionContext(app_id="unknown-app")
        assert engine._rule_matches(rule, unknown_ctx) is False

    def test_rule_matches_feature_condition(self):
        """Rule should match only specified features."""
        engine = PolicyEngine()
        rule = PolicyRule(name="feature-rule", features=["chat", "completion"])

        from backend.core.base import ConditionContext

        chat_ctx = ConditionContext(feature="chat")
        assert engine._rule_matches(rule, chat_ctx) is True

        embed_ctx = ConditionContext(feature="embedding")
        assert engine._rule_matches(rule, embed_ctx) is False


class TestPolicyEngineAsync:
    """Tests for async methods of PolicyEngine."""

    @pytest.mark.asyncio
    async def test_evaluate_async_loads_first(self, mock_contract, mock_credentials):
        """evaluate_async should ensure data is loaded first."""
        rules = [PolicyRule(name="test-rule")]
        engine = PolicyEngine(rules=rules)

        result = await engine.evaluate_async(mock_contract, mock_credentials, "gpt-4o")

        assert result.decision == PolicyDecision.ALLOW
        assert engine.is_loaded is True

    @pytest.mark.asyncio
    async def test_get_default_value(self):
        """_get_default_value should return empty list."""
        engine = PolicyEngine()
        assert engine._get_default_value() == []