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
//! Builder Validation Enhancement Tests
//!
//! This module tests enhanced builder functionality including:
//! - Builder validation methods
//! - Convenience builder methods
//! - Enhanced error handling for builders
//! - Advanced builder features
use api_openai::exposed::
{
components ::
{
responses ::
{
CreateResponseRequest,
ResponseInput,
},
common ::
{
ModelIdsResponses,
},
input ::
{
InputMessage,
InputContentPart,
InputText,
},
tools ::
{
FunctionTool,
FunctionParameters,
},
}
};
/// Test that builders should have validation methods
#[ test ]
fn test_builder_validation_methods()
{
// Test that we can validate a builder before calling form()
let builder = CreateResponseRequest::former()
.model(ModelIdsResponses::from("gpt-4".to_string()));
// This should fail because input is required but not set
// Expected : builder should have a validate() method that returns Result< (), ValidationError >
// Complete the builder properly
let complete_builder = builder
.input(ResponseInput::String("Test input".to_string()));
// This should succeed because all required fields are set
// For now, we'll just test that the builder can be formed
let request = complete_builder.form();
assert_eq!(request.input, ResponseInput::String("Test input".to_string()));
}
/// Test builder convenience methods for common patterns
#[ test ]
fn test_builder_convenience_methods()
{
// Test that builders should have convenience methods for common usage patterns
// Expected : CreateResponseRequest should have a with_simple_text() convenience method
// Current verbose way:
let request_verbose = CreateResponseRequest::former()
.model(ModelIdsResponses::from("gpt-4".to_string()))
.input(ResponseInput::String("Tell me a story".to_string()))
.temperature(0.7)
.form();
// Desired convenience method (this will fail until implemented)
// For now, verify the verbose way works
assert_eq!(request_verbose.input, ResponseInput::String("Tell me a story".to_string()));
}
/// Test enhanced builder error handling
#[ test ]
fn test_builder_enhanced_error_handling()
{
// Test that builders should provide better error messages for common mistakes
// This currently compiles but could have better error messages
let request = CreateResponseRequest::former()
.model(ModelIdsResponses::from("invalid-model".to_string()))
.input(ResponseInput::String("Test".to_string()))
.temperature(2.5) // This is outside the typical range (0.0-2.0)
.form();
// Expected : Builder should have validation that warns about or prevents invalid values
assert_eq!(request.temperature, Some(2.5)); // Currently allows any f32 value
}
/// Test builder chaining with validation
#[ test ]
fn test_builder_chaining_with_validation()
{
// Test building complex structures with validation at each step
let message = InputMessage::former()
.role("user".to_string())
.content(vec![
InputContentPart::Text(InputText::former()
.text(String::new()) // Empty text - could be validated
.form())
])
.form();
// Currently allows empty text, but enhanced builders might validate this
assert_eq!(message.content[0], InputContentPart::Text(InputText { text : String::new() }));
}
/// Test builder documentation generation
#[ test ]
fn test_builder_documentation_features()
{
// Test that builders can provide helpful documentation about their usage
// This is more about the builder pattern having good documentation strings
let request = CreateResponseRequest::former()
.model(ModelIdsResponses::from("gpt-4".to_string()))
.input(ResponseInput::String("Test".to_string()))
.form();
// The builder should have comprehensive documentation
// This test just verifies the builder works for now
assert!(!request.model.value.is_empty());
}
/// Test builder performance optimizations
#[ test ]
fn test_builder_performance_optimizations()
{
use std::time::Instant;
let start = Instant::now();
// Test that builders are efficiently constructed
for i in 0..10000
{
let _request = CreateResponseRequest::former()
.model(ModelIdsResponses::from(format!("gpt-4-{}", i % 10)))
.input(ResponseInput::String(format!("Request {i}")))
.temperature(0.1 * (i % 10) as f32)
.form();
}
let duration = start.elapsed();
println!("Built 10000 requests in {duration:?}");
// Enhanced builders should be very fast - expect under 100ms for 10k builds
assert!(duration.as_millis() < 1000, "Builder performance should be optimized : {duration:?}");
}
/// Test builder pattern consistency across different types
#[ test ]
fn test_builder_pattern_consistency()
{
// Test that all builders follow consistent patterns
// All builders should support the same basic operations
let response_request = CreateResponseRequest::former()
.model(ModelIdsResponses::from("gpt-4".to_string()))
.input(ResponseInput::String("Test".to_string()))
.form();
let message = InputMessage::former()
.role("user".to_string())
.content(vec![InputContentPart::Text(InputText::former().text("Test".to_string()).form())])
.form();
let function_tool = FunctionTool::former()
.name("test_function".to_string())
.parameters(FunctionParameters::new(serde_json::json!({"type": "object"})))
.form();
// All should be successfully created
assert!(!response_request.model.value.is_empty());
assert!(!message.role.is_empty());
assert!(!function_tool.name.is_empty());
}