defmodule BerrypodWeb.Plugs.BrokenUrlTrackerTest do
  use BerrypodWeb.ConnCase, async: true

  alias Berrypod.Redirects

  setup do
    Redirects.create_table()
    :ok
  end

  test "records broken URL on 404", %{conn: conn} do
    # Multi-segment path — not caught by the /:slug catch-all route
    conn = get(conn, "/zz/nonexistent-path")

    assert conn.status in [404, 500]

    [broken_url] = Redirects.list_broken_urls()
    assert broken_url.path == "/zz/nonexistent-path"
    assert broken_url.recent_404_count == 1
  end

  test "skips static asset paths", %{conn: conn} do
    get(conn, "/assets/missing-file.js")

    assert Redirects.list_broken_urls() == []
  end
end
