require_relative "test_helper"

class TriageCheckTest < Minitest::Test
  def test_card_missing_both_labels_needs_triage
    assert Backlog::TriageCheck.needs_triage?([])
  end

  def test_card_missing_priority_label_needs_triage
    assert Backlog::TriageCheck.needs_triage?(["semi"])
  end

  def test_card_missing_autonomy_label_needs_triage
    assert Backlog::TriageCheck.needs_triage?(["P1"])
  end

  def test_fully_labeled_card_does_not_need_triage
    refute Backlog::TriageCheck.needs_triage?(%w[P1 semi])
  end

  def test_note_for_no_unlabeled_cards_is_nil
    assert_nil Backlog::TriageCheck.note_for("cc-os", [%w[P1 semi], %w[P0 hitl]])
  end

  def test_note_for_counts_only_unlabeled_cards
    labels = [%w[P1 semi], [], ["P2"]]
    note = Backlog::TriageCheck.note_for("cc-os", labels)

    refute_nil note
    assert_includes note, "2 unlabeled Backlog cards on board cc-os"
    assert_includes note, "card-triage"
    assert_includes note, "haiku"
    assert_includes note, "background"
  end

  def test_note_for_singular_card_uses_singular_wording
    note = Backlog::TriageCheck.note_for("cc-os", [[]])

    assert_includes note, "1 unlabeled Backlog card on board cc-os"
    refute_includes note, "1 unlabeled Backlog cards"
  end

  def test_board_name_for_subdir_config_overrides_root_config
    name = Backlog::TriageCheck.board_name_for(
      cwd: "/home/jared/servers/proxmox", root: "/home/jared/servers",
      cwd_config: "tracker=planka:proxmox-board\n", root_config: "board=servers\n"
    )

    assert_equal "proxmox-board", name
  end

  def test_board_name_for_falls_back_to_root_config
    name = Backlog::TriageCheck.board_name_for(
      cwd: "/home/jared/dev/cc-os/plugins", root: "/home/jared/dev/cc-os",
      cwd_config: nil, root_config: "tracker=planka:cc-os\n"
    )

    assert_equal "cc-os", name
  end

  def test_board_name_for_subdir_config_without_board_names_the_subdir
    name = Backlog::TriageCheck.board_name_for(
      cwd: "/home/jared/servers/proxmox", root: "/home/jared/servers",
      cwd_config: "version=3\n", root_config: nil
    )

    assert_equal "proxmox", name
  end

  def test_board_name_for_no_config_uses_heuristic_repo_basename
    name = Backlog::TriageCheck.board_name_for(
      cwd: "/home/jared/dev/cc-os", root: "/home/jared/dev/cc-os",
      cwd_config: nil, root_config: nil
    )

    assert_equal "cc-os", name
  end

  def test_board_name_for_unrecognized_path_without_config_is_nil
    name = Backlog::TriageCheck.board_name_for(
      cwd: "/home/jared/scratch/x", root: "/home/jared/scratch/x",
      cwd_config: nil, root_config: nil
    )

    assert_nil name
  end
end
