adk-bench 1.0.0

Benchmarking framework for ADK-Rust agent performance measurement
Documentation
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
//! Workload schema, loading, and validation.
//!
//! Defines the JSON workload format for reproducible benchmarks
//! and provides built-in workload definitions.
//!
//! # Schema
//!
//! Workloads are JSON files conforming to the [`Workload`] schema. Each workload
//! describes an agent scenario with instructions, tools, expected turns, and
//! metadata annotations.
//!
//! # Built-in Workloads
//!
//! Three standard workloads are provided via [`builtin_workloads()`]:
//! - **simple_tool_call** — single tool invocation measuring basic dispatch overhead
//! - **multi_step_reasoning** — multi-turn reasoning chain with sequential tool use
//! - **parallel_tool_invocation** — concurrent tool calls measuring parallel dispatch
//!
//! A fourth workload, **multi_agent_delegation**, is available via
//! [`multi_agent_delegation_workload()`] and intended for use when the
//! `experimental` runtime flag is enabled.
//!
//! # Example
//!
//! ```rust,ignore
//! use adk_bench::workload::{load_workload, builtin_workloads};
//! use std::path::Path;
//!
//! // Load from file
//! let workload = load_workload(Path::new("my_workload.json"))?;
//!
//! // Use built-in workloads
//! let workloads = builtin_workloads();
//! ```

use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::Path;

use crate::error::{BenchError, Result};

/// A benchmark workload definition loaded from JSON.
///
/// Workloads define reproducible agent scenarios for benchmarking,
/// including agent configuration, expected behavior, and metadata.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct Workload {
    /// Unique workload name.
    pub name: String,
    /// Human-readable description.
    pub description: String,
    /// Agent configuration for this workload.
    pub agent: AgentConfig,
    /// LLM model identifier to use.
    pub model: String,
    /// Structured output schema (JSON Schema for response format).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub output_schema: Option<serde_json::Value>,
    /// Expected number of agent turns.
    pub expected_turns: usize,
    /// Optional metadata annotations (arbitrary key-value pairs).
    #[serde(default, skip_serializing_if = "HashMap::is_empty")]
    pub metadata: HashMap<String, serde_json::Value>,
    /// Schema version for forward compatibility.
    #[serde(default = "default_schema_version")]
    pub schema_version: u32,
}

fn default_schema_version() -> u32 {
    1
}

/// Agent configuration within a workload.
///
/// Specifies the agent's instructions, available tools, and the
/// initial user message to benchmark.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct AgentConfig {
    /// System instructions for the agent.
    pub instructions: String,
    /// Tool definitions available to the agent.
    #[serde(default, skip_serializing_if = "HashMap::is_empty")]
    pub tools: HashMap<String, ToolDefinition>,
    /// User message to send as the initial prompt.
    pub user_message: String,
}

/// Tool definition within a workload.
///
/// Describes a simulated tool for benchmarking purposes, including
/// its schema and optional fixed response for deterministic execution.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct ToolDefinition {
    /// Tool description for the LLM.
    pub description: String,
    /// JSON Schema for tool parameters.
    pub parameters: serde_json::Value,
    /// Simulated execution time in milliseconds (for benchmarking tool dispatch).
    #[serde(default)]
    pub simulated_latency_ms: u64,
    /// Fixed response value returned by the simulated tool.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub fixed_response: Option<serde_json::Value>,
}

/// Loads and validates a workload from a JSON file path.
///
/// Reads the file, parses JSON, and validates all required fields are present
/// and well-formed. Returns a descriptive [`BenchError::WorkloadValidation`]
/// error on schema violations.
///
/// # Errors
///
/// - [`BenchError::WorkloadNotFound`] if the file does not exist
/// - [`BenchError::WorkloadValidation`] if JSON parsing fails or required fields are invalid
pub fn load_workload(path: &Path) -> Result<Workload> {
    let path_str = path.display().to_string();

    if !path.exists() {
        return Err(BenchError::WorkloadNotFound { path: path_str });
    }

    let content = std::fs::read_to_string(path).map_err(|e| BenchError::WorkloadValidation {
        field: "file".to_string(),
        reason: format!("failed to read workload file '{path_str}': {e}"),
    })?;

    let workload: Workload =
        serde_json::from_str(&content).map_err(|e| BenchError::WorkloadValidation {
            field: parse_error_field(&e),
            reason: format!("invalid workload JSON: {e}"),
        })?;

    validate_workload(&workload)?;

    Ok(workload)
}

/// Returns built-in benchmark workloads.
///
/// Provides three standard workloads for common benchmarking scenarios:
/// - `simple_tool_call` — single tool invocation
/// - `multi_step_reasoning` — multi-turn reasoning chain
/// - `parallel_tool_invocation` — concurrent tool calls
///
/// The multi-agent delegation workload is intentionally excluded here;
/// use [`multi_agent_delegation_workload()`] when the `experimental`
/// runtime flag is enabled.
pub fn builtin_workloads() -> Vec<Workload> {
    vec![
        simple_tool_call_workload(),
        multi_step_reasoning_workload(),
        parallel_tool_invocation_workload(),
    ]
}

/// Returns the multi-agent delegation workload.
///
/// This workload exercises multi-agent orchestration where a coordinator
/// agent delegates subtasks to specialist agents. It is intended for use
/// only when the `experimental` runtime configuration flag is enabled,
/// as the multi-agent API may not be stable.
pub fn multi_agent_delegation_workload() -> Workload {
    let mut tools = HashMap::new();
    tools.insert(
        "delegate_to_researcher".to_string(),
        ToolDefinition {
            description: "Delegate a research subtask to the researcher agent".to_string(),
            parameters: serde_json::json!({
                "type": "object",
                "properties": {
                    "query": {
                        "type": "string",
                        "description": "The research query to investigate"
                    },
                    "depth": {
                        "type": "string",
                        "enum": ["shallow", "deep"],
                        "description": "How thorough the research should be"
                    }
                },
                "required": ["query"]
            }),
            simulated_latency_ms: 50,
            fixed_response: Some(serde_json::json!({
                "findings": "Research results on the topic",
                "confidence": 0.85,
                "sources": ["source_1", "source_2"]
            })),
        },
    );
    tools.insert(
        "delegate_to_writer".to_string(),
        ToolDefinition {
            description: "Delegate a writing subtask to the writer agent".to_string(),
            parameters: serde_json::json!({
                "type": "object",
                "properties": {
                    "topic": {
                        "type": "string",
                        "description": "The topic to write about"
                    },
                    "style": {
                        "type": "string",
                        "enum": ["formal", "casual", "technical"],
                        "description": "Writing style"
                    },
                    "max_words": {
                        "type": "integer",
                        "description": "Maximum word count"
                    }
                },
                "required": ["topic", "style"]
            }),
            simulated_latency_ms: 75,
            fixed_response: Some(serde_json::json!({
                "content": "Generated content based on research findings",
                "word_count": 250
            })),
        },
    );

    let mut metadata = HashMap::new();
    metadata.insert("category".to_string(), serde_json::Value::String("multi-agent".to_string()));
    metadata.insert("stability".to_string(), serde_json::Value::String("experimental".to_string()));

    Workload {
        name: "multi_agent_delegation".to_string(),
        description: "Coordinator agent delegates research and writing subtasks to specialist agents, measuring multi-agent orchestration overhead".to_string(),
        agent: AgentConfig {
            instructions: "You are a project coordinator. Break down the user's request into research and writing subtasks. First delegate research to gather information, then delegate writing to produce the final output.".to_string(),
            tools,
            user_message: "Write a technical summary about the performance benefits of async runtimes in systems programming.".to_string(),
        },
        model: "gemini-2.5-flash".to_string(),
        output_schema: Some(serde_json::json!({
            "type": "object",
            "properties": {
                "summary": { "type": "string" },
                "research_quality": { "type": "number" },
                "delegations_made": { "type": "integer" }
            },
            "required": ["summary", "delegations_made"]
        })),
        expected_turns: 5,
        metadata,
        schema_version: 1,
    }
}

fn simple_tool_call_workload() -> Workload {
    let mut tools = HashMap::new();
    tools.insert(
        "get_weather".to_string(),
        ToolDefinition {
            description: "Get the current weather for a given city".to_string(),
            parameters: serde_json::json!({
                "type": "object",
                "properties": {
                    "city": {
                        "type": "string",
                        "description": "The city name to get weather for"
                    },
                    "units": {
                        "type": "string",
                        "enum": ["celsius", "fahrenheit"],
                        "description": "Temperature units"
                    }
                },
                "required": ["city"]
            }),
            simulated_latency_ms: 10,
            fixed_response: Some(serde_json::json!({
                "temperature": 22.5,
                "condition": "sunny",
                "humidity": 45
            })),
        },
    );

    Workload {
        name: "simple_tool_call".to_string(),
        description: "Single tool invocation measuring basic dispatch overhead. The agent receives a weather query and must call one tool to respond."
            .to_string(),
        agent: AgentConfig {
            instructions: "You are a helpful weather assistant. When asked about weather, use the get_weather tool to retrieve current conditions.".to_string(),
            tools,
            user_message: "What is the weather in San Francisco?".to_string(),
        },
        model: "gemini-2.5-flash".to_string(),
        output_schema: Some(serde_json::json!({
            "type": "object",
            "properties": {
                "temperature": { "type": "number" },
                "condition": { "type": "string" },
                "city": { "type": "string" }
            },
            "required": ["temperature", "condition", "city"]
        })),
        expected_turns: 2,
        metadata: HashMap::new(),
        schema_version: 1,
    }
}

fn multi_step_reasoning_workload() -> Workload {
    let mut tools = HashMap::new();
    tools.insert(
        "search_database".to_string(),
        ToolDefinition {
            description: "Search a product database by query".to_string(),
            parameters: serde_json::json!({
                "type": "object",
                "properties": {
                    "query": {
                        "type": "string",
                        "description": "Search query"
                    },
                    "category": {
                        "type": "string",
                        "description": "Product category filter"
                    },
                    "max_results": {
                        "type": "integer",
                        "description": "Maximum number of results to return"
                    }
                },
                "required": ["query"]
            }),
            simulated_latency_ms: 15,
            fixed_response: Some(serde_json::json!({
                "results": [
                    {"id": "p1", "name": "Widget A", "price": 29.99, "rating": 4.5},
                    {"id": "p2", "name": "Widget B", "price": 19.99, "rating": 4.2},
                    {"id": "p3", "name": "Widget C", "price": 39.99, "rating": 4.8}
                ],
                "total_count": 3
            })),
        },
    );
    tools.insert(
        "get_product_details".to_string(),
        ToolDefinition {
            description: "Get detailed information about a specific product".to_string(),
            parameters: serde_json::json!({
                "type": "object",
                "properties": {
                    "product_id": {
                        "type": "string",
                        "description": "The product identifier"
                    }
                },
                "required": ["product_id"]
            }),
            simulated_latency_ms: 10,
            fixed_response: Some(serde_json::json!({
                "id": "p3",
                "name": "Widget C",
                "price": 39.99,
                "rating": 4.8,
                "reviews": 128,
                "in_stock": true,
                "description": "Premium widget with advanced features"
            })),
        },
    );
    tools.insert(
        "calculate_shipping".to_string(),
        ToolDefinition {
            description: "Calculate shipping cost for a product to a destination".to_string(),
            parameters: serde_json::json!({
                "type": "object",
                "properties": {
                    "product_id": {
                        "type": "string",
                        "description": "The product identifier"
                    },
                    "destination": {
                        "type": "string",
                        "description": "Shipping destination (zip code or city)"
                    }
                },
                "required": ["product_id", "destination"]
            }),
            simulated_latency_ms: 10,
            fixed_response: Some(serde_json::json!({
                "cost": 5.99,
                "estimated_days": 3,
                "carrier": "standard"
            })),
        },
    );

    Workload {
        name: "multi_step_reasoning".to_string(),
        description: "Multi-turn reasoning chain with sequential tool use. The agent must search products, get details on the best match, and calculate shipping — each step depends on previous results."
            .to_string(),
        agent: AgentConfig {
            instructions: "You are a shopping assistant. Help the user find the best product by searching the database, getting details on the top-rated result, and calculating shipping to their location.".to_string(),
            tools,
            user_message: "Find me the best-rated widget and tell me the total cost including shipping to 94105.".to_string(),
        },
        model: "gemini-2.5-flash".to_string(),
        output_schema: Some(serde_json::json!({
            "type": "object",
            "properties": {
                "product_name": { "type": "string" },
                "product_price": { "type": "number" },
                "shipping_cost": { "type": "number" },
                "total_cost": { "type": "number" },
                "estimated_delivery_days": { "type": "integer" }
            },
            "required": ["product_name", "total_cost"]
        })),
        expected_turns: 4,
        metadata: HashMap::new(),
        schema_version: 1,
    }
}

fn parallel_tool_invocation_workload() -> Workload {
    let mut tools = HashMap::new();
    tools.insert(
        "fetch_stock_price".to_string(),
        ToolDefinition {
            description: "Fetch the current stock price for a ticker symbol".to_string(),
            parameters: serde_json::json!({
                "type": "object",
                "properties": {
                    "ticker": {
                        "type": "string",
                        "description": "Stock ticker symbol (e.g., AAPL, GOOGL)"
                    }
                },
                "required": ["ticker"]
            }),
            simulated_latency_ms: 20,
            fixed_response: Some(serde_json::json!({
                "ticker": "AAPL",
                "price": 178.50,
                "change": 2.30,
                "change_percent": 1.31
            })),
        },
    );
    tools.insert(
        "fetch_company_news".to_string(),
        ToolDefinition {
            description: "Fetch recent news headlines for a company".to_string(),
            parameters: serde_json::json!({
                "type": "object",
                "properties": {
                    "ticker": {
                        "type": "string",
                        "description": "Stock ticker symbol"
                    },
                    "limit": {
                        "type": "integer",
                        "description": "Maximum number of headlines"
                    }
                },
                "required": ["ticker"]
            }),
            simulated_latency_ms: 25,
            fixed_response: Some(serde_json::json!({
                "headlines": [
                    "Company reports strong Q4 earnings",
                    "New product launch announced for next quarter"
                ]
            })),
        },
    );
    tools.insert(
        "fetch_analyst_rating".to_string(),
        ToolDefinition {
            description: "Fetch analyst consensus rating for a stock".to_string(),
            parameters: serde_json::json!({
                "type": "object",
                "properties": {
                    "ticker": {
                        "type": "string",
                        "description": "Stock ticker symbol"
                    }
                },
                "required": ["ticker"]
            }),
            simulated_latency_ms: 15,
            fixed_response: Some(serde_json::json!({
                "rating": "buy",
                "target_price": 195.00,
                "analyst_count": 32
            })),
        },
    );

    Workload {
        name: "parallel_tool_invocation".to_string(),
        description: "Concurrent tool calls measuring parallel dispatch efficiency. The agent must fetch stock price, news, and analyst rating simultaneously for a portfolio analysis."
            .to_string(),
        agent: AgentConfig {
            instructions: "You are a financial analyst assistant. When asked about a stock, fetch the current price, recent news, and analyst rating in parallel to provide a comprehensive summary.".to_string(),
            tools,
            user_message: "Give me a complete analysis of AAPL including current price, recent news, and analyst consensus.".to_string(),
        },
        model: "gemini-2.5-flash".to_string(),
        output_schema: Some(serde_json::json!({
            "type": "object",
            "properties": {
                "ticker": { "type": "string" },
                "current_price": { "type": "number" },
                "analyst_rating": { "type": "string" },
                "target_price": { "type": "number" },
                "summary": { "type": "string" }
            },
            "required": ["ticker", "current_price", "analyst_rating"]
        })),
        expected_turns: 2,
        metadata: HashMap::new(),
        schema_version: 1,
    }
}

/// Validates a workload's required fields and constraints.
fn validate_workload(workload: &Workload) -> Result<()> {
    if workload.name.is_empty() {
        return Err(BenchError::WorkloadValidation {
            field: "name".to_string(),
            reason: "workload name must not be empty".to_string(),
        });
    }

    if workload.description.is_empty() {
        return Err(BenchError::WorkloadValidation {
            field: "description".to_string(),
            reason: "workload description must not be empty".to_string(),
        });
    }

    if workload.model.is_empty() {
        return Err(BenchError::WorkloadValidation {
            field: "model".to_string(),
            reason: "model identifier must not be empty".to_string(),
        });
    }

    if workload.agent.instructions.is_empty() {
        return Err(BenchError::WorkloadValidation {
            field: "agent.instructions".to_string(),
            reason: "agent instructions must not be empty".to_string(),
        });
    }

    if workload.agent.user_message.is_empty() {
        return Err(BenchError::WorkloadValidation {
            field: "agent.userMessage".to_string(),
            reason: "agent user message must not be empty".to_string(),
        });
    }

    if workload.expected_turns == 0 {
        return Err(BenchError::WorkloadValidation {
            field: "expectedTurns".to_string(),
            reason: "expected turns must be at least 1".to_string(),
        });
    }

    if workload.schema_version == 0 {
        return Err(BenchError::WorkloadValidation {
            field: "schemaVersion".to_string(),
            reason: "schema version must be at least 1".to_string(),
        });
    }

    // Validate tool definitions
    for (tool_name, tool_def) in &workload.agent.tools {
        if tool_def.description.is_empty() {
            return Err(BenchError::WorkloadValidation {
                field: format!("agent.tools.{tool_name}.description"),
                reason: "tool description must not be empty".to_string(),
            });
        }
    }

    Ok(())
}

/// Extracts the field name from a serde_json parse error when possible.
fn parse_error_field(error: &serde_json::Error) -> String {
    // serde_json errors include line/column but not always the field name.
    // We provide the best context available.
    let msg = error.to_string();
    if msg.contains("missing field") {
        // Extract field name from "missing field `fieldName`"
        if let Some(start) = msg.find('`')
            && let Some(end) = msg[start + 1..].find('`')
        {
            return msg[start + 1..start + 1 + end].to_string();
        }
    }
    "root".to_string()
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Write;
    use tempfile::NamedTempFile;

    #[test]
    fn test_builtin_workloads_count() {
        let workloads = builtin_workloads();
        assert_eq!(workloads.len(), 3);
    }

    #[test]
    fn test_builtin_workload_names() {
        let workloads = builtin_workloads();
        let names: Vec<&str> = workloads.iter().map(|w| w.name.as_str()).collect();
        assert!(names.contains(&"simple_tool_call"));
        assert!(names.contains(&"multi_step_reasoning"));
        assert!(names.contains(&"parallel_tool_invocation"));
    }

    #[test]
    fn test_multi_agent_delegation_not_in_builtin() {
        let workloads = builtin_workloads();
        let names: Vec<&str> = workloads.iter().map(|w| w.name.as_str()).collect();
        assert!(!names.contains(&"multi_agent_delegation"));
    }

    #[test]
    fn test_multi_agent_delegation_workload() {
        let workload = multi_agent_delegation_workload();
        assert_eq!(workload.name, "multi_agent_delegation");
        assert_eq!(workload.expected_turns, 5);
        assert!(workload.agent.tools.contains_key("delegate_to_researcher"));
        assert!(workload.agent.tools.contains_key("delegate_to_writer"));
        assert!(workload.metadata.contains_key("stability"));
    }

    #[test]
    fn test_workload_serialization_round_trip() {
        let workloads = builtin_workloads();
        for workload in &workloads {
            let json = serde_json::to_string(workload).unwrap();
            let deserialized: Workload = serde_json::from_str(&json).unwrap();
            assert_eq!(workload, &deserialized);
        }
    }

    #[test]
    fn test_load_workload_not_found() {
        let result = load_workload(Path::new("/nonexistent/path.json"));
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(matches!(err, BenchError::WorkloadNotFound { .. }));
    }

    #[test]
    fn test_load_workload_invalid_json() {
        let mut file = NamedTempFile::new().unwrap();
        writeln!(file, "not valid json").unwrap();
        let result = load_workload(file.path());
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(matches!(err, BenchError::WorkloadValidation { .. }));
    }

    #[test]
    fn test_load_workload_missing_field() {
        let mut file = NamedTempFile::new().unwrap();
        writeln!(file, r#"{{"name": "test"}}"#).unwrap();
        let result = load_workload(file.path());
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(matches!(err, BenchError::WorkloadValidation { .. }));
    }

    #[test]
    fn test_load_workload_valid() {
        let workload = simple_tool_call_workload();
        let json = serde_json::to_string_pretty(&workload).unwrap();

        let mut file = NamedTempFile::new().unwrap();
        write!(file, "{json}").unwrap();

        let loaded = load_workload(file.path()).unwrap();
        assert_eq!(workload, loaded);
    }

    #[test]
    fn test_validate_empty_name() {
        let mut workload = simple_tool_call_workload();
        workload.name = String::new();
        let result = validate_workload(&workload);
        assert!(result.is_err());
    }

    #[test]
    fn test_validate_zero_expected_turns() {
        let mut workload = simple_tool_call_workload();
        workload.expected_turns = 0;
        let result = validate_workload(&workload);
        assert!(result.is_err());
    }

    #[test]
    fn test_schema_version_defaults_to_1() {
        let json = r#"{
            "name": "test",
            "description": "test workload",
            "agent": {
                "instructions": "do something",
                "userMessage": "hello"
            },
            "model": "gemini-2.5-flash",
            "expectedTurns": 2
        }"#;
        let workload: Workload = serde_json::from_str(json).unwrap();
        assert_eq!(workload.schema_version, 1);
    }

    #[test]
    fn test_metadata_preserved_in_round_trip() {
        let mut workload = simple_tool_call_workload();
        workload
            .metadata
            .insert("author".to_string(), serde_json::Value::String("test-user".to_string()));
        workload.metadata.insert("version".to_string(), serde_json::json!(2));

        let json = serde_json::to_string(&workload).unwrap();
        let deserialized: Workload = serde_json::from_str(&json).unwrap();
        assert_eq!(workload.metadata, deserialized.metadata);
    }
}