nika-engine 0.38.0

Nika workflow engine — embeddable runtime, provider, DAG, and binding logic
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
//! End-to-end vision workflow tests
//!
//! Tests vision (multimodal) workflow parsing, validation, mock provider execution,
//! InferParams edge cases, and event emission for the vision pipeline.

use super::*;
use crate::ast::content::{ContentPart, ImageDetail};
use crate::ast::{InferParams, TaskAction};
use crate::event::EventKind;
use crate::store::RunContext;

// ═══════════════════════════════════════════════════════════════
// 1. FULL WORKFLOW PARSE + VALIDATE (YAML → Workflow struct)
// ═══════════════════════════════════════════════════════════════

fn parse_ok(yaml: &str) -> crate::ast::Workflow {
    crate::ast::parse_workflow(yaml).expect("Workflow YAML should parse without errors")
}

fn parse_err(yaml: &str) -> crate::error::NikaError {
    crate::ast::parse_workflow(yaml).expect_err("Workflow YAML should fail to parse")
}

#[test]
fn parse_vision_workflow_with_cas_image() {
    let yaml = r#"
schema: "nika/workflow@0.12"
provider: mock
model: test
tasks:
  - id: describe
    infer:
      content:
        - type: image
          source: "blake3:deadbeef1234567890"
          detail: high
        - type: text
          text: "Describe this image"
"#;
    let wf = parse_ok(yaml);
    assert_eq!(wf.provider, "mock");
    assert_eq!(wf.tasks.len(), 1);
    assert_eq!(wf.tasks[0].id, "describe");
    match &wf.tasks[0].action {
        TaskAction::Infer { infer } => {
            let content = infer.content.as_ref().expect("content should be present");
            assert_eq!(content.len(), 2);
            match &content[0] {
                ContentPart::Image { source, detail } => {
                    assert_eq!(source, "blake3:deadbeef1234567890");
                    assert_eq!(*detail, ImageDetail::High);
                }
                other => panic!("Expected Image, got: {:?}", other),
            }
            match &content[1] {
                ContentPart::Text { text } => assert_eq!(text, "Describe this image"),
                other => panic!("Expected Text, got: {:?}", other),
            }
        }
        other => panic!("Expected Infer action, got: {:?}", other),
    }
}

#[test]
fn parse_mixed_vision_and_text_workflow() {
    let yaml = r#"
schema: "nika/workflow@0.12"
provider: mock
tasks:
  - id: step1
    infer: "Plain text prompt"
  - id: step2
    infer:
      content:
        - type: text
          text: "Vision task"
    depends_on: [step1]
"#;
    let wf = parse_ok(yaml);
    assert_eq!(wf.tasks.len(), 2);
    match &wf.tasks[0].action {
        TaskAction::Infer { infer } => {
            assert_eq!(infer.prompt, "Plain text prompt");
            assert!(infer.content.is_none());
        }
        other => panic!("Expected Infer, got: {:?}", other),
    }
    match &wf.tasks[1].action {
        TaskAction::Infer { infer } => {
            let content = infer.content.as_ref().unwrap();
            assert_eq!(content.len(), 1);
            assert!(matches!(&content[0], ContentPart::Text { text } if text == "Vision task"));
        }
        other => panic!("Expected Infer, got: {:?}", other),
    }
}

#[test]
fn parse_vision_with_image_url() {
    let yaml = r#"
schema: "nika/workflow@0.12"
provider: mock
tasks:
  - id: analyze
    infer:
      content:
        - type: image_url
          url: "https://example.com/photo.jpg"
          detail: low
        - type: text
          text: "What is in this image?"
"#;
    let wf = parse_ok(yaml);
    match &wf.tasks[0].action {
        TaskAction::Infer { infer } => {
            let content = infer.content.as_ref().unwrap();
            match &content[0] {
                ContentPart::ImageUrl { url, detail } => {
                    assert_eq!(url, "https://example.com/photo.jpg");
                    assert_eq!(*detail, ImageDetail::Low);
                }
                other => panic!("Expected ImageUrl, got: {:?}", other),
            }
        }
        other => panic!("Expected Infer, got: {:?}", other),
    }
}

#[test]
fn parse_vision_with_template_source() {
    let yaml = r#"
schema: "nika/workflow@0.12"
provider: mock
tasks:
  - id: import_photo
    exec: "echo done"
  - id: describe
    infer:
      content:
        - type: image
          source: "{{with.photo.media[0].hash}}"
          detail: high
        - type: text
          text: "Describe this image"
    with:
      photo: $import_photo
    depends_on: [import_photo]
"#;
    let wf = parse_ok(yaml);
    assert_eq!(wf.tasks[1].id, "describe");
    match &wf.tasks[1].action {
        TaskAction::Infer { infer } => {
            let content = infer.content.as_ref().unwrap();
            match &content[0] {
                ContentPart::Image { source, .. } => {
                    assert_eq!(source, "{{with.photo.media[0].hash}}");
                }
                other => panic!("Expected Image, got: {:?}", other),
            }
        }
        other => panic!("Expected Infer, got: {:?}", other),
    }
}

#[test]
fn parse_vision_multiple_images() {
    let yaml = r#"
schema: "nika/workflow@0.12"
provider: mock
tasks:
  - id: compare
    infer:
      content:
        - type: image
          source: "blake3:aaa111"
        - type: image
          source: "blake3:bbb222"
        - type: text
          text: "Compare these two images"
"#;
    let wf = parse_ok(yaml);
    let content = match &wf.tasks[0].action {
        TaskAction::Infer { infer } => infer.content.as_ref().unwrap(),
        other => panic!("Expected Infer, got: {:?}", other),
    };
    assert_eq!(content.len(), 3);
    assert_eq!(content.iter().filter(|p| matches!(p, ContentPart::Image { .. })).count(), 2);
}

#[test]
fn parse_vision_default_detail_is_auto() {
    let yaml = r#"
schema: "nika/workflow@0.12"
provider: mock
tasks:
  - id: auto_detail
    infer:
      content:
        - type: image
          source: "blake3:abc123"
        - type: text
          text: "Describe"
"#;
    let wf = parse_ok(yaml);
    match &wf.tasks[0].action {
        TaskAction::Infer { infer } => match &infer.content.as_ref().unwrap()[0] {
            ContentPart::Image { detail, .. } => assert_eq!(*detail, ImageDetail::Auto),
            other => panic!("Expected Image, got: {:?}", other),
        },
        other => panic!("Expected Infer, got: {:?}", other),
    }
}

#[test]
fn parse_vision_with_prompt_and_content() {
    let yaml = r#"
schema: "nika/workflow@0.12"
provider: mock
tasks:
  - id: hybrid
    infer:
      prompt: "Additional context for the model"
      content:
        - type: image
          source: "blake3:abc"
          detail: high
        - type: text
          text: "What is this?"
"#;
    let wf = parse_ok(yaml);
    match &wf.tasks[0].action {
        TaskAction::Infer { infer } => {
            assert_eq!(infer.prompt, "Additional context for the model");
            assert_eq!(infer.content.as_ref().unwrap().len(), 2);
        }
        other => panic!("Expected Infer, got: {:?}", other),
    }
}

#[test]
fn parse_vision_with_provider_and_model_overrides() {
    let yaml = r#"
schema: "nika/workflow@0.12"
provider: mock
model: default-model
tasks:
  - id: vision_with_override
    provider: openai
    model: gpt-4o
    infer:
      prompt: "describe"
      content:
        - type: image_url
          url: "https://example.com/photo.png"
        - type: text
          text: "What is this?"
"#;
    let wf = parse_ok(yaml);
    match &wf.tasks[0].action {
        TaskAction::Infer { infer } => {
            assert_eq!(infer.provider.as_deref(), Some("openai"));
            assert_eq!(infer.model.as_deref(), Some("gpt-4o"));
            assert!(infer.content.is_some());
        }
        other => panic!("Expected Infer, got: {:?}", other),
    }
}

#[test]
fn parse_vision_image_url_with_auto_detail() {
    let yaml = r#"
schema: "nika/workflow@0.12"
provider: mock
tasks:
  - id: url_auto
    infer:
      content:
        - type: image_url
          url: "https://example.com/img.webp"
          detail: auto
        - type: text
          text: "Describe"
"#;
    let wf = parse_ok(yaml);
    match &wf.tasks[0].action {
        TaskAction::Infer { infer } => match &infer.content.as_ref().unwrap()[0] {
            ContentPart::ImageUrl { url, detail } => {
                assert_eq!(url, "https://example.com/img.webp");
                assert_eq!(*detail, ImageDetail::Auto);
            }
            other => panic!("Expected ImageUrl, got: {:?}", other),
        },
        other => panic!("Expected Infer, got: {:?}", other),
    }
}

#[test]
fn parse_vision_with_system_prompt() {
    let yaml = r#"
schema: "nika/workflow@0.12"
provider: mock
tasks:
  - id: with_system
    infer:
      system: "You are a professional art critic."
      content:
        - type: image
          source: "blake3:deadbeef"
        - type: text
          text: "Critique this artwork"
"#;
    let wf = parse_ok(yaml);
    match &wf.tasks[0].action {
        TaskAction::Infer { infer } => {
            assert_eq!(infer.system.as_deref(), Some("You are a professional art critic."));
            assert!(infer.content.is_some());
        }
        other => panic!("Expected Infer, got: {:?}", other),
    }
}

#[test]
fn parse_vision_content_only_no_prompt() {
    let yaml = r#"
schema: "nika/workflow@0.12"
provider: mock
tasks:
  - id: content_only
    infer:
      content:
        - type: text
          text: "This is the only text"
"#;
    let wf = parse_ok(yaml);
    match &wf.tasks[0].action {
        TaskAction::Infer { infer } => {
            assert!(infer.prompt.is_empty());
            assert!(infer.content.is_some());
        }
        other => panic!("Expected Infer, got: {:?}", other),
    }
}

#[test]
fn parse_vision_empty_content_fails() {
    let yaml = r#"
schema: "nika/workflow@0.12"
provider: mock
tasks:
  - id: empty_content
    infer:
      content: []
"#;
    parse_err(yaml);
}

#[test]
fn parse_vision_mixed_image_types_in_one_task() {
    let yaml = r#"
schema: "nika/workflow@0.12"
provider: mock
tasks:
  - id: mixed_images
    infer:
      content:
        - type: image
          source: "blake3:cas_image"
          detail: high
        - type: image_url
          url: "https://example.com/remote.jpg"
          detail: low
        - type: text
          text: "Compare CAS image with remote URL image"
"#;
    let wf = parse_ok(yaml);
    let content = match &wf.tasks[0].action {
        TaskAction::Infer { infer } => infer.content.as_ref().unwrap(),
        other => panic!("Expected Infer, got: {:?}", other),
    };
    assert_eq!(content.len(), 3);
    assert!(matches!(&content[0], ContentPart::Image { .. }));
    assert!(matches!(&content[1], ContentPart::ImageUrl { .. }));
    assert!(matches!(&content[2], ContentPart::Text { .. }));
}

// ═══════════════════════════════════════════════════════════════
// 2. MOCK PROVIDER VISION EXECUTION
// ═══════════════════════════════════════════════════════════════

#[tokio::test]
async fn mock_infer_with_content_text_returns_response() {
    let executor = TaskExecutor::new("mock", None, None, EventLog::new());
    let task_id: Arc<str> = Arc::from("vision-mock-text");
    let bindings = ResolvedBindings::default();
    let datastore = RunContext::default();
    let infer = InferParams {
        prompt: String::new(),
        content: Some(vec![ContentPart::Text { text: "Describe this scene".to_string() }]),
        ..Default::default()
    };
    let result = executor.run_infer(&task_id, &infer, &bindings, &datastore, None).await;
    assert!(result.is_ok(), "Mock infer with content should succeed: {:?}", result.err());
    let parsed: serde_json::Value = serde_json::from_str(&result.unwrap()).unwrap();
    assert_eq!(parsed["mock"], true);
    assert_eq!(parsed["task_id"], "vision-mock-text");
}

#[tokio::test]
async fn mock_infer_with_content_image_returns_response() {
    let executor = TaskExecutor::new("mock", None, None, EventLog::new());
    let task_id: Arc<str> = Arc::from("vision-mock-image");
    let bindings = ResolvedBindings::default();
    let datastore = RunContext::default();
    let infer = InferParams {
        prompt: "Describe this".to_string(),
        content: Some(vec![
            ContentPart::Image { source: "blake3:deadbeef".to_string(), detail: ImageDetail::High },
            ContentPart::Text { text: "What is in this image?".to_string() },
        ]),
        ..Default::default()
    };
    let result = executor.run_infer(&task_id, &infer, &bindings, &datastore, None).await;
    assert!(result.is_ok(), "Mock infer with image content should succeed: {:?}", result.err());
}

#[tokio::test]
async fn mock_infer_vision_emits_template_resolved() {
    let event_log = EventLog::new();
    let executor = TaskExecutor::new("mock", None, None, event_log.clone());
    let task_id: Arc<str> = Arc::from("vision-tpl-event");
    let bindings = ResolvedBindings::default();
    let datastore = RunContext::default();
    let infer = InferParams {
        prompt: "Describe the image".to_string(),
        content: Some(vec![ContentPart::Text { text: "Image description task".to_string() }]),
        ..Default::default()
    };
    executor.run_infer(&task_id, &infer, &bindings, &datastore, None).await.unwrap();
    let events = event_log.filter_task("vision-tpl-event");
    let tpl: Vec<_> = events.iter().filter(|e| matches!(e.kind, EventKind::TemplateResolved { .. })).collect();
    assert_eq!(tpl.len(), 1, "Should emit exactly one TemplateResolved event");
}

#[tokio::test]
async fn mock_infer_vision_emits_provider_responded() {
    let event_log = EventLog::new();
    let executor = TaskExecutor::new("mock", None, None, event_log.clone());
    let task_id: Arc<str> = Arc::from("vision-pr-event");
    let bindings = ResolvedBindings::default();
    let datastore = RunContext::default();
    let infer = InferParams {
        prompt: "Analyze".to_string(),
        content: Some(vec![ContentPart::Image { source: "blake3:abc123".to_string(), detail: ImageDetail::Auto }]),
        ..Default::default()
    };
    executor.run_infer(&task_id, &infer, &bindings, &datastore, None).await.unwrap();
    let events = event_log.filter_task("vision-pr-event");
    let pr: Vec<_> = events.iter().filter(|e| matches!(e.kind, EventKind::ProviderResponded { .. })).collect();
    assert_eq!(pr.len(), 1, "Should emit exactly one ProviderResponded event");
    if let EventKind::ProviderResponded { task_id: ref tid, .. } = &pr[0].kind {
        assert_eq!(tid.as_ref(), "vision-pr-event");
    }
}

#[tokio::test]
async fn mock_infer_vision_with_task_level_provider_override() {
    let executor = TaskExecutor::new("claude", None, None, EventLog::new());
    let task_id: Arc<str> = Arc::from("vision-override");
    let bindings = ResolvedBindings::default();
    let datastore = RunContext::default();
    let infer = InferParams {
        prompt: "Vision with override".to_string(),
        provider: Some("mock".to_string()),
        content: Some(vec![ContentPart::Text { text: "Override test".to_string() }]),
        ..Default::default()
    };
    let result = executor.run_infer(&task_id, &infer, &bindings, &datastore, None).await;
    assert!(result.is_ok(), "Task-level mock override should work: {:?}", result.err());
}

// ═══════════════════════════════════════════════════════════════
// 3. INFER PARAMS EDGE CASES
// ═══════════════════════════════════════════════════════════════

#[tokio::test]
async fn infer_no_content_text_prompt_normal_path() {
    let executor = TaskExecutor::new("mock", None, None, EventLog::new());
    let task_id: Arc<str> = Arc::from("text-only");
    let bindings = ResolvedBindings::default();
    let datastore = RunContext::default();
    let infer = InferParams { prompt: "Generate something".to_string(), content: None, ..Default::default() };
    let result = executor.run_infer(&task_id, &infer, &bindings, &datastore, None).await;
    assert!(result.is_ok(), "Normal text-only path should work");
}

#[tokio::test]
async fn infer_content_text_empty_prompt_vision_path() {
    let executor = TaskExecutor::new("mock", None, None, EventLog::new());
    let task_id: Arc<str> = Arc::from("vision-empty-prompt");
    let bindings = ResolvedBindings::default();
    let datastore = RunContext::default();
    let infer = InferParams {
        prompt: String::new(),
        content: Some(vec![ContentPart::Text { text: "Content-only vision task".to_string() }]),
        ..Default::default()
    };
    let result = executor.run_infer(&task_id, &infer, &bindings, &datastore, None).await;
    assert!(result.is_ok(), "Vision path with empty prompt should succeed: {:?}", result.err());
}

#[tokio::test]
async fn infer_empty_content_empty_prompt_fails() {
    let executor = TaskExecutor::new("mock", None, None, EventLog::new());
    let task_id: Arc<str> = Arc::from("both-empty");
    let bindings = ResolvedBindings::default();
    let datastore = RunContext::default();
    let infer = InferParams { prompt: "   ".to_string(), content: Some(Vec::new()), ..Default::default() };
    let result = executor.run_infer(&task_id, &infer, &bindings, &datastore, None).await;
    assert!(result.is_err(), "Empty prompt + empty content should fail validation");
}

#[tokio::test]
async fn infer_content_image_with_nonempty_prompt() {
    let executor = TaskExecutor::new("mock", None, None, EventLog::new());
    let task_id: Arc<str> = Arc::from("prompt-plus-content");
    let bindings = ResolvedBindings::default();
    let datastore = RunContext::default();
    let infer = InferParams {
        prompt: "Additional context".to_string(),
        content: Some(vec![ContentPart::Image { source: "blake3:abc".to_string(), detail: ImageDetail::High }]),
        ..Default::default()
    };
    let result = executor.run_infer(&task_id, &infer, &bindings, &datastore, None).await;
    assert!(result.is_ok(), "Prompt + content should work: {:?}", result.err());
    let response: serde_json::Value = serde_json::from_str(&result.unwrap()).unwrap();
    assert!(response["prompt_len"].as_u64().unwrap() > 0);
}

#[tokio::test]
async fn infer_content_with_extended_thinking_non_claude_fails() {
    let executor = TaskExecutor::new("mock", None, None, EventLog::new());
    let task_id: Arc<str> = Arc::from("vision-thinking");
    let bindings = ResolvedBindings::default();
    let datastore = RunContext::default();
    let infer = InferParams {
        prompt: "Analyze carefully".to_string(),
        content: Some(vec![ContentPart::Text { text: "test".to_string() }]),
        extended_thinking: Some(true),
        provider: Some("mock".to_string()),
        ..Default::default()
    };
    let result = executor.run_infer(&task_id, &infer, &bindings, &datastore, None).await;
    assert!(result.is_err(), "extended_thinking with mock provider should fail");
    assert!(result.unwrap_err().to_string().contains("claude"));
}

#[tokio::test]
async fn infer_content_with_temperature_and_max_tokens() {
    let executor = TaskExecutor::new("mock", None, None, EventLog::new());
    let task_id: Arc<str> = Arc::from("vision-llm-opts");
    let bindings = ResolvedBindings::default();
    let datastore = RunContext::default();
    let infer = InferParams {
        prompt: "Describe briefly".to_string(),
        content: Some(vec![ContentPart::Image { source: "blake3:img_hash".to_string(), detail: ImageDetail::Low }]),
        temperature: Some(0.7), max_tokens: Some(256),
        ..Default::default()
    };
    let result = executor.run_infer(&task_id, &infer, &bindings, &datastore, None).await;
    assert!(result.is_ok(), "Vision with temperature + max_tokens should work: {:?}", result.err());
}

#[tokio::test]
async fn infer_content_invalid_temperature_fails() {
    let executor = TaskExecutor::new("mock", None, None, EventLog::new());
    let task_id: Arc<str> = Arc::from("bad-temp");
    let bindings = ResolvedBindings::default();
    let datastore = RunContext::default();
    let infer = InferParams {
        prompt: "test".to_string(),
        content: Some(vec![ContentPart::Text { text: "test".to_string() }]),
        temperature: Some(5.0),
        ..Default::default()
    };
    let result = executor.run_infer(&task_id, &infer, &bindings, &datastore, None).await;
    assert!(result.is_err(), "Invalid temperature should fail validation");
}

#[tokio::test]
async fn infer_content_none_prompt_whitespace_fails() {
    let executor = TaskExecutor::new("mock", None, None, EventLog::new());
    let task_id: Arc<str> = Arc::from("whitespace-only");
    let bindings = ResolvedBindings::default();
    let datastore = RunContext::default();
    let infer = InferParams { prompt: "   \t\n  ".to_string(), content: None, ..Default::default() };
    let result = executor.run_infer(&task_id, &infer, &bindings, &datastore, None).await;
    assert!(result.is_err(), "Whitespace-only prompt with no content should fail");
}

// ═══════════════════════════════════════════════════════════════
// 4. EVENT EMISSION VERIFICATION
// ═══════════════════════════════════════════════════════════════

#[tokio::test]
async fn vision_mock_emits_context_assembled_event() {
    let event_log = EventLog::new();
    let executor = TaskExecutor::new("mock", None, None, event_log.clone());
    let task_id: Arc<str> = Arc::from("vision-ctx-event");
    let bindings = ResolvedBindings::default();
    let datastore = RunContext::default();
    let infer = InferParams {
        prompt: "Analyze image".to_string(),
        content: Some(vec![ContentPart::Text { text: "Vision content".to_string() }]),
        ..Default::default()
    };
    executor.run_infer(&task_id, &infer, &bindings, &datastore, None).await.unwrap();
    let events = event_log.filter_task("vision-ctx-event");
    let ctx: Vec<_> = events.iter().filter(|e| matches!(e.kind, EventKind::ContextAssembled { .. })).collect();
    assert_eq!(ctx.len(), 1, "Should emit ContextAssembled event");
}

#[tokio::test]
async fn vision_mock_full_event_sequence() {
    let event_log = EventLog::new();
    let executor = TaskExecutor::new("mock", None, None, event_log.clone());
    let task_id: Arc<str> = Arc::from("vision-full-seq");
    let bindings = ResolvedBindings::default();
    let datastore = RunContext::default();
    let infer = InferParams {
        prompt: "Describe this".to_string(),
        content: Some(vec![
            ContentPart::Image { source: "blake3:abc".to_string(), detail: ImageDetail::High },
            ContentPart::Text { text: "What is it?".to_string() },
        ]),
        ..Default::default()
    };
    executor.run_infer(&task_id, &infer, &bindings, &datastore, None).await.unwrap();
    let events = event_log.filter_task("vision-full-seq");
    assert!(events.iter().any(|e| matches!(e.kind, EventKind::TemplateResolved { .. })), "Should emit TemplateResolved");
    assert!(events.iter().any(|e| matches!(e.kind, EventKind::ContextAssembled { .. })), "Should emit ContextAssembled");
    assert!(events.iter().any(|e| matches!(e.kind, EventKind::ProviderResponded { .. })), "Should emit ProviderResponded");
    assert!(!events.iter().any(|e| matches!(e.kind, EventKind::VisionContentResolved { .. })), "Mock should NOT emit VisionContentResolved");
}

#[tokio::test]
async fn vision_mock_provider_responded_has_tokens() {
    let event_log = EventLog::new();
    let executor = TaskExecutor::new("mock", None, None, event_log.clone());
    let task_id: Arc<str> = Arc::from("vision-tokens");
    let bindings = ResolvedBindings::default();
    let datastore = RunContext::default();
    let infer = InferParams {
        prompt: "Count the objects in this image".to_string(),
        content: Some(vec![ContentPart::Image { source: "blake3:some_hash".to_string(), detail: ImageDetail::Auto }]),
        ..Default::default()
    };
    executor.run_infer(&task_id, &infer, &bindings, &datastore, None).await.unwrap();
    let events = event_log.filter_task("vision-tokens");
    let pr: Vec<_> = events.iter().filter(|e| matches!(e.kind, EventKind::ProviderResponded { .. })).collect();
    assert_eq!(pr.len(), 1);
    if let EventKind::ProviderResponded { input_tokens, output_tokens, finish_reason, .. } = &pr[0].kind {
        assert!(*input_tokens > 0, "Input tokens should be non-zero");
        assert!(*output_tokens > 0, "Output tokens should be non-zero");
        assert_eq!(finish_reason, "mock");
    }
}

// ═══════════════════════════════════════════════════════════════
// 5. EXECUTE DISPATCH WITH VISION CONTENT
// ═══════════════════════════════════════════════════════════════

#[tokio::test]
async fn execute_dispatch_infer_with_content() {
    let executor = TaskExecutor::new("mock", None, None, EventLog::new());
    let task_id: Arc<str> = Arc::from("dispatch-vision");
    let bindings = ResolvedBindings::default();
    let datastore = RunContext::default();
    let action = TaskAction::Infer {
        infer: InferParams {
            prompt: "Describe".to_string(),
            content: Some(vec![
                ContentPart::Image { source: "blake3:dispatch_test".to_string(), detail: ImageDetail::High },
                ContentPart::Text { text: "What is this?".to_string() },
            ]),
            ..Default::default()
        },
    };
    let result = executor.execute(&task_id, &action, &bindings, &datastore, None).await;
    assert!(result.is_ok(), "Execute dispatch should work for vision infer: {:?}", result.err());
    let response: serde_json::Value = serde_json::from_str(&result.unwrap()).unwrap();
    assert_eq!(response["mock"], true);
}

#[tokio::test]
async fn execute_dispatch_vision_with_template_bindings() {
    let executor = TaskExecutor::new("mock", None, None, EventLog::new());
    let task_id: Arc<str> = Arc::from("dispatch-tpl");
    let mut bindings = ResolvedBindings::new();
    bindings.set("question", serde_json::json!("What colors do you see?"));
    let datastore = RunContext::default();
    let action = TaskAction::Infer {
        infer: InferParams {
            prompt: "{{with.question}}".to_string(),
            content: Some(vec![ContentPart::Image { source: "blake3:tpl_test".to_string(), detail: ImageDetail::Auto }]),
            ..Default::default()
        },
    };
    let result = executor.execute(&task_id, &action, &bindings, &datastore, None).await;
    assert!(result.is_ok(), "Vision with template bindings should succeed: {:?}", result.err());
}

// ═══════════════════════════════════════════════════════════════
// 6. VALIDATE() METHOD EDGE CASES FOR CONTENT
// ═══════════════════════════════════════════════════════════════

#[test]
fn validate_infer_prompt_only() {
    let infer = InferParams { prompt: "Hello".to_string(), ..Default::default() };
    assert!(infer.validate().is_ok());
}

#[test]
fn validate_infer_content_only() {
    let infer = InferParams {
        prompt: String::new(),
        content: Some(vec![ContentPart::Text { text: "Content only".to_string() }]),
        ..Default::default()
    };
    assert!(infer.validate().is_ok());
}

#[test]
fn validate_infer_both_prompt_and_content() {
    let infer = InferParams {
        prompt: "Also a prompt".to_string(),
        content: Some(vec![ContentPart::Image { source: "blake3:abc".to_string(), detail: ImageDetail::High }]),
        ..Default::default()
    };
    assert!(infer.validate().is_ok());
}

#[test]
fn validate_infer_neither_prompt_nor_content() {
    let infer = InferParams { prompt: "  ".to_string(), content: None, ..Default::default() };
    assert!(infer.validate().is_err());
}

#[test]
fn validate_infer_empty_content_empty_prompt() {
    let infer = InferParams { prompt: String::new(), content: Some(Vec::new()), ..Default::default() };
    assert!(infer.validate().is_err(), "Empty content vec with empty prompt should fail");
}

#[test]
fn validate_infer_content_with_image_empty_prompt() {
    let infer = InferParams {
        prompt: String::new(),
        content: Some(vec![ContentPart::Image { source: "blake3:test".to_string(), detail: ImageDetail::Auto }]),
        ..Default::default()
    };
    assert!(infer.validate().is_ok(), "Image content with empty prompt should pass");
}