Skip to content

[BUG] Algoliax compiles in prod mode during tests, causing real API calls instead of using mock server #114

@Kasse-Dembele

Description

@Kasse-Dembele

Problem

When running tests, the Algoliax library is making real Algolia API calls instead of using the mock server. This happens because the library is being compiled in production mode (prod) even during test execution.

Current Behavior

  • Library sends requests to https://.algolia.net/1/indexes/... during tests
  • Expected to use mock server URLs (http://localhost:8002/...) in test environment
  • Added debug logging shows Compiling in environment: prod during compilation

Expected Behavior

  • Library should compile in test mode when running tests
  • Should use mock server URLs in test environment as defined in the if Mix.env() == :test block

Technical Details

The issue appears to be in the compilation process. The environment check in lib/algoliax/routes.ex is not working as expected:

if Mix.env() == :test do
  defp url_read do
    port = System.get_env("SLACK_MOCK_API_PORT", "8002")
    "http://localhost:#{port}/{{application_id}}/read"
  end
  # ...
else
  defp url_read do
    "https://{{application_id}}-dsn.algolia.net/1/indexes"
  end
  # ...
end

Debug Attempt

I added debug logging in lib/algoliax/routes.ex to verify the compilation environment:

IO.puts("=== Algoliax.Routes Compilation ===")
IO.puts "Compiling in environment: #{Mix.env()}"
IO.puts "Time: #{DateTime.utc_now()}"

The output confirmed the issue:

=== Algoliax.Routes Compilation ===
Compiling in environment: prod

Steps to Reproduce

  1. Add a test that uses Algoliax
  2. Run the test
  3. Observe that requests are being sent to real Algolia endpoints instead of the mock server

Environment

  • Elixir version: 1.16.1
  • Erlang version: 26.2
  • Algoliax version: 0.10.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions