forked from Dicklesworthstone/pi_agent_rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathe2e_golden_transcript_diff.rs
More file actions
1886 lines (1743 loc) · 68.8 KB
/
Copy pathe2e_golden_transcript_diff.rs
File metadata and controls
1886 lines (1743 loc) · 68.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
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
//! Golden transcript capture + cross-provider diff tooling (bd-3uqg.8.9).
//!
//! Captures normalized event streams from multiple provider families using
//! identical prompts, diffs them across providers, and produces JSONL/Markdown
//! reports for drift detection.
//!
//! # Approach
//!
//! 1. Each provider family has a "golden transcript" — a normalized event
//! sequence captured from a deterministic mock response.
//! 2. Cross-provider diffs compare text extraction, tool-call shape, stop
//! reasons, and event sequence structure.
//! 3. All output is JSONL for machine consumption + human-readable summaries.
//!
//! Run:
//! ```bash
//! cargo test --test e2e_golden_transcript_diff
//! ```
#![allow(clippy::too_many_lines)]
#![allow(clippy::doc_markdown)]
#![allow(clippy::items_after_statements)]
#![allow(clippy::similar_names)]
#![allow(clippy::cast_precision_loss)]
#![allow(clippy::missing_const_for_fn)]
#![allow(clippy::redundant_clone)]
#![allow(clippy::type_complexity)]
#![allow(clippy::redundant_closure)]
mod common;
use common::{MockHttpResponse, MockHttpServer, TestHarness};
use futures::StreamExt;
use pi::model::{Message, StreamEvent, UserContent, UserMessage};
use pi::models::ModelEntry;
use pi::provider::{Context, InputType, Model, ModelCost, StreamOptions, ToolDef};
use pi::providers::create_provider;
use serde::Serialize;
use serde_json::json;
use std::collections::HashMap;
use std::fmt::Write as _;
use std::sync::Arc;
// ═══════════════════════════════════════════════════════════════════════
// Core types
// ═══════════════════════════════════════════════════════════════════════
/// Schema for golden transcript JSONL artifacts.
const TRANSCRIPT_SCHEMA: &str = "pi.golden_transcript.v1";
/// A single normalized event in the golden transcript.
#[derive(Debug, Clone, Serialize)]
struct NormalizedEvent {
/// Event kind (Start, TextDelta, TextEnd, ToolCallEnd, Done, Error, etc.)
kind: String,
/// Content index for block events.
content_index: Option<usize>,
/// Text content (for TextDelta/TextEnd).
text: Option<String>,
/// Tool call details (for ToolCallEnd).
tool_name: Option<String>,
tool_arguments: Option<String>,
/// Stop reason (for Done/Error).
stop_reason: Option<String>,
/// Error message (for Error events).
error_message: Option<String>,
}
/// Complete golden transcript for one provider.
#[derive(Debug, Clone, Serialize)]
struct GoldenTranscript {
/// Provider family name.
family: String,
/// Provider ID.
provider: String,
/// Scenario name (e.g., "text", "tool_call").
scenario: String,
/// Normalized event sequence.
events: Vec<NormalizedEvent>,
/// Extracted final text content.
final_text: String,
/// Number of tool calls.
tool_call_count: usize,
/// Stop reason string.
stop_reason: Option<String>,
/// Whether the event sequence passed structural validation.
sequence_valid: bool,
/// Validation error, if any.
sequence_error: Option<String>,
/// Total event count.
event_count: usize,
}
/// A difference found between two provider transcripts.
#[derive(Debug, Clone, Serialize)]
struct TranscriptDiff {
/// Field that differs.
field: String,
/// Baseline provider family.
baseline_family: String,
/// Baseline value.
baseline_value: String,
/// Comparison provider family.
compare_family: String,
/// Comparison value.
compare_value: String,
/// Severity: "structural" (event shape), "semantic" (content), "cosmetic" (non-critical).
severity: String,
}
/// Cross-provider diff report.
#[derive(Debug, Clone, Serialize)]
struct DiffReport {
schema: String,
scenario: String,
baseline_family: String,
families_compared: Vec<String>,
diffs: Vec<TranscriptDiff>,
all_match: bool,
}
// ═══════════════════════════════════════════════════════════════════════
// Helpers
// ═══════════════════════════════════════════════════════════════════════
fn make_entry(provider: &str, model_id: &str, base_url: &str) -> ModelEntry {
ModelEntry {
model: Model {
id: model_id.to_string(),
name: format!("{provider} golden model"),
api: String::new(),
provider: provider.to_string(),
base_url: base_url.to_string(),
reasoning: false,
input: vec![InputType::Text],
cost: ModelCost {
input: 0.0,
output: 0.0,
cache_read: 0.0,
cache_write: 0.0,
},
context_window: 8192,
max_tokens: 4096,
headers: HashMap::new(),
},
api_key: None,
headers: HashMap::new(),
auth_header: false,
compat: None,
oauth_config: None,
}
}
fn simple_context() -> Context<'static> {
Context::owned(
Some("You are a helpful assistant.".to_string()),
vec![Message::User(UserMessage {
content: UserContent::Text("Say hello world.".to_string()),
timestamp: 0,
})],
Vec::new(),
)
}
fn tool_context() -> Context<'static> {
Context::owned(
Some("You are a helpful assistant.".to_string()),
vec![Message::User(UserMessage {
content: UserContent::Text("Call the echo function with text hello.".to_string()),
timestamp: 0,
})],
vec![ToolDef {
name: "echo".to_string(),
description: "Echo text back".to_string(),
parameters: json!({
"type": "object",
"properties": {
"text": {"type": "string", "description": "text to echo"}
},
"required": ["text"],
}),
}],
)
}
fn default_options() -> StreamOptions {
StreamOptions {
api_key: Some("golden-test-key".to_string()),
max_tokens: Some(64),
..Default::default()
}
}
fn make_sse_response(body: &str) -> MockHttpResponse {
MockHttpResponse {
status: 200,
headers: vec![("Content-Type".to_string(), "text/event-stream".to_string())],
body: body.as_bytes().to_vec(),
}
}
fn collect_events(
provider: Arc<dyn pi::provider::Provider>,
context: Context<'static>,
options: StreamOptions,
) -> Result<Vec<StreamEvent>, String> {
common::run_async(async move {
let stream = provider
.stream(&context, &options)
.await
.map_err(|e| e.to_string())?;
let mut pinned = std::pin::pin!(stream);
let mut events = Vec::new();
while let Some(item) = pinned.next().await {
let event = item.map_err(|e| e.to_string())?;
let terminal = matches!(event, StreamEvent::Done { .. } | StreamEvent::Error { .. });
events.push(event);
if terminal {
break;
}
}
Ok(events)
})
}
// ═══════════════════════════════════════════════════════════════════════
// Normalization
// ═══════════════════════════════════════════════════════════════════════
fn event_kind(event: &StreamEvent) -> &'static str {
match event {
StreamEvent::Start { .. } => "Start",
StreamEvent::TextStart { .. } => "TextStart",
StreamEvent::TextDelta { .. } => "TextDelta",
StreamEvent::TextEnd { .. } => "TextEnd",
StreamEvent::ThinkingStart { .. } => "ThinkingStart",
StreamEvent::ThinkingDelta { .. } => "ThinkingDelta",
StreamEvent::ThinkingEnd { .. } => "ThinkingEnd",
StreamEvent::ToolCallStart { .. } => "ToolCallStart",
StreamEvent::ToolCallDelta { .. } => "ToolCallDelta",
StreamEvent::ToolCallEnd { .. } => "ToolCallEnd",
StreamEvent::Done { .. } => "Done",
StreamEvent::Error { .. } => "Error",
}
}
fn normalize_event(event: &StreamEvent) -> NormalizedEvent {
match event {
StreamEvent::Start { .. } => NormalizedEvent {
kind: "Start".to_string(),
content_index: None,
text: None,
tool_name: None,
tool_arguments: None,
stop_reason: None,
error_message: None,
},
StreamEvent::TextDelta {
content_index,
delta,
..
} => NormalizedEvent {
kind: "TextDelta".to_string(),
content_index: Some(*content_index),
text: Some(delta.clone()),
tool_name: None,
tool_arguments: None,
stop_reason: None,
error_message: None,
},
StreamEvent::TextEnd {
content_index,
content,
..
} => NormalizedEvent {
kind: "TextEnd".to_string(),
content_index: Some(*content_index),
text: Some(content.clone()),
tool_name: None,
tool_arguments: None,
stop_reason: None,
error_message: None,
},
StreamEvent::TextStart { content_index, .. } => NormalizedEvent {
kind: "TextStart".to_string(),
content_index: Some(*content_index),
text: None,
tool_name: None,
tool_arguments: None,
stop_reason: None,
error_message: None,
},
StreamEvent::ToolCallStart { content_index, .. } => NormalizedEvent {
kind: "ToolCallStart".to_string(),
content_index: Some(*content_index),
text: None,
tool_name: None,
tool_arguments: None,
stop_reason: None,
error_message: None,
},
StreamEvent::ToolCallDelta {
content_index,
delta,
..
} => NormalizedEvent {
kind: "ToolCallDelta".to_string(),
content_index: Some(*content_index),
text: Some(delta.clone()),
tool_name: None,
tool_arguments: None,
stop_reason: None,
error_message: None,
},
StreamEvent::ToolCallEnd {
content_index,
tool_call,
..
} => NormalizedEvent {
kind: "ToolCallEnd".to_string(),
content_index: Some(*content_index),
text: None,
tool_name: Some(tool_call.name.clone()),
tool_arguments: Some(tool_call.arguments.to_string()),
stop_reason: None,
error_message: None,
},
StreamEvent::Done { reason, .. } => NormalizedEvent {
kind: "Done".to_string(),
content_index: None,
text: None,
tool_name: None,
tool_arguments: None,
stop_reason: Some(format!("{reason:?}")),
error_message: None,
},
StreamEvent::Error { reason, error, .. } => NormalizedEvent {
kind: "Error".to_string(),
content_index: None,
text: None,
tool_name: None,
tool_arguments: None,
stop_reason: Some(format!("{reason:?}")),
error_message: error.error_message.clone(),
},
StreamEvent::ThinkingStart { content_index, .. } => NormalizedEvent {
kind: "ThinkingStart".to_string(),
content_index: Some(*content_index),
text: None,
tool_name: None,
tool_arguments: None,
stop_reason: None,
error_message: None,
},
StreamEvent::ThinkingDelta {
content_index,
delta,
..
} => NormalizedEvent {
kind: "ThinkingDelta".to_string(),
content_index: Some(*content_index),
text: Some(delta.clone()),
tool_name: None,
tool_arguments: None,
stop_reason: None,
error_message: None,
},
StreamEvent::ThinkingEnd {
content_index,
content,
..
} => NormalizedEvent {
kind: "ThinkingEnd".to_string(),
content_index: Some(*content_index),
text: Some(content.clone()),
tool_name: None,
tool_arguments: None,
stop_reason: None,
error_message: None,
},
}
}
fn validate_sequence(events: &[StreamEvent]) -> Result<(), String> {
if events.is_empty() {
return Err("no events emitted".to_string());
}
if !matches!(events.first(), Some(StreamEvent::Start { .. })) {
return Err("first event must be Start".to_string());
}
if !matches!(
events.last(),
Some(StreamEvent::Done { .. } | StreamEvent::Error { .. })
) {
return Err("last event must be Done or Error".to_string());
}
Ok(())
}
fn build_transcript(
family: &str,
provider: &str,
scenario: &str,
result: &Result<Vec<StreamEvent>, String>,
) -> GoldenTranscript {
match result {
Ok(events) => {
let normalized: Vec<NormalizedEvent> = events.iter().map(normalize_event).collect();
let final_text = extract_final_text(events);
let tool_call_count = events
.iter()
.filter(|e| matches!(e, StreamEvent::ToolCallEnd { .. }))
.count();
let stop_reason = events.iter().find_map(|e| match e {
StreamEvent::Done { reason, .. } | StreamEvent::Error { reason, .. } => {
Some(format!("{reason:?}"))
}
_ => None,
});
let validation = validate_sequence(events);
GoldenTranscript {
family: family.to_string(),
provider: provider.to_string(),
scenario: scenario.to_string(),
event_count: normalized.len(),
events: normalized,
final_text,
tool_call_count,
stop_reason,
sequence_valid: validation.is_ok(),
sequence_error: validation.err(),
}
}
Err(e) => GoldenTranscript {
family: family.to_string(),
provider: provider.to_string(),
scenario: scenario.to_string(),
events: Vec::new(),
final_text: String::new(),
tool_call_count: 0,
stop_reason: None,
sequence_valid: false,
sequence_error: Some(e.clone()),
event_count: 0,
},
}
}
fn extract_final_text(events: &[StreamEvent]) -> String {
// Prefer TextEnd content; fall back to concatenating TextDelta.
let from_end: Option<String> = events.iter().find_map(|e| match e {
StreamEvent::TextEnd { content, .. } => Some(content.clone()),
_ => None,
});
if let Some(text) = from_end {
return text;
}
let mut buf = String::new();
for e in events {
if let StreamEvent::TextDelta { delta, .. } = e {
buf.push_str(delta);
}
}
buf
}
// ═══════════════════════════════════════════════════════════════════════
// Diff engine
// ═══════════════════════════════════════════════════════════════════════
fn diff_transcripts(
baseline: &GoldenTranscript,
compare: &GoldenTranscript,
) -> Vec<TranscriptDiff> {
let mut diffs = Vec::new();
// 1. Final text content
if baseline.final_text != compare.final_text {
diffs.push(TranscriptDiff {
field: "final_text".to_string(),
baseline_family: baseline.family.clone(),
baseline_value: baseline.final_text.clone(),
compare_family: compare.family.clone(),
compare_value: compare.final_text.clone(),
severity: "semantic".to_string(),
});
}
// 2. Tool call count
if baseline.tool_call_count != compare.tool_call_count {
diffs.push(TranscriptDiff {
field: "tool_call_count".to_string(),
baseline_family: baseline.family.clone(),
baseline_value: baseline.tool_call_count.to_string(),
compare_family: compare.family.clone(),
compare_value: compare.tool_call_count.to_string(),
severity: "structural".to_string(),
});
}
// 3. Stop reason
if baseline.stop_reason != compare.stop_reason {
diffs.push(TranscriptDiff {
field: "stop_reason".to_string(),
baseline_family: baseline.family.clone(),
baseline_value: baseline.stop_reason.clone().unwrap_or_default(),
compare_family: compare.family.clone(),
compare_value: compare.stop_reason.clone().unwrap_or_default(),
severity: "structural".to_string(),
});
}
// 4. Event kind sequence (abstract shape)
let baseline_kinds: Vec<&str> = baseline.events.iter().map(|e| e.kind.as_str()).collect();
let compare_kinds: Vec<&str> = compare.events.iter().map(|e| e.kind.as_str()).collect();
let baseline_shape = abstract_event_shape(&baseline_kinds);
let compare_shape = abstract_event_shape(&compare_kinds);
if baseline_shape != compare_shape {
diffs.push(TranscriptDiff {
field: "event_shape".to_string(),
baseline_family: baseline.family.clone(),
baseline_value: baseline_shape,
compare_family: compare.family.clone(),
compare_value: compare_shape,
severity: "structural".to_string(),
});
}
// 5. Sequence validity
if baseline.sequence_valid != compare.sequence_valid {
diffs.push(TranscriptDiff {
field: "sequence_valid".to_string(),
baseline_family: baseline.family.clone(),
baseline_value: baseline.sequence_valid.to_string(),
compare_family: compare.family.clone(),
compare_value: compare.sequence_valid.to_string(),
severity: "structural".to_string(),
});
}
// 6. Tool call names (if both have tool calls)
if baseline.tool_call_count > 0 && compare.tool_call_count > 0 {
let baseline_names: Vec<&str> = baseline
.events
.iter()
.filter_map(|e| e.tool_name.as_deref())
.collect();
let compare_names: Vec<&str> = compare
.events
.iter()
.filter_map(|e| e.tool_name.as_deref())
.collect();
if baseline_names != compare_names {
diffs.push(TranscriptDiff {
field: "tool_names".to_string(),
baseline_family: baseline.family.clone(),
baseline_value: format!("{baseline_names:?}"),
compare_family: compare.family.clone(),
compare_value: format!("{compare_names:?}"),
severity: "semantic".to_string(),
});
}
}
diffs
}
/// Collapse event kinds into an abstract shape, merging consecutive deltas.
fn abstract_event_shape(kinds: &[&str]) -> String {
let mut shape = Vec::new();
let mut last = "";
for kind in kinds {
if *kind == last && kind.contains("Delta") {
// Merge consecutive deltas
continue;
}
shape.push(*kind);
last = kind;
}
shape.join(" → ")
}
fn build_diff_report(
scenario: &str,
baseline: &GoldenTranscript,
others: &[GoldenTranscript],
) -> DiffReport {
let mut all_diffs = Vec::new();
let mut families = Vec::new();
for other in others {
families.push(other.family.clone());
all_diffs.extend(diff_transcripts(baseline, other));
}
DiffReport {
schema: TRANSCRIPT_SCHEMA.to_string(),
scenario: scenario.to_string(),
baseline_family: baseline.family.clone(),
families_compared: families,
all_match: all_diffs.is_empty(),
diffs: all_diffs,
}
}
// ═══════════════════════════════════════════════════════════════════════
// Output
// ═══════════════════════════════════════════════════════════════════════
fn write_transcript_jsonl(harness: &TestHarness, name: &str, transcripts: &[GoldenTranscript]) {
let mut buf = String::new();
for t in transcripts {
let _ = writeln!(buf, "{}", serde_json::to_string(t).unwrap_or_default());
}
let path = harness.temp_path(format!("{name}_transcripts.jsonl"));
std::fs::write(&path, &buf).expect("write transcript JSONL");
harness.record_artifact(format!("{name}_transcripts.jsonl"), &path);
}
fn write_diff_report_jsonl(harness: &TestHarness, name: &str, report: &DiffReport) {
let content = serde_json::to_string_pretty(report).unwrap_or_default();
let path = harness.temp_path(format!("{name}_diff.json"));
std::fs::write(&path, &content).expect("write diff report");
harness.record_artifact(format!("{name}_diff.json"), &path);
}
fn write_markdown_summary(harness: &TestHarness, name: &str, report: &DiffReport) {
let mut md = String::new();
let _ = writeln!(md, "# Golden Transcript Diff: {}", report.scenario);
let _ = writeln!(md, "\nBaseline: **{}**", report.baseline_family);
let _ = writeln!(md, "Compared: {}", report.families_compared.join(", "));
let _ = writeln!(
md,
"Result: **{}**\n",
if report.all_match {
"ALL MATCH"
} else {
"DIFFS FOUND"
}
);
if !report.diffs.is_empty() {
let _ = writeln!(md, "| Field | Baseline | Compare | Severity |");
let _ = writeln!(md, "|-------|----------|---------|----------|");
for d in &report.diffs {
let _ = writeln!(
md,
"| {} | {}={} | {}={} | {} |",
d.field,
d.baseline_family,
truncate_for_table(&d.baseline_value, 30),
d.compare_family,
truncate_for_table(&d.compare_value, 30),
d.severity,
);
}
}
let path = harness.temp_path(format!("{name}_diff.md"));
std::fs::write(&path, &md).expect("write diff markdown");
harness.record_artifact(format!("{name}_diff.md"), &path);
}
fn truncate_for_table(s: &str, max: usize) -> String {
if s.len() <= max {
s.to_string()
} else {
// Find a char boundary at or before `max` to avoid panicking on
// multi-byte UTF-8 sequences.
let mut end = max.min(s.len());
while end > 0 && !s.is_char_boundary(end) {
end -= 1;
}
format!("{}...", &s[..end])
}
}
// ═══════════════════════════════════════════════════════════════════════
// SSE fixtures (reused from e2e_provider_scenarios patterns)
// ═══════════════════════════════════════════════════════════════════════
fn openai_responses_text_sse() -> String {
[
r#"data: {"type":"response.output_text.delta","item_id":"msg_1","content_index":0,"delta":"Hello"}"#,
"",
r#"data: {"type":"response.output_text.delta","item_id":"msg_1","content_index":0,"delta":" world!"}"#,
"",
r#"data: {"type":"response.completed","response":{"incomplete_details":null,"usage":{"input_tokens":10,"output_tokens":5,"total_tokens":15}}}"#,
"",
]
.join("\n")
}
fn anthropic_text_sse() -> String {
[
r"event: message_start",
r#"data: {"type":"message_start","message":{"id":"msg_001","type":"message","role":"assistant","content":[],"model":"claude-test","stop_reason":null,"usage":{"input_tokens":10,"output_tokens":0}}}"#,
"",
r"event: content_block_start",
r#"data: {"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}"#,
"",
r"event: content_block_delta",
r#"data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"Hello world!"}}"#,
"",
r"event: content_block_stop",
r#"data: {"type":"content_block_stop","index":0}"#,
"",
r"event: message_delta",
r#"data: {"type":"message_delta","delta":{"stop_reason":"end_turn"},"usage":{"output_tokens":5}}"#,
"",
r"event: message_stop",
r#"data: {"type":"message_stop"}"#,
"",
]
.join("\n")
}
fn gemini_text_sse() -> String {
[
r#"data: {"candidates":[{"content":{"parts":[{"text":"Hello world!"}],"role":"model"},"finishReason":"STOP","index":0}],"usageMetadata":{"promptTokenCount":10,"candidatesTokenCount":5,"totalTokenCount":15}}"#,
"",
]
.join("\n")
}
fn openai_completions_text_sse() -> String {
[
r#"data: {"id":"oai-001","object":"chat.completion.chunk","created":1700000000,"model":"gpt-test","choices":[{"index":0,"delta":{"role":"assistant","content":"Hello"},"finish_reason":null}]}"#,
"",
r#"data: {"id":"oai-001","object":"chat.completion.chunk","created":1700000000,"model":"gpt-test","choices":[{"index":0,"delta":{"content":" world!"},"finish_reason":null}]}"#,
"",
r#"data: {"id":"oai-001","object":"chat.completion.chunk","created":1700000000,"model":"gpt-test","choices":[{"index":0,"delta":{},"finish_reason":"stop"}],"usage":{"prompt_tokens":10,"completion_tokens":5,"total_tokens":15}}"#,
"",
"data: [DONE]",
"",
]
.join("\n")
}
fn openai_responses_tool_sse() -> String {
[
r#"data: {"type":"response.output_item.added","output_index":0,"item":{"type":"function_call","id":"fc_1","call_id":"call_001","name":"echo","arguments":""}}"#,
"",
r#"data: {"type":"response.function_call_arguments.delta","item_id":"fc_1","output_index":0,"delta":"{\"text\":\"hello\"}"}"#,
"",
r#"data: {"type":"response.output_item.done","output_index":0,"item":{"type":"function_call","id":"fc_1","call_id":"call_001","name":"echo","arguments":"{\"text\":\"hello\"}","status":"completed"}}"#,
"",
r#"data: {"type":"response.completed","response":{"incomplete_details":null,"usage":{"input_tokens":15,"output_tokens":12,"total_tokens":27}}}"#,
"",
]
.join("\n")
}
fn anthropic_tool_sse() -> String {
[
r"event: message_start",
r#"data: {"type":"message_start","message":{"id":"msg_tool","type":"message","role":"assistant","content":[],"model":"claude-test","stop_reason":null,"usage":{"input_tokens":15,"output_tokens":0}}}"#,
"",
r"event: content_block_start",
r#"data: {"type":"content_block_start","index":0,"content_block":{"type":"tool_use","id":"toolu_001","name":"echo","input":{}}}"#,
"",
r"event: content_block_delta",
r#"data: {"type":"content_block_delta","index":0,"delta":{"type":"input_json_delta","partial_json":"{\"text\":\"hello\"}"}}"#,
"",
r"event: content_block_stop",
r#"data: {"type":"content_block_stop","index":0}"#,
"",
r"event: message_delta",
r#"data: {"type":"message_delta","delta":{"stop_reason":"tool_use","stop_sequence":null},"usage":{"output_tokens":12}}"#,
"",
r"event: message_stop",
r#"data: {"type":"message_stop"}"#,
"",
]
.join("\n")
}
fn gemini_tool_sse() -> String {
let chunk = json!({
"candidates": [{
"content": {
"role": "model",
"parts": [{"functionCall": {"name": "echo", "args": {"text": "hello"}}}]
},
"finishReason": "STOP"
}],
"usageMetadata": {
"promptTokenCount": 15,
"candidatesTokenCount": 12,
"totalTokenCount": 27
}
});
format!("data: {}\n\n", serde_json::to_string(&chunk).unwrap())
}
// ═══════════════════════════════════════════════════════════════════════
// Provider setup helpers
// ═══════════════════════════════════════════════════════════════════════
fn setup_openai_responses(
harness: &TestHarness,
sse: &str,
) -> (Arc<dyn pi::provider::Provider>, MockHttpServer) {
let server = harness.start_mock_http_server();
server.add_route("POST", "/v1/responses", make_sse_response(sse));
let base_url = format!("{}/v1", server.base_url());
let mut entry = make_entry("openai", "golden-gpt", &base_url);
entry.model.api.clear();
let provider = create_provider(&entry, None).expect("create openai provider");
(provider, server)
}
fn setup_openai_completions(
harness: &TestHarness,
sse: &str,
) -> (Arc<dyn pi::provider::Provider>, MockHttpServer) {
let server = harness.start_mock_http_server();
server.add_route(
"POST",
"/openai/v1/chat/completions",
make_sse_response(sse),
);
let base_url = format!("{}/openai/v1", server.base_url());
let mut entry = make_entry("groq", "golden-llama", &base_url);
entry.model.api.clear();
let provider = create_provider(&entry, None).expect("create openai-compat provider");
(provider, server)
}
fn setup_anthropic(
harness: &TestHarness,
sse: &str,
) -> (Arc<dyn pi::provider::Provider>, MockHttpServer) {
let server = harness.start_mock_http_server();
server.add_route("POST", "/v1/messages", make_sse_response(sse));
let base_url = format!("{}/v1/messages", server.base_url());
let mut entry = make_entry("anthropic", "golden-claude", &base_url);
entry.model.api.clear();
let provider = create_provider(&entry, None).expect("create anthropic provider");
(provider, server)
}
fn setup_gemini(
harness: &TestHarness,
sse: &str,
) -> (Arc<dyn pi::provider::Provider>, MockHttpServer) {
let server = harness.start_mock_http_server();
let route = "/v1beta/models/golden-gemini:streamGenerateContent?alt=sse";
server.add_route("POST", route, make_sse_response(sse));
let base_url = format!("{}/v1beta", server.base_url());
let mut entry = make_entry("google", "golden-gemini", &base_url);
entry.model.api.clear();
let provider = create_provider(&entry, None).expect("create gemini provider");
(provider, server)
}
// ═══════════════════════════════════════════════════════════════════════
// Section 1: Golden transcript capture for text responses
// ═══════════════════════════════════════════════════════════════════════
#[test]
fn capture_text_golden_transcripts() {
let harness = TestHarness::new("capture_text_golden_transcripts");
let ctx = simple_context();
let opts = default_options();
let mut transcripts = Vec::new();
// OpenAI Responses API
let (provider, _server) = setup_openai_responses(&harness, &openai_responses_text_sse());
let result = collect_events(provider, ctx.clone(), opts.clone());
transcripts.push(build_transcript(
"openai-responses",
"openai",
"text",
&result,
));
// Anthropic
let (provider, _server) = setup_anthropic(&harness, &anthropic_text_sse());
let result = collect_events(provider, ctx.clone(), opts.clone());
transcripts.push(build_transcript(
"anthropic-messages",
"anthropic",
"text",
&result,
));
// Gemini
let (provider, _server) = setup_gemini(&harness, &gemini_text_sse());
let result = collect_events(provider, ctx.clone(), opts.clone());
transcripts.push(build_transcript(
"gemini-generative",
"google",
"text",
&result,
));
// OpenAI Completions (via groq)
let (provider, _server) = setup_openai_completions(&harness, &openai_completions_text_sse());
let result = collect_events(provider, ctx.clone(), opts.clone());
transcripts.push(build_transcript(
"openai-completions",
"groq",
"text",
&result,
));
// All should produce valid transcripts
for t in &transcripts {
assert!(
t.sequence_valid,
"{} transcript should be valid: {:?}",
t.family, t.sequence_error
);
}
write_transcript_jsonl(&harness, "text", &transcripts);
}
#[test]
fn text_transcripts_all_extract_hello_world() {
let harness = TestHarness::new("text_transcripts_all_extract_hello_world");
let ctx = simple_context();
let opts = default_options();
let families: Vec<(
&str,
&str,
String,
Box<dyn Fn(&TestHarness, &str) -> (Arc<dyn pi::provider::Provider>, MockHttpServer)>,
)> = vec![
(
"openai-responses",
"openai",
openai_responses_text_sse(),
Box::new(|h, s| setup_openai_responses(h, s)),
),
(
"anthropic-messages",
"anthropic",
anthropic_text_sse(),
Box::new(|h, s| setup_anthropic(h, s)),
),
(
"gemini-generative",
"google",
gemini_text_sse(),
Box::new(|h, s| setup_gemini(h, s)),
),
(
"openai-completions",
"groq",
openai_completions_text_sse(),
Box::new(|h, s| setup_openai_completions(h, s)),
),
];
for (family, provider, sse, setup) in &families {
let (prov, _server) = setup(&harness, sse);
let result = collect_events(prov, ctx.clone(), opts.clone());
let transcript = build_transcript(family, provider, "text", &result);
assert!(
transcript.final_text.contains("Hello") && transcript.final_text.contains("world"),
"{family}: expected 'Hello world' but got '{}'",
transcript.final_text
);
}
}
#[test]
fn text_transcripts_all_have_stop_reason() {
let harness = TestHarness::new("text_transcripts_all_have_stop_reason");
let ctx = simple_context();
let opts = default_options();
let setups: Vec<(
&str,
String,
Box<dyn Fn(&TestHarness, &str) -> (Arc<dyn pi::provider::Provider>, MockHttpServer)>,
)> = vec![
(
"openai-responses",
openai_responses_text_sse(),
Box::new(|h, s| setup_openai_responses(h, s)),
),
(
"anthropic-messages",
anthropic_text_sse(),
Box::new(|h, s| setup_anthropic(h, s)),
),
(
"gemini-generative",
gemini_text_sse(),
Box::new(|h, s| setup_gemini(h, s)),
),
(
"openai-completions",
openai_completions_text_sse(),
Box::new(|h, s| setup_openai_completions(h, s)),
),
];