From d94b20515cbe334a16d4d3f6577006398a5cf6b3 Mon Sep 17 00:00:00 2001 From: Cameron Ring Date: Mon, 10 Nov 2025 11:19:26 -0800 Subject: [PATCH] test(core): switch mode sets model correctly also fix nil check diagnostic for get_opencode_config() --- lua/opencode/core.lua | 5 ++-- tests/unit/core_spec.lua | 64 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 2 deletions(-) diff --git a/lua/opencode/core.lua b/lua/opencode/core.lua index a2c7ee05..749893cc 100644 --- a/lua/opencode/core.lua +++ b/lua/opencode/core.lua @@ -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 diff --git a/tests/unit/core_spec.lua b/tests/unit/core_spec.lua index e7f8889a..f325cc5f 100644 --- a/tests/unit/core_spec.lua +++ b/tests/unit/core_spec.lua @@ -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)