Skip to content

Commit a431f60

Browse files
authored
tests: fix tests relying on Copilot (#2431)
Co-authored-by: Oli Morris <[email protected]>
1 parent d348080 commit a431f60

File tree

6 files changed

+34
-11
lines changed

6 files changed

+34
-11
lines changed

tests/adapters/test_adapters.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ T = new_set({
88
pre_case = function()
99
h.child_start(child)
1010
child.lua([[
11+
h = require('tests.helpers')
1112
utils = require("codecompanion.utils.adapters")
1213
1314
_G.test_adapter = {
@@ -290,7 +291,7 @@ end
290291

291292
T["Adapter"]["can pass in the name of the model"] = function()
292293
local result = child.lua([[
293-
require("codecompanion").setup({
294+
h.setup_plugin({
294295
strategies = {
295296
chat = {
296297
adapter = {

tests/helpers.lua

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,29 @@ local function mock_config()
1616
end
1717

1818
---Set up the CodeCompanion plugin with test configuration
19+
---@param config? table
1920
---@return nil
20-
Helpers.setup_plugin = function()
21-
local test_config = require("tests.config")
22-
test_config.strategies.chat.adapter = "test_adapter"
21+
Helpers.setup_plugin = function(config)
22+
local test_config = config or require("tests.config")
23+
24+
-- To be safe, ensure that we mock some of the Copilot adapter's features
25+
-- that make HTTP requests. This slows tests down and makes them fail
26+
-- in GitHub Actions.
27+
local function mock_external_calls()
28+
local ok, copilot = pcall(require, "codecompanion.adapters.http.copilot")
29+
if ok then
30+
copilot.schema.max_tokens.default = 16000
31+
32+
local get_models_ok, get_models = pcall(require, "codecompanion.adapters.http.copilot.get_models")
33+
if get_models_ok then
34+
get_models.choices = function(adapter, opts, provided_token)
35+
return { "gpt-4.1" }
36+
end
37+
end
38+
end
39+
end
40+
41+
mock_external_calls()
2342

2443
local codecompanion = require("codecompanion")
2544
codecompanion.setup(test_config)

tests/strategies/chat/memory/test_memory.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ T["Memory.make() integration: memory is added to a real chat messages stack"] =
182182
child.lua(string.format(
183183
[[
184184
local h = require("tests.helpers")
185+
185186
-- Initialize plugin with test config (h.setup_plugin returns the codecompanion module)
186187
local cc = h.setup_plugin()
187188

tests/strategies/chat/test_chat.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ T = new_set({
1313
codecompanion = require("codecompanion")
1414
h = require('tests.helpers')
1515
_G.chat, _G.tools = h.setup_chat_buffer()
16-
]])
16+
]])
1717
end,
1818
post_case = function()
1919
child.lua([[h.teardown_chat_buffer()]])
@@ -233,11 +233,11 @@ T["Chat"]["ftplugin window options override plugin defaults"] = function()
233233
_G.test_temp_dir = temp_dir
234234
_G.test_ftplugin_path = ftplugin_path
235235
236+
h = require('tests.helpers')
237+
236238
-- Setup codecompanion
237-
codecompanion = require("codecompanion")
239+
local codecompanion = h.setup_plugin()
238240
codecompanion.setup()
239-
240-
-- Open a new chat
241241
codecompanion.chat()
242242
243243
-- Manually source the ftplugin file to simulate user's after/ftplugin

tests/test_cmds.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ T = new_set({
1212
h = require('tests.helpers')
1313
config = require('codecompanion.config')
1414
config.memory.opts.chat.enabled = false
15-
h.setup_plugin()
15+
h.setup_plugin(config)
1616
]])
1717
end,
1818
post_once = child.stop,
@@ -22,6 +22,7 @@ T = new_set({
2222
T["cmds"] = new_set()
2323
T["cmds"][":CodeCompanionChat"] = function()
2424
child.lua([[
25+
2526
-- Mock the submit function
2627
local original = h.mock_submit("This is a mocked response: 1 + 1 = 2")
2728
@@ -161,7 +162,7 @@ T["cmds"]["chat variable syntax highlighting"] = function()
161162
vim.bo[buf].filetype = 'codecompanion'
162163
vim.cmd('doautocmd FileType codecompanion')
163164
vim.cmd('doautocmd BufEnter ' .. tostring(buf))
164-
165+
165166
-- wait for some time so that the `vim.schedule`ed :syntax commands are executed
166167
vim.wait(10)
167168

tests/test_prompt_library.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ T = new_set({
88
pre_case = function()
99
h.child_start(child)
1010
child.lua([[
11-
codecompanion = require("codecompanion")
11+
h = require('tests.helpers')
12+
codecompanion = h.setup_plugin()
1213
]])
1314
end,
1415
post_once = child.stop,

0 commit comments

Comments
 (0)