@@ -3,41 +3,53 @@ local h = require("tests.helpers")
33local new_set = MiniTest .new_set
44local T = MiniTest .new_set ()
55
6- local inline
6+ local child = MiniTest . new_child_neovim ()
77
88T [" Inline" ] = new_set ({
99 hooks = {
1010 pre_case = function ()
11- inline = h .setup_inline ({
12- adapters = {
13- http = {
14- fake_adapter = { name = " fake_adapter" },
11+ h .child_start (child )
12+ child .lua ([[
13+ h = require('tests.helpers')
14+ config = require("tests.config")
15+
16+ -- Setup inline in child process
17+ inline = h.setup_inline({
18+ adapters = {
19+ http = {
20+ fake_adapter = { name = "fake_adapter" },
21+ },
1522 },
16- },
17- })
23+ })
24+ ]] )
25+ end ,
26+ post_case = function ()
27+ child .lua ([[ inline = nil]] )
1828 end ,
19- post_case = function () end ,
29+ post_once = child . stop ,
2030 },
2131})
2232
2333T [" Inline" ][" can parse json output correctly" ] = function ()
24- local json = inline : parse_output ( [[ {
34+ local json_str = [[ {
2535 "code": "function test() end",
2636 "placement": "add"
27- }]] )
37+ }]]
2838
39+ local json = child .lua ([[ return inline:parse_output(...)]] , { json_str })
2940 h .eq (" function test() end" , json .code )
3041 h .eq (" add" , json .placement )
3142end
3243
3344T [" Inline" ][" can parse markdown output correctly" ] = function ()
34- local json = inline : parse_output ( [[ ```json
45+ local markdown_str = [[ ```json
3546{
3647 "code": "function test() end",
3748 "placement": "add"
3849}
39- ```]] )
50+ ```]]
4051
52+ local json = child .lua ([[ return inline:parse_output(...)]] , { markdown_str })
4153 h .eq (" function test() end" , json .code )
4254 h .eq (" add" , json .placement )
4355end
@@ -49,7 +61,7 @@ T["Inline"]["can parse Ollama output correctly"] = function()
4961 .. ' "placement": "before"\n '
5062 .. " }"
5163
52- local json = inline :parse_output (ollama_response_str )
64+ local json = child . lua_get ( [[ inline:parse_output(...) ]] , { ollama_response_str } )
5365 local expected_code_block = [[
5466
5567
@@ -64,152 +76,176 @@ end
6476
6577T [" Inline" ][" handles different placements" ] = function ()
6678 -- Test 'add' placement
67- inline :place (" add" )
68- h .eq (inline .classification .pos , {
69- line = inline .buffer_context .end_line + 1 ,
79+ child .lua ([[ inline:place("add")]] )
80+ local pos = child .lua ([[ return inline.classification.pos]] )
81+ local buffer_context = child .lua ([[ return inline.buffer_context]] )
82+ h .eq (pos , {
83+ line = buffer_context .end_line + 1 ,
7084 col = 0 ,
71- bufnr = inline . buffer_context .bufnr ,
85+ bufnr = buffer_context .bufnr ,
7286 })
7387
7488 -- Test 'replace' placement
75- inline :place (" replace" )
76- h .eq (inline .classification .pos , {
77- line = inline .buffer_context .start_line ,
78- col = inline .buffer_context .start_col ,
79- bufnr = inline .buffer_context .bufnr ,
89+ child .lua ([[ inline:place("replace")]] )
90+ pos = child .lua ([[ return inline.classification.pos]] )
91+ buffer_context = child .lua ([[ return inline.buffer_context]] )
92+ h .eq (pos , {
93+ line = buffer_context .start_line ,
94+ col = buffer_context .start_col ,
95+ bufnr = buffer_context .bufnr ,
8096 })
8197
8298 -- Test 'before' placement
83- inline :place (" before" )
84- h .eq (inline .classification .pos , {
85- line = inline .buffer_context .start_line - 1 ,
86- col = math.max (0 , inline .buffer_context .start_col - 1 ),
87- bufnr = inline .buffer_context .bufnr ,
99+ child .lua ([[ inline:place("before")]] )
100+ pos = child .lua ([[ return inline.classification.pos]] )
101+ buffer_context = child .lua ([[ return inline.buffer_context]] )
102+ h .eq (pos , {
103+ line = buffer_context .start_line - 1 ,
104+ col = math.max (0 , buffer_context .start_col - 1 ),
105+ bufnr = buffer_context .bufnr ,
88106 })
89107end
90108
91109T [" Inline" ][" forms correct prompts" ] = function ()
92- local prompts = {
93- {
94- role = " user" ,
95- content = " test prompt" ,
96- opts = { contains_code = true },
97- },
98- }
110+ child .lua ([[
111+ local prompts = {
112+ {
113+ role = "user",
114+ content = "test prompt",
115+ opts = { contains_code = true },
116+ },
117+ }
99118
100- inline .prompts = prompts
101- inline .buffer_context .is_visual = true
102- inline .buffer_context .lines = { " local x = 1" }
119+ inline.prompts = prompts
120+ inline.buffer_context.is_visual = true
121+ inline.buffer_context.lines = { "local x = 1" }
103122
104- inline :prompt (" Hello World" )
123+ inline:prompt("Hello World")
124+ ]] )
105125
106- h .eq (# inline .prompts , 4 )
126+ local prompts = child .lua ([[ return inline.prompts]] )
127+ h .eq (# prompts , 4 )
107128 -- System prompt
108- h .expect_starts_with (" You are a knowledgeable" , inline . prompts [1 ].content )
129+ h .expect_starts_with (" You are a knowledgeable" , prompts [1 ].content )
109130 -- Visual selection
110131 h .eq (
111132 " For context, this is the code that I've visually selected in the buffer, which is relevant to my prompt:\n <code>\n ```lua\n local x = 1\n ```\n </code>" ,
112- inline . prompts [3 ].content
133+ prompts [3 ].content
113134 )
114135 -- User prompt
115- h .eq (" <prompt>Hello World</prompt>" , inline . prompts [# inline . prompts ].content )
136+ h .eq (" <prompt>Hello World</prompt>" , prompts [# prompts ].content )
116137end
117138
118139T [" Inline" ][" generates correct prompt structure" ] = function ()
119- local submitted_prompts = {}
120-
121- -- Mock the submit function
122- function inline :submit (prompts )
123- submitted_prompts = prompts
124- end
140+ child . lua ( [[
141+ -- Mock the submit function
142+ _G.submitted_prompts = {}
143+ function inline:submit(prompts)
144+ _G. submitted_prompts = prompts
145+ end
125146
126- inline :prompt (" Test prompt" )
147+ inline:prompt("Test prompt")
148+ ]] )
127149
150+ local submitted_prompts = child .lua ([[ return _G.submitted_prompts]] )
128151 h .eq (# submitted_prompts , 2 ) -- Should be a system prompt and the user prompt
129152 h .eq (submitted_prompts [1 ].role , " system" )
130153 h .eq (submitted_prompts [2 ].role , " user" )
131154 h .eq (submitted_prompts [2 ].content , " <prompt>Test prompt</prompt>" )
132155end
133156
134157T [" Inline" ][" the first word can be an adapter" ] = function ()
135- -- Mock the submit function
136- local submitted_prompts = {}
137- function inline :submit (prompts )
138- submitted_prompts = prompts
139- end
158+ child .lua ([[
159+ -- Mock the submit function
160+ _G.submitted_prompts = {}
161+ function inline:submit(prompts)
162+ _G.submitted_prompts = prompts
163+ end
164+ ]] )
140165
141166 -- Adapter is the default
142- h .eq (inline .adapter .name , " test_adapter" )
167+ h .eq (child . lua ( [[ return inline.adapter.name]] ) , " test_adapter" )
143168
144- inline :prompt (" fake_adapter print hello world" )
169+ child . lua ( [[ inline:prompt("fake_adapter print hello world") ]] )
145170
146171 -- Adapter has been changed
147- h .eq (" fake_adapter" , inline .adapter .name )
172+ h .eq (" fake_adapter" , child . lua ( [[ return inline.adapter.name]] ) )
148173
149174 -- Adapter is removed from the prompt
175+ local submitted_prompts = child .lua ([[ return _G.submitted_prompts]] )
150176 h .eq (submitted_prompts [2 ].content , " <prompt>print hello world</prompt>" )
151177end
152178
153179T [" Inline" ][" can be called from the action palette" ] = function ()
154- local prompt = {
155- name = " test" ,
156- strategy = " inline" ,
157- prompts = {
158- {
159- role = " user" ,
160- content = " Action Palette test" ,
180+ child .lua ([[
181+ local prompt = {
182+ name = "test",
183+ strategy = "inline",
184+ prompts = {
185+ {
186+ role = "user",
187+ content = "Action Palette test",
188+ },
161189 },
162- },
163- }
190+ }
164191
165- local interaction = require (" codecompanion.interactions" ).new ({
166- buffer_context = inline .buffer_context ,
167- selected = prompt ,
168- })
169- interaction :start (" inline" )
192+ local interaction = require("codecompanion.interactions").new({
193+ buffer_context = inline.buffer_context,
194+ selected = prompt,
195+ })
196+ interaction:start("inline")
197+
198+ _G.test_interaction = interaction
199+ ]] )
170200
171201 -- System prompt is added
172- h .eq (2 , # interaction . called .prompts )
202+ h .eq (2 , child . lua ( [[ return #_G.test_interaction. called.prompts]] ) )
173203
174204 -- User prompt is added
175- h .eq (" Action Palette test" , interaction . called .prompts [2 ].content )
205+ h .eq (" Action Palette test" , child . lua ( [[ return _G.test_interaction. called.prompts[2].content]] ) )
176206end
177207
178208T [" Inline" ][" integration" ] = function ()
179- -- Mock the submit function
180- local submitted_prompts = {}
181- function inline :submit (prompts )
182- submitted_prompts = prompts
183- end
209+ child .lua ([[
210+ -- Mock the submit function
211+ _G.submitted_prompts = {}
212+ function inline:submit(prompts)
213+ _G.submitted_prompts = prompts
214+ end
184215
185- inline :prompt (" #{foo} can you print hello world?" )
216+ inline:prompt("#{foo} can you print hello world?")
217+ ]] )
186218
219+ local submitted_prompts = child .lua ([[ return _G.submitted_prompts]] )
187220 h .eq (" The output from foo variable" , submitted_prompts [2 ].content )
188221 h .eq (" <prompt>can you print hello world?</prompt>" , submitted_prompts [3 ].content )
189222end
190223
191224T [" Inline" ][" can parse adapter syntax" ] = function ()
192- local submitted_prompts = {}
193- function inline :submit (prompts )
194- submitted_prompts = prompts
195- end
196-
197- -- Mock the buffer variable to return predictable content
198- local original_buffer_variable = require (" codecompanion.config" ).interactions .inline .variables .buffer
199- require (" codecompanion.config" ).interactions .inline .variables .buffer = {
200- callback = function ()
201- return " mocked buffer content"
202- end ,
203- description = " Mock buffer for testing" ,
204- }
225+ child .lua ([[
226+ _G.submitted_prompts = {}
227+ function inline:submit(prompts)
228+ _G.submitted_prompts = prompts
229+ end
230+
231+ -- Mock the buffer variable to return predictable content
232+ _G.original_buffer_variable = require("codecompanion.config").interactions.inline.variables.buffer
233+ require("codecompanion.config").interactions.inline.variables.buffer = {
234+ callback = function()
235+ return "mocked buffer content"
236+ end,
237+ description = "Mock buffer for testing",
238+ }
239+ ]] )
205240
206241 -- Default adapter
207- h .eq (inline .adapter .name , " test_adapter" )
242+ h .eq (child . lua ( [[ return inline.adapter.name]] ) , " test_adapter" )
208243
209- inline :prompt (" adapter=fake_adapter #{buffer} print hello world" )
210- h .eq (" fake_adapter" , inline .adapter .name )
244+ child . lua ( [[ inline:prompt("adapter=fake_adapter #{buffer} print hello world") ]] )
245+ h .eq (" fake_adapter" , child . lua ( [[ return inline.adapter.name]] ) )
211246
212247 -- Should be system + buffer content + user prompt
248+ local submitted_prompts = child .lua ([[ return _G.submitted_prompts]] )
213249 h .eq (3 , # submitted_prompts )
214250
215251 h .eq (" mocked buffer content" , submitted_prompts [2 ].content )
@@ -218,7 +254,9 @@ T["Inline"]["can parse adapter syntax"] = function()
218254 h .eq (" <prompt>print hello world</prompt>" , submitted_prompts [# submitted_prompts ].content )
219255
220256 -- Restore original buffer variable
221- require (" codecompanion.config" ).interactions .inline .variables .buffer = original_buffer_variable
257+ child .lua ([[
258+ require("codecompanion.config").interactions.inline.variables.buffer = _G.original_buffer_variable
259+ ]] )
222260end
223261
224262return T
0 commit comments