11.3 KiB · text History e569b6d
defmodule CartosnapAppWeb.ShareLiveTest do
  use CartosnapAppWeb.ConnCase, async: true

  import Phoenix.LiveViewTest
  import CartosnapApp.PhotosFixtures

  alias CartosnapApp.Photos
  alias CartosnapApp.Trips

  setup :register_and_log_in_user

  setup %{scope: scope} do
    trip = trip_fixture(scope, name: "Maine")

    photos =
      for {day, lat} <- [{1, 44.0}, {2, 44.1}, {3, 44.2}] do
        photo =
          photo_fixture(scope,
            date: DateTime.new!(Date.new!(2025, 6, day), ~T[12:00:00]),
            lat: lat,
            lon: -68.2
          )

        {:ok, _} = Trips.add_photos(scope, trip.id, [photo.id])
        photo
      end

    {:ok, trip} = Trips.share_trip(scope, trip)
    %{trip: trip, photos: photos, path: ~p"/s/#{trip.share_token}"}
  end

  # Presence diffs reach other viewers through the tracker, asynchronously;
  # re-render until they have (or give up after about a second).
  defp eventually(view, selector, text \\ nil, tries \\ 100) do
    cond do
      has_element?(view, selector, text) ->
        true

      tries == 0 ->
        flunk("#{selector} never appeared:\n" <> render(view))

      true ->
        # Presence diffs arrive on their own; give them a moment.
        receive do
        after
          10 -> eventually(view, selector, text, tries - 1)
        end
    end
  end

  test "anyone with the link can view, without an account", %{path: path, photos: [first | _]} do
    {:ok, view, _html} = live(build_conn(), path)

    assert has_element?(view, "#share-photo")
    assert has_element?(view, "#share-cursors[data-photo='#{first.id}']")
    assert has_element?(view, "#share-counter", "1 / 3")
    assert has_element?(view, "#share-map[phx-hook=PhotoMap][data-select-event=select]")
    assert has_element?(view, "#no-host")
  end

  test "a bad or revoked link goes nowhere", %{conn: conn, scope: scope, trip: trip, path: path} do
    assert {:error, {:live_redirect, %{to: "/"}}} =
             live(build_conn(), ~p"/s/#{String.duplicate("x", 43)}")

    {:ok, view, _html} = live(build_conn(), path)
    {:ok, _} = Trips.stop_sharing(scope, trip)
    assert_redirect(view, ~p"/")

    assert {:error, {:live_redirect, %{to: "/"}}} = live(conn, path)
  end

  test "pages with buttons, keys, the strip and the map", %{path: path, photos: [a, b, c]} do
    {:ok, view, _html} = live(build_conn(), path)

    view |> element("#share-next") |> render_click()
    assert has_element?(view, "#share-cursors[data-photo='#{b.id}']")

    render_keydown(view, "key", %{"key" => "ArrowRight"})
    assert has_element?(view, "#share-counter", "3 / 3")
    refute has_element?(view, "#share-next")

    view |> element("#strip-#{a.id}") |> render_click()
    assert has_element?(view, "#share-counter", "1 / 3")

    render_hook(view, "select", %{"id" => c.id})
    assert has_element?(view, "#share-cursors[data-photo='#{c.id}']")
  end

  test "followers follow the host, can wander off, and catch up", %{
    conn: conn,
    path: path,
    photos: [_a, b, c]
  } do
    {:ok, host, _html} = live(conn, path)
    assert has_element?(host, "#hosting")

    {:ok, guest, _html} = live(build_conn(), path)
    assert eventually(guest, "#following")
    assert eventually(host, "#share-roster li:nth-child(2)")

    # The host pages; the guest goes along.
    host |> element("#share-next") |> render_click()
    assert eventually(guest, "#share-cursors[data-photo='#{b.id}']")

    # The guest goes their own way and stops following...
    guest |> element("#share-next") |> render_click()
    assert has_element?(guest, "#follow-host")
    host |> element("#strip-#{b.id}") |> render_click()
    assert has_element?(guest, "#share-cursors[data-photo='#{c.id}']")

    # ...until they follow again, which catches them up.
    guest |> element("#follow-host") |> render_click()
    assert has_element?(guest, "#following")
    assert has_element?(guest, "#share-cursors[data-photo='#{b.id}']")
  end

  test "a guest who arrives late lands where the host is", %{
    conn: conn,
    path: path,
    photos: [_a, _b, c]
  } do
    {:ok, host, _html} = live(conn, path)
    render_hook(host, "select", %{"id" => c.id})

    {:ok, guest, _html} = live(build_conn(), path)
    assert has_element?(guest, "#share-cursors[data-photo='#{c.id}']")
    assert has_element?(guest, "#following")
  end

  test "everyone sees who's here", %{conn: conn, path: path} do
    {:ok, host, _html} = live(conn, path)
    {:ok, _guest, _html} = live(build_conn(), path)

    assert eventually(host, "#hosting", "1 watching")
    assert has_element?(host, "#share-roster li:nth-child(2)")
  end

  test "pointers go to the others, and leave with their owner", %{path: path, photos: [first | _]} do
    {:ok, one, _html} = live(build_conn(), path)
    {:ok, two, _html} = live(build_conn(), path)

    render_hook(one, "cursor", %{"x" => 25.5, "y" => 70.0})
    assert_push_event(two, "share:cursor", %{x: 25.5, y: 70.0, photo: photo_id, name: name})
    assert photo_id == first.id
    assert is_binary(name)

    # Out-of-range positions are dropped to "not over the photo".
    render_hook(one, "cursor", %{"x" => 900, "y" => 70})
    assert_push_event(two, "share:cursor", %{x: nil, y: nil})

    GenServer.stop(one.pid)
    assert_push_event(two, "share:cursor-leave", %{id: "v-" <> _})
  end

  test "promoted only narrows what's shown", %{
    scope: scope,
    trip: trip,
    path: path,
    photos: [_a, b, _c]
  } do
    {:ok, view, _html} = live(build_conn(), path)
    {:ok, _} = Photos.update_photo(scope, b, %{promoted: true})
    {:ok, _} = Trips.set_share_promoted_only(scope, trip, true)

    assert has_element?(view, "#share-counter", "1 / 1")
    assert has_element?(view, "#share-cursors[data-photo='#{b.id}']")
  end

  test "a photo sphere shows in the viewer, without cursors", %{
    scope: scope,
    trip: trip,
    path: path
  } do
    sphere =
      photo_fixture(scope,
        date: ~U[2025-05-01 12:00:00Z],
        kind: "sphere",
        pano: %{"full_width" => 2000, "full_height" => 1000, "width" => 2000, "height" => 1000}
      )

    {:ok, _} = Trips.add_photos(scope, trip.id, [sphere.id])
    {:ok, view, _html} = live(build_conn(), path <> "?photo=#{sphere.id}")

    assert has_element?(view, "#share-stage-#{sphere.id}[phx-hook=PhotoSphere]")
    refute has_element?(view, "#share-cursors")
  end

  describe "following anyone" do
    defp key_of(view) do
      view
      |> element("#share-roster li:first-child")
      |> render()
      |> LazyHTML.from_fragment()
      |> LazyHTML.attribute("id")
      |> hd()
      |> String.replace_prefix("present-", "")
    end

    test "a guest follows another guest, and stops when they leave", %{
      path: path,
      photos: [_a, b, c]
    } do
      {:ok, leader, _html} = live(build_conn(), path)
      {:ok, follower, _html} = live(build_conn(), path)
      leader_key = key_of(leader)

      assert eventually(follower, "#present-#{leader_key}")
      follower |> element("#present-#{leader_key} button") |> render_click()
      assert has_element?(follower, "#following")

      leader |> element("#strip-#{b.id}") |> render_click()
      assert eventually(follower, "#share-cursors[data-photo='#{b.id}']")

      # Following the follower follows the chain.
      {:ok, third, _html} = live(build_conn(), path)
      follower_key = key_of(follower)
      assert eventually(third, "#present-#{follower_key}")
      third |> element("#present-#{follower_key} button") |> render_click()
      leader |> element("#strip-#{c.id}") |> render_click()
      assert eventually(third, "#share-cursors[data-photo='#{c.id}']")

      GenServer.stop(leader.pid)
      assert eventually(follower, "#no-host")
      refute has_element?(follower, "#following")
    end

    test "the host brings everyone back", %{conn: conn, path: path, photos: [a, _b, c]} do
      {:ok, host, _html} = live(conn, path)
      {:ok, guest, _html} = live(build_conn(), path)
      assert eventually(guest, "#following")

      # The guest wanders off...
      guest |> element("#strip-#{c.id}") |> render_click()
      assert has_element?(guest, "#follow-host")

      # ...and the host calls everyone back to where they are.
      assert eventually(host, "#summon")
      host |> element("#summon") |> render_click()
      assert eventually(guest, "#share-cursors[data-photo='#{a.id}']")
      assert has_element?(guest, "#following")
    end

    test "only the host can summon", %{path: path} do
      {:ok, guest, _html} = live(build_conn(), path)
      refute has_element?(guest, "#summon")
      render_click(guest, "summon", %{})
      assert has_element?(guest, "#no-host")
    end
  end

  describe "public" do
    test "a public trip opens at /t/<id> for anyone; a private one doesn't", %{
      scope: scope,
      trip: trip
    } do
      assert {:error, {:live_redirect, %{to: "/"}}} = live(build_conn(), ~p"/t/#{trip.id}")
      assert {:error, {:live_redirect, %{to: "/"}}} = live(build_conn(), ~p"/t/not-a-uuid")

      {:ok, _} = Trips.set_public(scope, trip, true)
      {:ok, view, _html} = live(build_conn(), ~p"/t/#{trip.id}")
      assert has_element?(view, "#view-kind", "Public")
      assert has_element?(view, "#share-counter", "1 / 3")
    end

    test "?photo= opens at that photo", %{
      scope: scope,
      trip: trip,
      path: path,
      photos: [_a, b, _c]
    } do
      {:ok, _} = Trips.set_public(scope, trip, true)
      {:ok, view, _html} = live(build_conn(), ~p"/t/#{trip.id}?#{[photo: b.id]}")
      assert has_element?(view, "#share-cursors[data-photo='#{b.id}']")

      {:ok, view, _html} = live(build_conn(), path <> "?photo=#{b.id}")
      assert has_element?(view, "#share-counter", "2 / 3")
    end

    test "public viewers and link viewers are in separate rooms", %{
      scope: scope,
      trip: trip,
      path: path
    } do
      {:ok, _} = Trips.set_public(scope, trip, true)
      {:ok, public_view, _html} = live(build_conn(), ~p"/t/#{trip.id}")
      {:ok, link_view, _html} = live(build_conn(), path)
      {:ok, other_public, _html} = live(build_conn(), ~p"/t/#{trip.id}")

      # The two public viewers see each other...
      assert eventually(public_view, "#share-roster li:nth-child(2)")
      # ...the link viewer sees nobody else.
      refute has_element?(link_view, "#share-roster li:nth-child(2)")
      assert other_public
    end

    test "going private sends the public away and leaves the link alone",
         %{scope: scope, trip: trip, path: path} do
      {:ok, trip} = Trips.set_public(scope, trip, true)
      {:ok, public_view, _html} = live(build_conn(), ~p"/t/#{trip.id}")
      {:ok, link_view, _html} = live(build_conn(), path)

      {:ok, _} = Trips.set_public(scope, trip, false)
      assert_redirect(public_view, ~p"/")
      assert has_element?(link_view, "#share-photo")
    end

    test "the public's promoted-only setting is their own", %{
      scope: scope,
      trip: trip,
      path: path,
      photos: [_a, b, _c]
    } do
      {:ok, trip} = Trips.set_public(scope, trip, true)
      {:ok, _} = CartosnapApp.Photos.update_photo(scope, b, %{promoted: true})
      {:ok, _} = Trips.set_public_promoted_only(scope, trip, true)

      {:ok, public_view, _html} = live(build_conn(), ~p"/t/#{trip.id}")
      assert has_element?(public_view, "#share-counter", "1 / 1")

      {:ok, link_view, _html} = live(build_conn(), path)
      assert has_element?(link_view, "#share-counter", "1 / 3")
    end
  end
end