defmodule LabelmakerWeb.UIURLParityTest do use LabelmakerWeb.ConnCase, async: false import Phoenix.LiveViewTest @label_dir Path.join(:code.priv_dir(:labelmaker), "static/labels") setup do # Clean up test images before each test File.rm_rf!(@label_dir) File.mkdir_p!(@label_dir) :ok end describe "UI to URL parameter consistency" do test "form submission generates same image as direct URL access", %{conn: conn} do # Step 1: Set up parameters via UI {:ok, view, _html} = live(conn, ~p"/") # Update label and parameters via form view |> element("form") |> render_change(%{ "label" => "Test", "color" => "red", "font" => "Impact", "outline" => "blue", "size" => "96" }) # The UI generates a link based on the parameters # We'll directly construct what the UI would generate ui_url = "/Test?color=red&font=Impact&outline=blue&size=96" # Step 2: Access directly via URL conn1 = get(conn, ui_url) assert conn1.status == 200 image_from_url = conn1.resp_body # Step 3: Simulate what the UI form submit would do # Looking at home.ex line 56: redirect to ~p"/#{params["label"]}?#{Map.drop(params, ["label"])}" # The form params come from the HTML form, which are all strings form_params = %{ "label" => "Test", "color" => "red", "font" => "Impact", "outline" => "blue", "size" => "96" } redirect_url = "/#{form_params["label"]}?#{URI.encode_query(Map.drop(form_params, ["label"]))}" conn2 = get(conn, redirect_url) assert conn2.status == 200 image_from_form = conn2.resp_body # Step 4: Images should be identical assert image_from_url == image_from_form end test "wxh mode parameters match between UI and direct URL", %{conn: conn} do {:ok, view, _html} = live(conn, ~p"/") # Switch to wxh mode and set parameters render_click(view, "update_sizing", %{"sizing" => "wxh"}) view |> element("form") |> render_change(%{ "label" => "WxHTest", "color" => "yellow", "font" => "Helvetica", "outline" => "black", "width" => "500", "height" => "300", "align" => "center" }) # Direct URL access direct_url = "/WxHTest?color=yellow&font=Helvetica&outline=black&width=500&height=300&align=center" conn1 = get(conn, direct_url) assert conn1.status == 200 image_from_url = conn1.resp_body # Form redirect (simulated) form_params = %{ "label" => "WxHTest", "color" => "yellow", "font" => "Helvetica", "outline" => "black", "width" => "500", "height" => "300", "align" => "center" } redirect_url = "/#{form_params["label"]}?#{URI.encode_query(Map.drop(form_params, ["label"]))}" conn2 = get(conn, redirect_url) assert conn2.status == 200 image_from_form = conn2.resp_body # Images should be identical assert image_from_url == image_from_form end test "font shortcuts work consistently", %{conn: conn} do # Direct URL with shortcut conn1 = get(conn, "/Test?font=h") assert conn1.status == 200 image_with_shortcut = conn1.resp_body # Direct URL with full name conn2 = get(conn, "/Test?font=Helvetica") assert conn2.status == 200 image_with_full = conn2.resp_body # Should produce same image assert image_with_shortcut == image_with_full end test "default values match between UI mount and direct URL", %{conn: conn} do # Get image from UI's default values {:ok, _view, _html} = live(conn, ~p"/") # The UI redirects to the label with parameters # Default is: black color, Helvetica font, white outline, 72 size conn1 = get(conn, "/DefaultTest") assert conn1.status == 200 image_with_defaults = conn1.resp_body # Explicit URL with same defaults conn2 = get(conn, "/DefaultTest?color=black&font=Helvetica&outline=white&size=72") assert conn2.status == 200 image_with_explicit = conn2.resp_body # Should produce same image assert image_with_defaults == image_with_explicit end test "multiline labels match between UI and URL", %{conn: conn} do {:ok, view, _html} = live(conn, ~p"/") # UI entry with escaped newlines view |> element("form") |> render_change(%{ "label" => "Line1\\nLine2\\nLine3" }) # Direct URL (the form submit would URL-encode the backslash-n) conn1 = get(conn, "/Line1%5CnLine2%5CnLine3") assert conn1.status == 200 image_from_url = conn1.resp_body # URL with actual newline encoding conn2 = get(conn, "/Line1%0ALine2%0ALine3") assert conn2.status == 200 image_with_newline = conn2.resp_body # The escaped version should have actual newlines after processing # Both should produce the same image assert image_from_url == image_with_newline end end end