Skip to content

Commit 591810f

Browse files
test(hooks): Test on_done_thinking and on_permission_requested hooks
1 parent cd5edbd commit 591810f

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

tests/unit/hooks_spec.lua

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ describe('hooks', function()
1010
config.hooks = {
1111
on_file_edited = nil,
1212
on_session_loaded = nil,
13+
on_done_thinking = nil,
14+
on_permission_requested = nil,
1315
}
1416
end)
1517

@@ -20,6 +22,8 @@ describe('hooks', function()
2022
config.hooks = {
2123
on_file_edited = nil,
2224
on_session_loaded = nil,
25+
on_done_thinking = nil,
26+
on_permission_requested = nil,
2327
}
2428
end)
2529

@@ -107,4 +111,74 @@ describe('hooks', function()
107111
end)
108112
end)
109113
end)
114+
115+
describe('on_done_thinking', function()
116+
it('should call hook when thinking is done', function()
117+
local called = false
118+
119+
config.hooks.on_done_thinking = function()
120+
called = true
121+
end
122+
123+
-- Simulate job count change from 1 to 0 (done thinking)
124+
state.user_message_count = 1
125+
state.user_message_count = 0
126+
127+
assert.is_true(called)
128+
end)
129+
130+
it('should not error when hook is nil', function()
131+
config.hooks.on_done_thinking = nil
132+
state.user_message_count = 1
133+
assert.has_no.errors(function()
134+
state.user_message_count = 0
135+
end)
136+
end)
137+
138+
it('should not crash when hook throws error', function()
139+
config.hooks.on_done_thinking = function()
140+
error('test error')
141+
end
142+
143+
state.user_message_count = 1
144+
assert.has_no.errors(function()
145+
state.user_message_count = 0
146+
end)
147+
end)
148+
end)
149+
150+
describe('on_permission_requested', function()
151+
it('should call hook when permission is requested', function()
152+
local called = false
153+
154+
config.hooks.on_permission_requested = function()
155+
called = true
156+
end
157+
158+
-- Simulate permission change from nil to a value
159+
state.current_permission = nil
160+
state.current_permission = { tool = 'test_tool', action = 'read' }
161+
162+
assert.is_true(called)
163+
end)
164+
165+
it('should not error when hook is nil', function()
166+
config.hooks.on_permission_requested = nil
167+
state.current_permission = nil
168+
assert.has_no.errors(function()
169+
state.current_permission = { tool = 'test_tool', action = 'read' }
170+
end)
171+
end)
172+
173+
it('should not crash when hook throws error', function()
174+
config.hooks.on_permission_requested = function()
175+
error('test error')
176+
end
177+
178+
state.current_permission = nil
179+
assert.has_no.errors(function()
180+
state.current_permission = { tool = 'test_tool', action = 'read' }
181+
end)
182+
end)
183+
end)
110184
end)

0 commit comments

Comments
 (0)