require("plenary.async").tests.add_to_env()
local TmpDir = require("tests.tmpdir")
local oil = require("oil")
local test_util = require("tests.test_util")
local util = require("oil.util")

a.describe("oil preview", function()
  local tmpdir
  a.before_each(function()
    tmpdir = TmpDir.new()
  end)
  a.after_each(function()
    if tmpdir then
      tmpdir:dispose()
    end
    test_util.reset_editor()
  end)

  a.it("opens preview window", function()
    tmpdir:create({ "a.txt" })
    test_util.oil_open(tmpdir.path)
    a.wrap(oil.open_preview, 2)()
    local preview_win = util.get_preview_win()
    assert.not_nil(preview_win)
    assert(preview_win)
    local bufnr = vim.api.nvim_win_get_buf(preview_win)
    local preview_lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
    assert.are.same({ "a.txt" }, preview_lines)
  end)

  a.it("opens preview window when open(preview={})", function()
    tmpdir:create({ "a.txt" })
    test_util.oil_open(tmpdir.path, { preview = {} })
    local preview_win = util.get_preview_win()
    assert.not_nil(preview_win)
    assert(preview_win)
    local bufnr = vim.api.nvim_win_get_buf(preview_win)
    local preview_lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
    assert.are.same({ "a.txt" }, preview_lines)
  end)
end)
