Skip to content

Commit d94b205

Browse files
committed
test(core): switch mode sets model correctly
also fix nil check diagnostic for get_opencode_config()
1 parent 16a03d2 commit d94b205

File tree

2 files changed

+67
-2
lines changed

2 files changed

+67
-2
lines changed

lua/opencode/core.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,8 +316,9 @@ function M.switch_to_mode(mode)
316316
end
317317

318318
state.current_mode = mode
319-
local agents_config = config_file.get_opencode_config().agent or {}
320-
local mode_config = agents_config[mode] or {}
319+
local opencode_config = config_file.get_opencode_config()
320+
local agent_config = opencode_config and opencode_config.agent or {}
321+
local mode_config = agent_config[mode] or {}
321322
if mode_config.model and mode_config.model ~= '' then
322323
state.current_model = mode_config.model
323324
end

tests/unit/core_spec.lua

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,4 +337,68 @@ describe('opencode.core', function()
337337
assert.is_true(core.opencode_ok())
338338
end)
339339
end)
340+
341+
describe('switch_to_mode', function()
342+
it('sets current model from config file when mode has a model configured', function()
343+
stub(config_file, 'get_opencode_agents').returns({ 'plan', 'build', 'custom' })
344+
stub(config_file, 'get_opencode_config').returns({
345+
agent = {
346+
custom = {
347+
model = 'anthropic/claude-3-opus',
348+
},
349+
},
350+
})
351+
352+
state.current_mode = nil
353+
state.current_model = nil
354+
355+
local success = core.switch_to_mode('custom')
356+
357+
assert.is_true(success)
358+
assert.equal('custom', state.current_mode)
359+
assert.equal('anthropic/claude-3-opus', state.current_model)
360+
361+
config_file.get_opencode_agents:revert()
362+
config_file.get_opencode_config:revert()
363+
end)
364+
365+
it('does not change current model when mode has no model configured', function()
366+
stub(config_file, 'get_opencode_agents').returns({ 'plan', 'build' })
367+
stub(config_file, 'get_opencode_config').returns({
368+
agent = {
369+
plan = {},
370+
},
371+
})
372+
373+
state.current_mode = nil
374+
state.current_model = 'existing/model'
375+
376+
local success = core.switch_to_mode('plan')
377+
378+
assert.is_true(success)
379+
assert.equal('plan', state.current_mode)
380+
assert.equal('existing/model', state.current_model)
381+
382+
config_file.get_opencode_agents:revert()
383+
config_file.get_opencode_config:revert()
384+
end)
385+
386+
it('returns false when mode is invalid', function()
387+
stub(config_file, 'get_opencode_agents').returns({ 'plan', 'build' })
388+
389+
local success = core.switch_to_mode('nonexistent')
390+
391+
assert.is_false(success)
392+
393+
config_file.get_opencode_agents:revert()
394+
end)
395+
396+
it('returns false when mode is empty', function()
397+
local success = core.switch_to_mode('')
398+
assert.is_false(success)
399+
400+
success = core.switch_to_mode(nil)
401+
assert.is_false(success)
402+
end)
403+
end)
340404
end)

0 commit comments

Comments
 (0)