|
| 1 | +// @license MIT |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | +import { describe, expect, it } from "vitest"; |
| 4 | +import { actorId, transitionId, type TransitionSpec } from "@loopengine/core"; |
| 5 | +import { buildActorEvidence } from "../evidence"; |
| 6 | +import { canActorExecuteTransition } from "../constraints"; |
| 7 | + |
| 8 | +const humanTransition: TransitionSpec = { |
| 9 | + id: transitionId("approve"), |
| 10 | + from: "OPEN" as never, |
| 11 | + to: "CLOSED" as never, |
| 12 | + allowedActors: ["human"] |
| 13 | +}; |
| 14 | + |
| 15 | +const aiTransition: TransitionSpec = { |
| 16 | + id: transitionId("recommend"), |
| 17 | + from: "OPEN" as never, |
| 18 | + to: "IN_REVIEW" as never, |
| 19 | + allowedActors: ["human", "ai-agent"] |
| 20 | +}; |
| 21 | + |
| 22 | +describe("actors constraints", () => { |
| 23 | + it("Human authorized for human transition", () => { |
| 24 | + const result = canActorExecuteTransition( |
| 25 | + { type: "human", id: actorId("user@example.com"), sessionId: "s1" }, |
| 26 | + humanTransition |
| 27 | + ); |
| 28 | + expect(result.authorized).toBe(true); |
| 29 | + }); |
| 30 | + |
| 31 | + it("AI authorized for ai-allowed transition", () => { |
| 32 | + const result = canActorExecuteTransition( |
| 33 | + { type: "ai-agent", id: actorId("agent:icp"), agentId: "icp", gatewaySessionId: "g1" }, |
| 34 | + aiTransition |
| 35 | + ); |
| 36 | + expect(result.authorized).toBe(true); |
| 37 | + }); |
| 38 | + |
| 39 | + it("AI rejected for human-only transition (requiresApproval: true)", () => { |
| 40 | + const result = canActorExecuteTransition( |
| 41 | + { type: "ai-agent", id: actorId("agent:icp"), agentId: "icp", gatewaySessionId: "g1" }, |
| 42 | + humanTransition |
| 43 | + ); |
| 44 | + expect(result.authorized).toBe(false); |
| 45 | + expect(result.requiresApproval).toBe(true); |
| 46 | + }); |
| 47 | + |
| 48 | + it("Circuit breaker fires after maxConsecutiveAITransitions", () => { |
| 49 | + const result = canActorExecuteTransition( |
| 50 | + { type: "ai-agent", id: actorId("agent:icp"), agentId: "icp", gatewaySessionId: "g1" }, |
| 51 | + aiTransition, |
| 52 | + { |
| 53 | + canRecommendTransitions: true, |
| 54 | + canExecuteTransitions: true, |
| 55 | + requiresHumanApprovalFor: [], |
| 56 | + maxConsecutiveAITransitions: 2, |
| 57 | + currentConsecutiveAITransitions: 2 |
| 58 | + } |
| 59 | + ); |
| 60 | + expect(result.authorized).toBe(false); |
| 61 | + expect(result.reason).toBe("ai_circuit_breaker"); |
| 62 | + }); |
| 63 | + |
| 64 | + it("buildActorEvidence includes actor_type for all actor types", () => { |
| 65 | + const ev = buildActorEvidence( |
| 66 | + { type: "automation", id: actorId("system:sync"), serviceId: "sync" }, |
| 67 | + { source: "test" } |
| 68 | + ); |
| 69 | + expect(ev.actor_type).toBe("automation"); |
| 70 | + }); |
| 71 | + |
| 72 | + it("buildActorEvidence includes ai_reasoning only for AIAgentActor", () => { |
| 73 | + const ai = buildActorEvidence( |
| 74 | + { type: "ai-agent", id: actorId("agent:icp"), agentId: "icp", gatewaySessionId: "g1" }, |
| 75 | + { ai_reasoning: "looks good", ai_confidence: 0.9 } |
| 76 | + ); |
| 77 | + const human = buildActorEvidence( |
| 78 | + { type: "human", id: actorId("user@example.com"), sessionId: "s1" }, |
| 79 | + { ai_reasoning: "ignored" } |
| 80 | + ); |
| 81 | + expect(ai.ai_reasoning).toBe("looks good"); |
| 82 | + expect(human.ai_agent_id).toBeUndefined(); |
| 83 | + }); |
| 84 | +}); |
0 commit comments