local load_fixture = require "tests.load_fixture"
local tw = require 'treewalker'
local h = require 'tests.treewalker.helpers'

describe("Movement in a YAML file:", function()
  before_each(function()
    load_fixture("/yaml.yml")
  end)

  h.ensure_has_parser("yaml")

  it("moves in", function()
    vim.fn.cursor(1, 1)
    tw.move_in()
    h.assert_cursor_at(2, 3)
    tw.move_in()
    h.assert_cursor_at(7, 5)
  end)

  it("moves down on keys", function()
    vim.fn.cursor(2, 3)
    tw.move_down()
    h.assert_cursor_at(3, 3)
    tw.move_down()
    h.assert_cursor_at(4, 3)
    tw.move_down()
    h.assert_cursor_at(5, 3)
    tw.move_down()
    h.assert_cursor_at(6, 3)
    tw.move_down()
    h.assert_cursor_at(10, 3)
    tw.move_down()
    h.assert_cursor_at(19, 3)
    tw.move_down()
    h.assert_cursor_at(23, 3)
    tw.move_down()
    h.assert_cursor_at(26, 3)
    tw.move_down()
    h.assert_cursor_at(27, 3)
    tw.move_down()
    h.assert_cursor_at(28, 3)
    tw.move_down()
    h.assert_cursor_at(29, 3)
    tw.move_down()
    h.assert_cursor_at(35, 3)
    tw.move_down()
    h.assert_cursor_at(43, 3)
  end)

  it("moves up on keys", function()
    vim.fn.cursor(35, 3)
    tw.move_up()
    h.assert_cursor_at(29, 3)
    tw.move_up()
    h.assert_cursor_at(28, 3)
    tw.move_up()
    h.assert_cursor_at(27, 3)
    tw.move_up()
    h.assert_cursor_at(26, 3)
    tw.move_up()
    h.assert_cursor_at(23, 3)
    tw.move_up()
    h.assert_cursor_at(19, 3)
    tw.move_up()
    h.assert_cursor_at(10, 3)
    tw.move_up()
    h.assert_cursor_at(6, 3)
    tw.move_up()
    h.assert_cursor_at(5, 3)
    tw.move_up()
    h.assert_cursor_at(4, 3)
    tw.move_up()
    h.assert_cursor_at(3, 3)
    tw.move_up()
    h.assert_cursor_at(2, 3)
  end)

  it("moves out within just keys", function()
    vim.fn.cursor(30, 5) -- red: null
    tw.move_out()
    h.assert_cursor_at(29, 3)
    tw.move_out()
    h.assert_cursor_at(1, 1)
  end)

  it("moves out within arrays", function()
    vim.fn.cursor(41, 9) -- theme: dark
    tw.move_out()
    h.assert_cursor_at(40, 7)
    tw.move_out()
    h.assert_cursor_at(38, 5)
    tw.move_out()
    h.assert_cursor_at(35, 3)
    tw.move_out()
    h.assert_cursor_at(34, 1)
  end)

  it("moves down in array", function()
    vim.fn.cursor(7, 5) -- - authentication
    tw.move_down()
    h.assert_cursor_at(8, 5)
    tw.move_down()
    h.assert_cursor_at(9, 5)
  end)

  it("moves up in array", function()
    vim.fn.cursor(9, 5) -- - notifications
    tw.move_up()
    h.assert_cursor_at(8, 5)
    tw.move_up()
    h.assert_cursor_at(7, 5)
  end)

  it("navigates mixedList part", function()
    vim.fn.cursor(72, 1) -- mixedList:
    tw.move_in()
    h.assert_cursor_at(73, 3)
    tw.move_down()
    h.assert_cursor_at(74, 3)
    tw.move_in()
    h.assert_cursor_at(78, 5)
    tw.move_out()
    h.assert_cursor_at(77, 3)
    tw.move_out()
    h.assert_cursor_at(72, 1)
  end)

  it("navigates complexKeys part", function()
    vim.fn.cursor(49, 1) -- complexKeys:
    tw.move_in()
    h.assert_cursor_at(50, 3)
    tw.move_down()
    h.assert_cursor_at(52, 3)
    tw.move_in()
    h.assert_cursor_at(55, 5)
    tw.move_out()
    h.assert_cursor_at(54, 3)
    tw.move_out()
    h.assert_cursor_at(49, 1)
  end)
end)
