loopctl 0.1.0

A trait-based framework for building agent loops with pluggable LLM clients, tools, and memory
Documentation
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
//! Testing utilities — mock components and fixture factories for loopctl tests.
//!
//! Reusable mocks and fixture factories for testing
//! code that depends on loopctl traits. Instead of wiring up real API
//! clients and tools in tests, these stubs can be used to exercise
//! agent logic in isolation, assert on streaming events, and verify tool
//! dispatch — all without network calls or external dependencies.
//!
//! The module is only compiled when the `testing` feature is enabled, so
//! it has zero impact on production builds.
//!
//! # Architecture
//!
//! The testing module follows a standard mock-object pattern: each mock
//! implements a production trait ([`ApiClient`], [`Tool`]) but returns
//! preconfigured data instead of calling external services. Builder methods
//! let you configure what the mock returns before passing it to the code
//! under test.
//!
//! ```text
//! ┌──────────────┐     implements      ┌──────────────┐
//! │ MockApiClient├────────────────────►│  ApiClient   │
//! └──────┬───────┘                     └──────────────┘
//!        │ returns                            ▲
//!        ▼                                    │
//! ┌──────────────┐                            │
//! │ MockResponse │   emitted as StreamEvents  │
//! └──────────────┘                            │
//!//! ┌──────────────┐     implements      ┌──────┴───────┐
//! │   MockTool   ├────────────────────►│    Tool      │
//! └──────────────┘                     └──────────────┘
//! ```
//!
//! # Available Mocks
//!
//! - [`MockApiClient`] — A canned [`ApiClient`] that returns preconfigured
//!   streaming responses, tool calls, or errors.
//! - [`MockTool`] — A stub [`Tool`] that returns a fixed result or error
//!   when invoked.
//!
//! # Supporting Types
//!
//! - [`MockResponse`] — A single canned response used by [`MockApiClient`].
//! - [`MockToolCall`] — A single canned tool call embedded in a [`MockResponse`].
//!
//! # Fixture Factories
//!
//! - [`test_message`] — Create a test user [`Message`].
//! - [`test_assistant_message`] — Create a test assistant [`Message`].
//! - [`test_tool_use_message`] — Create an assistant [`Message`] with
//!   tool-call content blocks.
//! - [`test_config`] — Create a test [`LoopConfig`] with sensible defaults.
//! - [`test_config_with_id`] — Create a test [`LoopConfig`] with a specific
//!   session ID.
//!
//! # Quick Start
//!
//! ```rust
//! use loopctl::testing::{MockApiClient, MockTool, test_config};
//! use loopctl::tool::ToolRegistry;
//!
//! // Build a mock client that streams "Hello, world!" and stops.
//! let client = MockApiClient::new("test-model")
//!     .with_text_response("Hello, world!")
//!     .with_stop_reason("end_turn");
//!
//! // Build a mock tool and register it.
//! let tool = MockTool::new("echo", "Echoes input")
//!     .with_result("Echo: hello");
//!
//! let mut registry = ToolRegistry::new();
//! registry.register(tool);
//!
//! // Grab a test agent config.
//! let config = test_config();
//! ```
//!
//! # Multi-turn Example
//!
//! ```
//! use loopctl::testing::{MockApiClient, MockResponse, MockToolCall};
//! use serde_json::json;
//!
//! let client = MockApiClient::new("test-model").with_responses(vec![
//!     MockResponse {
//!         text: "Let me look that up.".into(),
//!         tool_call: Some(MockToolCall {
//!             id: "call_1".into(),
//!             name: "search".into(),
//!             input: json!({"query": "rust"}),
//!         }),
//!         stop_reason: "tool_use".into(),
//!     },
//!     MockResponse {
//!         text: "Here is what I found.".into(),
//!         tool_call: None,
//!         stop_reason: "end_turn".into(),
//!     },
//! ]);

use crate::api::ApiClient;
use crate::api::error::ApiError;
use crate::config::LoopConfig;
use crate::message::{Message, MessagePart, Role};
use crate::stream::{
    DeltaPart, IndexedDelta, MessageDelta, MessageDeltaPayload, MessageMetadata, MessageStart,
    PartStart, StreamEvent, Usage,
};
use crate::tool::{Tool, ToolContext, ToolError, ToolOutput, ToolSchema};
use futures::Stream;
use serde_json::{Value, json};
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;

use parking_lot::Mutex;
use uuid::Uuid;

// ==================================================
// MockApiClient
// ==================================================

/// A mock [`ApiClient`] that returns preconfigured streaming responses.
///
/// Use this in unit tests to exercise agent logic without making real
/// API calls. Configure what the client returns using the builder-style
/// methods:
///
/// - [`with_text_response`](MockApiClient::with_text_response) — set the
///   text the model "says".
/// - [`with_tool_call`](MockApiClient::with_tool_call) — make the model
///   request a tool invocation.
/// - [`with_stop_reason`](MockApiClient::with_stop_reason) — override the
///   stop reason (e.g. `"end_turn"`, `"tool_use"`).
/// - [`with_responses`](MockApiClient::with_responses) — queue multiple
///   responses for multi-turn tests.
/// - [`with_error`](MockApiClient::with_error) — simulate an API error on
///   every call.
///
/// For multi-turn scenarios, chain multiple [`MockResponse`] values via
/// [`with_responses`](MockApiClient::with_responses). Each call to
/// [`stream_messages`](ApiClient::stream_messages) pops the next response;
/// once exhausted the last response is repeated.
///
/// # Construction
///
/// ```rust
/// use loopctl::testing::MockApiClient;
///
/// let client = MockApiClient::new("test-model")
///     .with_text_response("I'm here to help!");
/// ```
///
/// # Multi-turn
///
/// ```rust
/// use loopctl::testing::{MockApiClient, MockResponse, MockToolCall};
/// use serde_json::json;
///
/// let client = MockApiClient::new("test-model").with_responses(vec![
///     MockResponse {
///         text: "Let me look that up.".into(),
///         tool_call: Some(MockToolCall {
///             id: "call_1".into(),
///             name: "search".into(),
///             input: json!({"query": "rust"}),
///         }),
///         stop_reason: "tool_use".into(),
///     },
///     MockResponse {
///         text: "Here is what I found.".into(),
///         tool_call: None,
///         stop_reason: "end_turn".into(),
///     },
/// ]);
/// ```
#[derive(Clone)]
pub struct MockApiClient {
    /// The model name returned by [`model`](ApiClient::model).
    ///
    /// Set via [`MockApiClient::new`]. Typically something like
    /// `"test-model"` — it is not validated against any real model
    /// registry.
    ///
    /// The value is returned verbatim by the [`ApiClient::model`]
    /// implementation. It appears in log messages and
    /// [`MessageMetadata`] fields.
    model_name: Arc<parking_lot::Mutex<String>>,

    /// The queue of canned responses.
    ///
    /// Each call to [`stream_messages`](ApiClient::stream_messages) pops
    /// the front entry. When only one entry remains it is cloned and
    /// reused so the client never runs dry.
    ///
    /// Shared via `Arc<Mutex<...>>` so the cloned handle inside the
    /// `impl ApiClient` methods can mutate the queue.
    ///
    /// Use [`MockApiClient::with_responses`] to replace the entire
    /// queue or individual builder methods to mutate the front entry.
    responses: Arc<Mutex<Vec<MockResponse>>>,

    /// When set, every call returns an [`ApiError`] instead of a normal
    /// response.
    ///
    /// Set via [`MockApiClient::with_error`]. Takes precedence over all
    /// other configuration — useful for testing error-handling paths.
    error: Option<String>,
}

/// A single canned response produced by [`MockApiClient`].
///
/// Each [`MockResponse`] describes exactly one assistant reply: the text
/// the model "says", an optional tool call, and a stop reason. The mock
/// client translates these fields into the appropriate
/// [`StreamEvent`] sequence when [`stream_messages`](ApiClient::stream_messages)
/// is called.
///
/// # Construction
///
/// Build directly or via [`MockApiClient::with_responses`]:
///
/// ```rust
/// use loopctl::testing::MockResponse;
///
/// let response = MockResponse {
///     text: "Done.".to_string(),
///     tool_call: None,
///     stop_reason: "end_turn".to_string(),
/// };
/// ```
#[derive(Clone, Default)]
pub struct MockResponse {
    /// The text content the assistant should produce.
    ///
    /// Emitted as a [`IndexedDelta`] event containing a
    /// [`DeltaPart::Text`] variant. Defaults to `"Hello!"` when
    /// constructed via [`MockApiClient::new`].
    ///
    /// The text is sent as a single delta — the mock does not break it
    /// into chunks. If your test needs to verify incremental streaming,
    /// construct the [`StreamEvent`] sequence manually.
    pub text: String,

    /// An optional tool call the assistant should make.
    ///
    /// When present, the mock emits a second content block of type
    /// [`MessagePart::ToolCall`] and sets the stop reason to
    /// `"tool_use"`. See [`MockToolCall`] for the fields involved.
    ///
    /// Set to `None` for a plain text response. Set to `Some(...)` when
    /// testing tool dispatch or multi-turn tool-call scenarios.
    pub tool_call: Option<MockToolCall>,

    /// The stop reason for this response.
    ///
    /// Common values are `"end_turn"` (normal completion) and
    /// `"tool_use"` (agent should execute a tool). Defaults to
    /// `"end_turn"`.
    ///
    /// The agent loop uses this to decide whether to continue
    /// processing or to finalize the session. Setting it to
    /// `"max_tokens"` is useful for testing truncation handling.
    pub stop_reason: String,
}

/// A single canned tool call embedded in a [`MockResponse`].
///
/// When [`MockResponse::tool_call`] is `Some`, the mock client emits a
/// `tool_use` content block with these fields, allowing tests to verify
/// that the agent correctly dispatches tool invocations.
///
/// # Example
///
/// ```rust
/// use loopctl::testing::MockToolCall;
/// use serde_json::json;
///
/// let call = MockToolCall {
///     id: "call_1".to_string(),
///     name: "search".to_string(),
///     input: json!({"query": "hello"}),
/// };
/// ```
#[derive(Clone)]
pub struct MockToolCall {
    /// The tool call identifier.
    ///
    /// Matches the `id` field on the [`MessagePart::ToolCall`] variant
    /// produced by the mock. Agents use this ID to correlate the tool
    /// result back to the originating request.
    ///
    /// Typically a string like `"call_1"`, `"call_2"`, etc. Must be
    /// unique within a single assistant turn to avoid ambiguity.
    pub id: String,

    /// The name of the tool to invoke.
    ///
    /// Must correspond to a tool registered in the agent's
    /// [`ToolRegistry`](crate::tool::ToolRegistry). The mock does not
    /// validate this — passes the name through directly.
    ///
    /// During test execution, if the agent loop cannot find a tool
    /// with this name in the registry, it will return an error.
    pub name: String,

    /// The tool input as a JSON value.
    ///
    /// Serialized as the `input` field on the [`MessagePart::ToolCall`]
    /// variant. Use [`serde_json::json!`] to construct it ergonomically.
    ///
    /// The value is passed as-is to the tool's [`Tool::call`] method
    /// (in a real scenario; the mock itself ignores it).
    pub input: Value,
}

// ===================================================
// MockApiClient — construction & builder methods
// ===================================================

/// Construction and builder methods for [`MockApiClient`].
///
/// The builder pattern lets you configure mock responses fluently.
/// All builder methods consume and return `Self`, so you can chain
/// them directly after [`MockApiClient::new`].
///
/// # Response lifecycle
///
/// ```text
/// new(model) → [MockResponse { text: "Hello!", stop: "end_turn" }]
//////   ├── with_text_response(...)  → mutate text on front response
///   ├── with_tool_call(...)      → add tool_call + set stop to "tool_use"
///   ├── with_stop_reason(...)    → override stop reason
///   ├── with_responses(...)      → replace entire queue
///   └── with_error(...)          → force error on every call
/// ```
impl MockApiClient {
    /// Create a new mock client with the given model name.
    ///
    /// Returns a client preloaded with a single default response:
    /// text `"Hello!"` with stop reason `"end_turn"` and no tool call.
    /// Use the builder methods to customize before passing the client
    /// to the code under test.
    ///
    /// The default response is simple so that most tests
    /// only need to call [`with_text_response`](MockApiClient::with_text_response)
    /// to get started.
    ///
    /// # Example
    ///
    /// ```rust
    /// use loopctl::api::ApiClient;
    /// use loopctl::testing::MockApiClient;
    ///
    /// let client = MockApiClient::new("test-model");
    /// assert_eq!(client.model(), "test-model");
    /// ```
    #[must_use]
    pub fn new(model: &str) -> Self {
        // Build the default single-response queue.
        //
        // This response is intentionally minimal — just text `"Hello!"`
        // with `"end_turn"` — so that tests that don't care about the
        // response content can use the mock without any configuration.
        let default_response = MockResponse {
            text: "Hello!".to_string(),
            tool_call: None,
            stop_reason: "end_turn".to_string(),
        };
        Self {
            model_name: Arc::new(parking_lot::Mutex::new(model.to_string())),
            responses: Arc::new(Mutex::new(vec![default_response])),
            error: None,
        }
    }

    /// Set the text response for the first (or only) turn.
    ///
    /// Overwrites the `text` field on the initial [`MockResponse`]
    /// created by [`new`](MockApiClient::new). Simplest way
    /// to configure a single-turn mock — the model will "say" the given
    /// text and stop.
    ///
    /// If you need to set text for multiple turns, use
    /// [`with_responses`](MockApiClient::with_responses) instead.
    ///
    /// # Example
    ///
    /// ```rust
    /// use loopctl::testing::MockApiClient;
    ///
    /// let client = MockApiClient::new("test-model")
    ///     .with_text_response("I am a test assistant.");
    /// ```
    #[must_use]
    pub fn with_text_response(self, text: &str) -> Self {
        if let Some(r) = self.responses.lock().first_mut() {
            r.text = text.to_string();
        }
        self
    }

    /// Add a tool call to the first (or only) response.
    ///
    /// The mock will emit a `tool_use` content block and set the stop
    /// reason to `"tool_use"` so the agent loop knows to execute the
    /// tool. The `id`, `name`, and `input` parameters map directly to
    /// the fields on [`MockToolCall`].
    ///
    /// The tool `name` should match a tool registered in the agent's
    /// [`ToolRegistry`](crate::tool::ToolRegistry) — otherwise the
    /// agent loop will fail when it tries to dispatch the call.
    ///
    /// # Example
    ///
    /// ```rust
    /// use loopctl::testing::MockApiClient;
    /// use serde_json::json;
    ///
    /// let client = MockApiClient::new("test-model")
    ///     .with_tool_call("call_1", "bash", json!({"command": "ls"}));
    /// ```
    #[must_use]
    pub fn with_tool_call(self, id: &str, name: &str, input: Value) -> Self {
        let mut responses = self.responses.lock();
        if let Some(r) = responses.first_mut() {
            r.tool_call = Some(MockToolCall {
                id: id.to_string(),
                name: name.to_string(),
                input,
            });
            r.stop_reason = "tool_use".to_string();
        }
        drop(responses);
        self
    }

    /// Override the stop reason on the first (or only) response.
    ///
    /// Common values are `"end_turn"` (default) and `"tool_use"`.
    ///
    /// Note that [`with_tool_call`](MockApiClient::with_tool_call)
    /// automatically sets the stop reason to `"tool_use"`, so this method
    /// should be used when you want a different value.
    ///
    /// # Example
    ///
    /// ```rust
    /// use loopctl::testing::MockApiClient;
    ///
    /// let client = MockApiClient::new("test-model")
    ///     .with_stop_reason("max_tokens");
    /// ```
    #[must_use]
    pub fn with_stop_reason(self, reason: &str) -> Self {
        if let Some(r) = self.responses.lock().first_mut() {
            r.stop_reason = reason.to_string();
        }
        self
    }

    /// Set the full response queue for multi-turn behaviour.
    ///
    /// Each call to [`stream_messages`](ApiClient::stream_messages)
    /// pops the front entry. When only one entry remains it is cloned
    /// and reused, so the mock never panics on an empty queue.
    ///
    /// If `responses` is empty the call is a no-op (the default response
    /// is retained). Recommended way to set up complex
    /// multi-turn scenarios where the model needs to reply differently
    /// across successive turns.
    ///
    /// # Example
    ///
    /// ```rust
    /// use loopctl::testing::{MockApiClient, MockResponse};
    ///
    /// let client = MockApiClient::new("test-model").with_responses(vec![
    ///     MockResponse {
    ///         text: "First reply".into(),
    ///         tool_call: None,
    ///         stop_reason: "end_turn".into(),
    ///     },
    ///     MockResponse {
    ///         text: "Second reply".into(),
    ///         tool_call: None,
    ///         stop_reason: "end_turn".into(),
    ///     },
    /// ]);
    /// ```
    #[must_use]
    pub fn with_responses(self, responses: Vec<MockResponse>) -> Self {
        if !responses.is_empty() {
            *self.responses.lock() = responses;
        }
        self
    }

    /// Simulate an API error on every call.
    ///
    /// Once set, both [`stream_messages`](ApiClient::stream_messages)
    /// and [`create_message`](ApiClient::create_message) will return
    /// an [`ApiError`] immediately. This overrides any response
    /// configuration.
    ///
    /// Useful for exercising agent error-handling and retry paths.
    /// The error message is passed through to the [`ApiError`] so
    /// tests can assert on the specific error string.
    ///
    /// # Example
    ///
    /// ```rust
    /// use loopctl::testing::MockApiClient;
    ///
    /// let client = MockApiClient::new("test-model")
    ///     .with_error("rate limit exceeded");
    /// ```
    #[must_use]
    pub fn with_error(mut self, error: &str) -> Self {
        self.error = Some(error.to_string());
        self
    }

    /// Pop the next [`MockResponse`] from the internal queue.
    ///
    /// If more than one response is queued the front entry is removed
    /// and returned. If only one remains it is cloned in place so the
    /// queue never empties. This ensures repeated calls to
    /// [`stream_messages`](ApiClient::stream_messages) always succeed.
    ///
    /// Helper used by both
    /// [`stream_messages`](ApiClient::stream_messages) and
    /// [`create_message`](ApiClient::create_message). The method
    /// acquires the `Mutex` guard, so callers do not need
    /// to handle locking.
    ///
    /// # Queue exhaustion strategy
    ///
    /// ```text
    /// [R1, R2, R3]  → pop → R1, queue becomes [R2, R3]
    /// [R2, R3]      → pop → R2, queue becomes [R3]
    /// [R3]          → pop → R3, queue stays   [R3] (cloned)
    /// ```
    fn pop_response(&self) -> MockResponse {
        let mut guard = self.responses.lock();
        if guard.len() > 1 {
            guard.remove(0)
        } else {
            guard.first().cloned().unwrap_or_default()
        }
    }
}

// ===================================================
// MockApiClient — ApiClient trait implementation
// ===================================================

/// Trait implementation that turns canned [`MockResponse`] values into
/// real [`StreamEvent`] sequences and JSON payloads.
///
/// Both [`stream_messages`](ApiClient::stream_messages) and
/// [`create_message`](ApiClient::create_message) consult the internal
/// response queue (or the error override) and produce output that is
/// indistinguishable from a live API at the protocol level.
///
/// The translation layer converts each [`MockResponse`] into a
/// well-formed stream of [`StreamEvent`] variants — `MessageStart`,
/// `PartStart`, `IndexedDelta`, `MessagePartStop`,
/// `MessageDelta`, and `MessageStop` — so that any consumer expecting
/// the real API event protocol works without modification.
///
/// # Error path
///
/// When [`MockApiClient::with_error`] has been called, both methods
/// short-circuit and return an [`ApiError`] immediately, bypassing the
/// response queue entirely. This allows tests to verify error-handling
/// and retry logic without needing a flaky network.
///
/// # Ignored parameters
///
/// The `_messages`, `_system`, and `_tools` parameters are accepted for
/// trait compatibility but ignored — the mock always
/// returns its preconfigured response regardless of the input.
impl ApiClient for MockApiClient {
    /// Return the model name this mock was created with.
    ///
    /// Called by the framework to identify which model is being used
    /// throughout the session. Always returns the string passed to
    /// [`MockApiClient::new`] — the value is not validated against any
    /// real model registry, so any string is acceptable for testing.
    ///
    /// # Example
    ///
    /// ```rust
    /// use loopctl::api::ApiClient;
    /// use loopctl::testing::MockApiClient;
    ///
    /// let client = MockApiClient::new("my-test-model");
    /// assert_eq!(client.model(), "my-test-model");
    /// ```
    fn model(&self) -> String {
        self.model_name.lock().clone()
    }

    fn set_model(&self, model: &str) -> bool {
        if model.trim().is_empty() {
            return false;
        }
        *self.model_name.lock() = model.to_string();
        true
    }

    /// Stream a canned sequence of [`StreamEvent`]s for the next response.
    ///
    /// Called by the agent loop to obtain the model's reply. The mock
    /// translates the current [`MockResponse`] into the standard event
    /// sequence:
    ///
    /// ```text
    /// MessageStart → PartStart(text) → IndexedDelta(text) → MessagePartStop
    ///              → [PartStart(tool_use) → MessagePartStop]   (if tool_call is set)
    ///              → MessageDelta(stop_reason, usage) → MessageStop
    /// ```
    ///
    /// If [`with_error`](MockApiClient::with_error) was called the stream
    /// contains a single [`ApiError`] event instead.
    ///
    /// The `_messages`, `_system`, and `_tools` parameters are accepted for
    /// trait compatibility but ignored — the mock always
    /// returns the preconfigured response.
    ///
    /// # Usage tokens
    ///
    /// The mock always reports 50 input tokens and 25 output tokens in
    /// the [`MessageDelta`] event. This lets tests assert on usage data
    /// without needing a real model response.
    ///
    /// # Example
    ///
    /// ```rust
    /// # tokio::runtime::Runtime::new().unwrap().block_on(async {
    /// use loopctl::api::ApiClient;
    /// use loopctl::testing::MockApiClient;
    ///
    /// let client = MockApiClient::new("test-model").with_text_response("Hi!");
    /// let stream = client.stream_messages(vec![], None, None);
    /// let events: Vec<_> = futures::StreamExt::collect(stream).await;
    /// assert!(events.len() >= 4);
    /// # });
    /// ```
    fn stream_messages(
        &self,
        _messages: Vec<Message>,
        _system: Option<String>,
        _tools: Option<Vec<ToolSchema>>,
    ) -> Pin<Box<dyn Stream<Item = Result<StreamEvent, ApiError>> + Send + 'static>> {
        if let Some(ref err) = self.error {
            let err = err.clone();
            return Box::pin(futures::stream::once(
                async move { Err(ApiError::api(&err)) },
            ));
        }

        let response = self.pop_response();
        let model = self.model_name.lock().clone();
        let mut events: Vec<Result<StreamEvent, ApiError>> =
            vec![Ok(StreamEvent::MessageStart(MessageStart {
                message: MessageMetadata {
                    id: "msg_test".to_string(),
                    role: "assistant".to_string(),
                    model,
                },
            }))];

        // Text content block
        let text = response.text.clone();
        events.push(Ok(StreamEvent::PartStart(PartStart {
            index: 0,
            part: Some(MessagePart::text("")),
        })));

        // Text delta
        events.push(Ok(StreamEvent::IndexedDelta(IndexedDelta {
            index: 0,
            delta: DeltaPart::Text { text },
        })));

        events.push(Ok(StreamEvent::PartStop));

        // Tool call content block (if any)
        if let Some(tc) = &response.tool_call {
            events.push(Ok(StreamEvent::PartStart(PartStart {
                index: 1,
                part: Some(MessagePart::tool_call(&tc.id, &tc.name, tc.input.clone())),
            })));
            events.push(Ok(StreamEvent::PartStop));
        }

        // Message delta with stop reason
        events.push(Ok(StreamEvent::MessageDelta(MessageDelta {
            delta: MessageDeltaPayload {
                stop_reason: Some(response.stop_reason),
            },
            usage: Some(Usage::new(50, 25)),
        })));

        events.push(Ok(StreamEvent::MessageStop));

        Box::pin(futures::stream::iter(events))
    }

    /// Return a canned non-streaming JSON response.
    ///
    /// Called by code paths that use the non-streaming API. The mock
    /// returns a JSON object with a `content` array containing a single
    /// text block drawn from the current [`MockResponse`].
    ///
    /// If [`with_error`](MockApiClient::with_error) was called the
    /// future resolves to an [`ApiError`] instead, bypassing the
    /// response queue entirely.
    ///
    /// The `_messages`, `_system`, and `_tools` parameters are accepted
    /// for trait compatibility but ignored.
    ///
    /// # Example
    ///
    /// ```rust
    /// # tokio::runtime::Runtime::new().unwrap().block_on(async {
    /// use loopctl::api::ApiClient;
    /// use loopctl::testing::MockApiClient;
    ///
    /// let client = MockApiClient::new("test-model").with_text_response("Hi!");
    /// let result = client.create_message(vec![], None, None).await;
    /// assert_eq!(result.unwrap()["content"][0]["text"], "Hi!");
    /// # });
    /// ```
    fn create_message(
        &self,
        _messages: Vec<Message>,
        _system: Option<String>,
        _tools: Option<Vec<ToolSchema>>,
    ) -> Pin<Box<dyn Future<Output = Result<Value, ApiError>> + Send + '_>> {
        if let Some(ref err) = self.error {
            let err = err.clone();
            return Box::pin(async move { Err(ApiError::api(&err)) });
        }

        let response = self.pop_response();
        Box::pin(async move {
            Ok(json!({
                "content": [{"type": "text", "text": response.text}]
            }))
        })
    }
}

// ==================================================
// MockTool
// ==================================================

/// A mock [`Tool`] that returns a fixed result or error when called.
///
/// Useful for testing tool dispatch, registries, and agent-loop tool
/// execution without implementing a real tool. Configure the behaviour
/// via the builder-style methods:
///
/// - [`with_result`](MockTool::with_result) — set the text result
///   returned on success.
/// - [`with_error`](MockTool::with_error) — make the tool return a
///   [`ToolError::Execution`] instead.
/// - [`with_concurrency_safe`](MockTool::with_concurrency_safe) — set
///   the concurrency-safety flag.
/// - [`with_read_only`](MockTool::with_read_only) — set the read-only
///   flag.
/// - [`with_schema`](MockTool::with_schema) — override the input JSON
///   schema.
/// - [`with_system_prompt`](MockTool::with_system_prompt) — attach a
///   system prompt that the framework injects when the tool is available.
///
/// # Construction
///
/// ```rust
/// use loopctl::testing::MockTool;
///
/// let tool = MockTool::new("echo", "Echoes input")
///     .with_result("Echo: hello")
///     .with_concurrency_safe(true);
/// ```
///
/// # Example — registering in a tool registry
///
/// ```rust
/// use loopctl::testing::MockTool;
/// use loopctl::tool::ToolRegistry;
///
/// let tool = MockTool::new("echo", "Echoes input")
///     .with_result("Echo: hello");
///
/// let mut registry = ToolRegistry::new();
/// registry.register(tool);
///
/// assert!(registry.contains("echo"));
/// ```
pub struct MockTool {
    /// The tool name, returned by [`Tool::name`].
    ///
    /// Set via [`MockTool::new`]. Must be unique within a
    /// [`ToolRegistry`](crate::tool::ToolRegistry).
    ///
    /// The framework uses this to match tool-call requests from the
    /// model to the correct [`Tool`] implementation.
    name: String,

    /// The human-readable description, returned by [`Tool::description`].
    ///
    /// Set via [`MockTool::new`]. Shown to the model as part of the
    /// tool schema so it can decide which tool to invoke.
    ///
    /// The description should be concise yet informative — it is
    /// included verbatim in the [`ToolSchema`] sent to the API.
    description: String,

    /// The JSON schema for tool input, returned by [`Tool::schema`].
    ///
    /// Defaults to a trivial `{"type": "object", "properties": {"input": {"type": "string"}}}`.
    /// Override with [`MockTool::with_schema`] when the code under test
    /// validates the schema.
    ///
    /// The schema is included in the [`ToolSchema`] struct that the
    /// framework sends to the model API when listing available tools.
    input_schema: Value,

    /// The text result returned on success.
    ///
    /// Wrapped in [`ToolOutput::text`] by the [`Tool::call`]
    /// implementation. Set via [`MockTool::with_result`]. Defaults to
    /// `"mock result"`.
    ///
    /// If [`MockTool::with_error`] is called, this string is used as
    /// the error message in the [`ToolError::Execution`] variant instead.
    result: String,

    /// Whether [`Tool::call`] should return a [`ToolError::Execution`].
    ///
    /// When `true`, the `result` field is used as the error message.
    /// Set via [`MockTool::with_error`].
    ///
    /// Defaults to `false`. When testing error paths, set this to `true`
    /// after configuring the result message.
    is_error: bool,

    /// Whether this tool is safe to run concurrently with others.
    ///
    /// Returned by [`Tool::is_concurrency_safe`]. Defaults to `false`.
    /// Set via [`MockTool::with_concurrency_safe`].
    ///
    /// When `true`, the tool executor may schedule this tool in parallel
    /// with other concurrency-safe tools, reducing overall latency.
    is_concurrency_safe: bool,

    /// Whether this tool only reads data (no side effects).
    ///
    /// Returned by [`Tool::is_read_only`]. Defaults to `true` because
    /// most test tools don't need to simulate writes.
    ///
    /// The framework may use this hint to optimise scheduling — for
    /// example, running multiple read-only tools in parallel while
    /// serialising write operations.
    is_read_only: bool,

    /// An optional system prompt injected when the tool is available.
    ///
    /// Returned by [`Tool::system_prompt`]. Defaults to `None`. Set via
    /// [`MockTool::with_system_prompt`] when testing system-prompt
    /// assembly logic.
    ///
    /// The framework concatenates all tool system prompts and appends
    /// them to the agent's system message before sending to the model.
    system_prompt: Option<String>,
}

// ===================================================
// MockTool — construction & builder methods
// ===================================================

/// Construction and builder methods for [`MockTool`].
///
/// The builder pattern lets you configure the mock's behaviour fluently.
/// All builder methods consume and return `Self`, so you can chain them
/// directly after [`MockTool::new`].
///
/// # Configuration matrix
///
/// | Method                                | Affects                  |
/// |---------------------------------------|--------------------------|
/// | [`MockTool::with_result`]             | Text returned on success |
/// | [`MockTool::with_error`]              | Switches to error path   |
/// | [`MockTool::with_concurrency_safe`]   | Concurrency flag         |
/// | [`MockTool::with_read_only`]          | Read-only flag           |
/// | [`MockTool::with_schema`]             | JSON input schema        |
/// | [`MockTool::with_system_prompt`]      | System prompt text       |
impl MockTool {
    /// Create a new mock tool with the given name and description.
    ///
    /// Returns a tool with sensible defaults that can be registered in a
    /// [`ToolRegistry`](crate::tool::ToolRegistry) immediately. Use the
    /// builder methods to customise behaviour before registration.
    ///
    /// Defaults:
    ///
    /// | Property              | Default                                                            |
    /// |-----------------------|--------------------------------------------------------------------|
    /// | `result`              | `"mock result"`                                                    |
    /// | `is_error`            | `false`                                                            |
    /// | `is_concurrency_safe` | `false`                                                            |
    /// | `is_read_only`        | `true`                                                             |
    /// | `input_schema`        | `{"type":"object","properties":{"input":{"type":"string"}}}`       |
    /// | `system_prompt`       | `None`                                                             |
    ///
    /// # Example
    ///
    /// ```rust
    /// use loopctl::testing::MockTool;
    ///
    /// let tool = MockTool::new("calculator", "Performs arithmetic");
    /// ```
    #[must_use]
    pub fn new(name: &str, description: &str) -> Self {
        Self {
            name: name.to_string(),
            description: description.to_string(),
            input_schema: json!({
                "type": "object",
                "properties": { "input": { "type": "string" } }
            }),
            result: "mock result".to_string(),
            is_error: false,
            is_concurrency_safe: false,
            is_read_only: true,
            system_prompt: None,
        }
    }

    /// Set the text result this tool returns on success.
    ///
    /// The value is wrapped in [`ToolOutput::text`] when
    /// [`Tool::call`] is invoked. If [`MockTool::with_error`] is also
    /// called, this string is used as the error message instead.
    ///
    /// # Example
    ///
    /// ```rust
    /// use loopctl::testing::MockTool;
    ///
    /// let tool = MockTool::new("echo", "Echoes input")
    ///     .with_result("Echo: hello");
    /// ```
    #[must_use]
    pub fn with_result(mut self, result: &str) -> Self {
        self.result = result.to_string();
        self
    }

    /// Make this tool return a [`ToolError::Execution`] instead of a
    /// successful result.
    ///
    /// The `result` value (set via [`MockTool::with_result`]) is used as the error
    /// message string. Useful for testing agent error-handling and
    /// retry logic. Call this *after* [`MockTool::with_result`] to
    /// ensure the error message is set correctly.
    ///
    /// # Example
    ///
    /// ```rust
    /// use loopctl::testing::MockTool;
    ///
    /// let tool = MockTool::new("fail", "Always fails")
    ///     .with_result("something went wrong")
    ///     .with_error();
    /// ```
    #[must_use]
    pub fn with_error(mut self) -> Self {
        self.is_error = true;
        self
    }

    /// Set the concurrency-safety flag.
    ///
    /// When `true`, the framework may invoke this tool concurrently
    /// with other concurrency-safe tools. Returned by
    /// [`Tool::is_concurrency_safe`].
    ///
    /// Defaults to `false` — most test tools don't need concurrency.
    /// Set to `true` when testing the framework's parallel tool
    /// execution logic.
    ///
    /// # Example
    ///
    /// ```rust
    /// use loopctl::testing::MockTool;
    ///
    /// let tool = MockTool::new("read", "Reads data")
    ///     .with_concurrency_safe(true);
    /// ```
    #[must_use]
    pub fn with_concurrency_safe(mut self, safe: bool) -> Self {
        self.is_concurrency_safe = safe;
        self
    }

    /// Set the read-only flag.
    ///
    /// When `true` (the default), the tool is considered side-effect
    /// free. Returned by [`Tool::is_read_only`].
    ///
    /// Set to `false` when testing that the framework serialises
    /// write operations correctly — e.g. two `write` tools should
    /// not execute concurrently.
    ///
    /// # Example
    ///
    /// ```rust
    /// use loopctl::testing::MockTool;
    ///
    /// let tool = MockTool::new("write", "Writes data")
    ///     .with_read_only(false);
    /// ```
    #[must_use]
    pub fn with_read_only(mut self, read_only: bool) -> Self {
        self.is_read_only = read_only;
        self
    }

    /// Override the input JSON schema.
    ///
    /// The default schema is a trivial object with a single `input`
    /// string property. Use this when the code under test validates
    /// tool schemas, generates documentation from them, or when the
    /// model needs a richer schema to produce correct tool calls.
    ///
    /// # Example
    ///
    /// ```rust
    /// use loopctl::testing::MockTool;
    /// use serde_json::json;
    ///
    /// let tool = MockTool::new("search", "Searches the web")
    ///     .with_schema(json!({
    ///         "type": "object",
    ///         "properties": {
    ///             "query": { "type": "string" },
    ///             "limit": { "type": "integer" }
    ///         },
    ///         "required": ["query"]
    ///     }));
    /// ```
    #[must_use]
    pub fn with_schema(mut self, schema: Value) -> Self {
        self.input_schema = schema;
        self
    }

    /// Attach a system prompt that the framework injects when this tool
    /// is available.
    ///
    /// Returned by [`Tool::system_prompt`]. Useful for testing that the
    /// agent correctly assembles system prompts from tool metadata.
    ///
    /// When the tool is registered, the framework concatenates all tool
    /// system prompts into the system message sent to the model.
    ///
    /// # Example
    ///
    /// ```rust
    /// use loopctl::testing::MockTool;
    ///
    /// let tool = MockTool::new("bash", "Runs shell commands")
    ///     .with_system_prompt("Prefer simple commands over pipelines.");
    /// ```
    #[must_use]
    pub fn with_system_prompt(mut self, prompt: &str) -> Self {
        self.system_prompt = Some(prompt.to_string());
        self
    }
}

// ===================================================
// MockTool — Tool trait implementation
// ===================================================

/// Trait implementation that returns canned tool metadata and results.
///
/// Every method delegates to the fields configured via the builder
/// methods on [`MockTool`]. The [`call`](Tool::call) implementation
/// ignores its `_input` and `_context` parameters entirely, returning
/// either [`ToolOutput::text`] or [`ToolError::Execution`] depending
/// on whether [`MockTool::with_error`] was called.
///
/// # Metadata methods
///
/// The [`name`](Tool::name), [`description`](Tool::description), and
/// [`schema`](Tool::schema) methods return the values set at
/// construction time via [`MockTool::new`]. The
/// [`is_concurrency_safe`](Tool::is_concurrency_safe),
/// [`is_read_only`](Tool::is_read_only), and
/// [`system_prompt`](Tool::system_prompt) methods reflect the flags
/// configured through their respective builder methods.
///
/// # Execution semantics
///
/// The [`call`](Tool::call) future resolves immediately — there is no
/// artificial delay. If your test needs to verify timeout or
/// cancellation behaviour, wrap the mock in a layer that adds delays.
impl Tool for MockTool {
    /// Return the tool name.
    ///
    /// Always returns the string passed to [`MockTool::new`]. The
    /// framework uses this to look up tools in the
    /// [`ToolRegistry`](crate::tool::ToolRegistry) and to correlate
    /// tool-call requests from the model with the right implementation.
    fn name(&self) -> &str {
        &self.name
    }

    /// Return the tool description.
    ///
    /// Always returns the string passed to [`MockTool::new`]. The
    /// description is included in the [`ToolSchema`] sent to the model
    /// so it can decide which tool to invoke.
    fn description(&self) -> &str {
        &self.description
    }

    /// Build the [`ToolSchema`] for this mock tool.
    ///
    /// Combines the `name`, `description`, and `input_schema` fields
    /// into the schema struct the framework sends to the model. The
    /// schema is also used by the [`ToolRegistry`](crate::tool::ToolRegistry)
    /// to describe available tools when calling the API.
    fn schema(&self) -> ToolSchema {
        ToolSchema {
            tool: self.name.clone(),
            description: self.description.clone(),
            input_schema: self.input_schema.clone(),
        }
    }

    /// Execute the mock tool, returning the canned result or error.
    ///
    /// - If [`with_error`](MockTool::with_error) was called, returns
    ///   [`ToolError::Execution`] with the result string as the message.
    /// - Otherwise returns [`ToolOutput::text`] containing the result
    ///   string.
    ///
    /// The `_input` and `_context` parameters are ignored — the mock
    /// always returns the preconfigured value. This means you cannot
    /// test input validation through the mock; if you need that, write
    /// a real tool implementation.
    ///
    /// The future resolves immediately (zero delay), making tests fast
    /// and deterministic.
    ///
    /// # Example
    ///
    /// ```rust
    /// # tokio::runtime::Runtime::new().unwrap().block_on(async {
    /// use loopctl::testing::MockTool;
    /// use loopctl::tool::{Tool, ToolContext};
    /// use serde_json::json;
    ///
    /// let tool = MockTool::new("echo", "Echoes").with_result("pong");
    /// let ctx = ToolContext::default();
    /// let result = tool.call(json!({"msg": "ping"}), &ctx).await;
    /// assert_eq!(result.unwrap().text_content(), "pong");
    /// # });
    /// ```
    fn call(
        &self,
        _input: Value,
        _context: &ToolContext,
    ) -> Pin<Box<dyn Future<Output = Result<ToolOutput, ToolError>> + Send + '_>> {
        let result = self.result.clone();
        let is_error = self.is_error;
        Box::pin(async move {
            if is_error {
                Err(ToolError::Execution(result))
            } else {
                Ok(ToolOutput::text(result))
            }
        })
    }

    /// Return whether this tool is safe to run concurrently.
    ///
    /// Set via [`MockTool::with_concurrency_safe`]. Defaults to `false`.
    /// When `true`, the framework's tool executor may invoke this tool
    /// in parallel with other concurrency-safe tools, improving
    /// throughput for read-only or independent operations.
    fn is_concurrency_safe(&self) -> bool {
        self.is_concurrency_safe
    }

    /// Return whether this tool is read-only (no side effects).
    ///
    /// Set via [`MockTool::with_read_only`]. Defaults to `true` because
    /// most test tools don't need to simulate writes.
    fn is_read_only(&self) -> bool {
        self.is_read_only
    }

    /// Return the optional system prompt for this tool.
    ///
    /// Set via [`MockTool::with_system_prompt`]. Defaults to `None`.
    /// When present, the framework appends this prompt to the agent's
    /// system message, giving the model contextual guidance on how to
    /// use the tool effectively.
    fn system_prompt(&self) -> Option<String> {
        self.system_prompt.clone()
    }
}

// ==================================================
// Fixture Factories
// ==================================================

/// Create a test user [`Message`] with the given text.
///
/// Shorthand for `Message::user(text)`. Useful when building message
/// histories for tests — avoids importing [`Message`] constructors
/// directly. The returned message has [`Role::User`] and a single
/// [`MessagePart::Text`] variant containing the provided string.
///
/// Most common fixture for constructing the "user says"
/// part of a conversation history.
///
/// # Example
///
/// ```rust
/// use loopctl::testing::test_message;
/// use loopctl::message::Role;
///
/// let msg = test_message("What is 2 + 2?");
/// assert_eq!(msg.role, Role::User);
/// ```
#[must_use]
pub fn test_message(text: &str) -> Message {
    Message::user(text)
}

/// Create a test assistant [`Message`] with the given text.
///
/// Shorthand for `Message::assistant(text)`. Useful for constructing
/// conversation histories where the assistant has already replied.
/// The returned message has [`Role::Assistant`] and a single
/// [`MessagePart::Text`] variant.
///
/// Use this when simulating a multi-turn conversation where the
/// assistant's previous reply is part of the context sent to the model.
///
/// # Example
///
/// ```rust
/// use loopctl::testing::test_assistant_message;
/// use loopctl::message::Role;
///
/// let msg = test_assistant_message("The answer is 4.");
/// assert_eq!(msg.role, Role::Assistant);
/// ```
#[must_use]
pub fn test_assistant_message(text: &str) -> Message {
    Message::assistant(text)
}

/// Create a test assistant [`Message`] containing tool-call content blocks.
///
/// Each tuple in `calls` is `(tool_use_id, tool_name, input_json)`.
/// If `tool_use_id` is an empty string a unique ID of the form
/// `"call_{i}"` is generated automatically (where `i` is the index).
///
/// Message the agent loop produces when the model requests
/// tool execution — use it to simulate the "assistant asked for a tool"
/// step in multi-turn tests. Each tuple becomes a [`MessagePart::ToolCall`]
/// variant in the message's `content` vector.
///
/// # Example
///
/// ```rust
/// use loopctl::testing::test_tool_use_message;
/// use loopctl::message::Role;
/// use serde_json::json;
///
/// let msg = test_tool_use_message(&[
///     ("call_1", "bash", json!({"command": "ls"})),
///     ("", "search", json!({"query": "hello"})),  // auto-ID: "call_1"
/// ]);
/// assert_eq!(msg.role, Role::Assistant);
/// assert_eq!(msg.parts.len(), 2);
/// ```
#[must_use]
pub fn test_tool_use_message(calls: &[(&str, &str, Value)]) -> Message {
    let blocks: Vec<MessagePart> = calls
        .iter()
        .enumerate()
        .map(|(i, (id, name, input))| {
            MessagePart::tool_call(
                if id.is_empty() {
                    format!("call_{i}")
                } else {
                    id.to_string()
                },
                *name,
                input.clone(),
            )
        })
        .collect();
    Message::new(Role::Assistant, blocks)
}

/// Create a test [`LoopConfig`] with sensible defaults.
///
/// The returned config has:
///
/// - A random [`Uuid`] session ID.
/// - `max_turns` set to `10`.
/// - `system_prompt` set to `"You are a test assistant."`.
/// - All other fields at their [`Default`] values.
///
/// Useful as a starting point when you don't care about specific
/// configuration values. If you need a deterministic session ID for
/// log correlation or assertion, use [`test_config_with_id`] instead.
///
/// # Fields
///
/// ```text
/// session_id    → Uuid::new_v4()
/// max_turns     → 10
/// system_prompt → Some("You are a test assistant.")
/// ```
///
/// # Example
///
/// ```rust
/// use loopctl::testing::test_config;
///
/// let config = test_config();
/// assert_eq!(config.max_turns, 10);
/// ```
#[must_use]
pub fn test_config() -> LoopConfig {
    LoopConfig {
        session_id: Uuid::new_v4(),
        max_turns: 10,
        system_prompt: Some("You are a test assistant.".to_string()),
        ..Default::default()
    }
}

/// Create a test [`LoopConfig`] with a specific session ID.
///
/// Same as [`test_config`] but with a caller-supplied session ID.
/// Useful when tests need to assert on the ID — for example verifying
/// log correlation, session persistence, or that a session resumes
/// with the correct identity after a restart.
///
/// # Fields
///
/// ```text
/// session_id    → <caller-supplied>
/// max_turns     → 10
/// system_prompt → Some("You are a test assistant.")
/// ```
///
/// # Example
///
/// ```rust
/// use loopctl::testing::test_config_with_id;
/// use uuid::Uuid;
///
/// let id = Uuid::new_v4();
/// let config = test_config_with_id(id);
/// assert_eq!(config.session_id, id);
/// ```
#[must_use]
pub fn test_config_with_id(id: Uuid) -> LoopConfig {
    LoopConfig {
        session_id: id,
        max_turns: 10,
        system_prompt: Some("You are a test assistant.".to_string()),
        ..Default::default()
    }
}

// ==================================================
// Tests
// ==================================================

/// Unit tests for the testing module itself.
///
/// These tests verify that the mock implementations and fixture
/// factories behave correctly. They are not part of the public API
/// but serve as a safety net for future changes to this module.
///
/// # Test categories
///
/// - **MockApiClient** — model name, default response, custom text,
///   tool calls, error simulation, multi-turn behaviour, non-streaming.
/// - **MockTool** — success path, error path, registry integration.
/// - **Fixtures** — [`test_message`], [`test_assistant_message`],
///   [`test_tool_use_message`], [`test_config`].
#[cfg(test)]
mod tests {
    use super::*;
    use crate::tool::ToolRegistry;
    use futures::StreamExt;

    #[test]
    fn test_mock_client_model() {
        let client = MockApiClient::new("test-model");
        assert_eq!(client.model(), "test-model");
    }

    #[tokio::test]
    async fn test_mock_client_default_response() {
        let client = MockApiClient::new("test-model");
        let stream = client.stream_messages(vec![Message::user("Hi")], None, None);
        let events: Vec<_> = stream.collect().await;
        assert!(events.len() >= 4);
        assert!(events[0].is_ok());
    }

    #[tokio::test]
    async fn test_mock_client_custom_text() {
        let client = MockApiClient::new("test-model").with_text_response("Custom response");
        let stream = client.stream_messages(vec![Message::user("Hi")], None, None);
        let events: Vec<_> = stream.collect().await;
        let has_text = events.iter().any(|e| {
            if let Ok(StreamEvent::IndexedDelta(delta)) = e {
                if let DeltaPart::Text { text } = &delta.delta {
                    return text == "Custom response";
                }
            }
            false
        });
        assert!(has_text);
    }

    #[tokio::test]
    async fn test_mock_client_tool_call() {
        let client = MockApiClient::new("test-model").with_tool_call(
            "call_1",
            "echo",
            json!({"message": "hi"}),
        );
        let stream = client.stream_messages(vec![Message::user("Hi")], None, None);
        let events: Vec<_> = stream.collect().await;

        let has_tool_use = events.iter().any(|e| {
            if let Ok(StreamEvent::PartStart(start)) = e {
                if let Some(MessagePart::ToolCall { name, .. }) = &start.part {
                    return name == "echo";
                }
            }
            false
        });
        assert!(has_tool_use);

        let has_tool_stop = events.iter().any(|e| {
            if let Ok(StreamEvent::MessageDelta(delta)) = e {
                delta.delta.stop_reason.as_deref() == Some("tool_use")
            } else {
                false
            }
        });
        assert!(has_tool_stop);
    }

    #[tokio::test]
    async fn test_mock_client_error() {
        let client = MockApiClient::new("test-model").with_error("API error");
        let stream = client.stream_messages(vec![Message::user("Hi")], None, None);
        let events: Vec<_> = stream.collect().await;
        assert_eq!(events.len(), 1);
        assert!(events[0].is_err());
    }

    #[tokio::test]
    async fn test_mock_client_multi_turn() {
        let client = MockApiClient::new("test-model").with_responses(vec![
            MockResponse {
                text: "First".to_string(),
                tool_call: None,
                stop_reason: "end_turn".to_string(),
            },
            MockResponse {
                text: "Second".to_string(),
                tool_call: None,
                stop_reason: "end_turn".to_string(),
            },
        ]);

        let stream1 = client.stream_messages(vec![Message::user("Hi")], None, None);
        let events1: Vec<_> = stream1.collect().await;
        let has_first = events1.iter().any(|e| {
            if let Ok(StreamEvent::IndexedDelta(delta)) = e {
                if let DeltaPart::Text { text } = &delta.delta {
                    return text == "First";
                }
            }
            false
        });
        assert!(has_first);

        let stream2 = client.stream_messages(vec![Message::user("Hi")], None, None);
        let events2: Vec<_> = stream2.collect().await;
        let has_second = events2.iter().any(|e| {
            if let Ok(StreamEvent::IndexedDelta(delta)) = e {
                if let DeltaPart::Text { text } = &delta.delta {
                    return text == "Second";
                }
            }
            false
        });
        assert!(has_second);
    }

    #[tokio::test]
    async fn test_mock_client_create_message() {
        let client = MockApiClient::new("test-model").with_text_response("Hello!");
        let result = client
            .create_message(vec![Message::user("Hi")], None, None)
            .await;
        assert!(result.is_ok());
        let json = result.unwrap();
        assert_eq!(json["content"][0]["text"], "Hello!");
    }

    #[tokio::test]
    async fn test_mock_tool() {
        let tool = MockTool::new("echo", "Echoes input").with_result("Echo: hello");
        let ctx = ToolContext::default();
        let result = tool.call(json!({"input": "hello"}), &ctx).await;
        assert!(result.is_ok());
        assert_eq!(result.unwrap().text_content(), "Echo: hello");
    }

    #[tokio::test]
    async fn test_mock_tool_error() {
        let tool = MockTool::new("fail", "Always fails")
            .with_result("Something went wrong")
            .with_error();
        let ctx = ToolContext::default();
        let result = tool.call(json!({}), &ctx).await;
        assert!(result.is_err());
    }

    #[test]
    fn test_mock_tool_registry() {
        let tool = MockTool::new("echo", "Echoes input").with_concurrency_safe(true);
        let mut registry = ToolRegistry::new();
        registry.register(tool);

        assert!(registry.contains("echo"));
        assert_eq!(registry.len(), 1);

        let t = registry.get("echo").unwrap();
        assert_eq!(t.name(), "echo");
        assert!(t.is_concurrency_safe());
        assert!(t.is_read_only());
    }

    #[test]
    fn test_fixture_test_message() {
        let msg = test_message("Hello");
        assert_eq!(msg.role, Role::User);
        assert_eq!(msg.parts.len(), 1);
    }

    #[test]
    fn test_fixture_test_assistant_message() {
        let msg = test_assistant_message("Hi there");
        assert_eq!(msg.role, Role::Assistant);
    }

    #[test]
    fn test_fixture_test_tool_use_message() {
        let msg = test_tool_use_message(&[("call_1", "bash", json!({"command": "ls"}))]);
        assert_eq!(msg.role, Role::Assistant);
        assert_eq!(msg.parts.len(), 1);
        assert!(msg.parts[0].is_tool_call());
    }

    #[test]
    fn test_fixture_test_config() {
        let config = test_config();
        assert_eq!(config.max_turns, 10);
        assert!(config.system_prompt.is_some());
    }

    #[test]
    fn mock_api_client_set_model() {
        let client = MockApiClient::new("model-a");
        assert_eq!(client.model(), "model-a");

        assert!(client.set_model("model-b"));
        assert_eq!(client.model(), "model-b");
    }

    #[test]
    fn mock_api_client_set_model_rejects_empty() {
        let client = MockApiClient::new("model-a");
        assert!(!client.set_model(""));
        assert!(!client.set_model("   "));
        assert_eq!(client.model(), "model-a");
    }
}