summaryrefslogtreecommitdiff
path: root/lua/99/test/worker_spec.lua
blob: 02e78b0a205d00f9e02f2ead22c15dd51dfc76b0 (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
-- luacheck: globals describe it assert before_each after_each
local _99 = require("99")
local Window = require("99.window")
local Worker = require("99.extensions.work.worker")
local test_utils = require("99.test.test_utils")
local utils = require("99.utils")
local eq = assert.are.same

describe("worker", function()
  local previous_capture_input
  local captured_content
  local tmp_dir

  before_each(function()
    tmp_dir = vim.fn.tempname()
    vim.fn.mkdir(tmp_dir, "p")

    local provider = test_utils.TestProvider.new()
    _99.setup(
      test_utils.get_test_setup_options({ tmp_dir = tmp_dir }, provider)
    )

    Worker.current_work_item = nil
    Worker.last_work_search = nil

    captured_content = nil
    previous_capture_input = Window.capture_input
    Window.capture_input = function(_, opts)
      captured_content = opts.content
    end
  end)

  after_each(function()
    Window.capture_input = previous_capture_input
    Worker.current_work_item = nil
    Worker.last_work_search = nil

    if tmp_dir then
      vim.fn.delete(tmp_dir, "rf")
    end
  end)

  it("set_work preloads existing persisted work", function()
    local work_path = utils.named_tmp_file(tmp_dir, "work-item")
    local file = assert(io.open(work_path, "w"))
    file:write("fix flaky tests")
    file:close()

    Worker.set_work()

    eq({ "fix flaky tests" }, captured_content)
  end)

  it("set_work shows default when no persisted work exists", function()
    Worker.set_work()

    eq(
      { "Put in the description of the work you want to complete" },
      captured_content
    )
  end)
end)