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
//! Rust e2e test-file rendering.
use std::fmt::Write as FmtWrite;
use crate::e2e::config::E2eConfig;
use crate::e2e::fixture::Fixture;
use super::helpers::{body_references_symbol, resolve_function_name_for_call, resolve_module, resolve_module_for_call};
use super::test_function::render_test_function;
use crate::e2e::codegen::rust::args::{resolve_handle_config_type, resolve_visitor_trait};
pub fn render_test_file(
category: &str,
fixtures: &[&Fixture],
e2e_config: &E2eConfig,
config: &crate::core::config::ResolvedCrateConfig,
type_defs: &[crate::core::ir::TypeDef],
dep_name: &str,
needs_mock_server: bool,
) -> String {
let mut out = String::new();
out.push_str(&crate::core::hash::header(crate::core::hash::CommentStyle::DoubleSlash));
let _ = writeln!(out, "//! E2e tests for category: {category}");
let _ = writeln!(out);
let module = resolve_module(e2e_config, dep_name);
// Call-based: has mock_response OR is a plain function-call fixture (no http, no mock) with a
// configured function name. Pure schema/spec stubs (function name empty) use the stub path.
let file_has_call_based = fixtures.iter().any(|f| {
if f.mock_response.is_some() {
return true;
}
if f.http.is_none() && f.mock_response.is_none() {
let call_config = e2e_config.resolve_call_for_fixture(
f.call.as_deref(),
&f.id,
&f.resolved_category(),
&f.tags,
&f.input,
);
let fn_name = resolve_function_name_for_call(call_config);
return !fn_name.is_empty();
}
false
});
// Collect all unique (module, function) pairs needed across call-based fixtures only.
// Resolve client_factory from the default call's rust override. When set, the generated tests
// create a client via `module::factory(...)` and call methods on it rather than importing and
// calling free functions. In that case we skip the function `use` imports entirely.
let rust_call_override = e2e_config.call.overrides.get("rust");
let client_factory = rust_call_override.and_then(|o| o.client_factory.as_deref());
// Http fixtures and pure stub fixtures use different code paths and don't import the call function.
if file_has_call_based && client_factory.is_none() {
let mut imported: std::collections::BTreeSet<(String, String)> = std::collections::BTreeSet::new();
for fixture in fixtures.iter().filter(|f| {
if f.mock_response.is_some() {
return true;
}
if f.http.is_none() && f.mock_response.is_none() {
let call_config = e2e_config.resolve_call_for_fixture(
f.call.as_deref(),
&f.id,
&f.resolved_category(),
&f.tags,
&f.input,
);
let fn_name = resolve_function_name_for_call(call_config);
return !fn_name.is_empty();
}
false
}) {
let call_config = e2e_config.resolve_call_for_fixture(
fixture.call.as_deref(),
&fixture.id,
&fixture.resolved_category(),
&fixture.tags,
&fixture.input,
);
let fn_name = resolve_function_name_for_call(call_config);
let mod_name = resolve_module_for_call(call_config, dep_name);
imported.insert((mod_name, fn_name));
}
// Emit use statements, grouping by module when possible.
let mut by_module: std::collections::BTreeMap<String, Vec<String>> = std::collections::BTreeMap::new();
for (mod_name, fn_name) in &imported {
by_module.entry(mod_name.clone()).or_default().push(fn_name.clone());
}
for (mod_name, fns) in &by_module {
if fns.len() == 1 {
let _ = writeln!(out, "use {mod_name}::{};", fns[0]);
} else {
let joined = fns.join(", ");
let _ = writeln!(out, "use {mod_name}::{{{joined}}};");
}
}
}
// Http fixtures reference `App` and `RequestContext` via their fully-qualified
// module path (`{dep_name}::App::new()`, `{dep_name}::RequestContext`) — no
// top-of-file `use` import is required and emitting one trips `-D unused_imports`.
// Render test function bodies into a side buffer so we can gate optional imports
// on whether the body actually references each imported symbol. Emitting unused
// imports trips `-D unused_imports` in the consumer crate.
let mut body_buf = String::new();
for fixture in fixtures {
render_test_function(
&mut body_buf,
fixture,
e2e_config,
config,
type_defs,
dep_name,
client_factory,
);
let _ = writeln!(body_buf);
}
// Collect all crate-level type imports (handle config types, constructor names, trait
// imports, options_type annotations) into one BTreeSet so a symbol that appears in
// more than one source is emitted only once, preventing E0252 "defined multiple times".
let mut crate_imports: std::collections::BTreeSet<String> = std::collections::BTreeSet::new();
// Import handle constructor functions and any resolved config types they use.
let has_handle_args = fixtures.iter().any(|fixture| {
let call_config = e2e_config.resolve_call_for_fixture(
fixture.call.as_deref(),
&fixture.id,
&fixture.resolved_category(),
&fixture.tags,
&fixture.input,
);
let recipe = crate::e2e::codegen::recipe::E2eCallRecipe::resolve("rust", fixture, call_config, type_defs);
recipe.args.iter().any(|a| a.arg_type == "handle")
});
if has_handle_args {
let mut handle_config_types: std::collections::BTreeSet<String> = std::collections::BTreeSet::new();
for fixture in fixtures {
let call_config = e2e_config.resolve_call_for_fixture(
fixture.call.as_deref(),
&fixture.id,
&fixture.resolved_category(),
&fixture.tags,
&fixture.input,
);
let recipe = crate::e2e::codegen::recipe::E2eCallRecipe::resolve("rust", fixture, call_config, type_defs);
for arg in recipe.args.iter().filter(|arg| arg.arg_type == "handle") {
let value = crate::e2e::codegen::resolve_field(&fixture.input, &arg.field);
if value.is_null() || value.is_object() && value.as_object().is_some_and(|o| o.is_empty()) {
continue;
}
if let Some(config_type) = resolve_handle_config_type(arg, recipe.options_type, type_defs) {
handle_config_types.insert(config_type);
}
}
}
for config_type in handle_config_types {
if body_references_symbol(&body_buf, &config_type) {
crate_imports.insert(config_type);
}
}
let mut constructor_names: std::collections::BTreeSet<String> = std::collections::BTreeSet::new();
for fixture in fixtures {
let call_config = e2e_config.resolve_call_for_fixture(
fixture.call.as_deref(),
&fixture.id,
&fixture.resolved_category(),
&fixture.tags,
&fixture.input,
);
let recipe = crate::e2e::codegen::recipe::E2eCallRecipe::resolve("rust", fixture, call_config, type_defs);
for arg in recipe.args.iter().filter(|a| a.arg_type == "handle") {
use heck::ToSnakeCase;
constructor_names.insert(format!("create_{}", arg.name.to_snake_case()));
}
}
for constructor_name in constructor_names {
if body_references_symbol(&body_buf, &constructor_name) {
crate_imports.insert(constructor_name);
}
}
}
// When client_factory is set, emit trait imports required to call methods on the client object.
// Traits like LlmClient, FileClient, etc. must be in scope for method dispatch to work.
if client_factory.is_some() && file_has_call_based {
let trait_imports: Vec<String> = e2e_config
.call
.overrides
.get("rust")
.map(|o| o.trait_imports.clone())
.unwrap_or_default();
for trait_name in trait_imports {
crate_imports.insert(trait_name);
}
}
// When the rust override specifies an `options_type`,
// type annotations are emitted on json_object bindings so that `Default::default()`
// and `serde_json::from_value(…)` can be resolved without a trailing positional arg.
// Import the named type so it is in scope in every test function in this file.
// Only consider json_object args without an element_type: those with an element_type
// are annotated as Vec<ElemType> and do not use options_type for their annotation.
if file_has_call_based {
for fixture in fixtures {
let call_config = e2e_config.resolve_call_for_fixture(
fixture.call.as_deref(),
&fixture.id,
&fixture.resolved_category(),
&fixture.tags,
&fixture.input,
);
let recipe = crate::e2e::codegen::recipe::E2eCallRecipe::resolve("rust", fixture, call_config, type_defs);
if recipe
.args
.iter()
.any(|a| a.arg_type == "json_object" && a.element_type.is_none())
{
if let Some(opts_type) = recipe.options_type {
if body_references_symbol(&body_buf, opts_type) {
crate_imports.insert(opts_type.to_string());
}
}
}
}
}
// Emit all collected crate-level imports in a single sorted pass (deduped by BTreeSet).
for symbol in &crate_imports {
let _ = writeln!(out, "use {module}::{symbol};");
}
// Import mock_server and common modules when any fixture in this file uses mock_response.
let file_needs_mock = needs_mock_server
&& fixtures
.iter()
.any(|f| f.mock_response.is_some() || f.needs_mock_server());
if file_needs_mock {
let _ = writeln!(out, "mod common;");
let _ = writeln!(out, "mod mock_server;");
let _ = writeln!(out, "#[allow(unused_imports)]");
let _ = writeln!(out, "use mock_server::{{MockRoute, MockServer}};");
}
// Import the visitor trait, result enum, and node context when any fixture
// in this file declares a `visitor` block. Without these, the inline
// `impl <visitor_trait> for _TestVisitor` block fails to resolve.
// Visitor types live in the `visitor` sub-module of the crate, not the crate root.
// The trait name is read from `[e2e.call.overrides.rust] visitor_trait`; omitting it
// while a fixture declares a visitor is a configuration error.
let file_needs_visitor = fixtures.iter().any(|f| f.visitor.is_some());
if file_needs_visitor {
let visitor_trait = resolve_visitor_trait(rust_call_override).unwrap_or_else(|| {
panic!(
"category '{}': fixture declares a visitor block but \
`[e2e.call.overrides.rust] visitor_trait` is not configured",
category
)
});
let _ = writeln!(
out,
"use {module}::visitor::{{{visitor_trait}, NodeContext, VisitResult}};"
);
}
// Collect and import element types from json_object args that have an element_type specified.
// These types are used in serde_json::from_value::<Vec<{elem}>>() for batch operations.
// Collect from all calls used in call-based fixtures (not just the default call).
if file_has_call_based {
let mut element_types: std::collections::BTreeSet<String> = std::collections::BTreeSet::new();
for fixture in fixtures.iter().filter(|f| {
if f.mock_response.is_some() {
return true;
}
if f.http.is_none() && f.mock_response.is_none() {
let call_config = e2e_config.resolve_call_for_fixture(
f.call.as_deref(),
&f.id,
&f.resolved_category(),
&f.tags,
&f.input,
);
let fn_name = resolve_function_name_for_call(call_config);
return !fn_name.is_empty();
}
false
}) {
let call_config = e2e_config.resolve_call_for_fixture(
fixture.call.as_deref(),
&fixture.id,
&fixture.resolved_category(),
&fixture.tags,
&fixture.input,
);
for arg in fixture.resolved_args(call_config) {
if arg.arg_type == "json_object" {
if let Some(ref elem_type) = arg.element_type {
element_types.insert(elem_type.clone());
}
}
}
}
for elem_type in &element_types {
// Skip primitive / std types — they're already in scope via the Rust prelude
// and emitting `use demo_crate::String;` (etc.) would fail with E0432.
if matches!(
elem_type.as_str(),
"String"
| "str"
| "bool"
| "i8"
| "i16"
| "i32"
| "i64"
| "i128"
| "isize"
| "u8"
| "u16"
| "u32"
| "u64"
| "u128"
| "usize"
| "f32"
| "f64"
| "char"
) {
continue;
}
if body_references_symbol(&body_buf, elem_type) {
let _ = writeln!(out, "use {module}::{elem_type};");
}
}
}
let _ = writeln!(out);
out.push_str(&body_buf);
if !out.ends_with('\n') {
out.push('\n');
}
out
}