prop/test/test_helper.exs
Graham McIntire 32818d5866
Some checks failed
Build and Push / Build and Push Docker Image (push) Has been cancelled
fix: repair mix test by removing forced MIX_ENV=dev and silencing test noise
- nix/shell.nix: remove export MIX_ENV=dev — it overrode Mix's automatic
  env selection, causing mix test to load dev config with no sandbox pool
- config/runtime.exs: use Ecto.Adapters.SQL.Sandbox pool (not
  DBConnection.Ownership) for test env — test_helper.exs uses the
  Sandbox API which requires the sandbox pool type
- test/test_helper.exs: silence NarrFetchWorker and ObanErrorReporter
  log noise at :critical level — expected error-path output when inline
  Oban jobs hit stubbed HTTP 500s
2026-08-05 08:28:50 -05:00

81 lines
3.5 KiB
Elixir

alias Ecto.Adapters.SQL.Sandbox
# Elixir 1.20 loads test_helper before dependency ebin dirs are on the
# code path. Add them manually so `use` / `import` in test files can
# resolve modules from :only test deps (Mox, ExUnitProperties, etc.).
for dep_dir <- Path.wildcard("_build/test/lib/*/ebin") do
:code.add_patha(String.to_charlist(dep_dir))
end
# Ensure test support modules are compiled and loaded before ExUnit
# starts lazily compiling the test suite. The parallel compiler workers
# inherit the code server state, so loading them here makes them visible
# everywhere.
# Use ensure_loaded!/1 (not require_file) because mix compile already
# compiles these into _build — require_file recompiles them redundantly
# and triggers "redefining module" warnings that fail --warnings-as-errors.
Enum.each(
[
Microwaveprop.DataCase,
MicrowavepropWeb.ConnCase,
Microwaveprop.AccountsFixtures,
Microwaveprop.BeaconsFixtures,
Microwaveprop.ContactsFixtures
],
&Code.ensure_loaded!/1
)
ExUnit.start()
# :manual mode: only the test process (via start_owner! / checkout) and
# processes explicitly allowed via Sandbox.allow/4 or Caller Tracking
# ($callers) can access the database. Background processes (Oban plugins,
# boot Tasks) that auto-checked out during app boot are checked in here,
# preventing any stale data from leaking across tests or across runs.
Sandbox.mode(Microwaveprop.Repo, :manual)
# stream_data is a test-only dep — its ebin is on the code path from the
# wildcard above, but the application isn't started. `check all` etc.
# call Application.fetch_env!(:stream_data, :initial_size), which needs
# the app to be loaded.
Application.ensure_all_started(:stream_data)
# lazy_html provides an Enumerable protocol implementation for the
# LazyHTML struct. When protocol consolidation is enabled (the default),
# the consolidated Elixir.Enumerable.beam file is generated during
# `mix compile`. If that consolidation pass runs before lazy_html is on
# the code path, the dispatch table omits LazyHTML — causing every test
# that uses Phoenix.LiveViewTest (which calls Enum.each/2 on LazyHTML
# structs via DOM.parse_document/2) to crash with:
#
# (Protocol.UndefinedError) protocol Enumerable not implemented for
# LazyHTML (a struct)
#
# mix.exs disables `consolidate_protocols` in the test environment so
# the protocol uses dynamic dispatch instead, which always finds the
# Enumerable.LazyHTML implementation at runtime.
# Silence sandbox-cleanup disconnect noise. When a test process exits
# while still owning a Postgrex connection, the protocol logs an
# `[error] Postgrex.Protocol ... disconnected: ... client #PID<...>
# exited`. It's benign — the connection is being torn down on purpose
# — but at :error level it bypasses `capture_log: true` (which only
# captures logs from the test process). Pinning these modules to
# :critical drops the noise without affecting real DB error logging.
Logger.put_module_level(
[Postgrex.Protocol, DBConnection.Connection, DBConnection.Holder, Task.Supervised],
:critical
)
# Silence expected error-path noise during tests. Background workers
# (NarrFetch, etc.) run inline under `testing: :inline` and hit stubbed
# HTTP 500 responses — the warnings and Oban job-exception logs are
# deliberate test behaviour, not bugs.
Logger.put_module_level(
[Microwaveprop.Workers.NarrFetchWorker, Microwaveprop.ObanErrorReporter],
:critical
)
if Code.ensure_loaded?(Mox) do
Mox.defmock(Microwaveprop.Valkey.MockAdapter, for: Microwaveprop.Valkey.Adapter)
end