ExAgent.Case (ExAgent v3.0.0)

View Source

ExUnit assertions for deterministic agent tests.

Stability — Stable.

Use this module in a test case with the same options as ExUnit.Case, or import its helpers into an existing test module. See the testing guide.

Summary

Functions

Use ExUnit.Case and import the agent helpers.

Assert {:estimated, cents}, {:partial, subtotal_cents} or :unknown.

Return the first matching event from a collected list, or fail the assertion.

Assert a successful terminal and compare its accepted output with ===.

Assert that the retained calls of this run include the named tool.

Assert any of :input, :output and :quality. For token counts use {:complete, count}, {:partial, subtotal} or {:unknown, nil}. The optional quality string is the accounting marker, for example "normalized". Coverage follows ExAgent.Eval.Report, including interrupted attempts.

Decode a portable evaluation case. See ExAgent.Eval.Case.from_map/1.

Build a deterministic TestModel with a nonempty script of requests. Use strings, tool-call batches, ExAgent.Message.Response structs or ExAgent.Models.Test.stream/2 entries, as with ExAgent.Models.Test.

Encode a portable evaluation case. See ExAgent.Eval.Case.to_map/1.

Types

cost()

@type cost() :: {:estimated | :partial, non_neg_integer() | float()} | :unknown

count()

@type count() :: {:complete | :partial, non_neg_integer()} | {:unknown, nil}

event()

@type event() ::
  ExAgent.Event.t() | {atom(), term()} | {:child, map(), {atom(), term()}}

terminal()

@type terminal() ::
  ExAgent.Result.t()
  | ExAgent.RunError.t()
  | {:ok | :result, ExAgent.Result.t()}
  | {:error, ExAgent.RunError.t()}

Functions

__using__(opts)

(macro)

Use ExUnit.Case and import the agent helpers.

assert_cost(terminal, expected)

@spec assert_cost(terminal(), cost()) :: :ok

Assert {:estimated, cents}, {:partial, subtotal_cents} or :unknown.

assert_event(events, type, predicate \\ fn _ -> true end)

@spec assert_event([event()], atom(), (term() -> as_boolean(term()))) :: event()

Return the first matching event from a collected list, or fail the assertion.

Accepts ExAgent.Event structs and elements of ExAgent.run_stream/3. The optional predicate receives the payload. For :child it receives a pair of the child lineage and its stream element. Collect the stream once before asserting on it.

assert_output(terminal, expected)

@spec assert_output(terminal(), term()) :: :ok

Assert a successful terminal and compare its accepted output with ===.

assert_tool_called(terminal, name, opts \\ [])

@spec assert_tool_called(terminal(), String.t(), keyword()) :: :ok

Assert that the retained calls of this run include the named tool.

Options are :times for an exact count, :status for a matching tool return, and :args for a predicate on the original arguments. See ExAgent.Eval.tool_called/2 for how batches, omitted parts and retries affect the evidence.

assert_usage(terminal, opts)

@spec assert_usage(terminal(), input: count(), output: count(), quality: String.t()) ::
  :ok

Assert any of :input, :output and :quality. For token counts use {:complete, count}, {:partial, subtotal} or {:unknown, nil}. The optional quality string is the accounting marker, for example "normalized". Coverage follows ExAgent.Eval.Report, including interrupted attempts.

from_map(doc)

@spec from_map(map()) ::
  {:ok, ExAgent.Eval.Case.t()}
  | {:error, :invalid_case | :unsupported_case_version}

Decode a portable evaluation case. See ExAgent.Eval.Case.from_map/1.

test_model(script)

Build a deterministic TestModel with a nonempty script of requests. Use strings, tool-call batches, ExAgent.Message.Response structs or ExAgent.Models.Test.stream/2 entries, as with ExAgent.Models.Test.

to_map(entry)

@spec to_map(ExAgent.Eval.Case.t()) :: {:ok, map()} | {:error, :invalid_case}

Encode a portable evaluation case. See ExAgent.Eval.Case.to_map/1.