require_relative "test_helper"

class ConfigTest < Minitest::Test
  def test_nil_contents_has_no_tracker
    config = Backlog::Config.new(nil)
    refute config.tracker_configured?
    assert_nil config.tracker
  end

  def test_reads_legacy_yaml_style_key
    config = Backlog::Config.new("---\ntracker: forgejo:jared/cc-os\n")
    assert config.tracker_configured?
    assert_equal "forgejo:jared/cc-os", config.tracker
  end

  def test_lines_without_separator_are_ignored
    config = Backlog::Config.new("# comment\njunk line\n")
    assert_empty config.to_h
  end

  def test_reads_tracker_key
    config = Backlog::Config.new("tracker=forgejo:jared/cc-os\n")
    assert config.tracker_configured?
    assert_equal "forgejo:jared/cc-os", config.tracker
  end

  def test_tracker_unconfigured_when_absent
    config = Backlog::Config.new("other=value\n")
    refute config.tracker_configured?
  end

  def test_merge_sets_new_key_on_nil_contents
    contents = Backlog::Config.merge(nil, "tracker", "forgejo:jared/cc-os")
    assert_equal "tracker=forgejo:jared/cc-os\n", contents
  end

  def test_merge_preserves_other_keys
    existing = "board=llf-schema\nother=kept\n"
    updated_contents = Backlog::Config.merge(existing, "tracker", "forgejo:jared/cc-os")
    updated = Backlog::Config.new(updated_contents)

    assert_equal "llf-schema", updated.to_h["board"]
    assert_equal "forgejo:jared/cc-os", updated.tracker
    assert_equal "kept", updated.to_h["other"]
  end

  def test_merge_overwrites_existing_value_for_key
    existing = "tracker=repo:old\n"
    updated = Backlog::Config.new(Backlog::Config.merge(existing, "tracker", "repo:new"))
    assert_equal "repo:new", updated.tracker
  end

  def test_reads_spaced_key_value_lines
    config = Backlog::Config.new("version = 1\nhub = my-hub\n")
    assert_equal "1", config.to_h["version"]
    assert_equal "my-hub", config.to_h["hub"]
  end

  def test_merge_output_has_no_spaces_around_equals
    contents = Backlog::Config.merge(nil, "tracker", "forgejo:jared/cc-os")
    assert_equal "tracker=forgejo:jared/cc-os\n", contents
    refute_includes contents, " = "
    refute_includes contents, "= "
    refute_includes contents, " ="
  end

  def test_merge_normalizes_existing_spaced_lines_on_rewrite
    existing = "version = 1\nhub = my-hub\n"
    updated_contents = Backlog::Config.merge(existing, "tracker", "forgejo:jared/demo")
    updated_contents.each_line do |line|
      next if line.strip.empty?

      refute_match(/\s=\s|\s=|=\s/, line, "expected normalized key=value, got #{line.inspect}")
    end
    updated = Backlog::Config.new(updated_contents)
    assert_equal "1", updated.to_h["version"]
    assert_equal "my-hub", updated.to_h["hub"]
    assert_equal "forgejo:jared/demo", updated.tracker
  end

  def test_board_id_reader
    config = Backlog::Config.new("tracker=forgejo:jared/demo\nboard_id=42\n")
    assert_equal "42", config.board_id
    assert_nil Backlog::Config.new(nil).board_id
  end

  def test_set_preserves_comments_and_blank_lines
    existing = "# managed by cc-os\ntracker=forgejo:jared/demo\n\n# stamped by os-status\nversion=3\n"
    updated = Backlog::Config.set(existing, "board_id", "42")
    assert_equal "# managed by cc-os\ntracker=forgejo:jared/demo\n\n# stamped by os-status\nversion=3\nboard_id=42\n",
                 updated
  end

  def test_set_replaces_existing_key_in_place
    existing = "# top comment\nboard_id=old\ntracker=forgejo:jared/demo\n"
    updated = Backlog::Config.set(existing, "board_id", "new")
    assert_equal "# top comment\nboard_id=new\ntracker=forgejo:jared/demo\n", updated
  end

  def test_set_normalizes_spaced_key_value_lines_only
    existing = "# note\nversion = 1\n"
    updated = Backlog::Config.set(existing, "board_id", "42")
    assert_equal "# note\nversion=1\nboard_id=42\n", updated
  end

  def test_set_on_nil_contents
    assert_equal "board_id=42\n", Backlog::Config.set(nil, "board_id", "42")
  end
end
