3.8 KiB · text History a625083
defmodule GitGud.Packages.AboutTest do
  @moduledoc """
  What a package says it is: explicit first, then the artifact's own
  words, and never the name said twice.
  """
  use GitGud.DataCase, async: true

  import GitGud.ForgeFixtures

  alias GitGud.Packages
  alias GitGud.Packages.About

  setup do
    {_user, repo} = repository_fixture()
    %{repo: repo}
  end

  defp publish(repo, format, name, attrs \\ %{}) do
    {:ok, package, _v, _f} =
      Packages.publish_file(
        repo,
        format,
        name,
        "1.0.0",
        Map.merge(
          %{filename: "#{name}-1.0.0.bin", body: "#{format} #{name}", metadata: %{}},
          attrs
        )
      )

    package
  end

  test "a wheel's own Summary", %{repo: repo} do
    package =
      publish(repo, "pypi", "widget", %{version_metadata: %{"summary" => "Makes widgets"}})

    assert %{title: "widget", summary: "Makes widgets", description: nil} =
             About.for_package(package)
  end

  test "a .deb's synopsis and extended description", %{repo: repo} do
    control = %{"Description" => "The forge\n Serves git.\n .\n And packages."}
    package = publish(repo, "deb", "gitgud", %{version_metadata: %{"control" => control}})

    about = About.for_package(package)
    assert about.summary == "The forge"
    assert about.description == "Serves git.\n\nAnd packages."
  end

  test "explicit wins over the artifact", %{repo: repo} do
    package =
      publish(repo, "pypi", "widget", %{
        version_metadata: %{"summary" => "Makes widgets"},
        about: %{"summary" => "The widget library"}
      })

    assert About.for_package(package).summary == "The widget library"
  end

  test "an APK's label is its title, and its icon is noticed", %{repo: repo} do
    package =
      publish(repo, "fdroid", "org.example.app", %{
        metadata: %{"label" => "Example", "icon_key" => "fdroid-icons/abc"}
      })

    assert %{title: "Example", icon?: true, summary: nil} = About.for_package(package)
  end

  test "a summary that only repeats the title is dropped", %{repo: repo} do
    package =
      publish(repo, "fdroid", "org.example.app", %{
        metadata: %{"label" => "Example"},
        about: %{"summary" => "Example."}
      })

    assert About.for_package(package).summary == nil
  end

  test "per package, so two artifacts of one repository say different things", %{repo: repo} do
    app = publish(repo, "fdroid", "org.example.app", %{about: %{"summary" => "The phone app"}})
    server = publish(repo, "deb", "example", %{about: %{"summary" => "The server"}})

    about = About.for_packages([app, server])
    assert about[app.id].summary == "The phone app"
    assert about[server.id].summary == "The server"
  end

  describe "set_about/2" do
    test "stores blank as nil", %{repo: repo} do
      package = publish(repo, "raw", "thing", %{about: %{"summary" => "A thing"}})

      {:ok, package} = Packages.set_about(package, %{"summary" => "   "})
      assert package.summary == nil
    end

    test "refuses a multi-line or overlong summary", %{repo: repo} do
      package = publish(repo, "raw", "thing")

      assert {:error, cs} = Packages.set_about(package, %{"summary" => "one\ntwo"})
      assert %{summary: [_]} = errors_on(cs)

      assert {:error, cs} = Packages.set_about(package, %{"summary" => String.duplicate("x", 81)})
      assert %{summary: [_]} = errors_on(cs)
    end

    test "a publish that doesn't say leaves what's stored alone", %{repo: repo} do
      package = publish(repo, "raw", "thing", %{about: %{"summary" => "A thing"}})

      {:ok, _, _, _} =
        Packages.publish_file(repo, "raw", "thing", "2.0.0", %{
          filename: "thing-2.bin",
          body: "two",
          about: %{}
        })

      assert Packages.get_package!(package.id).summary == "A thing"
    end
  end
end