|
| 1 | +package hooks |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | +) |
| 8 | + |
| 9 | +// TestAggregateTracksMostRestrictiveDecision pins the new |
| 10 | +// Result.Decision contract: when multiple pre_tool_use hooks fire on a |
| 11 | +// single tool call, the aggregated verdict is the most-restrictive |
| 12 | +// (Deny > Ask > Allow). The runtime's tool-approval flow consults this |
| 13 | +// to short-circuit the user prompt for Allow and to escalate Ask, so |
| 14 | +// the ordering must be stable. |
| 15 | +func TestAggregateTracksMostRestrictiveDecision(t *testing.T) { |
| 16 | + t.Parallel() |
| 17 | + |
| 18 | + mk := func(d Decision, reason string) hookResult { |
| 19 | + return hookResult{HandlerResult: HandlerResult{Output: &Output{ |
| 20 | + HookSpecificOutput: &HookSpecificOutput{ |
| 21 | + HookEventName: EventPreToolUse, |
| 22 | + PermissionDecision: d, |
| 23 | + PermissionDecisionReason: reason, |
| 24 | + }, |
| 25 | + }}} |
| 26 | + } |
| 27 | + |
| 28 | + cases := []struct { |
| 29 | + name string |
| 30 | + results []hookResult |
| 31 | + wantVerdict Decision |
| 32 | + wantReason string |
| 33 | + wantAllowed bool |
| 34 | + }{ |
| 35 | + { |
| 36 | + name: "no decision: Allowed=true, Decision empty", |
| 37 | + results: []hookResult{{}}, |
| 38 | + wantVerdict: "", |
| 39 | + wantAllowed: true, |
| 40 | + }, |
| 41 | + { |
| 42 | + name: "single allow", |
| 43 | + results: []hookResult{mk(DecisionAllow, "safe")}, |
| 44 | + wantVerdict: DecisionAllow, |
| 45 | + wantReason: "safe", |
| 46 | + wantAllowed: true, |
| 47 | + }, |
| 48 | + { |
| 49 | + name: "single ask escalates over no decision", |
| 50 | + results: []hookResult{{}, mk(DecisionAsk, "unclear")}, |
| 51 | + wantVerdict: DecisionAsk, |
| 52 | + wantReason: "unclear", |
| 53 | + wantAllowed: true, // Ask doesn't flip Allowed; the runtime handles the prompt. |
| 54 | + }, |
| 55 | + { |
| 56 | + name: "deny beats ask beats allow", |
| 57 | + results: []hookResult{ |
| 58 | + mk(DecisionAllow, "looks fine"), |
| 59 | + mk(DecisionAsk, "second-guess"), |
| 60 | + mk(DecisionDeny, "destructive"), |
| 61 | + }, |
| 62 | + wantVerdict: DecisionDeny, |
| 63 | + wantReason: "destructive", |
| 64 | + wantAllowed: false, |
| 65 | + }, |
| 66 | + { |
| 67 | + name: "first reason wins on ties", |
| 68 | + results: []hookResult{ |
| 69 | + mk(DecisionAsk, "first ask"), |
| 70 | + mk(DecisionAsk, "second ask"), |
| 71 | + }, |
| 72 | + wantVerdict: DecisionAsk, |
| 73 | + wantReason: "first ask", |
| 74 | + wantAllowed: true, |
| 75 | + }, |
| 76 | + } |
| 77 | + for _, tc := range cases { |
| 78 | + t.Run(tc.name, func(t *testing.T) { |
| 79 | + t.Parallel() |
| 80 | + final := aggregate(tc.results, EventPreToolUse) |
| 81 | + assert.Equal(t, tc.wantVerdict, final.Decision) |
| 82 | + assert.Equal(t, tc.wantReason, final.DecisionReason) |
| 83 | + assert.Equal(t, tc.wantAllowed, final.Allowed) |
| 84 | + }) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +// TestAggregateDecisionEmptyForNonPreToolUse documents that |
| 89 | +// Result.Decision is meaningful only for pre_tool_use events. Other |
| 90 | +// events (turn_start, post_tool_use, ...) MUST leave it empty so a |
| 91 | +// runtime that consults it can't accidentally act on a stale verdict |
| 92 | +// from an unrelated hook. |
| 93 | +func TestAggregateDecisionEmptyForNonPreToolUse(t *testing.T) { |
| 94 | + t.Parallel() |
| 95 | + |
| 96 | + results := []hookResult{{HandlerResult: HandlerResult{Output: &Output{ |
| 97 | + HookSpecificOutput: &HookSpecificOutput{ |
| 98 | + HookEventName: EventTurnStart, |
| 99 | + PermissionDecision: DecisionAllow, // misconfigured but possible |
| 100 | + }, |
| 101 | + }}}} |
| 102 | + |
| 103 | + final := aggregate(results, EventTurnStart) |
| 104 | + assert.Equal(t, Decision(""), final.Decision) |
| 105 | + assert.Empty(t, final.DecisionReason) |
| 106 | +} |
0 commit comments