Skip to content

Commit cc1874b

Browse files
committed
test(replay): share setup, fix diagnostics
1 parent 780325e commit cc1874b

3 files changed

Lines changed: 47 additions & 44 deletions

File tree

tests/helpers.lua

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,38 @@
33

44
local M = {}
55

6+
function M.replay_setup()
7+
local config = require('opencode.config')
8+
local config_file = require('opencode.config_file')
9+
local state = require('opencode.state')
10+
local ui = require('opencode.ui.ui')
11+
local renderer = require('opencode.ui.renderer')
12+
13+
local empty_promise = require('opencode.promise').new():resolve(nil)
14+
config_file.config_promise = empty_promise
15+
config_file.project_promise = empty_promise
16+
config_file.providers_promise = empty_promise
17+
18+
state.windows = ui.create_windows()
19+
20+
-- we don't change any changes on session
21+
renderer._cleanup_subscriptions()
22+
renderer.reset()
23+
24+
if not config.config then
25+
config.config = vim.deepcopy(config.defaults)
26+
end
27+
end
28+
629
-- Create a temporary file with content
730
function M.create_temp_file(content)
831
local tmp_file = vim.fn.tempname()
932
local file = io.open(tmp_file, 'w')
33+
34+
if not file then
35+
return nil
36+
end
37+
1038
file:write(content or 'Test file content')
1139
file:close()
1240
return tmp_file
@@ -26,7 +54,7 @@ end
2654
-- Close a buffer
2755
function M.close_buffer(bufnr)
2856
if bufnr and vim.api.nvim_buf_is_valid(bufnr) then
29-
pcall(vim.cmd, 'bdelete! ' .. bufnr)
57+
pcall(vim.api.nvim_command, 'bdelete! ' .. bufnr)
3058
end
3159
end
3260

@@ -42,17 +70,18 @@ function M.reset_editor()
4270
for _, bufnr in ipairs(vim.api.nvim_list_bufs()) do
4371
-- Skip non-existing or invalid buffers
4472
if vim.api.nvim_buf_is_valid(bufnr) then
45-
pcall(vim.cmd, 'bdelete! ' .. bufnr)
73+
pcall(vim.api.nvim_command, 'bdelete! ' .. bufnr)
4674
end
4775
end
4876
-- Reset any other editor state as needed
49-
pcall(vim.cmd, 'silent! %bwipeout!')
77+
pcall(vim.api.nvim_command, 'silent! %bwipeout!')
5078
end
5179

5280
-- Mock input function
5381
function M.mock_input(return_value)
5482
local original_input = vim.fn.input
55-
vim.fn.input = function(...)
83+
---@diagnostic disable-next-line: duplicate-set-field
84+
vim.fn.input = function(_)
5685
return return_value
5786
end
5887
return function()
@@ -65,6 +94,7 @@ function M.mock_notify()
6594
local notifications = {}
6695
local original_notify = vim.notify
6796

97+
---@diagnostic disable-next-line: duplicate-set-field
6898
vim.notify = function(msg, level, opts)
6999
table.insert(notifications, {
70100
msg = msg,
@@ -90,6 +120,7 @@ function M.mock_time_ago()
90120
local util = require('opencode.util')
91121
local original_time_ago = util.time_ago
92122

123+
---@diagnostic disable-next-line: duplicate-set-field
93124
util.time_ago = function(timestamp)
94125
if timestamp > 1e12 then
95126
timestamp = math.floor(timestamp / 1000)

tests/manual/renderer_replay.lua

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -44,25 +44,7 @@ function M.load_events(file_path)
4444
end
4545

4646
function M.setup_windows(opts)
47-
renderer.reset()
48-
49-
M.restore_time_ago = helpers.mock_time_ago()
50-
51-
local config = require('opencode.config')
52-
if not config.config then
53-
config.config = vim.deepcopy(config.defaults)
54-
end
55-
56-
-- disable the config_file apis because topbar uses them
57-
local empty_promise = require('opencode.promise').new():resolve(nil)
58-
config_file.config_promise = empty_promise
59-
config_file.project_promise = empty_promise
60-
config_file.providers_promise = empty_promise
61-
62-
state.windows = ui.create_windows()
63-
64-
-- we don't want output_renderer responding to setting the session id
65-
require('opencode.ui.output_renderer')._cleanup_subscriptions()
47+
helpers.replay_setup()
6648

6749
vim.schedule(function()
6850
if state.windows and state.windows.output_win then
@@ -199,6 +181,11 @@ function M.save_output(filename)
199181
end
200182

201183
local buf = state.windows.output_buf
184+
185+
if not buf then
186+
return nil
187+
end
188+
202189
local lines = vim.api.nvim_buf_get_lines(buf, 0, -1, false)
203190
local extmarks = vim.api.nvim_buf_get_extmarks(buf, output_window.namespace, 0, -1, { details = true })
204191

@@ -232,6 +219,10 @@ function M.dump_buffer_and_quit()
232219
end
233220

234221
local buf = state.windows.output_buf
222+
if not buf then
223+
return
224+
end
225+
235226
local lines = vim.api.nvim_buf_get_lines(buf, 0, -1, false)
236227
local extmarks = vim.api.nvim_buf_get_extmarks(buf, renderer._namespace, 0, -1, { details = true })
237228

@@ -242,7 +233,7 @@ function M.dump_buffer_and_quit()
242233
extmarks_by_line[line] = {}
243234
end
244235
local details = mark[4]
245-
if details.virt_text then
236+
if details and details.virt_text then
246237
for _, vt in ipairs(details.virt_text) do
247238
table.insert(extmarks_by_line[line], vt[1])
248239
end

tests/unit/renderer_spec.lua

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,14 @@
1-
local renderer = require('opencode.ui.renderer')
21
local state = require('opencode.state')
32
local ui = require('opencode.ui.ui')
43
local helpers = require('tests.helpers')
5-
local output_renderer = require('opencode.ui.output_renderer')
64
local output_window = require('opencode.ui.output_window')
7-
local config_file = require('opencode.config_file')
85

96
describe('renderer', function()
107
local restore_time_ago
118

129
before_each(function()
13-
renderer.reset()
14-
15-
local empty_promise = require('opencode.promise').new():resolve(nil)
16-
config_file.config_promise = empty_promise
17-
config_file.project_promise = empty_promise
18-
config_file.providers_promise = empty_promise
19-
20-
state.windows = ui.create_windows()
21-
22-
-- FIXME: prolly not necessary any more?
23-
output_renderer._cleanup_subscriptions()
24-
10+
helpers.replay_setup()
2511
restore_time_ago = helpers.mock_time_ago()
26-
27-
local config = require('opencode.config')
28-
if not config.config then
29-
config.config = vim.deepcopy(config.defaults)
30-
end
3112
end)
3213

3314
after_each(function()

0 commit comments

Comments
 (0)