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
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
//! FETCH Verb Error Path Tests
//!
//! Tests error handling for the `fetch:` verb in TaskExecutor.
//! Uses a minimal mock HTTP server to simulate various failure scenarios.
//!
//! Coverage: Gap 2 from test-coverage-gaps.md
//! - HTTP timeout (HIGH)
//! - Invalid URL format (HIGH)
//! - Non-2xx HTTP status (MEDIUM)
//! - Connection refused (LOW)

use std::sync::Arc;
use std::time::Duration;

use nika::ast::{FetchParams, TaskAction};
use nika::binding::ResolvedBindings;
use nika::error::NikaError;
use nika::event::EventLog;
use nika::runtime::TaskExecutor;
use nika::store::RunContext;
use rustc_hash::FxHashMap;
use tokio::io::AsyncWriteExt;
use tokio::net::TcpListener;

fn mock_executor() -> TaskExecutor {
    let event_log = EventLog::new();
    TaskExecutor::new("mock", None, None, event_log)
}

fn test_context() -> (ResolvedBindings, RunContext) {
    (ResolvedBindings::new(), RunContext::new())
}

// ═══════════════════════════════════════════════════════════════════════════
// HELPERS
// ═══════════════════════════════════════════════════════════════════════════

/// Create a FetchParams with GET method and empty headers
fn fetch_params(url: &str) -> FetchParams {
    FetchParams {
        url: url.to_string(),
        method: "GET".to_string(),
        headers: FxHashMap::default(),
        body: None,
        json: None,
        timeout: None,
        retry: None,
        follow_redirects: None,
        response: None,
        extract: None,
        selector: None,
    }
}

/// Start a mock server that delays before responding
async fn start_delayed_server(delay: Duration) -> String {
    let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
    let addr = listener.local_addr().unwrap();
    let url = format!("http://{}", addr);

    tokio::spawn(async move {
        if let Ok((mut socket, _)) = listener.accept().await {
            // Wait before sending response
            tokio::time::sleep(delay).await;
            let response = "HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nOK";
            let _ = socket.write_all(response.as_bytes()).await;
        }
    });

    url
}

/// Start a mock server that returns a specific status code
async fn start_status_server(status: u16, body: &str) -> String {
    let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
    let addr = listener.local_addr().unwrap();
    let url = format!("http://{}", addr);

    let response = format!(
        "HTTP/1.1 {} Status\r\nContent-Type: text/plain\r\nContent-Length: {}\r\n\r\n{}",
        status,
        body.len(),
        body
    );

    tokio::spawn(async move {
        if let Ok((mut socket, _)) = listener.accept().await {
            // Read the request first (important for HTTP compliance)
            let mut buf = [0u8; 1024];
            let _ = tokio::io::AsyncReadExt::read(&mut socket, &mut buf).await;
            let _ = socket.write_all(response.as_bytes()).await;
        }
    });

    // Give the server a moment to start
    tokio::time::sleep(Duration::from_millis(10)).await;
    url
}

/// Start a mock server that returns malformed HTTP
async fn start_malformed_server() -> String {
    let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
    let addr = listener.local_addr().unwrap();
    let url = format!("http://{}", addr);

    tokio::spawn(async move {
        if let Ok((mut socket, _)) = listener.accept().await {
            // Read the request
            let mut buf = [0u8; 1024];
            let _ = tokio::io::AsyncReadExt::read(&mut socket, &mut buf).await;
            // Send malformed response (not valid HTTP)
            let _ = socket.write_all(b"NOT HTTP AT ALL\r\n").await;
        }
    });

    tokio::time::sleep(Duration::from_millis(10)).await;
    url
}

// ═══════════════════════════════════════════════════════════════════════════
// HIGH PRIORITY TESTS
// ═══════════════════════════════════════════════════════════════════════════

#[tokio::test]
async fn test_fetch_invalid_url_returns_execution_error() {
    // Arrange
    let executor = mock_executor();
    let task_id: Arc<str> = Arc::from("fetch_invalid");
    let (bindings, datastore) = test_context();

    // Use a completely invalid URL
    let action = TaskAction::Fetch {
        fetch: fetch_params("not-a-url"),
    };

    // Act
    let result = executor
        .execute(&task_id, &action, &bindings, &datastore, None)
        .await;

    // Assert
    assert!(result.is_err(), "Invalid URL should fail");
    match result.unwrap_err() {
        NikaError::Execution(msg) => {
            assert!(
                msg.contains("HTTP request failed"),
                "Error should mention HTTP failure: {msg}"
            );
        }
        err => panic!("Expected Execution error, got: {err:?}"),
    }
}

#[tokio::test]
async fn test_fetch_invalid_scheme_returns_error() {
    // Arrange
    let executor = mock_executor();
    let task_id: Arc<str> = Arc::from("fetch_bad_scheme");
    let (bindings, datastore) = test_context();

    // Use an unsupported scheme
    let action = TaskAction::Fetch {
        fetch: fetch_params("ftp://example.com/file"),
    };

    // Act
    let result = executor
        .execute(&task_id, &action, &bindings, &datastore, None)
        .await;

    // Assert
    assert!(result.is_err(), "Unsupported scheme should fail");
    match result.unwrap_err() {
        NikaError::Execution(msg) => {
            assert!(
                msg.contains("HTTP request failed"),
                "Error should mention HTTP failure: {msg}"
            );
        }
        err => panic!("Expected Execution error, got: {err:?}"),
    }
}

#[tokio::test]
async fn test_fetch_non_2xx_status_returns_body_not_error() {
    // NOTE: Current implementation returns body regardless of status code
    // This test documents the current behavior

    // Arrange
    let executor = mock_executor();
    let task_id: Arc<str> = Arc::from("fetch_500");
    let (bindings, datastore) = test_context();

    let url = start_status_server(500, "Internal Server Error").await;
    let action = TaskAction::Fetch {
        fetch: fetch_params(&url),
    };

    // Act
    let result = executor
        .execute(&task_id, &action, &bindings, &datastore, None)
        .await;

    // Assert - current behavior: returns body text regardless of status
    // This documents current behavior; a future change might return an error for non-2xx
    assert!(
        result.is_ok(),
        "Current impl returns body for any HTTP response: {:?}",
        result.err()
    );
    let body = result.unwrap();
    assert!(
        body.contains("Internal Server Error"),
        "Body should contain server response: {body}"
    );
}

#[tokio::test]
async fn test_fetch_404_returns_body() {
    // Arrange
    let executor = mock_executor();
    let task_id: Arc<str> = Arc::from("fetch_404");
    let (bindings, datastore) = test_context();

    let url = start_status_server(404, "Not Found").await;
    let action = TaskAction::Fetch {
        fetch: fetch_params(&url),
    };

    // Act
    let result = executor
        .execute(&task_id, &action, &bindings, &datastore, None)
        .await;

    // Assert
    assert!(result.is_ok(), "404 returns body: {:?}", result.err());
    assert_eq!(result.unwrap(), "Not Found");
}

#[tokio::test]
async fn test_fetch_connection_refused_returns_error() {
    // Arrange
    let executor = mock_executor();
    let task_id: Arc<str> = Arc::from("fetch_refused");
    let (bindings, datastore) = test_context();

    // Use a port that's definitely not listening
    // Port 1 requires root on Unix and is typically not available
    let action = TaskAction::Fetch {
        fetch: fetch_params("http://127.0.0.1:1/"),
    };

    // Act
    let result = executor
        .execute(&task_id, &action, &bindings, &datastore, None)
        .await;

    // Assert
    assert!(result.is_err(), "Connection refused should fail");
    match result.unwrap_err() {
        NikaError::Execution(msg) => {
            assert!(
                msg.contains("HTTP request failed"),
                "Error should mention HTTP failure: {msg}"
            );
        }
        err => panic!("Expected Execution error, got: {err:?}"),
    }
}

#[tokio::test]
async fn test_fetch_malformed_response_returns_error() {
    // Arrange
    let executor = mock_executor();
    let task_id: Arc<str> = Arc::from("fetch_malformed");
    let (bindings, datastore) = test_context();

    let url = start_malformed_server().await;
    let action = TaskAction::Fetch {
        fetch: fetch_params(&url),
    };

    // Act
    let result = executor
        .execute(&task_id, &action, &bindings, &datastore, None)
        .await;

    // Assert - malformed HTTP response should cause a parse error
    assert!(result.is_err(), "Malformed HTTP should fail");
    match result.unwrap_err() {
        NikaError::Execution(msg) => {
            // reqwest will fail to parse the response
            assert!(
                msg.contains("HTTP request failed") || msg.contains("Failed to read"),
                "Error should mention HTTP or read failure: {msg}"
            );
        }
        err => panic!("Expected Execution error, got: {err:?}"),
    }
}

// ═══════════════════════════════════════════════════════════════════════════
// TIMEOUT TESTS
// ═══════════════════════════════════════════════════════════════════════════

// NOTE: Timeout tests are tricky because the executor uses FETCH_TIMEOUT (30s)
// and CONNECT_TIMEOUT (10s) which are too long for unit tests.
// These tests document the expected behavior but may need to be run with
// a custom executor that has shorter timeouts for CI.

#[tokio::test]
#[ignore = "Requires custom executor with short timeout - takes too long for CI"]
async fn test_fetch_timeout_with_delayed_server() {
    // This test would need a way to inject a custom reqwest::Client
    // with a short timeout, which isn't currently supported.

    // Arrange
    let executor = mock_executor();
    let task_id: Arc<str> = Arc::from("fetch_timeout");
    let (bindings, datastore) = test_context();

    // Server delays 35 seconds (longer than FETCH_TIMEOUT of 30s)
    let url = start_delayed_server(Duration::from_secs(35)).await;
    let action = TaskAction::Fetch {
        fetch: fetch_params(&url),
    };

    // Act
    let result = executor
        .execute(&task_id, &action, &bindings, &datastore, None)
        .await;

    // Assert
    assert!(result.is_err(), "Timeout should fail");
    match result.unwrap_err() {
        NikaError::Execution(msg) => {
            assert!(
                msg.contains("timeout") || msg.contains("timed out"),
                "Error should mention timeout: {msg}"
            );
        }
        err => panic!("Expected Execution error with timeout, got: {err:?}"),
    }
}

// ═══════════════════════════════════════════════════════════════════════════
// SUCCESSFUL FETCH TESTS (for comparison)
// ═══════════════════════════════════════════════════════════════════════════

#[tokio::test]
async fn test_fetch_success_returns_body() {
    // Arrange
    let executor = mock_executor();
    let task_id: Arc<str> = Arc::from("fetch_success");
    let (bindings, datastore) = test_context();

    let url = start_status_server(200, "Hello, World!").await;
    let action = TaskAction::Fetch {
        fetch: fetch_params(&url),
    };

    // Act
    let result = executor
        .execute(&task_id, &action, &bindings, &datastore, None)
        .await;

    // Assert
    assert!(result.is_ok(), "200 OK should succeed: {:?}", result.err());
    assert_eq!(result.unwrap(), "Hello, World!");
}

#[tokio::test]
async fn test_fetch_with_json_body() {
    // Arrange
    let executor = mock_executor();
    let task_id: Arc<str> = Arc::from("fetch_json");
    let (bindings, datastore) = test_context();

    let json_body = r#"{"status":"ok","count":42}"#;
    let url = start_status_server(200, json_body).await;
    let action = TaskAction::Fetch {
        fetch: fetch_params(&url),
    };

    // Act
    let result = executor
        .execute(&task_id, &action, &bindings, &datastore, None)
        .await;

    // Assert
    assert!(result.is_ok(), "JSON response should succeed");
    let body = result.unwrap();
    assert_eq!(body, json_body);
    // Verify it's valid JSON
    let parsed: serde_json::Value = serde_json::from_str(&body).unwrap();
    assert_eq!(parsed["count"], 42);
}

// ═══════════════════════════════════════════════════════════════════════════
// EDGE CASES
// ═══════════════════════════════════════════════════════════════════════════

#[tokio::test]
async fn test_fetch_empty_url_fails() {
    // Arrange
    let executor = mock_executor();
    let task_id: Arc<str> = Arc::from("fetch_empty");
    let (bindings, datastore) = test_context();

    let action = TaskAction::Fetch {
        fetch: fetch_params(""),
    };

    // Act
    let result = executor
        .execute(&task_id, &action, &bindings, &datastore, None)
        .await;

    // Assert
    assert!(result.is_err(), "Empty URL should fail");
    match result.unwrap_err() {
        NikaError::Execution(msg) => {
            assert!(
                msg.contains("HTTP request failed"),
                "Error should mention HTTP failure: {msg}"
            );
        }
        err => panic!("Expected Execution error, got: {err:?}"),
    }
}

#[tokio::test]
async fn test_fetch_url_with_invalid_characters() {
    // Arrange
    let executor = mock_executor();
    let task_id: Arc<str> = Arc::from("fetch_invalid_chars");
    let (bindings, datastore) = test_context();

    // Use a URL with invalid characters that reqwest cannot handle
    // Note: reqwest is lenient with spaces (may URL-encode them)
    // but fails on truly malformed URLs
    let action = TaskAction::Fetch {
        fetch: fetch_params("http://[invalid-ipv6/path"),
    };

    // Act
    let result = executor
        .execute(&task_id, &action, &bindings, &datastore, None)
        .await;

    // Assert - Malformed URLs should fail
    assert!(result.is_err(), "Malformed URL should fail");
    match result.unwrap_err() {
        NikaError::Execution(msg) => {
            assert!(
                msg.contains("HTTP request failed"),
                "Error should mention HTTP failure: {msg}"
            );
        }
        err => panic!("Expected Execution error, got: {err:?}"),
    }
}

#[tokio::test]
async fn test_fetch_localhost_unreachable_fails() {
    // Arrange
    let executor = mock_executor();
    let task_id: Arc<str> = Arc::from("fetch_unreachable");
    let (bindings, datastore) = test_context();

    // Use a high port that's almost certainly not in use
    let action = TaskAction::Fetch {
        fetch: fetch_params("http://127.0.0.1:59999/nonexistent"),
    };

    // Act
    let result = executor
        .execute(&task_id, &action, &bindings, &datastore, None)
        .await;

    // Assert
    assert!(result.is_err(), "Unreachable host should fail");
}

#[tokio::test]
async fn test_fetch_dns_resolution_failure() {
    // Arrange
    let executor = mock_executor();
    let task_id: Arc<str> = Arc::from("fetch_dns_fail");
    let (bindings, datastore) = test_context();

    // Use a domain that definitely doesn't exist
    let action = TaskAction::Fetch {
        fetch: fetch_params("http://this-domain-definitely-does-not-exist-12345.invalid/"),
    };

    // Act
    let result = executor
        .execute(&task_id, &action, &bindings, &datastore, None)
        .await;

    // Assert
    assert!(result.is_err(), "DNS failure should fail");
    match result.unwrap_err() {
        NikaError::Execution(msg) => {
            // DNS failures show up as connection errors
            assert!(
                msg.contains("HTTP request failed"),
                "Error should mention HTTP failure: {msg}"
            );
        }
        err => panic!("Expected Execution error, got: {err:?}"),
    }
}