11local renderer = require (' opencode.ui.renderer' )
22local config = require (' opencode.config' )
33local state = require (' opencode.state' )
4+ local core = require (' opencode.core' )
45local helpers = require (' tests.helpers' )
56local ui = require (' opencode.ui.ui' )
67
@@ -115,23 +116,44 @@ describe('hooks', function()
115116 describe (' on_done_thinking' , function ()
116117 it (' should call hook when thinking is done' , function ()
117118 local called = false
119+ local called_session = nil
118120
119- config .hooks .on_done_thinking = function ()
121+ config .hooks .on_done_thinking = function (session )
120122 called = true
123+ called_session = session
124+ end
125+
126+ -- Mock session.get_all_workspace_sessions to return our test session
127+ local session_module = require (' opencode.session' )
128+ local original_get_all = session_module .get_all_workspace_sessions
129+ session_module .get_all_workspace_sessions = function ()
130+ return { { id = ' test-session' , title = ' Test' } }
121131 end
122132
123- -- Simulate job count change from 1 to 0 (done thinking)
124- state .user_message_count = 1
125- state .user_message_count = 0
133+ state .subscribe (' user_message_count' , core ._on_user_message_count_change )
134+
135+ -- Simulate job count change from 1 to 0 (done thinking) for a specific session
136+ state .active_session = { id = ' test-session' , title = ' Test' }
137+ state .user_message_count = { [' test-session' ] = 1 }
138+ state .user_message_count = { [' test-session' ] = 0 }
139+
140+ -- Wait for async notification
141+ vim .wait (100 , function () return called end )
142+
143+ -- Restore original function
144+ session_module .get_all_workspace_sessions = original_get_all
145+ state .unsubscribe (' user_message_count' , core ._on_user_message_count_change )
126146
127147 assert .is_true (called )
148+ assert .are .equal (called_session .id , ' test-session' )
128149 end )
129150
130151 it (' should not error when hook is nil' , function ()
131152 config .hooks .on_done_thinking = nil
132- state .user_message_count = 1
153+ state .active_session = { id = ' test-session' , title = ' Test' }
154+ state .user_message_count = { [' test-session' ] = 1 }
133155 assert .has_no .errors (function ()
134- state .user_message_count = 0
156+ state .user_message_count = { [ ' test-session ' ] = 0 }
135157 end )
136158 end )
137159
@@ -140,26 +162,48 @@ describe('hooks', function()
140162 error (' test error' )
141163 end
142164
143- state .user_message_count = 1
165+ state .active_session = { id = ' test-session' , title = ' Test' }
166+ state .user_message_count = { [' test-session' ] = 1 }
144167 assert .has_no .errors (function ()
145- state .user_message_count = 0
168+ state .user_message_count = { [ ' test-session ' ] = 0 }
146169 end )
147170 end )
148171 end )
149172
150173 describe (' on_permission_requested' , function ()
151174 it (' should call hook when permission is requested' , function ()
152175 local called = false
176+ local called_session = nil
153177
154- config .hooks .on_permission_requested = function ()
178+ config .hooks .on_permission_requested = function (session )
155179 called = true
180+ called_session = session
181+ end
182+
183+ -- Mock session.get_by_id to return our test session
184+ local session_module = require (' opencode.session' )
185+ local original_get_by_id = session_module .get_by_id
186+ session_module .get_by_id = function (id )
187+ return { id = id , title = ' Test' }
156188 end
157189
190+ -- Set up the subscription manually
191+ state .subscribe (' current_permission' , core ._on_current_permission_change )
192+
158193 -- Simulate permission change from nil to a value
194+ state .active_session = { id = ' test-session' , title = ' Test' }
159195 state .current_permission = nil
160196 state .current_permission = { tool = ' test_tool' , action = ' read' }
161197
198+ -- Wait for async notification
199+ vim .wait (100 , function () return called end )
200+
201+ -- Restore original function
202+ session_module .get_by_id = original_get_by_id
203+ state .unsubscribe (' current_permission' , core ._on_current_permission_change )
204+
162205 assert .is_true (called )
206+ assert .are .equal (called_session .id , ' test-session' )
163207 end )
164208
165209 it (' should not error when hook is nil' , function ()
0 commit comments