4.4 KiB · text History 44bc4f7
defmodule CartosnapApp.Photos.ImagesTest do
  use ExUnit.Case, async: true

  alias CartosnapApp.Photos.Images
  alias CartosnapApp.Photos.Photo

  describe "geo_point/1" do
    test "reads degrees, minutes and seconds with their hemispheres" do
      exif = %{
        gps: %{
          gps_latitude: [42, 22, 25.0],
          gps_latitude_ref: "N",
          gps_longitude: [71, 6, 35.0],
          gps_longitude_ref: "W"
        }
      }

      assert {lat, lon} = Photo.lat_lon(%Photo{geo_point: Images.geo_point(exif)})
      assert_in_delta lat, 42.3736, 0.0001
      assert_in_delta lon, -71.1097, 0.0001
    end

    test "ignores missing, partial and null-island positions" do
      assert Images.geo_point(%{}) == nil
      assert Images.geo_point(%{gps: %{gps_latitude: nil}}) == nil

      zero = %{
        gps: %{
          gps_latitude: [0, 0, 0],
          gps_latitude_ref: "N",
          gps_longitude: [0, 0, 0],
          gps_longitude_ref: "E"
        }
      }

      assert Images.geo_point(zero) == nil
    end
  end

  describe "shooting/1" do
    test "reads the camera and how the photo was taken" do
      exif = %{
        "make" => "NIKON CORPORATION",
        "model" => "NIKON D750 ",
        "exif" => %{
          "lens_model" => "50.0 mm f/1.8\0",
          "f_number" => 2.8,
          "focal_length" => 50,
          "iso_speed_ratings" => 400,
          "exposure_time" => "1/250"
        }
      }

      assert Images.shooting(exif) == %{
               camera: "NIKON D750",
               lens: "50.0 mm f/1.8",
               f_number: 2.8,
               focal_length: 50.0,
               iso: 400,
               exposure: 0.004
             }
    end

    test "names the maker when the model doesn't" do
      assert Images.shooting(%{"make" => "Apple", "model" => "iPhone 15"}).camera ==
               "Apple iPhone 15"

      assert Images.shooting(%{"make" => "Canon", "model" => "Canon EOS R6"}).camera ==
               "Canon EOS R6"

      assert Images.shooting(%{"model" => "Pixel 8"}).camera == "Pixel 8"
    end

    test "is all nil without EXIF, or with junk in it" do
      assert Images.shooting(nil) |> Map.values() |> Enum.all?(&is_nil/1)

      junk = %{
        "make" => "  ",
        "exif" => %{"f_number" => 0, "iso_speed_ratings" => [0], "exposure_time" => "infinity"}
      }

      assert Images.shooting(junk) |> Map.values() |> Enum.all?(&is_nil/1)
    end
  end

  describe "taken_at/1" do
    test "reads DateTimeOriginal" do
      assert Images.taken_at(%{exif: %{datetime_original: "2023:08:21 18:04:59"}}) ==
               ~U[2023-08-21 18:04:59Z]
    end

    test "is nil when absent or malformed" do
      assert Images.taken_at(%{}) == nil
      assert Images.taken_at(%{exif: %{datetime_original: "0000:00:00 00:00:00"}}) == nil
    end
  end

  describe "jsonable/1" do
    test "turns structs, tuples and atoms into JSON, dropping binary blobs" do
      input = %{
        gps: %URI{host: "x"},
        res: {72, 1},
        flag: false,
        none: nil,
        kind: :jpeg,
        maker_note: <<0, 255, 1>>
      }

      out = Images.jsonable(input)
      assert out["gps"]["host"] == "x"
      assert out["res"] == [72, 1]
      assert out["flag"] == false
      assert Map.has_key?(out, "none")
      assert out["kind"] == "jpeg"
      refute Map.has_key?(out, "maker_note")
      assert {:ok, _} = Jason.encode(out)
    end
  end

  describe "with ImageMagick" do
    setup do
      path =
        Path.join(System.tmp_dir!(), "cartosnap-images-#{System.unique_integer([:positive])}.png")

      {_, 0} = System.cmd("magick", ["-size", "800x600", "gradient:orange-purple", path])
      on_exit(fn -> File.rm(path) end)
      %{path: path}
    end

    test "blurhash/1 encodes a 4x3 placeholder", %{path: path} do
      hash = Images.blurhash(path)
      # 1 size + 1 max + 4 DC + 2 for each of the 11 AC components.
      assert String.length(hash) == 28
    end

    test "variants/1 makes the three sizes", %{path: path} do
      variants = Images.variants(path)

      try do
        assert Keyword.keys(variants) == [:full, :prev, :thumb]

        dims =
          Map.new(variants, fn {variant, file} ->
            {out, 0} = System.cmd("magick", ["identify", "-format", "%wx%h", file])
            {variant, out}
          end)

        assert dims == %{full: "800x600", prev: "640x480", thumb: "240x180"}
      after
        Enum.each(variants, fn {_, file} -> File.rm(file) end)
      end
    end
  end
end