chore: extraction and centralization of utility functions

This commit is contained in:
2026-05-01 17:04:21 +02:00
parent a95e9482a7
commit 79ee67c2e0
15 changed files with 272 additions and 254 deletions

View File

@@ -0,0 +1,31 @@
defmodule BDS.MapUtilsTest do
use ExUnit.Case, async: true
alias BDS.MapUtils
describe "attr/2" do
test "reads atom and string keys while preserving explicit nil" do
assert MapUtils.attr(%{title: "Atom title"}, :title) == "Atom title"
assert MapUtils.attr(%{"title" => "String title"}, :title) == "String title"
assert MapUtils.attr(%{"title" => "fallback", title: nil}, :title) == nil
assert MapUtils.attr(%{}, :title) == nil
end
end
describe "maybe_put/3" do
test "skips nil values and keeps other values" do
assert MapUtils.maybe_put(%{}, :title, nil) == %{}
assert MapUtils.maybe_put(%{}, :title, "") == %{title: ""}
assert MapUtils.maybe_put(%{}, :published, false) == %{published: false}
end
end
describe "blank_to_nil/1" do
test "normalizes nil and empty string only" do
assert MapUtils.blank_to_nil(nil) == nil
assert MapUtils.blank_to_nil("") == nil
assert MapUtils.blank_to_nil(" ") == " "
assert MapUtils.blank_to_nil(42) == 42
end
end
end

View File

@@ -0,0 +1,55 @@
defmodule BDS.ProgressReporterTest do
use ExUnit.Case, async: true
alias BDS.ProgressReporter
test "extracts only two-arity progress callbacks" do
callback = fn _progress, _message -> :ok end
assert ProgressReporter.callback(on_progress: callback) == callback
assert ProgressReporter.callback(on_progress: fn _progress -> :ok end) == nil
assert ProgressReporter.callback([]) == nil
end
test "reports rebuild start and progress messages" do
parent = self()
callback = fn progress, message -> send(parent, {progress, message}) end
assert ProgressReporter.report_rebuild_started(callback, 3, "post files") == :ok
assert ProgressReporter.report_rebuild_progress(callback, 2, 4, "post files") == :ok
assert_received {0.05, "Rebuilding post files (0/3)"}
assert_received {0.525, "Rebuilding post files (2/4)"}
end
test "reports empty rebuilds as complete" do
parent = self()
callback = fn progress, message -> send(parent, {progress, message}) end
assert ProgressReporter.report_rebuild_started(callback, 0, "media files") == :ok
assert_received {1.0, "No media files found"}
end
test "ignores nil callbacks and zero totals" do
assert ProgressReporter.report_rebuild_started(nil, 3, "post files") == :ok
assert ProgressReporter.report_rebuild_progress(nil, 1, 3, "post files") == :ok
assert ProgressReporter.report_rebuild_progress(
fn _, _ -> flunk("should not call") end,
1,
0,
"post files"
) == :ok
end
test "scales nested progress reporters" do
parent = self()
callback = fn progress, message -> send(parent, {progress, message}) end
scaled = ProgressReporter.scaled(callback, 0.25, 0.75)
scaled.(0.5, "Halfway")
assert_received {0.5, "Halfway"}
end
end