require_relative "test_helper"

class RecordTest < Minitest::Test
  include AdrTestHelpers

  def test_parses_frontmatter_and_sections
    record = Adr::Record.parse(sample_adr)
    assert_equal "0001", record.id
    assert_equal "Accepted", record.status
    assert_equal Date.new(2026, 7, 3), record.date
    assert_equal "Use Postgres", record.title
    assert_equal ["db/", "lib/storage"], record.frontmatter["affected-paths"]
    assert_equal "We need a database.", record.sections["Context"]
    assert_equal "SQLite — no concurrent writers.", record.sections["Alternatives rejected"]
  end

  def test_id_with_leading_zeros_stays_a_string
    record = Adr::Record.parse(sample_adr(id: "0007"))
    assert_equal "0007", record.id
  end

  def test_round_trip_preserves_content
    record = Adr::Record.parse(sample_adr)
    reparsed = Adr::Record.parse(record.to_markdown)
    assert_equal record.frontmatter, reparsed.frontmatter
    assert_equal record.title, reparsed.title
    assert_equal record.sections, reparsed.sections
  end

  def test_supersede_with_sets_both_fields
    record = Adr::Record.parse(sample_adr)
    record.supersede_with("0009")
    assert_equal "0009", record.frontmatter["superseded-by"]
    assert_equal "Superseded", record.status
    reparsed = Adr::Record.parse(record.to_markdown)
    assert_equal "0009", reparsed.frontmatter["superseded-by"]
    assert_equal "Superseded", reparsed.status
  end

  def test_raises_on_missing_frontmatter
    assert_raises(Adr::Record::ParseError) { Adr::Record.parse("# just a heading\n") }
  end

  def test_preserves_unknown_frontmatter_fields
    record = Adr::Record.parse(sample_adr(extra_frontmatter: "migration_confidence: low\n"))
    assert_equal "low", record.frontmatter["migration_confidence"]
    assert_includes record.to_markdown, "migration_confidence: low"
  end
end
