require_relative "test_helper"

class TrackerTest < Minitest::Test
  def test_rejects_planka_format
    refute Backlog::Tracker.valid?("planka:llf-schema")
    assert_nil Backlog::Tracker.kind("planka:llf-schema")
  end

  def test_accepts_forgejo_format
    assert Backlog::Tracker.valid?("forgejo:jared/cc-os")
    assert_equal :forgejo, Backlog::Tracker.kind("forgejo:jared/cc-os")
  end

  def test_accepts_github_format
    assert Backlog::Tracker.valid?("github:acme/widget")
    assert_equal :github, Backlog::Tracker.kind("github:acme/widget")
  end

  def test_accepts_repo_format
    assert Backlog::Tracker.valid?("repo:docs/issues")
    assert_equal :repo, Backlog::Tracker.kind("repo:docs/issues")
  end

  def test_rejects_garbage
    refute Backlog::Tracker.valid?("just some text")
    refute Backlog::Tracker.valid?("forgejo:missing-slash")
    refute Backlog::Tracker.valid?("")
    refute Backlog::Tracker.valid?(nil)
  end

  def test_rejects_unknown_prefix
    refute Backlog::Tracker.valid?("jira:PROJ-123")
  end

  def test_issues_destination_routes_git_hosts_to_git_issues
    assert_equal :git_issues, Backlog::Tracker.issues_destination("forgejo:jared/cc-os")
    assert_equal :git_issues, Backlog::Tracker.issues_destination("github:jared/cc-os")
  end

  def test_issues_destination_routes_repo_to_repo_files
    assert_equal :repo_files, Backlog::Tracker.issues_destination("repo:docs/issues")
  end

  def test_issues_destination_is_unrouted_for_planka_nil_or_invalid
    assert_equal :unrouted, Backlog::Tracker.issues_destination("planka:cc-os")
    assert_equal :unrouted, Backlog::Tracker.issues_destination(nil)
    assert_equal :unrouted, Backlog::Tracker.issues_destination("jira:whatever")
  end

  def test_parse_remote_detects_forgejo_ssh_url
    output = "origin\tssh://git@forgejo.swansoncloud.com:2222/jared/cc-os.git (fetch)\n" \
             "origin\tssh://git@forgejo.swansoncloud.com:2222/jared/cc-os.git (push)\n"
    info = Backlog::Tracker.parse_remote(output)
    assert_equal :forgejo, info[:kind]
    assert_equal "jared", info[:owner]
    assert_equal "cc-os", info[:repo]
  end

  def test_parse_remote_detects_github_https_url
    output = "origin\thttps://github.com/acme/widget.git (fetch)\n"
    info = Backlog::Tracker.parse_remote(output)
    assert_equal :github, info[:kind]
    assert_equal "acme", info[:owner]
    assert_equal "widget", info[:repo]
  end

  def test_parse_remote_detects_github_scp_url
    output = "origin\tgit@github.com:acme/widget.git (fetch)\n"
    info = Backlog::Tracker.parse_remote(output)
    assert_equal :github, info[:kind]
    assert_equal "acme", info[:owner]
    assert_equal "widget", info[:repo]
  end

  def test_parse_remote_classifies_other_hosts_as_unknown
    output = "origin\thttps://gitlab.com/acme/widget.git (fetch)\n"
    info = Backlog::Tracker.parse_remote(output)
    assert_equal :unknown, info[:kind]
  end

  def test_parse_remote_returns_nil_for_blank_output
    assert_nil Backlog::Tracker.parse_remote("")
    assert_nil Backlog::Tracker.parse_remote(nil)
  end
end
