defmodule MicrowavepropWeb.PageControllerTest do
  use MicrowavepropWeb.ConnCase

  test "GET / redirects to /map", %{conn: conn} do
    conn = get(conn, ~p"/")
    assert redirected_to(conn) == "/map"
  end

  # RFC 8288 Link headers for agent discovery. These advertise resources
  # that actually exist in this app to crawlers/agents that look at
  # response headers before parsing HTML.
  describe "Link headers (RFC 8288)" do
    test "homepage response carries Link headers", %{conn: conn} do
      conn = get(conn, ~p"/")
      [link] = get_resp_header(conn, "link")

      assert link =~ ~s(</algo>; rel="service-doc")
      assert link =~ ~s(</about>; rel="about")
      assert link =~ ~s(</privacy>; rel="privacy-policy")
      assert link =~ ~s(</sitemap.xml>; rel="sitemap")
    end

    test "browser-pipeline pages carry the same Link headers", %{conn: conn} do
      conn = get(conn, ~p"/about")
      [link] = get_resp_header(conn, "link")
      assert link =~ ~s(rel="service-doc")
      assert link =~ ~s(rel="about")
    end
  end

  # Markdown for Agents: content negotiation so agents that send
  # `Accept: text/markdown` get a real markdown document instead of a 406.
  # We only serve markdown for paths where we actually have markdown source
  # (the homepage summary and /algo). Other paths still 406 rather than lie
  # with a converted-on-the-fly body.
  describe "Markdown for Agents" do
    test "GET / with Accept: text/markdown returns a markdown document", %{conn: conn} do
      conn =
        conn
        |> put_req_header("accept", "text/markdown")
        |> get(~p"/")

      assert conn.status == 200
      assert ["text/markdown; charset=utf-8"] = get_resp_header(conn, "content-type")
      [tokens] = get_resp_header(conn, "x-markdown-tokens")
      assert String.to_integer(tokens) > 0
      assert conn.resp_body =~ "# "
    end

    test "GET /algo with Accept: text/markdown returns algo.md", %{conn: conn} do
      conn =
        conn
        |> put_req_header("accept", "text/markdown")
        |> get(~p"/algo")

      assert conn.status == 200
      assert ["text/markdown; charset=utf-8"] = get_resp_header(conn, "content-type")
      assert conn.resp_body =~ "Microwave Propagation Algorithm"
    end

    test "GET / without markdown Accept still redirects to /map", %{conn: conn} do
      conn = get(conn, ~p"/")
      assert redirected_to(conn) == "/map"
    end

    test "Accept lists with both html and markdown prefer markdown when specified", %{conn: conn} do
      conn =
        conn
        |> put_req_header("accept", "text/markdown, text/html;q=0.9")
        |> get(~p"/")

      assert ["text/markdown; charset=utf-8"] = get_resp_header(conn, "content-type")
    end
  end
end
