@@ -4,6 +4,7 @@ local ui = require('opencode.ui.ui')
44local state = require (' opencode.state' )
55local stub = require (' luassert.stub' )
66local assert = require (' luassert' )
7+ local Promise = require (' opencode.promise' )
78
89describe (' opencode.api' , function ()
910 local created_commands = {}
@@ -17,7 +18,10 @@ describe('opencode.api', function()
1718 opts = opts ,
1819 })
1920 end )
20- stub (core , ' open' )
21+ stub (core , ' open' ).invokes (function ()
22+ return Promise .new ():resolve (' done' )
23+ end )
24+
2125 stub (core , ' run' )
2226 stub (core , ' cancel' )
2327 stub (core , ' send_message' )
@@ -118,13 +122,13 @@ describe('opencode.api', function()
118122
119123 -- Test the exported functions
120124 assert .is_function (api .open_input , ' Should export open_input' )
121- api .open_input ()
125+ api .open_input (): wait ()
122126 assert .stub (core .open ).was_called ()
123127 assert .stub (core .open ).was_called_with ({ new_session = false , focus = ' input' , start_insert = true })
124128
125129 -- Test run function
126130 assert .is_function (api .run , ' Should export run' )
127- api .run (' test prompt' )
131+ api .run (' test prompt' ): wait ()
128132 assert .stub (core .send_message ).was_called ()
129133 assert .stub (core .send_message ).was_called_with (' test prompt' , {
130134 new_session = false ,
@@ -133,7 +137,7 @@ describe('opencode.api', function()
133137
134138 -- Test run_new_session function
135139 assert .is_function (api .run_new_session , ' Should export run_new_session' )
136- api .run_new_session (' test prompt new' )
140+ api .run_new_session (' test prompt new' ): wait ()
137141 assert .stub (core .send_message ).was_called ()
138142 assert .stub (core .send_message ).was_called_with (' test prompt new' , {
139143 new_session = true ,
@@ -144,7 +148,7 @@ describe('opencode.api', function()
144148
145149 describe (' run command argument parsing' , function ()
146150 it (' parses agent prefix and passes to send_message' , function ()
147- api .commands .run .fn ({ ' agent=plan' , ' analyze' , ' this' , ' code' })
151+ api .commands .run .fn ({ ' agent=plan' , ' analyze' , ' this' , ' code' }): wait ()
148152 assert .stub (core .send_message ).was_called ()
149153 assert .stub (core .send_message ).was_called_with (' analyze this code' , {
150154 new_session = false ,
@@ -154,7 +158,7 @@ describe('opencode.api', function()
154158 end )
155159
156160 it (' parses model prefix and passes to send_message' , function ()
157- api .commands .run .fn ({ ' model=openai/gpt-4' , ' test' , ' prompt' })
161+ api .commands .run .fn ({ ' model=openai/gpt-4' , ' test' , ' prompt' }): wait ()
158162 assert .stub (core .send_message ).was_called ()
159163 assert .stub (core .send_message ).was_called_with (' test prompt' , {
160164 new_session = false ,
@@ -164,7 +168,7 @@ describe('opencode.api', function()
164168 end )
165169
166170 it (' parses context prefix and passes to send_message' , function ()
167- api .commands .run .fn ({ ' context=current_file.enabled=false' , ' test' })
171+ api .commands .run .fn ({ ' context=current_file.enabled=false' , ' test' }): wait ()
168172 assert .stub (core .send_message ).was_called ()
169173 assert .stub (core .send_message ).was_called_with (' test' , {
170174 new_session = false ,
@@ -174,13 +178,15 @@ describe('opencode.api', function()
174178 end )
175179
176180 it (' parses multiple prefixes and passes all to send_message' , function ()
177- api .commands .run .fn ({
178- ' agent=plan' ,
179- ' model=openai/gpt-4' ,
180- ' context=current_file.enabled=false' ,
181- ' analyze' ,
182- ' code' ,
183- })
181+ api .commands .run
182+ .fn ({
183+ ' agent=plan' ,
184+ ' model=openai/gpt-4' ,
185+ ' context=current_file.enabled=false' ,
186+ ' analyze' ,
187+ ' code' ,
188+ })
189+ :wait ()
184190 assert .stub (core .send_message ).was_called ()
185191 assert .stub (core .send_message ).was_called_with (' analyze code' , {
186192 new_session = false ,
@@ -192,7 +198,7 @@ describe('opencode.api', function()
192198 end )
193199
194200 it (' works with run_new command' , function ()
195- api .commands .run_new .fn ({ ' agent=plan' , ' model=openai/gpt-4' , ' new' , ' session' , ' prompt' })
201+ api .commands .run_new .fn ({ ' agent=plan' , ' model=openai/gpt-4' , ' new' , ' session' , ' prompt' }): wait ()
196202 assert .stub (core .send_message ).was_called ()
197203 assert .stub (core .send_message ).was_called_with (' new session prompt' , {
198204 new_session = true ,
@@ -210,7 +216,7 @@ describe('opencode.api', function()
210216 end )
211217
212218 it (' Lua API accepts opts directly without parsing' , function ()
213- api .run (' test prompt' , { agent = ' plan' , model = ' openai/gpt-4' })
219+ api .run (' test prompt' , { agent = ' plan' , model = ' openai/gpt-4' }): wait ()
214220 assert .stub (core .send_message ).was_called ()
215221 assert .stub (core .send_message ).was_called_with (' test prompt' , {
216222 new_session = false ,
@@ -434,6 +440,12 @@ describe('opencode.api', function()
434440 end )
435441
436442 describe (' user command model/agent selection' , function ()
443+ before_each (function ()
444+ stub (api , ' open_input' ).invokes (function ()
445+ return Promise .new ():resolve (' done' )
446+ end )
447+ end )
448+
437449 it (' invokes run with correct model and agent' , function ()
438450 local config_file = require (' opencode.config_file' )
439451 local original_get_user_commands = config_file .get_user_commands
@@ -466,8 +478,6 @@ describe('opencode.api', function()
466478 end ,
467479 }
468480
469- stub (api , ' open_input' )
470-
471481 local slash_commands = api .get_slash_commands ()
472482
473483 local test_no_model_cmd = nil
@@ -484,7 +494,7 @@ describe('opencode.api', function()
484494 assert .truthy (test_no_model_cmd , ' Should find /test-no-model command' )
485495 assert .truthy (test_with_model_cmd , ' Should find /test-with-model command' )
486496
487- test_no_model_cmd .fn ()
497+ test_no_model_cmd .fn (): wait ()
488498 assert .equal (1 , # send_command_calls )
489499 assert .equal (' test-session' , send_command_calls [1 ].session_id )
490500 assert .equal (' test-no-model' , send_command_calls [1 ].command_data .command )
@@ -494,7 +504,7 @@ describe('opencode.api', function()
494504
495505 send_command_calls = {}
496506
497- test_with_model_cmd .fn ()
507+ test_with_model_cmd .fn (): wait ()
498508 assert .equal (1 , # send_command_calls )
499509 assert .equal (' test-session' , send_command_calls [1 ].session_id )
500510 assert .equal (' test-with-model' , send_command_calls [1 ].command_data .command )
0 commit comments