From 0fd243d25952a35432f3d7f1a41705abeb02f47c Mon Sep 17 00:00:00 2001 From: Alex Mickelson Date: Fri, 13 Mar 2026 15:21:06 -0600 Subject: [PATCH] disable external connections while testing with mimix --- lib/elixir_ai/ai_utils/chat_utils.ex | 1 - lib/elixir_ai/chat_runner.ex | 8 +- lib/elixir_ai/data/db_helpers.ex | 6 + lib/elixir_ai/data/repo.ex | 5 - mix.exs | 1 + mix.lock | 2 + test/db_helper_test.exs | 2 +- .../controllers/error_html_test.exs | 2 +- .../controllers/error_json_test.exs | 2 +- test/message_storage_test.exs | 112 ++++++++++++++++++ test/stream_line_utils_test.exs | 3 +- test/support/test_case.ex | 19 +++ test/test_helper.exs | 2 + 13 files changed, 151 insertions(+), 14 deletions(-) delete mode 100644 lib/elixir_ai/data/repo.ex create mode 100644 test/message_storage_test.exs create mode 100644 test/support/test_case.ex diff --git a/lib/elixir_ai/ai_utils/chat_utils.ex b/lib/elixir_ai/ai_utils/chat_utils.ex index d4347b7..4c9e894 100644 --- a/lib/elixir_ai/ai_utils/chat_utils.ex +++ b/lib/elixir_ai/ai_utils/chat_utils.ex @@ -36,7 +36,6 @@ defmodule ElixirAi.ChatUtils do %{ name: name, definition: schema, - # function: function, run_function: run_function } end diff --git a/lib/elixir_ai/chat_runner.ex b/lib/elixir_ai/chat_runner.ex index b99ae2a..fe26e99 100644 --- a/lib/elixir_ai/chat_runner.ex +++ b/lib/elixir_ai/chat_runner.ex @@ -1,7 +1,7 @@ defmodule ElixirAi.ChatRunner do require Logger use GenServer - import ElixirAi.ChatUtils + import ElixirAi.ChatUtils, only: [ai_tool: 1] alias ElixirAi.{Conversation, Message} defp via(name), do: {:via, Horde.Registry, {ElixirAi.ChatRegistry, name}} @@ -39,7 +39,7 @@ defmodule ElixirAi.ChatRunner do "Last message role was #{last_message.role}, requesting AI response for conversation #{name}" ) - request_ai_response(self(), messages, tools(self(), name)) + ElixirAi.ChatUtils.request_ai_response(self(), messages, tools(self(), name)) end {:ok, @@ -103,7 +103,7 @@ defmodule ElixirAi.ChatRunner do store_message(state.name, new_message) new_state = %{state | messages: state.messages ++ [new_message]} - request_ai_response(self(), new_state.messages, state.tools) + ElixirAi.ChatUtils.request_ai_response(self(), new_state.messages, state.tools) {:noreply, new_state} end @@ -283,7 +283,7 @@ defmodule ElixirAi.ChatRunner do if new_pending_tool_calls == [] do broadcast_ui(state.name, :tool_calls_finished) - request_ai_response(self(), state.messages ++ [new_message], state.tools) + ElixirAi.ChatUtils.request_ai_response(self(), state.messages ++ [new_message], state.tools) end {:noreply, diff --git a/lib/elixir_ai/data/db_helpers.ex b/lib/elixir_ai/data/db_helpers.ex index 7c9b0b9..fcc2b2e 100644 --- a/lib/elixir_ai/data/db_helpers.ex +++ b/lib/elixir_ai/data/db_helpers.ex @@ -83,3 +83,9 @@ defmodule ElixirAi.Data.DbHelpers do {positional_sql, ordered_values} end end + +defmodule ElixirAi.Repo do + use Ecto.Repo, + otp_app: :elixir_ai, + adapter: Ecto.Adapters.Postgres +end diff --git a/lib/elixir_ai/data/repo.ex b/lib/elixir_ai/data/repo.ex deleted file mode 100644 index cd9b0bc..0000000 --- a/lib/elixir_ai/data/repo.ex +++ /dev/null @@ -1,5 +0,0 @@ -defmodule ElixirAi.Repo do - use Ecto.Repo, - otp_app: :elixir_ai, - adapter: Ecto.Adapters.Postgres -end diff --git a/mix.exs b/mix.exs index 9ce4de3..ed92bcd 100644 --- a/mix.exs +++ b/mix.exs @@ -60,6 +60,7 @@ defmodule ElixirAi.MixProject do {:postgrex, ">= 0.0.0"}, {:horde, "~> 0.9"}, {:credo, "~> 1.7", only: [:dev, :test], runtime: false}, + {:mimic, "~> 2.3.0"}, {:zoi, "~> 0.17"} ] end diff --git a/mix.lock b/mix.lock index edd8cf2..4d25f2f 100644 --- a/mix.lock +++ b/mix.lock @@ -22,6 +22,7 @@ "gettext": {:hex, :gettext, "0.26.2", "5978aa7b21fada6deabf1f6341ddba50bc69c999e812211903b169799208f2a8", [:mix], [{:expo, "~> 0.5.1 or ~> 1.0", [hex: :expo, repo: "hexpm", optional: false]}], "hexpm", "aa978504bcf76511efdc22d580ba08e2279caab1066b76bb9aa81c4a1e0a32a5"}, "gproc": {:hex, :gproc, "0.9.1", "f1df0364423539cf0b80e8201c8b1839e229e5f9b3ccb944c5834626998f5b8c", [:rebar3], [], "hexpm", "905088e32e72127ed9466f0bac0d8e65704ca5e73ee5a62cb073c3117916d507"}, "grpcbox": {:hex, :grpcbox, "0.17.1", "6e040ab3ef16fe699ffb513b0ef8e2e896da7b18931a1ef817143037c454bcce", [:rebar3], [{:acceptor_pool, "~> 1.0.0", [hex: :acceptor_pool, repo: "hexpm", optional: false]}, {:chatterbox, "~> 0.15.1", [hex: :ts_chatterbox, repo: "hexpm", optional: false]}, {:ctx, "~> 0.6.0", [hex: :ctx, repo: "hexpm", optional: false]}, {:gproc, "~> 0.9.1", [hex: :gproc, repo: "hexpm", optional: false]}], "hexpm", "4a3b5d7111daabc569dc9cbd9b202a3237d81c80bf97212fbc676832cb0ceb17"}, + "ham": {:hex, :ham, "0.3.2", "02ae195f49970ef667faf9d01bc454fb80909a83d6c775bcac724ca567aeb7b3", [:mix], [], "hexpm", "b71cc684c0e5a3d32b5f94b186770551509e93a9ae44ca1c1a313700f2f6a69a"}, "heroicons": {:git, "https://github.com/tailwindlabs/heroicons.git", "88ab3a0d790e6a47404cba02800a6b25d2afae50", [tag: "v2.1.1", sparse: "optimized", depth: 1]}, "horde": {:hex, :horde, "0.10.0", "31c6a633057c3ec4e73064d7b11ba409c9f3c518aa185377d76bee441b76ceb0", [:mix], [{:delta_crdt, "~> 0.6.2", [hex: :delta_crdt, repo: "hexpm", optional: false]}, {:libring, "~> 1.7", [hex: :libring, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.0 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}, {:telemetry_poller, "~> 0.5.0 or ~> 1.0", [hex: :telemetry_poller, repo: "hexpm", optional: false]}], "hexpm", "0b51c435cb698cac9bf9c17391dce3ebb1376ae6154c81f077fc61db771b9432"}, "hpack": {:hex, :hpack_erl, "0.3.0", "2461899cc4ab6a0ef8e970c1661c5fc6a52d3c25580bc6dd204f84ce94669926", [:rebar3], [], "hexpm", "d6137d7079169d8c485c6962dfe261af5b9ef60fbc557344511c1e65e3d95fb0"}, @@ -33,6 +34,7 @@ "libring": {:hex, :libring, "1.7.0", "4f245d2f1476cd7ed8f03740f6431acba815401e40299208c7f5c640e1883bda", [:mix], [], "hexpm", "070e3593cb572e04f2c8470dd0c119bc1817a7a0a7f88229f43cf0345268ec42"}, "merkle_map": {:hex, :merkle_map, "0.2.2", "f36ff730cca1f2658e317a3c73406f50bbf5ac8aff54cf837d7ca2069a6e251c", [:mix], [], "hexpm", "383107f0503f230ac9175e0631647c424efd027e89ea65ab5ea12eeb54257aaf"}, "mime": {:hex, :mime, "2.0.7", "b8d739037be7cd402aee1ba0306edfdef982687ee7e9859bee6198c1e7e2f128", [:mix], [], "hexpm", "6171188e399ee16023ffc5b76ce445eb6d9672e2e241d2df6050f3c771e80ccd"}, + "mimic": {:hex, :mimic, "2.3.0", "88b1d13c285e57df6ea57204317bb56e49e7329668006cdcb80a9aafc73a9616", [:mix], [{:ham, "~> 0.3", [hex: :ham, repo: "hexpm", optional: false]}], "hexpm", "52771f23689398c5d41c7d05e91c2c28e10df273b784f40ca8b02e35e46850d3"}, "mint": {:hex, :mint, "1.7.1", "113fdb2b2f3b59e47c7955971854641c61f378549d73e829e1768de90fc1abf1", [:mix], [{:castore, "~> 0.1.0 or ~> 1.0", [hex: :castore, repo: "hexpm", optional: true]}, {:hpax, "~> 0.1.1 or ~> 0.2.0 or ~> 1.0", [hex: :hpax, repo: "hexpm", optional: false]}], "hexpm", "fceba0a4d0f24301ddee3024ae116df1c3f4bb7a563a731f45fdfeb9d39a231b"}, "mochiweb": {:hex, :mochiweb, "3.3.0", "2898ad0bfeee234e4cbae623c7052abc3ff0d73d499ba6e6ffef445b13ffd07a", [:rebar3], [], "hexpm", "aa85b777fb23e9972ebc424e40b5d35106f19bc998873e026dedd876df8ee50c"}, "nimble_options": {:hex, :nimble_options, "1.1.1", "e3a492d54d85fc3fd7c5baf411d9d2852922f66e69476317787a7b2bb000a61b", [:mix], [], "hexpm", "821b2470ca9442c4b6984882fe9bb0389371b8ddec4d45a9504f00a66f650b44"}, diff --git a/test/db_helper_test.exs b/test/db_helper_test.exs index 8394d40..af3dd70 100644 --- a/test/db_helper_test.exs +++ b/test/db_helper_test.exs @@ -1,5 +1,5 @@ defmodule SQLTest do - use ExUnit.Case + use ElixirAi.TestCase alias ElixirAi.Data.DbHelpers test "converts simple named parameters" do diff --git a/test/elixir_ai_web/controllers/error_html_test.exs b/test/elixir_ai_web/controllers/error_html_test.exs index 7ab603e..6457d34 100644 --- a/test/elixir_ai_web/controllers/error_html_test.exs +++ b/test/elixir_ai_web/controllers/error_html_test.exs @@ -1,5 +1,5 @@ defmodule ElixirAiWeb.ErrorHTMLTest do - use ElixirAiWeb.ConnCase, async: true + use ElixirAiWeb.ConnCase, async: false # Bring render_to_string/4 for testing custom views import Phoenix.Template diff --git a/test/elixir_ai_web/controllers/error_json_test.exs b/test/elixir_ai_web/controllers/error_json_test.exs index 87013fd..66343a6 100644 --- a/test/elixir_ai_web/controllers/error_json_test.exs +++ b/test/elixir_ai_web/controllers/error_json_test.exs @@ -1,5 +1,5 @@ defmodule ElixirAiWeb.ErrorJSONTest do - use ElixirAiWeb.ConnCase, async: true + use ElixirAiWeb.ConnCase, async: false test "renders 404" do assert ElixirAiWeb.ErrorJSON.render("404.json", %{}) == %{errors: %{detail: "Not Found"}} diff --git a/test/message_storage_test.exs b/test/message_storage_test.exs new file mode 100644 index 0000000..72fad3d --- /dev/null +++ b/test/message_storage_test.exs @@ -0,0 +1,112 @@ +defmodule ElixirAi.MessageStorageTest do + use ElixirAi.TestCase + + setup do + # Default run_sql and request_ai_response stubs are set by TestCase. + # Start ConversationManager AFTER stubs are active so its :load_conversations + # handler sees the stub rather than hitting the real (absent) DB. + case Horde.DynamicSupervisor.start_child( + ElixirAi.ChatRunnerSupervisor, + ElixirAi.ConversationManager + ) do + {:ok, _pid} -> :ok + {:error, {:already_started, _pid}} -> :ok + {:error, :already_present} -> :ok + end + + :ok + end + + # Stubs run_sql for all infrastructure calls (conversation lookup, message load, + # conversation insert) and notifies the test pid whenever a message INSERT occurs. + defp setup_conversation do + conv_name = "test_conv_#{System.unique_integer([:positive])}" + conv_id = :crypto.strong_rand_bytes(16) + test_pid = self() + + stub(ElixirAi.Data.DbHelpers, :run_sql, fn sql, params, _topic -> + cond do + String.contains?(sql, "SELECT id FROM conversations") -> + [%{"id" => conv_id}] + + String.contains?(sql, "INSERT INTO messages") -> + send(test_pid, {:insert_message, params}) + [] + + true -> + [] + end + end) + + # 4-arity version used by Conversation.all_names/0 + stub(ElixirAi.Data.DbHelpers, :run_sql, fn _sql, _params, _topic, _schema -> [] end) + + provider_id = Ecto.UUID.generate() + {:ok, _pid} = ElixirAi.ConversationManager.create_conversation(conv_name, provider_id) + conv_name + end + + test "run_sql is called with user message params" do + conv_name = setup_conversation() + stub(ElixirAi.ChatUtils, :request_ai_response, fn _server, _messages, _tools -> :ok end) + + ElixirAi.ChatRunner.new_user_message(conv_name, "hello world") + + assert_receive {:insert_message, params}, 2000 + assert params["role"] == "user" + assert params["content"] == "hello world" + end + + test "run_sql is called with assistant message params" do + conv_name = setup_conversation() + + stub(ElixirAi.ChatUtils, :request_ai_response, fn server, _messages, _tools -> + id = make_ref() + send(server, {:start_new_ai_response, id}) + send(server, {:ai_text_chunk, id, "Hello from AI"}) + send(server, {:ai_text_stream_finish, id}) + :ok + end) + + ElixirAi.ChatRunner.new_user_message(conv_name, "hi") + + assert_receive {:insert_message, %{"role" => "user"}}, 2000 + assert_receive {:insert_message, params}, 2000 + assert params["role"] == "assistant" + assert params["content"] == "Hello from AI" + end + + test "run_sql is called with tool request and tool result message params" do + conv_name = setup_conversation() + + # First AI call triggers the tool; subsequent calls (after tool completes) are no-ops. + expect(ElixirAi.ChatUtils, :request_ai_response, fn server, _messages, _tools -> + id = make_ref() + send(server, {:start_new_ai_response, id}) + + send( + server, + {:ai_tool_call_start, id, {"store_thing", ~s({"name":"k","value":"v"}), 0, "tc_1"}} + ) + + send(server, {:ai_tool_call_end, id}) + :ok + end) + + stub(ElixirAi.ChatUtils, :request_ai_response, fn _server, _messages, _tools -> :ok end) + + ElixirAi.ChatRunner.new_user_message(conv_name, "store something") + + assert_receive {:insert_message, %{"role" => "user"}}, 2000 + + # Assistant message that carries the tool_calls list + assert_receive {:insert_message, params}, 2000 + assert params["role"] == "assistant" + refute is_nil(params["tool_calls"]) + + # Tool result message + assert_receive {:insert_message, params}, 2000 + assert params["role"] == "tool" + assert params["tool_call_id"] == "tc_1" + end +end diff --git a/test/stream_line_utils_test.exs b/test/stream_line_utils_test.exs index 2d8de9e..338fd5c 100644 --- a/test/stream_line_utils_test.exs +++ b/test/stream_line_utils_test.exs @@ -1,5 +1,6 @@ defmodule ElixirAi.AiUtils.StreamLineUtilsTest do - use ExUnit.Case + use ElixirAi.TestCase + @moduletag capture_log: true import ElixirAi.StreamChunkHelpers alias ElixirAi.AiUtils.StreamLineUtils diff --git a/test/support/test_case.ex b/test/support/test_case.ex new file mode 100644 index 0000000..9c47659 --- /dev/null +++ b/test/support/test_case.ex @@ -0,0 +1,19 @@ +defmodule ElixirAi.TestCase do + use ExUnit.CaseTemplate + use Mimic + + using do + quote do + use Mimic + end + end + + setup :set_mimic_global + + setup do + stub(ElixirAi.Data.DbHelpers, :run_sql, fn _sql, _params, _topic -> [] end) + stub(ElixirAi.Data.DbHelpers, :run_sql, fn _sql, _params, _topic, _schema -> [] end) + stub(ElixirAi.ChatUtils, :request_ai_response, fn _server, _messages, _tools -> :ok end) + :ok + end +end diff --git a/test/test_helper.exs b/test/test_helper.exs index 869559e..6aaf218 100644 --- a/test/test_helper.exs +++ b/test/test_helper.exs @@ -1 +1,3 @@ ExUnit.start() +Mimic.copy(ElixirAi.Data.DbHelpers) +Mimic.copy(ElixirAi.ChatUtils)