local 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 = 0 local cmd = vim.cmd vim.cmd = function(command) if command == "redraw" then redraws = redraws + 1 return end cmd(command) end ui.render({ { type = "user", text = "redrawn" } }) vim.cmd = cmd assert(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.messages client.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 submitted ui.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.cmd vim.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 == "" then mapping.callback() break end end vim.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")