defmodule TarakanWeb.ModelAnalyticsLiveTest do
  use TarakanWeb.ConnCase, async: true

  import Phoenix.LiveViewTest

  alias Tarakan.Repo
  alias Tarakan.Scans.CanonicalFinding

  defp confirm_findings(scan) do
    for occurrence <- scan.findings do
      CanonicalFinding
      |> Repo.get!(occurrence.canonical_finding_id)
      |> Ecto.Changeset.change(status: "verified")
      |> Repo.update!()
    end
  end

  test "says so plainly when nothing has been reported", %{conn: conn} do
    {:ok, _view, html} = live(conn, ~p"/models")

    assert html =~ "No model has reported enough findings yet"
  end

  # The scoreboard hides models below five reported findings, so both models
  # need a real body of work before they show up at all.
  defp unrelated_findings_json(count) do
    findings =
      for index <- 1..count do
        %{
          "file" => "lib/unrelated/module_#{index}.ex",
          "line_start" => index * 7,
          "line_end" => index * 7 + 2,
          "severity" => "low",
          "title" => "Unrelated observation (#{index})",
          "description" => "Something else entirely."
        }
      end

    Jason.encode!(%{"tarakan_scan_format" => 1, "findings" => findings})
  end

  test "scores models and lists the selected model's blind spots", %{conn: conn} do
    account = github_account_fixture()
    repository = github_repository_fixture(account)

    found =
      scan_fixture(repository, account, %{
        "model" => "sharp-model",
        "findings_json" => findings_json_fixture(5)
      })

    confirm_findings(found)

    scan_fixture(repository, account, %{
      "model" => "blunt-model",
      "findings_json" => unrelated_findings_json(5)
    })

    {:ok, view, html} = live(conn, ~p"/models")

    assert html =~ "sharp-model"
    assert html =~ "blunt-model"

    html =
      view
      |> element("#model-row-blunt-model")
      |> render_click()

    assert html =~ "Blind spots"
    assert html =~ "Unsanitized input reaches interpolated query"
  end
end
