Scriba.Target.Test (Scriba v0.1.0)

Copy Markdown View Source

In-memory Scriba.Target for property tests and user-facing test helpers.

Backed by an Agent that holds the commit log, per-stream cursors, and the in-memory dead-letter list. Records every committed event as {event_id, stream_id, position} in the order it was applied. Events whose handler returned :skip are not recorded.

Dead-letter routing: the Pipeline hands the Target a list of {event, error} tuples for events whose handler returned {:error, _} or raised. The Test target records them in its in-memory :dead_letters list, letting property tests assert dead-letter routing without a Repo.

Usage

{:ok, agent} = Scriba.Target.Test.start_link()

# Wired into a projection's :target spec as
#   {Scriba.Target.Test, agent: agent}

Scriba.Target.Test.commits(agent)
# => [{"evt-1", "stream-0", 1}, {"evt-2", "stream-1", 2}, ...]

Scriba.Target.Test.dead_letters(agent)
# => [%{event: %Scriba.Event{...}, error: {:error, :boom}}, ...]

Scriba.Target.Test.stream_positions(agent)
# => %{"stream-0" => 4, "stream-1" => 5}

Scriba.Target.Test.safe_position(agent)
# => 4   (= min across all stream cursors)

Summary

Functions

Returns a specification to start this module under a supervisor.

Returns the recorded {event_id, stream_id, position} tuples in the order they were committed.

Returns dead-letter entries in the order they were routed. Each entry is a %{event: %Scriba.Event{}, error: term()} map — the same error shape the Pipeline passed (an {:error, reason} tuple, or the engine- internal {:exception, exception, stacktrace} triple).

Clears the commit log, dead-letter list, and per-stream cursors.

Returns the safe replay point: the minimum cursor across all streams. 0 when no streams have committed yet.

Starts an in-memory commit log.

Returns the per-stream cursor map.

Types

agent()

@type agent() :: pid() | atom() | {:via, module(), term()}

dead_letter()

@type dead_letter() :: %{event: Scriba.Event.t(), error: term()}

Functions

child_spec(arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

commits(agent)

@spec commits(agent()) :: [{String.t(), String.t(), non_neg_integer()}]

Returns the recorded {event_id, stream_id, position} tuples in the order they were committed.

dead_letters(agent)

@spec dead_letters(agent()) :: [dead_letter()]

Returns dead-letter entries in the order they were routed. Each entry is a %{event: %Scriba.Event{}, error: term()} map — the same error shape the Pipeline passed (an {:error, reason} tuple, or the engine- internal {:exception, exception, stacktrace} triple).

reset(agent)

@spec reset(agent()) :: :ok

Clears the commit log, dead-letter list, and per-stream cursors.

safe_position(agent)

@spec safe_position(agent()) :: non_neg_integer()

Returns the safe replay point: the minimum cursor across all streams. 0 when no streams have committed yet.

start_link(opts \\ [])

@spec start_link(keyword()) :: Agent.on_start()

Starts an in-memory commit log.

Standard Agent.start_link/2 options are supported (:name, etc).

stream_positions(agent)

@spec stream_positions(agent()) :: %{required(String.t()) => non_neg_integer()}

Returns the per-stream cursor map.