forked from Dicklesworthstone/pi_agent_rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathe2e_provider_streaming.rs
More file actions
936 lines (873 loc) · 33.8 KB
/
Copy pathe2e_provider_streaming.rs
File metadata and controls
936 lines (873 loc) · 33.8 KB
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
//! E2E provider streaming tests with VCR playback + verbose JSONL logs.
//!
//! These tests drive the full provider streaming pipeline using recorded VCR
//! cassettes, validating event ordering, stop reasons, usage tracking, tool-call
//! emissions, and error handling. All outputs are deterministic (VCR playback
//! only) and captured as JSONL artifacts.
//!
//! Run:
//! ```bash
//! VCR_MODE=playback VCR_CASSETTE_DIR=tests/fixtures/vcr \
//! cargo test --test e2e_provider_streaming
//! ```
mod common;
use common::TestHarness;
use futures::StreamExt;
use pi::http::client::Client;
use pi::model::{Message, StopReason, StreamEvent, ThinkingLevel, UserContent, UserMessage};
use pi::provider::{CacheRetention, Context, Provider, StreamOptions, ThinkingBudgets, ToolDef};
use pi::providers::anthropic::AnthropicProvider;
use pi::vcr::{VcrMode, VcrRecorder};
use serde_json::json;
use sha2::{Digest, Sha256};
use std::fmt::Write as _;
use std::path::PathBuf;
use std::time::Instant;
fn cassette_root() -> PathBuf {
std::env::var("VCR_CASSETTE_DIR").map_or_else(
|_| PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/vcr"),
PathBuf::from,
)
}
fn sha256_hex(bytes: &[u8]) -> String {
let digest = Sha256::digest(bytes);
let mut out = String::with_capacity(64);
for byte in digest {
let _ = write!(out, "{byte:02x}");
}
out
}
fn user_text(text: &str) -> Message {
Message::User(UserMessage {
content: UserContent::Text(text.to_string()),
timestamp: 0,
})
}
// ─── Event ordering validation ───────────────────────────────────────────────
/// Verify that the event sequence follows expected ordering invariants:
/// - `Start` must appear first (if any events at all)
/// - `Done` or `Error` must appear last (if present)
/// - Content events appear between start and done
/// - Text deltas appear between `TextStart` and `TextEnd`
fn validate_event_ordering(events: &[StreamEvent], harness: &TestHarness, scenario: &str) {
if events.is_empty() {
return;
}
harness.assert_log(&format!("{scenario}: validate event ordering"));
// First event should be Start
assert!(
matches!(events[0], StreamEvent::Start { .. }),
"{scenario}: first event should be Start, got {:?}",
std::mem::discriminant(&events[0])
);
// Last event should be Done or Error (if stream completed normally)
let last = &events[events.len() - 1];
let last_is_terminal = matches!(last, StreamEvent::Done { .. } | StreamEvent::Error { .. });
if last_is_terminal {
harness.assert_log(&format!("{scenario}: terminal event present"));
}
// Check that text deltas appear within text start/end pairs
let mut in_text_block = false;
let mut in_thinking_block = false;
let mut in_tool_call_block = false;
let mut has_started = false;
for (idx, event) in events.iter().enumerate() {
match event {
StreamEvent::Start { .. } => {
assert!(!has_started, "{scenario}: duplicate Start at index {idx}");
has_started = true;
}
StreamEvent::TextStart { .. } => {
assert!(has_started, "{scenario}: TextStart before Start at {idx}");
in_text_block = true;
}
StreamEvent::TextDelta { .. } => {
assert!(
in_text_block,
"{scenario}: TextDelta outside text block at {idx}"
);
}
StreamEvent::TextEnd { .. } => {
assert!(
in_text_block,
"{scenario}: TextEnd without TextStart at {idx}"
);
in_text_block = false;
}
StreamEvent::ThinkingStart { .. } => {
assert!(
has_started,
"{scenario}: ThinkingStart before Start at {idx}"
);
in_thinking_block = true;
}
StreamEvent::ThinkingDelta { .. } => {
assert!(
in_thinking_block,
"{scenario}: ThinkingDelta outside thinking block at {idx}"
);
}
StreamEvent::ThinkingEnd { .. } => {
assert!(
in_thinking_block,
"{scenario}: ThinkingEnd without ThinkingStart at {idx}"
);
in_thinking_block = false;
}
StreamEvent::ToolCallStart { .. } => {
assert!(
has_started,
"{scenario}: ToolCallStart before Start at {idx}"
);
in_tool_call_block = true;
}
StreamEvent::ToolCallDelta { .. } => {
assert!(
in_tool_call_block,
"{scenario}: ToolCallDelta outside tool call block at {idx}"
);
}
StreamEvent::ToolCallEnd { .. } => {
assert!(
in_tool_call_block,
"{scenario}: ToolCallEnd without ToolCallStart at {idx}"
);
in_tool_call_block = false;
}
StreamEvent::Done { .. } | StreamEvent::Error { .. } => {}
}
}
}
// ─── Scenario descriptor ─────────────────────────────────────────────────────
struct E2eScenario {
cassette_name: &'static str,
description: &'static str,
messages: Vec<Message>,
tools: Vec<ToolDef>,
max_tokens: u32,
thinking_level: Option<ThinkingLevel>,
expect_error: Option<u16>,
expect_text: bool,
expect_thinking: bool,
expect_tool_calls: usize,
expect_stop_reasons: Vec<StopReason>,
}
#[allow(clippy::too_many_lines)]
fn all_anthropic_scenarios() -> Vec<E2eScenario> {
vec![
// ── Happy path ──────────────────────────────────────────────
E2eScenario {
cassette_name: "anthropic_simple_text",
description: "Simple text response",
messages: vec![user_text("Reply with the single word: pong.")],
tools: Vec::new(),
max_tokens: 64,
thinking_level: None,
expect_error: None,
expect_text: true,
expect_thinking: false,
expect_tool_calls: 0,
expect_stop_reasons: vec![StopReason::Stop],
},
E2eScenario {
cassette_name: "anthropic_multi_paragraph",
description: "Multi-paragraph response",
messages: vec![user_text(
"Reply with two paragraphs separated by a blank line. \
Paragraph one must be 'Paragraph 1.' and paragraph two must be 'Paragraph 2.'.",
)],
tools: Vec::new(),
max_tokens: 256,
thinking_level: None,
expect_error: None,
expect_text: true,
expect_thinking: false,
expect_tool_calls: 0,
expect_stop_reasons: vec![StopReason::Stop],
},
E2eScenario {
cassette_name: "anthropic_extended_thinking",
description: "Extended thinking with text response",
messages: vec![user_text("Compute 12 * 13 and reply with 'Result: 156'.")],
tools: Vec::new(),
max_tokens: 256,
thinking_level: Some(ThinkingLevel::Medium),
expect_error: None,
expect_text: true,
expect_thinking: true,
expect_tool_calls: 0,
expect_stop_reasons: vec![StopReason::Stop],
},
E2eScenario {
cassette_name: "anthropic_tool_call_single",
description: "Single tool call",
messages: vec![user_text(
"Call the echo tool with text='hello'. Do not answer in text.",
)],
tools: vec![echo_tool()],
max_tokens: 256,
thinking_level: None,
expect_error: None,
expect_text: false,
expect_thinking: false,
expect_tool_calls: 1,
expect_stop_reasons: vec![StopReason::ToolUse],
},
E2eScenario {
cassette_name: "anthropic_tool_call_multiple",
description: "Multiple tool calls",
messages: vec![user_text(
"Call add with a=2 b=3, then call multiply with a=4 b=5. Do not answer in text.",
)],
tools: vec![add_tool(), multiply_tool()],
max_tokens: 256,
thinking_level: None,
expect_error: None,
expect_text: false,
expect_thinking: false,
expect_tool_calls: 2,
expect_stop_reasons: vec![StopReason::ToolUse],
},
E2eScenario {
cassette_name: "anthropic_unicode_content",
description: "Unicode content response",
messages: vec![user_text("Reply with: 😀 你好 שלום")],
tools: Vec::new(),
max_tokens: 256,
thinking_level: None,
expect_error: None,
expect_text: true,
expect_thinking: false,
expect_tool_calls: 0,
expect_stop_reasons: vec![StopReason::Stop],
},
E2eScenario {
cassette_name: "anthropic_very_long_response",
description: "Long response hitting max tokens",
messages: vec![user_text(
"Write 50 short sentences about Rust without stopping early.",
)],
tools: Vec::new(),
max_tokens: 64,
thinking_level: None,
expect_error: None,
expect_text: true,
expect_thinking: false,
expect_tool_calls: 0,
expect_stop_reasons: vec![StopReason::Length, StopReason::Stop],
},
// ── Thinking variants ───────────────────────────────────────
E2eScenario {
cassette_name: "anthropic_thinking_only",
description: "Thinking only, no final text",
messages: vec![user_text(
"Think about primes but do not provide any final text response.",
)],
tools: Vec::new(),
max_tokens: 256,
thinking_level: Some(ThinkingLevel::High),
expect_error: None,
expect_text: false,
expect_thinking: true,
expect_tool_calls: 0,
expect_stop_reasons: vec![],
},
E2eScenario {
cassette_name: "anthropic_thinking_with_tool_calls",
description: "Thinking with tool calls",
messages: vec![user_text(
"Think, then call echo with text='hi'. Do not answer in text.",
)],
tools: vec![echo_tool()],
max_tokens: 256,
thinking_level: Some(ThinkingLevel::High),
expect_error: None,
expect_text: false,
expect_thinking: true,
expect_tool_calls: 1,
expect_stop_reasons: vec![StopReason::ToolUse],
},
E2eScenario {
cassette_name: "anthropic_thinking_budget_exceeded",
description: "Thinking budget exceeded",
messages: vec![user_text(
"Think in detail about prime numbers, then answer with a summary.",
)],
tools: Vec::new(),
max_tokens: 96,
thinking_level: Some(ThinkingLevel::High),
expect_error: None,
expect_text: false,
expect_thinking: true,
expect_tool_calls: 0,
expect_stop_reasons: vec![StopReason::Length, StopReason::Stop],
},
// ── Error scenarios ─────────────────────────────────────────
E2eScenario {
cassette_name: "anthropic_auth_failure_401",
description: "Auth failure (HTTP 401)",
messages: vec![user_text("Trigger an auth failure.")],
tools: Vec::new(),
max_tokens: 256,
thinking_level: None,
expect_error: Some(401),
expect_text: false,
expect_thinking: false,
expect_tool_calls: 0,
expect_stop_reasons: vec![],
},
E2eScenario {
cassette_name: "anthropic_forbidden_403",
description: "Forbidden (HTTP 403)",
messages: vec![user_text("Trigger a forbidden error.")],
tools: Vec::new(),
max_tokens: 256,
thinking_level: None,
expect_error: Some(403),
expect_text: false,
expect_thinking: false,
expect_tool_calls: 0,
expect_stop_reasons: vec![],
},
E2eScenario {
cassette_name: "anthropic_bad_request_400",
description: "Bad request (HTTP 400)",
messages: vec![user_text("Trigger a bad request error.")],
tools: Vec::new(),
max_tokens: 256,
thinking_level: None,
expect_error: Some(400),
expect_text: false,
expect_thinking: false,
expect_tool_calls: 0,
expect_stop_reasons: vec![],
},
E2eScenario {
cassette_name: "anthropic_server_error_500",
description: "Server error (HTTP 500)",
messages: vec![user_text("Trigger a server error.")],
tools: Vec::new(),
max_tokens: 256,
thinking_level: None,
expect_error: Some(500),
expect_text: false,
expect_thinking: false,
expect_tool_calls: 0,
expect_stop_reasons: vec![],
},
E2eScenario {
cassette_name: "anthropic_rate_limit_429",
description: "Rate limit (HTTP 429)",
messages: vec![user_text("Trigger a rate limit error.")],
tools: Vec::new(),
max_tokens: 256,
thinking_level: None,
expect_error: Some(429),
expect_text: false,
expect_thinking: false,
expect_tool_calls: 0,
expect_stop_reasons: vec![],
},
E2eScenario {
cassette_name: "anthropic_overloaded_529",
description: "Overloaded (HTTP 529)",
messages: vec![user_text("Trigger an overloaded error.")],
tools: Vec::new(),
max_tokens: 256,
thinking_level: None,
expect_error: Some(529),
expect_text: false,
expect_thinking: false,
expect_tool_calls: 0,
expect_stop_reasons: vec![],
},
]
}
fn echo_tool() -> ToolDef {
ToolDef {
name: "echo".to_string(),
description: "Echo the provided text.".to_string(),
parameters: json!({
"type": "object",
"properties": { "text": { "type": "string" } },
"required": ["text"]
}),
}
}
fn add_tool() -> ToolDef {
ToolDef {
name: "add".to_string(),
description: "Add two numbers.".to_string(),
parameters: json!({
"type": "object",
"properties": { "a": { "type": "number" }, "b": { "type": "number" } },
"required": ["a", "b"]
}),
}
}
fn multiply_tool() -> ToolDef {
ToolDef {
name: "multiply".to_string(),
description: "Multiply two numbers.".to_string(),
parameters: json!({
"type": "object",
"properties": { "a": { "type": "number" }, "b": { "type": "number" } },
"required": ["a", "b"]
}),
}
}
// ─── Core runner ─────────────────────────────────────────────────────────────
#[allow(clippy::too_many_lines)]
async fn run_e2e_scenario(
scenario: &E2eScenario,
harness: &TestHarness,
model: &str,
) -> serde_json::Value {
let cassette_dir = cassette_root();
let cassette_path = cassette_dir.join(format!("{}.json", scenario.cassette_name));
harness.record_artifact(format!("{}.json", scenario.cassette_name), &cassette_path);
if !cassette_path.exists() {
harness.log().warn(
"vcr",
format!("Missing cassette {}; skipping", cassette_path.display()),
);
return json!({
"scenario": scenario.cassette_name,
"status": "skipped",
"reason": "missing_cassette",
});
}
let start = Instant::now();
let recorder = VcrRecorder::new_with(scenario.cassette_name, VcrMode::Playback, &cassette_dir);
let client = Client::new().with_vcr(recorder);
let provider = AnthropicProvider::new(model).with_client(client);
let context = Context::owned(
Some(
"You are a test harness model. Follow instructions precisely and deterministically."
.to_string(),
),
scenario.messages.clone(),
scenario.tools.clone(),
);
let thinking_enabled = scenario
.thinking_level
.is_some_and(|level| level != ThinkingLevel::Off);
let temperature = if thinking_enabled {
Some(1.0)
} else {
Some(0.0)
};
let thinking_budgets = thinking_enabled.then_some(ThinkingBudgets {
minimal: 1024,
low: 1024,
medium: 1024,
high: 1024,
xhigh: 1024,
});
let max_tokens = thinking_budgets
.as_ref()
.map_or(scenario.max_tokens, |budgets| {
let budget = match scenario.thinking_level.unwrap_or(ThinkingLevel::Off) {
ThinkingLevel::Off => 0,
ThinkingLevel::Minimal => budgets.minimal,
ThinkingLevel::Low => budgets.low,
ThinkingLevel::Medium => budgets.medium,
ThinkingLevel::High => budgets.high,
ThinkingLevel::XHigh => budgets.xhigh,
};
scenario.max_tokens.max(budget.saturating_add(256))
});
let options = StreamOptions {
api_key: Some("vcr-playback".to_string()),
max_tokens: Some(max_tokens),
temperature,
thinking_level: scenario.thinking_level,
thinking_budgets,
cache_retention: CacheRetention::None,
..Default::default()
};
// Run the stream
if let Some(expected_status) = scenario.expect_error {
let result = provider.stream(&context, &options).await;
let elapsed = start.elapsed();
let Err(err) = result else {
unreachable!(
"{}: expected error HTTP {expected_status}, got success",
scenario.cassette_name
);
};
let message = err.to_string();
let needle = format!("HTTP {expected_status}");
assert!(
message.contains(&needle),
"{}: expected error to contain '{needle}', got '{message}'",
scenario.cassette_name
);
harness.log().info_ctx(
"e2e",
format!("{}: error scenario validated", scenario.cassette_name),
|ctx| {
ctx.push(("status".into(), expected_status.to_string()));
ctx.push(("error".into(), message.clone()));
ctx.push(("elapsed_ms".into(), elapsed.as_millis().to_string()));
},
);
return json!({
"scenario": scenario.cassette_name,
"description": scenario.description,
"status": "pass",
"kind": "error",
"expected_http_status": expected_status,
"error_message": message,
"elapsed_ms": elapsed.as_millis(),
});
}
// Happy-path: collect stream events
let stream = provider
.stream(&context, &options)
.await
.unwrap_or_else(|e| unreachable!("{}: stream failed: {e}", scenario.cassette_name));
let mut events = Vec::new();
let mut stream_error = None;
let mut pinned = std::pin::pin!(stream);
while let Some(item) = pinned.next().await {
match item {
Ok(event) => events.push(event),
Err(err) => {
stream_error = Some(err.to_string());
break;
}
}
}
let elapsed = start.elapsed();
// Validate event ordering
validate_event_ordering(&events, harness, scenario.cassette_name);
// Aggregate statistics
let mut text = String::new();
let mut thinking = String::new();
let mut text_deltas = 0usize;
let mut thinking_deltas = 0usize;
let mut tool_calls = Vec::new();
let mut stop_reason: Option<StopReason> = None;
let mut timeline = Vec::new();
for event in &events {
match event {
StreamEvent::Start { .. } => timeline.push("start"),
StreamEvent::TextStart { .. } => timeline.push("text_start"),
StreamEvent::TextDelta { delta, .. } => {
text_deltas += 1;
text.push_str(delta);
timeline.push("text_delta");
}
StreamEvent::TextEnd { content, .. } => {
text.clone_from(content);
timeline.push("text_end");
}
StreamEvent::ThinkingStart { .. } => timeline.push("thinking_start"),
StreamEvent::ThinkingDelta { delta, .. } => {
thinking_deltas += 1;
thinking.push_str(delta);
timeline.push("thinking_delta");
}
StreamEvent::ThinkingEnd { content, .. } => {
thinking.clone_from(content);
timeline.push("thinking_end");
}
StreamEvent::ToolCallStart { .. } => timeline.push("tool_call_start"),
StreamEvent::ToolCallDelta { .. } => timeline.push("tool_call_delta"),
StreamEvent::ToolCallEnd { tool_call, .. } => {
tool_calls.push(tool_call.clone());
timeline.push("tool_call_end");
}
StreamEvent::Done { reason, .. } => {
stop_reason = Some(*reason);
timeline.push("done");
}
StreamEvent::Error { reason, .. } => {
stop_reason = Some(*reason);
timeline.push("error");
}
}
}
// Content hash for determinism tracking
let content_hash = sha256_hex(format!("{text}{thinking}").as_bytes());
// Assertions
if scenario.expect_text {
assert!(
text_deltas > 0,
"{}: expected text deltas, got 0",
scenario.cassette_name
);
}
if scenario.expect_thinking {
assert!(
thinking_deltas > 0,
"{}: expected thinking deltas, got 0",
scenario.cassette_name
);
}
if scenario.expect_tool_calls > 0 {
assert!(
tool_calls.len() >= scenario.expect_tool_calls,
"{}: expected >= {} tool calls, got {}",
scenario.cassette_name,
scenario.expect_tool_calls,
tool_calls.len()
);
}
if !scenario.expect_stop_reasons.is_empty() {
let Some(reason) = stop_reason else {
unreachable!("{}: missing stop reason", scenario.cassette_name);
};
assert!(
scenario.expect_stop_reasons.contains(&reason),
"{}: expected stop reason in {:?}, got {reason:?}",
scenario.cassette_name,
scenario.expect_stop_reasons
);
}
harness.log().info_ctx(
"e2e",
format!("{}: scenario validated", scenario.cassette_name),
|ctx| {
ctx.push(("events".into(), events.len().to_string()));
ctx.push(("text_deltas".into(), text_deltas.to_string()));
ctx.push(("thinking_deltas".into(), thinking_deltas.to_string()));
ctx.push(("tool_calls".into(), tool_calls.len().to_string()));
ctx.push(("content_hash".into(), content_hash.clone()));
ctx.push(("elapsed_ms".into(), elapsed.as_millis().to_string()));
if let Some(reason) = stop_reason {
ctx.push(("stop_reason".into(), format!("{reason:?}")));
}
if let Some(error) = &stream_error {
ctx.push(("stream_error".into(), error.clone()));
}
},
);
json!({
"scenario": scenario.cassette_name,
"description": scenario.description,
"status": "pass",
"kind": "stream",
"events": events.len(),
"text_deltas": text_deltas,
"thinking_deltas": thinking_deltas,
"tool_calls": tool_calls.len(),
"stop_reason": stop_reason.map(|r| format!("{r:?}")),
"content_hash": content_hash,
"timeline": timeline.join(" -> "),
"elapsed_ms": elapsed.as_millis(),
"stream_error": stream_error,
})
}
// ─── Tests ───────────────────────────────────────────────────────────────────
#[test]
fn e2e_anthropic_streaming_all_scenarios() {
let harness = TestHarness::new("e2e_anthropic_streaming_all_scenarios");
let model = std::env::var("ANTHROPIC_TEST_MODEL")
.unwrap_or_else(|_| "claude-sonnet-4-20250514".to_string());
harness
.log()
.info_ctx("e2e", "Starting E2E provider streaming suite", |ctx| {
ctx.push(("provider".into(), "anthropic".to_string()));
ctx.push(("model".into(), model.clone()));
ctx.push(("vcr_mode".into(), "playback".to_string()));
ctx.push(("cassette_dir".into(), cassette_root().display().to_string()));
});
let scenarios = all_anthropic_scenarios();
asupersync::test_utils::run_test(|| {
let model = model.clone();
let harness_ref = &harness;
let scenarios_ref = &scenarios;
async move {
let mut results = Vec::new();
let mut passed = 0usize;
let mut skipped = 0usize;
let mut failed = 0usize;
for scenario in scenarios_ref {
harness_ref.section(&format!("Scenario: {}", scenario.cassette_name));
let result = run_e2e_scenario(scenario, harness_ref, &model).await;
let status = result
.get("status")
.and_then(|v| v.as_str())
.unwrap_or("unknown");
match status {
"pass" => passed += 1,
"skipped" => skipped += 1,
_ => failed += 1,
}
results.push(result);
}
// Write JSONL summary
let summary_path = harness_ref.temp_path("e2e_streaming_results.jsonl");
let mut summary_content = String::new();
for result in &results {
let _ = writeln!(
summary_content,
"{}",
serde_json::to_string(result).unwrap_or_default()
);
}
std::fs::write(&summary_path, &summary_content).expect("write summary jsonl");
harness_ref.record_artifact("e2e_streaming_results.jsonl", &summary_path);
// Write JSONL logs
let log_path = harness_ref.temp_path("e2e_streaming_log.jsonl");
harness_ref
.write_jsonl_logs(&log_path)
.expect("write jsonl log");
harness_ref.record_artifact("e2e_streaming_log.jsonl", &log_path);
// Write artifact index
let artifact_path = harness_ref.temp_path("e2e_streaming_artifacts.jsonl");
harness_ref
.write_artifact_index_jsonl(&artifact_path)
.expect("write artifact index");
harness_ref.record_artifact("e2e_streaming_artifacts.jsonl", &artifact_path);
harness_ref.log().info_ctx("e2e", "Suite completed", |ctx| {
ctx.push(("total".into(), scenarios_ref.len().to_string()));
ctx.push(("passed".into(), passed.to_string()));
ctx.push(("skipped".into(), skipped.to_string()));
ctx.push(("failed".into(), failed.to_string()));
});
assert_eq!(
failed,
0,
"E2E streaming suite: {failed} scenarios failed out of {}",
scenarios_ref.len()
);
}
});
}
/// Verify that running the same cassette twice produces identical content hashes
/// (determinism proof).
#[test]
fn e2e_anthropic_streaming_determinism() {
let harness = TestHarness::new("e2e_anthropic_streaming_determinism");
let model = std::env::var("ANTHROPIC_TEST_MODEL")
.unwrap_or_else(|_| "claude-sonnet-4-20250514".to_string());
let scenario = E2eScenario {
cassette_name: "anthropic_simple_text",
description: "Determinism check: simple text",
messages: vec![user_text("Reply with the single word: pong.")],
tools: Vec::new(),
max_tokens: 64,
thinking_level: None,
expect_error: None,
expect_text: true,
expect_thinking: false,
expect_tool_calls: 0,
expect_stop_reasons: vec![StopReason::Stop],
};
asupersync::test_utils::run_test(|| {
let model = model.clone();
let harness_ref = &harness;
let scenario_ref = &scenario;
async move {
harness_ref.section("Run 1");
let result1 = run_e2e_scenario(scenario_ref, harness_ref, &model).await;
let hash1 = result1
.get("content_hash")
.and_then(|v| v.as_str())
.map(String::from);
harness_ref.section("Run 2");
let result2 = run_e2e_scenario(scenario_ref, harness_ref, &model).await;
let hash2 = result2
.get("content_hash")
.and_then(|v| v.as_str())
.map(String::from);
if let (Some(h1), Some(h2)) = (&hash1, &hash2) {
harness_ref
.log()
.info_ctx("determinism", "Hash comparison", |ctx| {
ctx.push(("run1".into(), h1.clone()));
ctx.push(("run2".into(), h2.clone()));
ctx.push(("match".into(), (h1 == h2).to_string()));
});
assert_eq!(
h1, h2,
"Content hashes differ between runs (non-deterministic)"
);
}
}
});
}
/// Verify that all error scenarios produce errors with appropriate HTTP status codes.
#[test]
fn e2e_anthropic_error_scenarios_comprehensive() {
let harness = TestHarness::new("e2e_anthropic_error_scenarios_comprehensive");
let model = std::env::var("ANTHROPIC_TEST_MODEL")
.unwrap_or_else(|_| "claude-sonnet-4-20250514".to_string());
let error_scenarios: Vec<(&str, u16, &str)> = vec![
(
"anthropic_auth_failure_401",
401,
"Trigger an auth failure.",
),
("anthropic_forbidden_403", 403, "Trigger a forbidden error."),
(
"anthropic_bad_request_400",
400,
"Trigger a bad request error.",
),
("anthropic_server_error_500", 500, "Trigger a server error."),
(
"anthropic_rate_limit_429",
429,
"Trigger a rate limit error.",
),
(
"anthropic_overloaded_529",
529,
"Trigger an overloaded error.",
),
];
for (cassette, expected_status, msg) in &error_scenarios {
harness.section(&format!("Error: HTTP {expected_status}"));
let cassette_path = cassette_root().join(format!("{cassette}.json"));
if !cassette_path.exists() {
harness
.log()
.warn("vcr", format!("Missing cassette {cassette}; skipping"));
continue;
}
asupersync::test_utils::run_test(|| {
let model = model.clone();
let cassette_name = *cassette;
let expected = *expected_status;
let message_text = *msg;
async move {
let cassette_dir = cassette_root();
let recorder =
VcrRecorder::new_with(cassette_name, VcrMode::Playback, &cassette_dir);
let client = Client::new().with_vcr(recorder);
let provider = AnthropicProvider::new(&model).with_client(client);
let context = Context::owned(
Some(
"You are a test harness model. Follow instructions precisely and deterministically."
.to_string(),
),
vec![user_text(message_text)],
Vec::new(),
);
let options = StreamOptions {
api_key: Some("vcr-playback".to_string()),
max_tokens: Some(256),
temperature: Some(0.0),
..Default::default()
};
let Err(err) = provider.stream(&context, &options).await else {
unreachable!("{cassette_name}: expected error, got success");
};
let message = err.to_string();
let needle = format!("HTTP {expected}");
assert!(
message.contains(&needle),
"{cassette_name}: expected '{needle}' in error, got: {message}"
);
}
});
harness.log().info(
"e2e",
format!("HTTP {expected_status} error validated via {cassette}"),
);
}
}