ML-182.3: migrate scrobble rules tests to PhoenixTest

This commit is contained in:
Claudio Ortolina
2026-05-15 07:00:15 +01:00
parent 9eda49ed46
commit 73c469e83d
@@ -2,23 +2,7 @@ defmodule MusicLibraryWeb.ScrobbleRulesLiveTest do
use MusicLibraryWeb.ConnCase use MusicLibraryWeb.ConnCase
import MusicLibrary.ScrobbleRulesFixtures import MusicLibrary.ScrobbleRulesFixtures
import Phoenix.LiveViewTest import Phoenix.LiveViewTest, only: [render_change: 1, form: 3]
# Test data
@invalid_attrs %{type: "", match_value: "", target_musicbrainz_id: ""}
@valid_attrs %{
type: :album,
match_value: "some match_value",
target_musicbrainz_id: "12345678-1234-1234-1234-123456789012",
description: "some description",
enabled: "true"
}
@update_attrs %{
type: :artist,
match_value: "some updated match_value",
target_musicbrainz_id: "87654321-4321-4321-4321-210987654321",
description: "some updated description"
}
defp create_scrobble_rule(_) do defp create_scrobble_rule(_) do
scrobble_rule = scrobble_rule_fixture() scrobble_rule = scrobble_rule_fixture()
@@ -29,106 +13,118 @@ defmodule MusicLibraryWeb.ScrobbleRulesLiveTest do
setup [:create_scrobble_rule] setup [:create_scrobble_rule]
test "lists all scrobble_rules", %{conn: conn, scrobble_rule: scrobble_rule} do test "lists all scrobble_rules", %{conn: conn, scrobble_rule: scrobble_rule} do
{:ok, _index_live, html} = live(conn, ~p"/scrobble-rules") conn
|> visit(~p"/scrobble-rules")
assert html =~ "Scrobble Rules" |> assert_has("p", scrobble_rule.match_value)
assert html =~ scrobble_rule.match_value
end end
test "saves new scrobble_rule", %{conn: conn} do test "saves new scrobble_rule", %{conn: conn} do
{:ok, index_live, _html} = live(conn, ~p"/scrobble-rules") session =
conn
|> visit(~p"/scrobble-rules")
|> click_link("Add")
assert index_live |> element("a", "Add") |> render_click() =~ assert_has(session, "h1", "New Scrobble Rule")
"New Scrobble Rule" assert_path(session, ~p"/scrobble-rules/new")
assert_patch(index_live, ~p"/scrobble-rules/new") # Validation
conn
|> visit(~p"/scrobble-rules/new")
|> click_button("Save Rule")
|> assert_has("[data-part='error']", "can't be blank")
assert index_live # Successful creation
|> form("#scrobble_rule-form", scrobble_rule: @invalid_attrs) conn
|> render_change() =~ "can't be blank" |> visit(~p"/scrobble-rules/new")
|> set_rule_type(:album)
index_live |> fill_in("Album Title", with: "some match_value")
|> form("#scrobble_rule-form", scrobble_rule: @valid_attrs) |> fill_in("Target MusicBrainz ID",
|> render_submit() with: "12345678-1234-1234-1234-123456789012"
)
# The rule should be created and visible in the list |> fill_in("Description (optional)", with: "some description")
html = render(index_live) |> check("Enable this rule")
assert html =~ "some match_value" |> click_button("Save Rule")
|> assert_has("p", "some match_value")
end end
test "updates scrobble_rule in listing", %{conn: conn, scrobble_rule: scrobble_rule} do test "updates scrobble_rule in listing", %{conn: conn, scrobble_rule: scrobble_rule} do
{:ok, index_live, _html} = live(conn, ~p"/scrobble-rules") session =
conn
|> visit(~p"/scrobble-rules")
|> click_link(
"#scrobble_rules-#{scrobble_rule.id} a[href*='edit']",
"Edit"
)
assert index_live assert_has(session, "h1", "Edit Scrobble Rule")
|> element("#scrobble_rules-#{scrobble_rule.id} a[href*='edit']") assert_path(session, ~p"/scrobble-rules/#{scrobble_rule}/edit")
|> render_click() =~
"Edit Scrobble Rule"
assert_patch(index_live, ~p"/scrobble-rules/#{scrobble_rule}/edit?page=1&page_size=50") # Validation with cleared fields
conn
|> visit(~p"/scrobble-rules/#{scrobble_rule}/edit")
|> unwrap(fn view ->
view
|> form("#scrobble_rule-form",
scrobble_rule: %{type: "", match_value: "", target_musicbrainz_id: ""}
)
|> render_change()
end)
|> assert_has("*", "can't be blank")
assert index_live # Successful update (fresh session)
|> form("#scrobble_rule-form", scrobble_rule: @invalid_attrs) conn
|> render_change() =~ "can't be blank" |> visit(~p"/scrobble-rules/#{scrobble_rule}/edit")
|> set_rule_type(:artist)
index_live |> fill_in("Artist Name", with: "some updated match_value")
|> form("#scrobble_rule-form", scrobble_rule: @update_attrs) |> fill_in("Target MusicBrainz ID",
|> render_submit() with: "87654321-4321-4321-4321-210987654321"
)
html = render(index_live) |> fill_in("Description (optional)", with: "some updated description")
assert html =~ "Scrobble rule updated successfully" |> click_button("Save Rule")
assert html =~ "some updated match_value" |> assert_has("p", "some updated match_value")
end end
test "deletes scrobble_rule in listing", %{conn: conn, scrobble_rule: scrobble_rule} do test "deletes scrobble_rule in listing", %{conn: conn, scrobble_rule: scrobble_rule} do
{:ok, index_live, _html} = live(conn, ~p"/scrobble-rules") conn
|> visit(~p"/scrobble-rules")
assert index_live |> click_button(
|> element("#scrobble_rules-#{scrobble_rule.id} button[phx-click='delete']") "#scrobble_rules-#{scrobble_rule.id} button[phx-click='delete']",
|> render_click() "Delete"
)
refute has_element?(index_live, "#scrobble_rule-#{scrobble_rule.id}") |> refute_has("#scrobble_rule-#{scrobble_rule.id}")
end end
test "toggles rule enabled status", %{conn: conn, scrobble_rule: scrobble_rule} do test "toggles rule enabled status", %{conn: conn, scrobble_rule: scrobble_rule} do
{:ok, index_live, _html} = live(conn, ~p"/scrobble-rules") session = conn |> visit(~p"/scrobble-rules")
# Toggle to disabled session
html = |> click_button(
index_live "#scrobble_rules-#{scrobble_rule.id} button[phx-click='toggle_enabled']",
|> element("#scrobble_rules-#{scrobble_rule.id} button[phx-click='toggle_enabled']") "Disable rule"
|> render_click() )
|> assert_has("button", "Enable rule")
assert html =~ "Enable rule" |> click_button(
"#scrobble_rules-#{scrobble_rule.id} button[phx-click='toggle_enabled']",
# Toggle back to enabled "Enable rule"
html = )
index_live |> assert_has("button", "Disable rule")
|> element("#scrobble_rules-#{scrobble_rule.id} button[phx-click='toggle_enabled']")
|> render_click()
assert html =~ "Disable rule"
end end
test "applies individual rule", %{conn: conn, scrobble_rule: scrobble_rule} do test "applies individual rule", %{conn: conn, scrobble_rule: scrobble_rule} do
{:ok, index_live, _html} = live(conn, ~p"/scrobble-rules") conn
|> visit(~p"/scrobble-rules")
assert index_live |> click_button(
|> element("#scrobble_rules-#{scrobble_rule.id} button[phx-click='apply_rule']") "#scrobble_rules-#{scrobble_rule.id} button[phx-click='apply_rule']",
|> render_click() "Apply rule"
)
# Should show success message (even if no tracks were updated) |> assert_has("p", "Rule applied successfully")
assert render(index_live) =~ "Rule applied successfully"
end end
test "applies all rules", %{conn: conn} do test "applies all rules", %{conn: conn} do
{:ok, index_live, _html} = live(conn, ~p"/scrobble-rules") conn
|> visit(~p"/scrobble-rules")
assert index_live |> click_button("button[phx-click='apply_all_rules']", "Apply")
|> element("button[phx-click='apply_all_rules']") |> assert_has("p", "All rules applied successfully")
|> render_click()
# Should show success message
assert render(index_live) =~ "All rules applied successfully"
end end
test "searches scrobble rules by match_value", %{conn: conn, scrobble_rule: scrobble_rule} do test "searches scrobble rules by match_value", %{conn: conn, scrobble_rule: scrobble_rule} do
@@ -137,57 +133,48 @@ defmodule MusicLibraryWeb.ScrobbleRulesLiveTest do
match_value: "Unrelated Album" match_value: "Unrelated Album"
}) })
{:ok, index_live, _html} = live(conn, ~p"/scrobble-rules") session =
conn
|> visit(~p"/scrobble-rules")
|> search_rules(scrobble_rule.match_value)
index_live assert_has(session, "p", scrobble_rule.match_value)
|> form("form[phx-change='search']:not([phx-target])", query: scrobble_rule.match_value) refute_has(session, "p", "Unrelated Album")
|> render_change()
assert_patch(index_live)
html = render(index_live)
assert html =~ scrobble_rule.match_value
refute html =~ "Unrelated Album"
end end
test "switches sort order", %{conn: conn} do test "switches sort order", %{conn: conn} do
{:ok, index_live, _html} = live(conn, ~p"/scrobble-rules") conn
|> visit(~p"/scrobble-rules")
# Switch to alphabetical |> click_link("a[href*='order=alphabetical']", "A->Z")
index_live |> assert_path(~p"/scrobble-rules", query_params: %{order: "alphabetical"})
|> element("a[href*='order=alphabetical']") |> click_link("a[href*='order=inserted_at']", "Updated")
|> render_click() |> assert_path(~p"/scrobble-rules", query_params: %{order: "inserted_at"})
assert_patch(index_live)
# Switch back to inserted_at
index_live
|> element("a[href*='order=inserted_at']")
|> render_click()
assert_patch(index_live)
end end
end end
test "updates form labels based on rule type", %{conn: conn} do test "updates form labels based on rule type", %{conn: conn} do
{:ok, index_live, _html} = live(conn, ~p"/scrobble-rules") conn
|> visit(~p"/scrobble-rules")
|> click_link("Add")
|> set_rule_type(:album)
|> assert_has("label", "Album Title")
|> set_rule_type(:artist)
|> assert_has("label", "Artist Name")
end
assert index_live |> element("a", "Add") |> render_click() defp search_rules(session, query) do
unwrap(session, fn view ->
# Select album type view
html = |> form("form[phx-change='search']:not([phx-target])", %{query: query})
index_live
|> form("#scrobble_rule-form", scrobble_rule: %{type: :album})
|> render_change() |> render_change()
end)
end
assert html =~ "Album Title" defp set_rule_type(session, type) do
unwrap(session, fn view ->
# Select artist type view
html = |> form("#scrobble_rule-form", scrobble_rule: %{type: type})
index_live
|> form("#scrobble_rule-form", scrobble_rule: %{type: :artist})
|> render_change() |> render_change()
end)
assert html =~ "Artist Name"
end end
end end