Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lua/opencode/core.lua
Original file line number Diff line number Diff line change
Expand Up @@ -316,8 +316,9 @@ function M.switch_to_mode(mode)
end

state.current_mode = mode
local agents_config = config_file.get_opencode_config().agent or {}
local mode_config = agents_config[mode] or {}
local opencode_config = config_file.get_opencode_config()
local agent_config = opencode_config and opencode_config.agent or {}
local mode_config = agent_config[mode] or {}
if mode_config.model and mode_config.model ~= '' then
state.current_model = mode_config.model
end
Expand Down
64 changes: 64 additions & 0 deletions tests/unit/core_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -337,4 +337,68 @@ describe('opencode.core', function()
assert.is_true(core.opencode_ok())
end)
end)

describe('switch_to_mode', function()
it('sets current model from config file when mode has a model configured', function()
stub(config_file, 'get_opencode_agents').returns({ 'plan', 'build', 'custom' })
stub(config_file, 'get_opencode_config').returns({
agent = {
custom = {
model = 'anthropic/claude-3-opus',
},
},
})

state.current_mode = nil
state.current_model = nil

local success = core.switch_to_mode('custom')

assert.is_true(success)
assert.equal('custom', state.current_mode)
assert.equal('anthropic/claude-3-opus', state.current_model)

config_file.get_opencode_agents:revert()
config_file.get_opencode_config:revert()
end)

it('does not change current model when mode has no model configured', function()
stub(config_file, 'get_opencode_agents').returns({ 'plan', 'build' })
stub(config_file, 'get_opencode_config').returns({
agent = {
plan = {},
},
})

state.current_mode = nil
state.current_model = 'existing/model'

local success = core.switch_to_mode('plan')

assert.is_true(success)
assert.equal('plan', state.current_mode)
assert.equal('existing/model', state.current_model)

config_file.get_opencode_agents:revert()
config_file.get_opencode_config:revert()
end)

it('returns false when mode is invalid', function()
stub(config_file, 'get_opencode_agents').returns({ 'plan', 'build' })

local success = core.switch_to_mode('nonexistent')

assert.is_false(success)

config_file.get_opencode_agents:revert()
end)

it('returns false when mode is empty', function()
local success = core.switch_to_mode('')
assert.is_false(success)

success = core.switch_to_mode(nil)
assert.is_false(success)
end)
end)
end)