nika 0.35.4

Semantic YAML workflow engine for AI tasks - DAG execution, MCP integration, multi-provider LLM support
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
//! Tests for the DAG Runner module
//!
//! Coverage targets:
//! - Runner initialization
//! - get_ready_tasks logic
//! - for_each parallelism and concurrency
//! - Event emission

use nika::ast::analyzed::{AnalyzedTaskAction, AnalyzedWorkflow};
use nika::ast::parse_analyzed;
use nika::event::EventLog;
use nika::runtime::Runner;

/// Helper to create a minimal workflow YAML and parse it
fn parse_workflow(yaml: &str) -> AnalyzedWorkflow {
    parse_analyzed(yaml).expect("Failed to parse workflow YAML")
}

// =============================================================================
// TEST 1: Runner Initialization
// =============================================================================

mod runner_initialization {
    use super::*;

    #[test]
    fn test_runner_new_creates_valid_runner() {
        let yaml = r#"
schema: nika/workflow@0.12
workflow: test-init
tasks:
  - id: step1
    infer:
      prompt: "Test prompt"
"#;
        let workflow = parse_workflow(yaml);
        let runner = Runner::new(workflow).unwrap();

        // Runner should be created without panic
        let event_log = runner.event_log();
        assert!(event_log.is_empty(), "Event log should start empty");
    }

    #[test]
    fn test_runner_with_event_log() {
        let yaml = r#"
schema: nika/workflow@0.12
workflow: test-event-log
tasks:
  - id: step1
    exec:
      command: "echo hello"
"#;
        let workflow = parse_workflow(yaml);
        let event_log = EventLog::new();
        let runner = Runner::with_event_log(workflow, event_log).unwrap();

        assert!(runner.event_log().is_empty());
    }

    #[test]
    fn test_runner_with_multiple_tasks() {
        let yaml = r#"
schema: nika/workflow@0.12
workflow: multi-task
tasks:
  - id: step1
    exec:
      command: "echo step1"
  - id: step2
    exec:
      command: "echo step2"
  - id: step3
    exec:
      command: "echo step3"
"#;
        let workflow = parse_workflow(yaml);
        assert_eq!(workflow.tasks.len(), 3);

        let runner = Runner::new(workflow).unwrap();
        assert!(runner.event_log().is_empty());
    }
}

// =============================================================================
// TEST 2: Workflow Parsing for Runner
// =============================================================================

mod workflow_parsing {
    use super::*;

    #[test]
    fn test_workflow_with_depends_on() {
        let yaml = r#"
schema: nika/workflow@0.12
workflow: with-deps
tasks:
  - id: step1
    exec:
      command: "echo start"
  - id: step2
    depends_on: [step1]
    exec:
      command: "echo end"
"#;
        let workflow = parse_workflow(yaml);
        assert_eq!(workflow.tasks.len(), 2);

        // step2 depends on step1
        let step2 = &workflow.tasks[1];
        assert_eq!(step2.depends_on.len(), 1);
    }

    #[test]
    fn test_workflow_with_for_each() {
        let yaml = r#"
schema: nika/workflow@0.12
workflow: with-for-each
tasks:
  - id: parallel_task
    for_each: ["item1", "item2", "item3"]
    as: item
    exec:
      command: "echo {{item}}"
"#;
        let workflow = parse_workflow(yaml);
        assert_eq!(workflow.tasks.len(), 1);

        let task = &workflow.tasks[0];
        assert!(task.for_each.is_some());
        let fe = task.for_each.as_ref().unwrap();
        assert_eq!(fe.as_var, "item");
    }

    #[test]
    fn test_workflow_with_concurrency_settings() {
        let yaml = r#"
schema: nika/workflow@0.12
workflow: with-concurrency
tasks:
  - id: parallel_task
    for_each: ["a", "b", "c", "d", "e"]
    as: item
    concurrency: 3
    fail_fast: false
    exec:
      command: "echo {{item}}"
"#;
        let workflow = parse_workflow(yaml);
        let task = &workflow.tasks[0];
        let fe = task.for_each.as_ref().unwrap();

        assert_eq!(fe.parallel, Some(3));
        assert!(!fe.fail_fast);
    }

    #[test]
    fn test_workflow_concurrency_defaults() {
        let yaml = r#"
schema: nika/workflow@0.12
workflow: concurrency-defaults
tasks:
  - id: parallel_task
    for_each: ["a", "b"]
    as: item
    exec:
      command: "echo {{item}}"
"#;
        let workflow = parse_workflow(yaml);
        let task = &workflow.tasks[0];
        let fe = task.for_each.as_ref().unwrap();

        // Default concurrency is 1 (sequential)
        assert_eq!(fe.parallel, Some(1));
        // Default fail_fast is true
        assert!(fe.fail_fast);
    }
}

// =============================================================================
// TEST 3: Task Action Variants
// =============================================================================

mod task_actions {
    use super::*;

    #[test]
    fn test_infer_task() {
        let yaml = r#"
schema: nika/workflow@0.12
workflow: infer-test
tasks:
  - id: infer_task
    infer:
      prompt: "Generate a response"
"#;
        let workflow = parse_workflow(yaml);
        let task = &workflow.tasks[0];
        assert!(matches!(task.action, AnalyzedTaskAction::Infer(..)));
    }

    #[test]
    fn test_exec_task() {
        let yaml = r#"
schema: nika/workflow@0.12
workflow: exec-test
tasks:
  - id: exec_task
    exec:
      command: "echo hello"
"#;
        let workflow = parse_workflow(yaml);
        let task = &workflow.tasks[0];
        assert!(matches!(task.action, AnalyzedTaskAction::Exec(..)));
    }

    #[test]
    fn test_fetch_task() {
        let yaml = r#"
schema: nika/workflow@0.12
workflow: fetch-test
tasks:
  - id: fetch_task
    fetch:
      url: "https://example.com/api"
      method: GET
"#;
        let workflow = parse_workflow(yaml);
        let task = &workflow.tasks[0];
        assert!(matches!(task.action, AnalyzedTaskAction::Fetch(..)));
    }

    #[test]
    fn test_invoke_task() {
        let yaml = r#"
schema: nika/workflow@0.12
workflow: invoke-test
tasks:
  - id: invoke_task
    invoke:
      mcp: novanet
      tool: novanet_describe
"#;
        let workflow = parse_workflow(yaml);
        let task = &workflow.tasks[0];
        assert!(matches!(task.action, AnalyzedTaskAction::Invoke(..)));
    }
}

// =============================================================================
// TEST 4: Dependency Graph Construction
// =============================================================================

mod dependency_graph {
    use super::*;

    #[test]
    fn test_no_dependencies() {
        let yaml = r#"
schema: nika/workflow@0.12
workflow: no-deps
tasks:
  - id: task1
    exec:
      command: "echo 1"
  - id: task2
    exec:
      command: "echo 2"
"#;
        let workflow = parse_workflow(yaml);

        // No dependencies - both tasks should have empty deps
        assert!(workflow.tasks[0].depends_on.is_empty());
        assert!(workflow.tasks[1].depends_on.is_empty());
    }

    #[test]
    fn test_linear_chain() {
        let yaml = r#"
schema: nika/workflow@0.12
workflow: linear-chain
tasks:
  - id: task1
    exec:
      command: "echo 1"
  - id: task2
    depends_on: [task1]
    exec:
      command: "echo 2"
  - id: task3
    depends_on: [task2]
    exec:
      command: "echo 3"
"#;
        let workflow = parse_workflow(yaml);

        assert!(workflow.tasks[0].depends_on.is_empty());
        assert_eq!(workflow.tasks[1].depends_on.len(), 1);
        assert_eq!(workflow.tasks[2].depends_on.len(), 1);
    }

    #[test]
    fn test_fan_out() {
        let yaml = r#"
schema: nika/workflow@0.12
workflow: fan-out
tasks:
  - id: root
    exec:
      command: "echo root"
  - id: branch1
    depends_on: [root]
    exec:
      command: "echo branch1"
  - id: branch2
    depends_on: [root]
    exec:
      command: "echo branch2"
  - id: branch3
    depends_on: [root]
    exec:
      command: "echo branch3"
"#;
        let workflow = parse_workflow(yaml);

        // Root has no dependencies
        assert!(workflow.tasks[0].depends_on.is_empty());

        // All branches depend on root
        for task in &workflow.tasks[1..] {
            assert_eq!(task.depends_on.len(), 1);
        }
    }

    #[test]
    fn test_fan_in() {
        let yaml = r#"
schema: nika/workflow@0.12
workflow: fan-in
tasks:
  - id: source1
    exec:
      command: "echo 1"
  - id: source2
    exec:
      command: "echo 2"
  - id: sink
    depends_on: [source1, source2]
    exec:
      command: "echo sink"
"#;
        let workflow = parse_workflow(yaml);

        // Sink depends on both sources
        let sink = &workflow.tasks[2];
        assert_eq!(sink.depends_on.len(), 2);
    }
}

// =============================================================================
// TEST 5: Event Log Integration
// =============================================================================

mod event_log_integration {
    use super::*;

    #[test]
    fn test_event_log_starts_empty() {
        let yaml = r#"
schema: nika/workflow@0.12
workflow: event-test
tasks:
  - id: task1
    exec:
      command: "echo test"
"#;
        let workflow = parse_workflow(yaml);
        let runner = Runner::new(workflow).unwrap();

        assert!(runner.event_log().is_empty());
    }

    #[test]
    fn test_event_log_with_broadcast() {
        let yaml = r#"
schema: nika/workflow@0.12
workflow: broadcast-test
tasks:
  - id: task1
    exec:
      command: "echo test"
"#;
        let workflow = parse_workflow(yaml);
        let (event_log, _rx) = EventLog::new_with_broadcast();
        let runner = Runner::with_event_log(workflow, event_log).unwrap();

        assert!(runner.event_log().is_empty());
    }
}

// =============================================================================
// TEST 6: Data Binding Setup
// =============================================================================

mod data_binding_setup {
    use super::*;

    #[test]
    fn test_workflow_with_binding() {
        let yaml = r#"
schema: nika/workflow@0.12
workflow: binding-test
tasks:
  - id: step1
    exec:
      command: "echo hello"

  - id: step2
    depends_on: [step1]
    exec:
      command: "echo {{with.result}}"
    with:
      result: $step1
"#;
        let workflow = parse_workflow(yaml);
        assert_eq!(workflow.tasks.len(), 2);

        // Second task has with: binding
        let task2 = &workflow.tasks[1];
        assert!(!task2.with_spec.is_empty());
    }

    #[test]
    fn test_workflow_with_multiple_bindings() {
        let yaml = r#"
schema: nika/workflow@0.12
workflow: multi-binding
tasks:
  - id: fetch_data
    exec:
      command: "echo data"

  - id: fetch_config
    exec:
      command: "echo config"

  - id: combine
    depends_on: [fetch_data, fetch_config]
    exec:
      command: "echo {{with.data}} {{with.config}}"
    with:
      data: $fetch_data
      config: $fetch_config
"#;
        let workflow = parse_workflow(yaml);
        assert_eq!(workflow.tasks.len(), 3);

        // combine task has 2 with: bindings
        let combine = &workflow.tasks[2];
        assert_eq!(combine.with_spec.len(), 2);
    }
}