test: wait on observable state instead of blind sleeps

Replace the fixed Process.sleep waits in the import, members-PDF and
field-visibility tests with event-based / bounded-poll waits on the
observable condition, removing a known flakiness vector.
This commit is contained in:
Moritz 2026-06-16 17:51:43 +02:00
parent ccd1f81e3e
commit 655fd80524
3 changed files with 55 additions and 36 deletions

View file

@ -37,7 +37,27 @@ defmodule MvWeb.ImportLiveTest do
confirm_import(view)
end
defp wait_for_import_completion, do: Process.sleep(1000)
# Waits for the asynchronous chunk-import to finish by polling the rendered
# results panel, instead of a fixed sleep. In the test sandbox the chunks run
# in-process and signal completion via self-messages; each render/2 forces the
# LiveView to drain its mailbox before replying, so the panel appears once all
# chunk messages have been processed. Bounded so a genuine stall still fails.
defp wait_for_import_completion(view) do
wait_for_import_completion(view, 200)
end
defp wait_for_import_completion(_view, 0) do
flunk("import did not complete: results panel never rendered")
end
defp wait_for_import_completion(view, attempts_left) do
if has_element?(view, "[data-testid='import-results-panel']") do
:ok
else
Process.sleep(10)
wait_for_import_completion(view, attempts_left - 1)
end
end
# ---------- Business logic: Authorization ----------
describe "Authorization" do
@ -67,7 +87,7 @@ defmodule MvWeb.ImportLiveTest do
{:ok, view, _html} = live(conn, ~p"/admin/import")
run_full_import(view, csv_content)
wait_for_import_completion()
wait_for_import_completion(view)
assert has_element?(view, "[data-testid='import-results-panel']")
assert has_element?(view, "[data-testid='import-summary']")
@ -131,7 +151,7 @@ defmodule MvWeb.ImportLiveTest do
} do
{:ok, view, _html} = live(conn, ~p"/admin/import")
run_full_import(view, csv_content, "invalid_import.csv")
wait_for_import_completion()
wait_for_import_completion(view)
assert has_element?(view, "[data-testid='import-results-panel']")
assert has_element?(view, "[data-testid='import-error-list']")
@ -150,7 +170,7 @@ defmodule MvWeb.ImportLiveTest do
for i <- 1..100, do: "Row#{i};Last#{i};;Country#{i};City#{i};Street#{i};12345\n"
run_full_import(view, header <> Enum.join(invalid_rows), "large_invalid.csv")
wait_for_import_completion()
wait_for_import_completion(view)
assert has_element?(view, "[data-testid='import-results-panel']")
assert has_element?(view, "[data-testid='import-error-list']")
@ -182,7 +202,7 @@ defmodule MvWeb.ImportLiveTest do
|> File.read!()
run_full_import(view, csv_content, "bom_import.csv")
wait_for_import_completion()
wait_for_import_completion(view)
assert has_element?(view, "[data-testid='import-results-panel']")
html = render(view)
@ -200,7 +220,7 @@ defmodule MvWeb.ImportLiveTest do
|> File.read!()
run_full_import(view, csv_content, "empty_lines.csv")
wait_for_import_completion()
wait_for_import_completion(view)
assert has_element?(view, "[data-testid='import-error-list']")
html = render(view)
@ -214,7 +234,7 @@ defmodule MvWeb.ImportLiveTest do
} do
{:ok, view, _html} = live(conn, ~p"/admin/import")
run_full_import(view, csv_content, "unknown_custom.csv")
wait_for_import_completion()
wait_for_import_completion(view)
assert has_element?(view, "[data-testid='import-results-panel']")
assert has_element?(view, "[data-testid='import-warnings']")
@ -279,7 +299,7 @@ defmodule MvWeb.ImportLiveTest do
{:ok, view, _html} = live(conn, ~p"/admin/import")
run_full_import(view, csv_content)
wait_for_import_completion()
wait_for_import_completion(view)
assert has_element?(view, "[data-testid='import-progress-container']")
html = render(view)
assert html =~ "aria-live"
@ -342,7 +362,7 @@ defmodule MvWeb.ImportLiveTest do
} do
{:ok, view, _html} = live(conn, ~p"/admin/import")
run_full_import(view, csv_content)
wait_for_import_completion()
wait_for_import_completion(view)
assert has_element?(view, "[data-testid='import-results-panel']")