1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
//! Type definitions for the harness testkit.
//!
//! Every public struct and enum in `crate::harness::testkit` is declared here.
//! Implementations, constructors, and trait impls live in `mod.rs`; focused
//! tests live in `test.rs`.
use VecDeque;
use ;
use Duration;
use crate;
use crate;
use crateToolCall;
// ---------------------------------------------------------------------------
// StreamingMock
// ---------------------------------------------------------------------------
/// A [`crate::harness::model::ChatModel`] that yields a scripted sequence of
/// [`ModelStreamItem`]s, exercising the real streaming pipeline deterministically.
///
/// Each call to [`crate::harness::model::ChatModel::stream`] replays the same
/// scripted items, and [`crate::harness::model::ChatModel::invoke`] returns the
/// merged response those items fold into (via
/// [`crate::harness::model::StreamAccumulator`]), so the mock behaves
/// consistently on both the streaming and unary paths.
///
/// # Example
///
/// ```rust
/// # use tinyagents::harness::testkit::StreamingMock;
/// // Streams "Hello, world" as three message deltas plus a merged completion.
/// let model = StreamingMock::from_text_chunks(["Hello", ", ", "world"]);
/// ```
// ---------------------------------------------------------------------------
// SlowModel
// ---------------------------------------------------------------------------
/// A [`crate::harness::model::ChatModel`] that sleeps for a fixed delay before
/// replying, used to deterministically trigger the agent loop's per-model-call
/// wall-clock timeout.
///
/// Both [`crate::harness::model::ChatModel::invoke`] and
/// [`crate::harness::model::ChatModel::stream`] first
/// `tokio::time::sleep(delay).await` and then return a fixed assistant reply.
/// (The `stream` path inherits the delay because the default trait
/// implementation delegates to `invoke`.) Configure the run with a wall-clock
/// timeout much smaller than `delay` (for example a 20 ms timeout against a
/// 200 ms delay) so the call is reliably interrupted with
/// [`crate::error::TinyAgentsError::Timeout`].
///
/// # Example
///
/// ```rust
/// # use std::time::Duration;
/// # use tinyagents::harness::testkit::SlowModel;
/// // Sleeps 200ms before echoing a fixed reply.
/// let model = SlowModel::new(Duration::from_millis(200), "slow reply");
/// ```
// ---------------------------------------------------------------------------
// ScriptedModel
// ---------------------------------------------------------------------------
/// A [`crate::harness::model::ChatModel`] that returns pre-loaded responses in
/// order, making model behavior fully deterministic in tests.
///
/// Responses are consumed from the front of the queue one per `invoke` call.
/// When the queue is exhausted `invoke` returns
/// [`crate::error::TinyAgentsError::Model`] rather than panicking so tests get
/// a clear error instead of a thread panic.
///
/// # Example
///
/// ```rust
/// # use tinyagents::harness::testkit::ScriptedModel;
/// # use tinyagents::harness::model::ModelResponse;
/// let model = ScriptedModel::replies(vec!["Hello", "World"]);
/// // Use in tests as a ChatModel<()>.
/// ```
// ---------------------------------------------------------------------------
// FakeTool
// ---------------------------------------------------------------------------
/// The runtime behavior chosen when a [`FakeTool`] is invoked.
pub
/// A configurable [`crate::harness::tool::Tool`] for testing.
///
/// Created with one of three factory methods:
///
/// - [`FakeTool::new`] — returns an empty string result.
/// - [`FakeTool::returning`] — returns a fixed text content.
/// - [`FakeTool::failing`] — returns a [`crate::error::TinyAgentsError::Tool`] error.
///
/// Every received [`ToolCall`] is recorded and available via
/// [`FakeTool::calls`].
///
/// # Example
///
/// ```rust
/// # use tinyagents::harness::testkit::FakeTool;
/// let tool = FakeTool::returning("search", "42");
/// // Use as Tool<()> in tests.
/// ```
// ---------------------------------------------------------------------------
// DeterministicClock
// ---------------------------------------------------------------------------
/// A controllable, monotonic clock for deterministic test scenarios.
///
/// Unlike `SystemTime`, `DeterministicClock` never advances on its own. Tests
/// call [`DeterministicClock::advance`] to move time forward in a controlled
/// way, making time-sensitive assertions reproducible.
///
/// # Example
///
/// ```rust
/// # use tinyagents::harness::testkit::DeterministicClock;
/// let clock = DeterministicClock::new(1_000);
/// assert_eq!(clock.now_millis(), 1_000);
/// clock.advance(500);
/// assert_eq!(clock.now_millis(), 1_500);
/// ```
// ---------------------------------------------------------------------------
// DeterministicIds
// ---------------------------------------------------------------------------
/// A monotonically incrementing identifier generator for stable test output.
///
/// Produces ids in the form `"{prefix}-0"`, `"{prefix}-1"`, ... so tests can
/// assert exact id values without relying on UUIDs or wall-clock timestamps.
///
/// # Example
///
/// ```rust
/// # use tinyagents::harness::testkit::DeterministicIds;
/// let ids = DeterministicIds::new("call");
/// assert_eq!(ids.next(), "call-0");
/// assert_eq!(ids.next(), "call-1");
/// ```
// ---------------------------------------------------------------------------
// EventRecorder
// ---------------------------------------------------------------------------
/// Captures [`AgentEvent`]s emitted through an [`EventSink`] for later
/// inspection.
///
/// The recorder owns an internal [`RecordingListener`] subscribed to a shared
/// [`EventSink`]. Callers obtain the sink via [`EventRecorder::sink`] and pass
/// it to the component under test. After the run, [`EventRecorder::events`]
/// and [`EventRecorder::kinds`] provide access to what was emitted.
///
/// # Example
///
/// ```rust
/// # use tinyagents::harness::testkit::EventRecorder;
/// # use tinyagents::harness::events::AgentEvent;
/// # use tinyagents::harness::ids::RunId;
/// let recorder = EventRecorder::new();
/// let sink = recorder.sink();
/// sink.emit(AgentEvent::RunStarted { run_id: RunId::new("r1"), thread_id: None });
/// assert_eq!(recorder.kinds(), vec!["run.started"]);
/// ```
// ---------------------------------------------------------------------------
// Trajectory
// ---------------------------------------------------------------------------
/// An ordered view of [`AgentEvent`]s with structural assertion helpers.
///
/// `Trajectory` lets tests make deterministic claims about *what happened*
/// during a run (which tools were called, how many model calls occurred, whether
/// the run completed) without depending on exact LLM prose or wall-clock timing.
///
/// ## `assert_*` variants vs predicate methods
///
/// Each feature is exposed both as a predicate (`tool_was_called`) and as an
/// asserting helper (`assert_tool_called`) that panics with a descriptive
/// message on failure, matching Rust's `assert!` / `assert_eq!` ergonomics.
///
/// ## Assertion methods returning `Result`
///
/// [`Trajectory::assert_order`] returns
/// [`crate::error::Result`]`<()>` rather than panicking so callers can
/// propagate the error or inspect the message programmatically.
///
/// # Example
///
/// ```rust
/// # use tinyagents::harness::testkit::Trajectory;
/// # use tinyagents::harness::events::AgentEvent;
/// # use tinyagents::harness::ids::{RunId, CallId};
/// let events = vec![
/// AgentEvent::RunStarted { run_id: RunId::new("r1"), thread_id: None },
/// AgentEvent::ModelStarted { call_id: CallId::new("c1"), model: "gpt".into() },
/// AgentEvent::ModelCompleted { call_id: CallId::new("c1"), usage: None },
/// AgentEvent::RunCompleted { run_id: RunId::new("r1") },
/// ];
/// let traj = Trajectory::from_events(events);
/// assert_eq!(traj.model_call_count(), 1);
/// traj.assert_completed();
/// ```