summaryrefslogtreecommitdiff
path: root/lua/99/test/open_spec.lua
blob: a65d8e8367ec26fe566c2d1a4b49d25a8c35536e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
-- luacheck: globals describe it assert before_each after_each
local _99 = require("99")
local Window = require("99.window")
local test_utils = require("99.test.test_utils")
local QFixHelpers = require("99.ops.qfix-helpers")
local eq = assert.are.same

local function some_window_has(str)
  local wins = vim.api.nvim_list_wins()

  for _, winid in ipairs(wins) do
    local bufnr = vim.api.nvim_win_get_buf(winid)
    local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
    local content = table.concat(lines, "\n")
    if str == content then
      return
    end
  end
  assert(
    false,
    "unable to find buffer with the str contents from an active window"
  )
end

local function qfix_items()
  local items = vim.fn.getqflist()
  local out = {}
  for _, item in ipairs(items) do
    table.insert(out, {
      filename = vim.api.nvim_buf_get_name(item.bufnr),
      col = item.col,
      lnum = item.lnum,
      text = item.text,
    })
  end
  return out
end

describe("open", function()
  local provider
  local previous_capture_select_input

  before_each(function()
    provider = test_utils.TestProvider.new()
    _99.setup(test_utils.get_test_setup_options({}, provider))

    previous_capture_select_input = Window.capture_select_input
  end)

  after_each(function()
    Window.capture_select_input = previous_capture_select_input
  end)

  --- @param term "search" | "tutorial" | "vibe"
  ---@param result_str any
  local function op(term, result_str)
    _99[term]({ additional_prompt = result_str })
    local out = result_str
    provider:resolve("success", out)
    test_utils.next_frame()
    return out
  end

  local function search()
    return op("search", "/tmp/foo.lua:1:1,search note")
  end

  local function vibe()
    return op("vibe", "/tmp/bar.lua:2:2,search bar note")
  end

  local function tutorial()
    return op("tutorial", "here is a large tutorial")
  end

  local function select_content(idx)
    Window.capture_select_input = function(_, opts)
      opts.cb(true, opts.content[idx])
    end
  end

  it("selects a previous search and passes edited output to vibe", function()
    local s = search()
    local v = vibe()
    local t = tutorial()

    select_content(1)
    _99.open()
    eq(QFixHelpers.create_qfix_entries(s), qfix_items())

    select_content(2)
    _99.open()
    eq(QFixHelpers.create_qfix_entries(v), qfix_items())

    select_content(3)
    _99.open()
    some_window_has(t)
  end)
end)