Skip to content

Commit 4a65851

Browse files
committed
refactor(tests): remove MemoryEngineTest and streamline Python test outputs
- Deleted `MemoryEngineTest` module for test suite simplification - Removed unused function `tool_result_content` in `memory_engine.ex` for cleaner code - Updated Python test cases to handle serialized outputs using `Output.json` - Added test for bounding oversized Python output in `python_test.exs` to ensure result size compliance
1 parent c5e67e5 commit 4a65851

4 files changed

Lines changed: 24 additions & 62 deletions

File tree

lib/console/ai/chat/memory_engine.ex

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ defmodule Console.AI.Chat.MemoryEngine do
77
alias Console.AI.{Provider, Tool}
88
alias Console.AI.Chat.EnabledTools
99
alias Console.AI.Tools.{EnableTools, ToolSearch}
10-
alias Console.AI.Tools.Workbench.Output
1110
require Logger
1211

1312
@type t :: %__MODULE__{}
@@ -181,19 +180,15 @@ defmodule Console.AI.Chat.MemoryEngine do
181180
defp tool_msg(content, id, name, args, fun, attrs \\ %{})
182181
defp tool_msg(content, id, name, args, _, attrs) when is_binary(content),
183182
do: {:tool, content, %{call_id: id, name: name, arguments: args, attributes: attrs}}
184-
defp tool_msg(%{content: content} = msg, id, name, args, _, _) when is_binary(content),
183+
defp tool_msg(%{content: content} = msg, id, name, args, _, _),
185184
do: {:tool, content, %{call_id: id, name: name, arguments: args, attributes: Map.delete(msg, :content)}}
186185
defp tool_msg(result, id, name, args, fmt, attrs) when is_function(fmt, 1) do
187186
case fmt.(result) do
188187
content when is_binary(content) -> {result, {:tool, content, %{call_id: id, name: name, arguments: args, attributes: attrs}}}
189-
_ -> tool_msg(tool_result_content(result), id, name, args, fmt, attrs)
188+
_ -> result
190189
end
191190
end
192191

193-
defp tool_result_content(result), do: tool_result_content(result, Output.json(result))
194-
defp tool_result_content(_, {:ok, content}), do: content
195-
defp tool_result_content(result, {:error, _}), do: inspect(result)
196-
197192
defp msg({res, {:tool, _, _}}, :result), do: res
198193
defp msg({_, {:tool, _, _} = tool}, :tool), do: tool
199194
defp msg(pass, _), do: pass

lib/console/ai/tools/workbench/python.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
defmodule Console.AI.Tools.Workbench.Python do
22
use Console.AI.Tools.Workbench.Base
33
alias CloudQuery.Client
4+
alias Console.AI.Tools.Workbench.Output
45
alias Toolquery.ToolQuery.Stub
56
alias Toolquery.{RunPythonInput, RunPythonOutput}
67

@@ -32,7 +33,7 @@ defmodule Console.AI.Tools.Workbench.Python do
3233
request = %RunPythonInput{script: code, input_json: input_json},
3334
{:ok, %RunPythonOutput{result_json: result_json, stdout: stdout}} <- Stub.run_python(client, request, Client.cloud_query_rpc_opts()),
3435
{:ok, result} <- Jason.decode(result_json) do
35-
{:ok, %{result: result, stdout: stdout}}
36+
Output.json(%{result: result, stdout: stdout})
3637
end
3738
end
3839
end

test/console/ai/chat/memory_engine_test.exs

Lines changed: 0 additions & 50 deletions
This file was deleted.

test/console/ai/tools/workbench/python_test.exs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ defmodule Console.AI.Tools.Workbench.PythonTest do
33
use Mimic
44

55
alias CloudQuery.Client
6-
alias Console.AI.Tools.Workbench.Python
6+
alias Console.AI.Tools.Workbench.{Output, Python}
77
alias Toolquery.{RunPythonOutput}
88
alias Toolquery.ToolQuery.Stub
99

@@ -22,7 +22,7 @@ defmodule Console.AI.Tools.Workbench.PythonTest do
2222
assert %{code: ["can't be blank"], explanation: ["can't be blank"]} = errors_on(changeset)
2323
end
2424

25-
test "runs Monty Python with JSON input and returns decoded output and stdout" do
25+
test "runs Monty Python with JSON input and returns serialized output and stdout" do
2626
expect(Client, :connect, fn -> {:ok, :channel} end)
2727

2828
expect(Stub, :run_python, fn :channel, request, opts ->
@@ -33,11 +33,13 @@ defmodule Console.AI.Tools.Workbench.PythonTest do
3333
{:ok, %RunPythonOutput{result_json: ~s({"total":42}), stdout: "calculated total\n"}}
3434
end)
3535

36-
assert {:ok, %{result: %{"total" => 42}, stdout: "calculated total\n"}} =
36+
assert {:ok, output} =
3737
Python.implement(%Python{
3838
code: "output['total'] = input['first'] + input['second']",
3939
input: %{"first" => 20, "second" => 22}
4040
})
41+
42+
assert Jason.decode!(output) == %{"result" => %{"total" => 42}, "stdout" => "calculated total\n"}
4143
end
4244

4345
test "defaults omitted input to an empty JSON object" do
@@ -48,6 +50,20 @@ defmodule Console.AI.Tools.Workbench.PythonTest do
4850
{:ok, %RunPythonOutput{result_json: "{}", stdout: ""}}
4951
end)
5052

51-
assert {:ok, %{result: %{}, stdout: ""}} = Python.implement(%Python{code: "output = {}"})
53+
assert {:ok, output} = Python.implement(%Python{code: "output = {}"})
54+
assert Jason.decode!(output) == %{"result" => %{}, "stdout" => ""}
55+
end
56+
57+
test "bounds oversized Python output" do
58+
expect(Client, :connect, fn -> {:ok, :channel} end)
59+
60+
expect(Stub, :run_python, fn :channel, _request, _opts ->
61+
result = Jason.encode!(%{"value" => String.duplicate("x", Output.max_bytes())})
62+
{:ok, %RunPythonOutput{result_json: result, stdout: ""}}
63+
end)
64+
65+
assert {:ok, output} = Python.implement(%Python{code: "output = {}"})
66+
assert byte_size(output) == Output.max_bytes()
67+
assert output =~ "output truncated at 50 KiB"
5268
end
5369
end

0 commit comments

Comments
 (0)