diff options
Diffstat (limited to 'lua/99/test')
| -rw-r--r-- | lua/99/test/in_flight_spec.lua | 71 | ||||
| -rw-r--r-- | lua/99/test/test_utils.lua | 36 |
2 files changed, 98 insertions, 9 deletions
diff --git a/lua/99/test/in_flight_spec.lua b/lua/99/test/in_flight_spec.lua new file mode 100644 index 0000000..c9ebc6b --- /dev/null +++ b/lua/99/test/in_flight_spec.lua @@ -0,0 +1,71 @@ +-- luacheck: globals describe it assert after_each +local _99 = require("99") +local Prompt = require("99.prompt") +local Window = require("99.window") +local test_utils = require("99.test.test_utils") +local eq = assert.are.same + +local content = { + "local function foo()", + " return 1", + "end", +} + +--- You have to override this or else things will crash since the ui itself +--- does not exist. this is a headless test so i fake it by returning a very +--- simple ui of 120x40 +local original_nvim_list_uis = vim.api.nvim_list_uis +local function nvim_list_uis() + return { + { width = 120, height = 40 }, + } +end + +describe("in_flight window", function() + local WAIT_TIME = 10 + before_each(function() + vim.api.nvim_list_uis = nvim_list_uis + end) + after_each(function() + vim.api.nvim_list_uis = original_nvim_list_uis + end) + it("shows active requests and clears when request completes", function() + local provider = test_utils.test_setup(content, 2, 4) + local state = _99.__get_state() + local context = Prompt.search(state) + + context:start_request() + vim.wait(WAIT_TIME * 2, function() end) + + eq(1, #Window.active_windows) + + local win = Window.active_windows[1] + vim.api.nvim_win_close(win.win_id, true) + + vim.wait(WAIT_TIME * 2, function() end) + local next_win = Window.active_windows[1] + + eq(true, win.win_id ~= next_win.win_id) + + provider:resolve("success", "results are in") + + vim.wait(WAIT_TIME * 2, function() end) + eq(0, #Window.active_windows) + end) + + it("enable false == do not show in flight", function() + local provider = test_utils.test_setup(content, 2, 4, "lua", { + in_flight_options = { enable = false }, + }) + local state = _99.__get_state() + local context = Prompt.search(state) + + context:start_request() + vim.wait(WAIT_TIME * 2, function() end) + + eq(0, #Window.active_windows) + provider:resolve("success", "results are in") + vim.wait(WAIT_TIME * 2, function() end) + eq(0, #Window.active_windows) + end) +end) diff --git a/lua/99/test/test_utils.lua b/lua/99/test/test_utils.lua index e5484f1..28fe1e4 100644 --- a/lua/99/test/test_utils.lua +++ b/lua/99/test/test_utils.lua @@ -108,21 +108,39 @@ function M.create_file(contents, file_type, row, col) return bufnr end +--- @param opts _99.Options | nil +--- @param provider _99.Providers.BaseProvider +--- @return _99.Options +function M.get_test_setup_options(opts, provider) + opts = opts or {} + opts.provider = provider + opts.logger = { + error_cache_level = Levels.ERROR, + type = "print", + } + opts.in_flight_options = opts.in_flight_options + or { + throbber_opts = { + tick_time = 10, + throb_time = 1000, + cooldown_time = 500, + }, + in_flight_interval = 10, + enable = true, + } + return opts +end + --- @param content string[] --- @param row number --- @param col number --- @param lang string? +--- @param opts _99.Options | nil --- @return _99.test.Provider, number -function M.test_setup(content, row, col, lang) - assert(lang, "lang must be provided") +function M.test_setup(content, row, col, lang, opts) + lang = lang or "lua" local provider = M.TestProvider.new() - require("99").setup({ - provider = provider, - logger = { - error_cache_level = Levels.ERROR, - type = "print", - }, - }) + require("99").setup(M.get_test_setup_options(opts, provider)) local buffer = M.create_file(content, lang, row, col) return provider, buffer |
