|
| 1 | +local state = require('opencode.state') |
| 2 | +local config = require('opencode.config') |
| 3 | + |
| 4 | +describe('cursor persistence (state)', function() |
| 5 | + before_each(function() |
| 6 | + state.set_cursor_position('input', nil) |
| 7 | + state.set_cursor_position('output', nil) |
| 8 | + end) |
| 9 | + |
| 10 | + describe('set/get round-trip', function() |
| 11 | + it('stores and retrieves input cursor', function() |
| 12 | + state.set_cursor_position('input', { 5, 3 }) |
| 13 | + assert.same({ 5, 3 }, state.get_cursor_position('input')) |
| 14 | + end) |
| 15 | + |
| 16 | + it('stores and retrieves output cursor', function() |
| 17 | + state.set_cursor_position('output', { 10, 0 }) |
| 18 | + assert.same({ 10, 0 }, state.get_cursor_position('output')) |
| 19 | + end) |
| 20 | + |
| 21 | + it('input and output are independent', function() |
| 22 | + state.set_cursor_position('input', { 1, 0 }) |
| 23 | + state.set_cursor_position('output', { 99, 5 }) |
| 24 | + assert.same({ 1, 0 }, state.get_cursor_position('input')) |
| 25 | + assert.same({ 99, 5 }, state.get_cursor_position('output')) |
| 26 | + end) |
| 27 | + |
| 28 | + it('returns nil for unknown win_type', function() |
| 29 | + assert.is_nil(state.get_cursor_position('footer')) |
| 30 | + end) |
| 31 | + end) |
| 32 | + |
| 33 | + describe('normalize_cursor edge cases', function() |
| 34 | + it('clamps negative line to 1', function() |
| 35 | + state.set_cursor_position('input', { -5, 3 }) |
| 36 | + local pos = state.get_cursor_position('input') |
| 37 | + assert.equals(1, pos[1]) |
| 38 | + end) |
| 39 | + |
| 40 | + it('clamps negative col to 0', function() |
| 41 | + state.set_cursor_position('input', { 1, -1 }) |
| 42 | + local pos = state.get_cursor_position('input') |
| 43 | + assert.equals(0, pos[2]) |
| 44 | + end) |
| 45 | + |
| 46 | + it('floors fractional values', function() |
| 47 | + state.set_cursor_position('input', { 3.7, 2.9 }) |
| 48 | + local pos = state.get_cursor_position('input') |
| 49 | + assert.equals(3, pos[1]) |
| 50 | + assert.equals(2, pos[2]) |
| 51 | + end) |
| 52 | + |
| 53 | + it('rejects non-table input', function() |
| 54 | + state.set_cursor_position('input', 'bad') |
| 55 | + assert.is_nil(state.get_cursor_position('input')) |
| 56 | + end) |
| 57 | + |
| 58 | + it('rejects table with fewer than 2 elements', function() |
| 59 | + state.set_cursor_position('input', { 1 }) |
| 60 | + assert.is_nil(state.get_cursor_position('input')) |
| 61 | + end) |
| 62 | + |
| 63 | + it('rejects non-numeric elements', function() |
| 64 | + state.set_cursor_position('input', { 'a', 'b' }) |
| 65 | + assert.is_nil(state.get_cursor_position('input')) |
| 66 | + end) |
| 67 | + |
| 68 | + it('clears position when set to nil', function() |
| 69 | + state.set_cursor_position('input', { 5, 3 }) |
| 70 | + state.set_cursor_position('input', nil) |
| 71 | + assert.is_nil(state.get_cursor_position('input')) |
| 72 | + end) |
| 73 | + end) |
| 74 | + |
| 75 | + describe('save_cursor_position', function() |
| 76 | + it('returns nil for invalid window', function() |
| 77 | + assert.is_nil(state.save_cursor_position('input', nil)) |
| 78 | + assert.is_nil(state.save_cursor_position('input', 999999)) |
| 79 | + end) |
| 80 | + |
| 81 | + it('captures and persists from a real window', function() |
| 82 | + local buf = vim.api.nvim_create_buf(false, true) |
| 83 | + vim.api.nvim_buf_set_lines(buf, 0, -1, false, { 'line1', 'line2', 'line3' }) |
| 84 | + local win = vim.api.nvim_open_win(buf, true, { |
| 85 | + relative = 'editor', width = 40, height = 10, row = 0, col = 0, |
| 86 | + }) |
| 87 | + vim.api.nvim_win_set_cursor(win, { 2, 3 }) |
| 88 | + |
| 89 | + local saved = state.save_cursor_position('output', win) |
| 90 | + assert.same({ 2, 3 }, saved) |
| 91 | + assert.same({ 2, 3 }, state.get_cursor_position('output')) |
| 92 | + |
| 93 | + pcall(vim.api.nvim_win_close, win, true) |
| 94 | + pcall(vim.api.nvim_buf_delete, buf, { force = true }) |
| 95 | + end) |
| 96 | + end) |
| 97 | +end) |
| 98 | + |
| 99 | +describe('output_window.is_at_bottom', function() |
| 100 | + local output_window = require('opencode.ui.output_window') |
| 101 | + local buf, win |
| 102 | + |
| 103 | + before_each(function() |
| 104 | + config.setup({}) |
| 105 | + buf = vim.api.nvim_create_buf(false, true) |
| 106 | + local lines = {} |
| 107 | + for i = 1, 50 do |
| 108 | + lines[i] = 'line ' .. i |
| 109 | + end |
| 110 | + vim.api.nvim_buf_set_lines(buf, 0, -1, false, lines) |
| 111 | + |
| 112 | + win = vim.api.nvim_open_win(buf, true, { |
| 113 | + relative = 'editor', width = 80, height = 10, row = 0, col = 0, |
| 114 | + }) |
| 115 | + |
| 116 | + state.windows = { output_win = win, output_buf = buf } |
| 117 | + end) |
| 118 | + |
| 119 | + after_each(function() |
| 120 | + pcall(vim.api.nvim_win_close, win, true) |
| 121 | + pcall(vim.api.nvim_buf_delete, buf, { force = true }) |
| 122 | + state.windows = nil |
| 123 | + end) |
| 124 | + |
| 125 | + it('returns true when cursor is on last line', function() |
| 126 | + vim.api.nvim_win_set_cursor(win, { 50, 0 }) |
| 127 | + assert.is_true(output_window.is_at_bottom(win)) |
| 128 | + end) |
| 129 | + |
| 130 | + it('returns false when cursor is on second-to-last line', function() |
| 131 | + vim.api.nvim_win_set_cursor(win, { 49, 0 }) |
| 132 | + assert.is_false(output_window.is_at_bottom(win)) |
| 133 | + end) |
| 134 | + |
| 135 | + it('returns false when cursor is far from bottom', function() |
| 136 | + vim.api.nvim_win_set_cursor(win, { 1, 0 }) |
| 137 | + assert.is_false(output_window.is_at_bottom(win)) |
| 138 | + end) |
| 139 | + |
| 140 | + it('returns false when cursor is a few lines above bottom', function() |
| 141 | + vim.api.nvim_win_set_cursor(win, { 45, 0 }) |
| 142 | + assert.is_false(output_window.is_at_bottom(win)) |
| 143 | + end) |
| 144 | + |
| 145 | + it('returns true when always_scroll_to_bottom is enabled', function() |
| 146 | + config.values.ui.output.always_scroll_to_bottom = true |
| 147 | + vim.api.nvim_win_set_cursor(win, { 1, 0 }) |
| 148 | + assert.is_true(output_window.is_at_bottom(win)) |
| 149 | + config.values.ui.output.always_scroll_to_bottom = false |
| 150 | + end) |
| 151 | + |
| 152 | + it('returns true for invalid window', function() |
| 153 | + assert.is_true(output_window.is_at_bottom(999999)) |
| 154 | + end) |
| 155 | + |
| 156 | + it('returns true when no windows in state', function() |
| 157 | + state.windows = nil |
| 158 | + assert.is_true(output_window.is_at_bottom(win)) |
| 159 | + end) |
| 160 | + |
| 161 | + it('returns true for empty buffer', function() |
| 162 | + local empty_buf = vim.api.nvim_create_buf(false, true) |
| 163 | + local empty_win = vim.api.nvim_open_win(empty_buf, true, { |
| 164 | + relative = 'editor', width = 40, height = 5, row = 0, col = 0, |
| 165 | + }) |
| 166 | + state.windows = { output_win = empty_win, output_buf = empty_buf } |
| 167 | + |
| 168 | + assert.is_true(output_window.is_at_bottom(empty_win)) |
| 169 | + |
| 170 | + pcall(vim.api.nvim_win_close, empty_win, true) |
| 171 | + pcall(vim.api.nvim_buf_delete, empty_buf, { force = true }) |
| 172 | + end) |
| 173 | + |
| 174 | + it('cursor-based: scrolling viewport without moving cursor does NOT change result', function() |
| 175 | + vim.api.nvim_win_set_cursor(win, { 50, 0 }) |
| 176 | + assert.is_true(output_window.is_at_bottom(win)) |
| 177 | + |
| 178 | + -- Scroll viewport up via winrestview, cursor stays at line 50 |
| 179 | + pcall(vim.api.nvim_win_call, win, function() |
| 180 | + vim.fn.winrestview({ topline = 1 }) |
| 181 | + end) |
| 182 | + |
| 183 | + -- Cursor is still at 50, so is_at_bottom should still be true |
| 184 | + -- This is the key behavioral difference from viewport-based check |
| 185 | + assert.is_true(output_window.is_at_bottom(win)) |
| 186 | + end) |
| 187 | +end) |
0 commit comments