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
//! Unit Tests for Response Structured Response Conversion
//!
//! UNIT UNDER TEST: Response conversion and validation
//!
//! BUSINESS RESPONSIBILITY:
//! - Convert raw LLM JSON responses to typed StructuredResponse objects
//! - Validate JSON schema compliance and handle parsing errors gracefully
//! - Preserve LLM metadata (token usage, model info) during conversion
//! - Support multiple conversion patterns (into_structured, TryInto trait)
//! - Provide meaningful error messages for malformed JSON responses
//!
//! TEST COVERAGE:
//! - Successful conversion of valid structured JSON to StructuredResponse
//! - Error handling for invalid/incomplete JSON structure
//! - Missing structured_response field handling with appropriate errors
//! - TryInto trait implementation for ergonomic conversion patterns
//! - Metadata preservation during structured response conversion
//! - JSON schema validation failure scenarios and error reporting
use crate;
// Note: Structured response types no longer needed since we work with JSON directly
use json;
/// Helper function to create a complete, valid structured response JSON for testing
/// Helper function to create Response with structured data for testing
// Temporarily commented out - these tests relied on conversion methods that are no longer needed
// since Response directly contains structured_response field
/*
#[test]
fn test_llm_response_into_structured_handles_invalid_json() {
// RED: This should fail because into_structured() method doesn't exist yet
// Arrange - Create Response with invalid structured JSON
let invalid_json = json!({
"conversation_response": {
"message": "Valid message",
// Missing required fields like confidence and response_type
}
// Missing required sections like user_analysis
});
let llm_response = Response {
content: "Valid message".to_string(),
structured_response: Some(invalid_json),
tool_calls: vec![],
usage: None,
model: Some("test-model".to_string()),
raw_body: None,
};
// Act - Try to convert (should fail because method doesn't exist)
let result = llm_response.into_structured();
// Assert - Should return meaningful error
assert!(result.is_err(), "Should fail with invalid JSON structure");
let error = result.unwrap_err();
let error_msg = error.to_string();
assert!(
error_msg.contains("missing field") || error_msg.contains("required"),
"Error should mention missing required fields: {}",
error_msg
);
}
#[test]
fn test_llm_response_into_structured_no_structured_data() {
// RED: This should fail because into_structured() method doesn't exist yet
// Arrange - Create Response without structured_response
let llm_response = Response {
content: "Plain text response".to_string(),
structured_response: None,
tool_calls: vec![],
usage: None,
model: Some("test-model".to_string()),
raw_body: None,
};
// Act - Try to convert (should fail because method doesn't exist)
let result = llm_response.into_structured();
// Assert - Should return error about missing structured data
assert!(
result.is_err(),
"Should fail when no structured response available"
);
let error = result.unwrap_err();
let error_msg = error.to_string();
assert!(
error_msg.contains("No structured response") || error_msg.contains("not available"),
"Error should mention missing structured response: {}",
error_msg
);
}
#[test]
fn test_try_into_structured_response_trait() {
// RED: This should fail because TryInto<StructuredResponse> isn't implemented
// Arrange
let structured_json = json!({
"conversation_response": {
"message": "Test message",
"confidence": 0.95,
"response_type": "acknowledgment"
},
"user_analysis": {
"engagement_level": 0.7,
"emotional_state": "happy",
"story_quality_score": 0.6,
"frustration_level": 0.2,
"coherence_score": 0.8
},
"story_elements": {
"characters": [],
"locations": [],
"time_period": null,
"themes": [],
"events": [],
"emotions": [],
"sensory_details": null
},
"response_metadata": {
"suggested_follow_ups": [],
"conversation_phase": "introduction",
"needs_clarification": false
}
});
let llm_response = Response {
content: "Test message".to_string(),
structured_response: Some(structured_json),
tool_calls: vec![],
usage: None,
model: Some("test-model".to_string()),
raw_body: None,
};
// Act - Try to use TryInto (should fail because trait isn't implemented)
let result: Result<StructuredResponse, _> = llm_response.try_into();
// Assert - Should succeed
assert!(
result.is_ok(),
"TryInto should work for valid structured JSON"
);
let structured = result.unwrap();
assert_eq!(structured.conversation_response.message, "Test message");
assert_eq!(structured.user_analysis.engagement_level, 0.7);
}
#[test]
fn test_structured_response_preserves_llm_metadata() {
// RED: This should fail because conversion doesn't preserve metadata yet
// Arrange
let structured_json = json!({
"conversation_response": {
"message": "Metadata test",
"confidence": 0.88,
"response_type": "greeting"
},
"user_analysis": {
"engagement_level": 0.75,
"emotional_state": "excited",
"story_quality_score": 0.65,
"frustration_level": 0.1,
"coherence_score": 0.9
},
"story_elements": {
"characters": [],
"locations": [],
"time_period": null,
"themes": [],
"events": [],
"emotions": [],
"sensory_details": null
},
"response_metadata": {
"suggested_follow_ups": ["How are you feeling?"],
"conversation_phase": "introduction",
"needs_clarification": false
}
});
let llm_response = Response {
content: "Metadata test".to_string(),
structured_response: Some(structured_json),
tool_calls: vec![],
usage: Some(TokenUsage {
prompt_tokens: 150,
completion_tokens: 85,
total_tokens: 235,
}),
raw_body: Some("raw response body".to_string()),
};
// Act - Try to convert with metadata preservation (should fail because method doesn't exist)
let (structured, metadata) = llm_response.into_structured_with_metadata().unwrap();
// Assert - Metadata should be preserved
assert_eq!(structured.conversation_response.message, "Metadata test");
assert_eq!(metadata.usage.unwrap().total_tokens, 235);
assert_eq!(metadata.raw_body.unwrap(), "raw response body");
}
*/