Something went wrong. Try again.
A neovim plugin for OpenCode
Something went wrong. Try again.
2.8 kB · 85 lines
Lua
at master
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586local ui = require("brokencode.ui")local client = require("brokencode.client")
local source_win = vim.api.nvim_get_current_win()
ui.render({ { type = "user", text = "first line\nsecond line" },})
assert(vim.api.nvim_get_current_win() == source_win, "render changed the current window")
local lines = vim.api.nvim_buf_get_lines(ui.chat_buf, 0, -1, false)assert(vim.tbl_contains(lines, "first line"), vim.inspect(lines))assert(vim.tbl_contains(lines, "second line"), vim.inspect(lines))
local redraws = 0local cmd = vim.cmdvim.cmd = function(command) if command == "redraw" then redraws = redraws + 1 return end cmd(command)endui.render({ { type = "user", text = "redrawn" } })vim.cmd = cmdassert(redraws == 1, "render did not redraw the chat window")
local replacement = vim.api.nvim_create_buf(false, true)vim.api.nvim_win_set_buf(ui.chat_win, replacement)ui.render({ { type = "user", text = "restored" } })assert(vim.api.nvim_win_get_buf(ui.chat_win) == ui.chat_buf, "chat buffer was not restored")
ui.set_running("ses_test", true)lines = vim.api.nvim_buf_get_lines(ui.chat_buf, 0, -1, false)assert(vim.tbl_contains(lines, "> **Request running...**"), vim.inspect(lines))
ui.set_running("ses_test", false)lines = vim.api.nvim_buf_get_lines(ui.chat_buf, 0, -1, false)assert(not vim.tbl_contains(lines, "> **Request running...**"), vim.inspect(lines))
local callbacks = {}local messages = client.messagesclient.messages = function(_, callback) table.insert(callbacks, callback)end
ui.refresh("ses_test")ui.refresh("ses_test")callbacks[2](nil, { { type = "user", text = "new response" } })callbacks[1](nil, { { type = "user", text = "stale response" } })lines = vim.api.nvim_buf_get_lines(ui.chat_buf, 0, -1, false)assert(vim.tbl_contains(lines, "new response"), vim.inspect(lines))assert(not vim.tbl_contains(lines, "stale response"), vim.inspect(lines))
ui.set_running("ses_new", true)ui.set_running("ses_old", false)ui.refresh("ses_old")callbacks[3](nil, { { type = "user", text = "old session" } })assert(ui.current_session == "ses_new", "old polling replaced the active session")
client.messages = messages
local submittedui.input(function(text) submitted = text end)local input_buf = vim.api.nvim_get_current_buf()vim.api.nvim_buf_set_lines(input_buf, 0, -1, false, { "send me" })
local commands = {}local original_cmd = vim.cmdvim.cmd = function(command) table.insert(commands, command) original_cmd(command)end
for _, mapping in ipairs(vim.api.nvim_buf_get_keymap(input_buf, "i")) do if mapping.lhs == "<C-S>" then mapping.callback() break endendvim.cmd = original_cmd
assert(submitted == "send me", "Ctrl-S did not submit the message")assert(vim.tbl_contains(commands, "stopinsert"), "Ctrl-S did not return to normal mode")