|
| 1 | +"""Unit tests for jupyter_jcli.hook_decision typed decision classes.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from jupyter_jcli.hook_decision import ( |
| 6 | + HookEvent, |
| 7 | + PostToolUseContext, |
| 8 | + PreToolUseDecision, |
| 9 | + PreToolUseOutcome, |
| 10 | +) |
| 11 | + |
| 12 | + |
| 13 | +class TestHookEvent: |
| 14 | + def test_pre_tool_use_value(self): |
| 15 | + assert HookEvent.PRE_TOOL_USE == "PreToolUse" |
| 16 | + assert isinstance(HookEvent.PRE_TOOL_USE, str) |
| 17 | + |
| 18 | + def test_post_tool_use_value(self): |
| 19 | + assert HookEvent.POST_TOOL_USE == "PostToolUse" |
| 20 | + assert isinstance(HookEvent.POST_TOOL_USE, str) |
| 21 | + |
| 22 | + def test_invalid_event_raises(self): |
| 23 | + with pytest.raises(ValueError): |
| 24 | + HookEvent("bogus") |
| 25 | + |
| 26 | + |
| 27 | +class TestPreToolUseOutcome: |
| 28 | + def test_allow(self): |
| 29 | + assert PreToolUseOutcome.ALLOW == "allow" |
| 30 | + assert isinstance(PreToolUseOutcome.ALLOW, str) |
| 31 | + |
| 32 | + def test_deny(self): |
| 33 | + assert PreToolUseOutcome.DENY == "deny" |
| 34 | + |
| 35 | + def test_ask(self): |
| 36 | + assert PreToolUseOutcome.ASK == "ask" |
| 37 | + |
| 38 | + def test_invalid_outcome_raises(self): |
| 39 | + with pytest.raises(ValueError): |
| 40 | + PreToolUseOutcome("bogus") |
| 41 | + |
| 42 | + |
| 43 | +class TestPreToolUseDecision: |
| 44 | + def test_deny_payload(self): |
| 45 | + d = PreToolUseDecision(PreToolUseOutcome.DENY, "you shall not pass") |
| 46 | + p = d.to_payload() |
| 47 | + assert p == { |
| 48 | + "hookSpecificOutput": { |
| 49 | + "hookEventName": "PreToolUse", |
| 50 | + "permissionDecision": "deny", |
| 51 | + "permissionDecisionReason": "you shall not pass", |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + def test_allow_payload(self): |
| 56 | + d = PreToolUseDecision(PreToolUseOutcome.ALLOW, "go ahead") |
| 57 | + p = d.to_payload() |
| 58 | + assert p["hookSpecificOutput"]["permissionDecision"] == "allow" |
| 59 | + assert p["hookSpecificOutput"]["permissionDecisionReason"] == "go ahead" |
| 60 | + assert "additionalContext" not in p["hookSpecificOutput"] |
| 61 | + |
| 62 | + def test_ask_payload(self): |
| 63 | + d = PreToolUseDecision(PreToolUseOutcome.ASK, "are you sure?") |
| 64 | + p = d.to_payload() |
| 65 | + assert p["hookSpecificOutput"]["permissionDecision"] == "ask" |
| 66 | + assert p["hookSpecificOutput"]["hookEventName"] == "PreToolUse" |
| 67 | + |
| 68 | + def test_no_additional_context_in_pre(self): |
| 69 | + d = PreToolUseDecision(PreToolUseOutcome.DENY, "reason") |
| 70 | + p = d.to_payload() |
| 71 | + assert "additionalContext" not in p["hookSpecificOutput"] |
| 72 | + assert "decision" not in p |
| 73 | + |
| 74 | + |
| 75 | +class TestPostToolUseContext: |
| 76 | + def test_payload_shape(self): |
| 77 | + c = PostToolUseContext("paired notebook drifted, run j-cli convert") |
| 78 | + p = c.to_payload() |
| 79 | + assert p == { |
| 80 | + "hookSpecificOutput": { |
| 81 | + "hookEventName": "PostToolUse", |
| 82 | + "additionalContext": "paired notebook drifted, run j-cli convert", |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + def test_no_permission_decision_in_post(self): |
| 87 | + c = PostToolUseContext("some context") |
| 88 | + p = c.to_payload() |
| 89 | + assert "permissionDecision" not in p["hookSpecificOutput"] |
| 90 | + assert "permissionDecisionReason" not in p["hookSpecificOutput"] |
| 91 | + assert "decision" not in p |
| 92 | + |
| 93 | + def test_auto_synced_context(self): |
| 94 | + c = PostToolUseContext("Auto-synced foo.py to bar.ipynb. Pair is now in sync.") |
| 95 | + p = c.to_payload() |
| 96 | + assert "Auto-synced" in p["hookSpecificOutput"]["additionalContext"] |
0 commit comments