Skip to content

Commit 15b74c9

Browse files
committed
fix(session): validate session ID path and update tests
Move session ID detection above tree-navigation active check so invalid inputs get clear feedback via empty_policy=notify. Update tests to match new behavior.
1 parent c551786 commit 15b74c9

3 files changed

Lines changed: 25 additions & 13 deletions

File tree

lua/opencode/commands/handlers/session.lua

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -180,24 +180,32 @@ local function compute_target_index(current_idx, total, direction, wrap)
180180
end
181181

182182
function M.actions.navigate_session_tree(direction, interaction, wrap, empty_policy)
183-
local active = state.active_session
184-
if not active then
185-
if empty_policy == 'notify' then vim.notify('No active session', vim.log.levels.WARN) end
186-
return
187-
end
188-
189-
-- If direction is not a known navigation direction, treat it as a target session ID
183+
-- If direction is not a known navigation direction, treat it as a target session ID.
184+
-- This path runs before the tree lookup so invalid inputs get clear feedback.
190185
if direction
191186
and not tree_directions[direction]
192187
and direction ~= 'forward'
193188
and direction ~= 'backward'
194189
then
190+
empty_policy = empty_policy or 'notify'
191+
if not state.active_session then
192+
if empty_policy == 'notify' then
193+
vim.notify('No active session to navigate from', vim.log.levels.WARN)
194+
end
195+
return
196+
end
195197
if interaction == 'picker' then
196198
return session_runtime.select_session(direction)
197199
end
198200
return session_runtime.switch_session(direction)
199201
end
200202

203+
local active = state.active_session
204+
if not active then
205+
if empty_policy == 'notify' then vim.notify('No active session', vim.log.levels.WARN) end
206+
return
207+
end
208+
201209
local dir = tree_directions[direction]
202210
if dir then
203211
local target_id = dir.get_target(active)

tests/unit/commands_handlers_spec.lua

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -517,11 +517,15 @@ describe('opencode.commands.handlers', function()
517517
end)
518518

519519
-- normalize_navigate_args tests via command_defs
520-
it('normalize_navigate_args rejects invalid direction', function()
520+
it('non-direction string is treated as session ID, notifies when no active session', function()
521521
local session_handler = require('opencode.commands.handlers.session')
522-
local ok, err = pcall(session_handler.command_defs.navigate_session_tree.execute, { 'up' })
523-
assert.is_false(ok)
524-
assert.equal('invalid_arguments', err.code)
522+
local state = require('opencode.state')
523+
local notify_stub = stub(vim, 'notify')
524+
state.session.clear_active()
525+
local ok = pcall(session_handler.command_defs.navigate_session_tree.execute, { 'up' })
526+
assert.is_true(ok)
527+
assert.stub(notify_stub).was_called()
528+
notify_stub:revert()
525529
end)
526530

527531
it('normalize_navigate_args rejects invalid interaction', function()

tests/unit/formatter_spec.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -585,9 +585,9 @@ describe('formatter', function()
585585
end)
586586

587587
assert.are.same({
588-
text = '[S]elect Child Session',
588+
text = '[S] Open this Session',
589589
type = 'navigate_session_tree',
590-
args = { 'child', 'picker' },
590+
args = { 'ses_child' },
591591
key = 'S',
592592
display_line = 1,
593593
range = { from = 2, to = 5 },

0 commit comments

Comments
 (0)