pipeflow 0.0.4

A lightweight, configuration-driven data pipeline framework
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
#![cfg(all(feature = "file", feature = "http-client"))]

//! E2E tests for HTTP Client to File pipeline
//!
//! These tests verify file sink behavior:
//! - Messages are written in JSONL format
//! - TSV and CSV output formats work correctly
//! - Append mode works correctly
//! - Multiple poll cycles accumulate in file
//!
//! Each test uses a temp file that is verified after pipeline shutdown.

mod common;

use wiremock::matchers::{method, path};
use wiremock::{Mock, ResponseTemplate};

/// Scenario: Basic File Writing
///
/// Verifies that messages are written to the file in JSONL format.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_basic_file_writing() {
    if common::skip_if_no_network() {
        return;
    }

    let harness = common::TestHarness::new().await;
    let output_path = harness.output_path("output.jsonl");

    Mock::given(method("GET"))
        .and(path("/"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
            "message": "hello",
            "count": 42
        })))
        .expect(1..)
        .mount(&harness.mock_server)
        .await;

    let yaml = format!(
        r#"
pipeline:
  sources:
    - id: source
      type: http_client
      config:
        url: "{}"
        interval: "100ms"
  transforms:
    - id: pass_through
      inputs: [source]
      outputs: [file_sink]
  sinks:
    - id: file_sink
      type: file
      config:
        path: "{}"
        append: true
"#,
        harness.mock_server.uri(),
        output_path.display()
    );

    let (shutdown_tx, engine_handle) = harness.run_pipeline(&yaml).await;

    // Wait for content to appear instead of sleeping blindly
    let content = common::wait_for_file_content(&output_path, common::default_test_timeout()).await;

    // Shutdown gracefully
    let _ = shutdown_tx.send(());
    let result = engine_handle.await.expect("Engine task panicked");
    assert!(result.is_ok(), "Engine should shutdown gracefully");

    let lines: Vec<&str> = content.lines().collect();

    assert!(!lines.is_empty(), "File should contain at least one line");

    // Parse first line as JSON
    let parsed: serde_json::Value =
        serde_json::from_str(lines[0]).expect("Line should be valid JSON");
    assert!(
        parsed.get("payload").is_some(),
        "Message should have payload"
    );
    assert_eq!(parsed["payload"]["message"], "hello");
    assert_eq!(parsed["payload"]["count"], 42);
}

/// Scenario: Multiple Messages Accumulate
///
/// Verifies that multiple poll cycles result in multiple lines in the file.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_multiple_messages_accumulate() {
    if common::skip_if_no_network() {
        return;
    }

    let harness = common::TestHarness::new().await;
    let output_path = harness.output_path("output.jsonl");

    Mock::given(method("GET"))
        .and(path("/"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
            "status": "ok"
        })))
        .expect(3..) // Expect at least 3 requests
        .mount(&harness.mock_server)
        .await;

    let yaml = format!(
        r#"
pipeline:
  sources:
    - id: poller
      type: http_client
      config:
        url: "{}"
        interval: "100ms"
  transforms:
    - id: pass_through
      inputs: [poller]
      outputs: [file_out]
  sinks:
    - id: file_out
      type: file
      config:
        path: "{}"
"#,
        harness.mock_server.uri(),
        output_path.display()
    );

    let (shutdown_tx, engine_handle) = harness.run_pipeline(&yaml).await;

    // Wait for at least 3 lines
    let lines = common::wait_for_lines(&output_path, 3, common::default_test_timeout()).await;

    let _ = shutdown_tx.send(());
    let result = engine_handle.await.expect("Engine task panicked");
    assert!(result.is_ok(), "Engine should run without error");

    assert!(
        lines.len() >= 3,
        "File should contain at least 3 lines, got {}",
        lines.len()
    );

    // Verify each line is valid JSON
    for line in &lines {
        let parsed: serde_json::Value = serde_json::from_str(line)
            .unwrap_or_else(|_| panic!("Line should be valid JSON: {}", line));
        assert_eq!(parsed["payload"]["status"], "ok");
    }
}

/// Scenario: Overwrite Mode
///
/// Verifies that append=false overwrites the file on startup.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_overwrite_mode() {
    if common::skip_if_no_network() {
        return;
    }

    let harness = common::TestHarness::new().await;
    let output_path = harness.output_path("output.jsonl");

    // Pre-populate the file with existing content
    std::fs::write(&output_path, "existing content\n").unwrap();

    Mock::given(method("GET"))
        .and(path("/"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
            "new": true
        })))
        .expect(1..)
        .mount(&harness.mock_server)
        .await;

    let yaml = format!(
        r#"
pipeline:
  sources:
    - id: source
      type: http_client
      config:
        url: "{}"
        interval: "100ms"
  transforms:
    - id: pass_through
      inputs: [source]
      outputs: [file_out]
  sinks:
    - id: file_out
      type: file
      config:
        path: "{}"
        append: false
"#,
        harness.mock_server.uri(),
        output_path.display()
    );

    let (shutdown_tx, engine_handle) = harness.run_pipeline(&yaml).await;

    // Wait for file content to NOT act like append (check for new content)
    // Here we can just wait for *any* content that matches our expectation,
    // assuming it overwrites quickly. But since overwrite is open-mode, it happens
    // on sink init.

    // We'll loop until we see "new": true
    let content =
        common::wait_for_file_condition(&output_path, common::default_test_timeout(), |content| {
            content.contains(r#""new":true"#) && !content.contains("existing content")
        })
        .await;
    assert!(
        content.contains(r#""new":true"#) && !content.contains("existing content"),
        "File should contain new content and NO old content"
    );

    let _ = shutdown_tx.send(());
    let result = engine_handle.await.expect("Engine task panicked");
    assert!(result.is_ok());
}

/// Scenario: TSV Format Output
///
/// Verifies that messages are written in tab-separated values format.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_file_sink_tsv_format() {
    if common::skip_if_no_network() {
        return;
    }

    let harness = common::TestHarness::new().await;
    let output_path = harness.output_path("output.tsv");

    Mock::given(method("GET"))
        .and(path("/"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
            "name": "Alice",
            "score": 95
        })))
        .expect(1..)
        .mount(&harness.mock_server)
        .await;

    let yaml = format!(
        r#"
pipeline:
  sources:
    - id: source
      type: http_client
      config:
        url: "{}"
        interval: "100ms"
  transforms:
    - id: pass_through
      inputs: [source]
      outputs: [file_sink]
  sinks:
    - id: file_sink
      type: file
      config:
        path: "{}"
        format: tsv
"#,
        harness.mock_server.uri(),
        output_path.display()
    );

    let (shutdown_tx, engine_handle) = harness.run_pipeline(&yaml).await;

    // Wait for content to appear
    let content = common::wait_for_file_content(&output_path, common::default_test_timeout()).await;

    // Shutdown gracefully
    let _ = shutdown_tx.send(());
    let result = engine_handle.await.expect("Engine task panicked");
    assert!(result.is_ok(), "Engine should shutdown gracefully");

    let lines: Vec<&str> = content.lines().collect();
    assert!(!lines.is_empty(), "File should contain at least one line");

    // TSV format should contain tab-separated values
    let first_line = lines[0];
    assert!(
        first_line.contains('\t'),
        "TSV output should contain tab separators, got: {}",
        first_line
    );
}

/// Scenario: CSV Format Output
///
/// Verifies that messages are written in comma-separated values format.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_file_sink_csv_format() {
    if common::skip_if_no_network() {
        return;
    }

    let harness = common::TestHarness::new().await;
    let output_path = harness.output_path("output.csv");

    Mock::given(method("GET"))
        .and(path("/"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
            "city": "New York",
            "temperature": 72
        })))
        .expect(1..)
        .mount(&harness.mock_server)
        .await;

    let yaml = format!(
        r#"
pipeline:
  sources:
    - id: source
      type: http_client
      config:
        url: "{}"
        interval: "100ms"
  transforms:
    - id: pass_through
      inputs: [source]
      outputs: [file_sink]
  sinks:
    - id: file_sink
      type: file
      config:
        path: "{}"
        format: csv
"#,
        harness.mock_server.uri(),
        output_path.display()
    );

    let (shutdown_tx, engine_handle) = harness.run_pipeline(&yaml).await;

    // Wait for content to appear
    let content = common::wait_for_file_content(&output_path, common::default_test_timeout()).await;

    // Shutdown gracefully
    let _ = shutdown_tx.send(());
    let result = engine_handle.await.expect("Engine task panicked");
    assert!(result.is_ok(), "Engine should shutdown gracefully");

    let lines: Vec<&str> = content.lines().collect();
    assert!(!lines.is_empty(), "File should contain at least one line");

    // CSV format should contain comma-separated values
    let first_line = lines[0];
    assert!(
        first_line.contains(','),
        "CSV output should contain comma separators, got: {}",
        first_line
    );
}

/// Scenario: CSV Format with Header Row
///
/// Verifies that include_header: true writes column names as the first line.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_file_sink_csv_with_header() {
    if common::skip_if_no_network() {
        return;
    }

    let harness = common::TestHarness::new().await;
    let output_path = harness.output_path("output_with_header.csv");

    Mock::given(method("GET"))
        .and(path("/"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
            "name": "Bob",
            "age": 30
        })))
        .expect(1..)
        .mount(&harness.mock_server)
        .await;

    let yaml = format!(
        r#"
pipeline:
  sources:
    - id: source
      type: http_client
      config:
        url: "{}"
        interval: "100ms"
  transforms:
    - id: pass_through
      inputs: [source]
      outputs: [file_sink]
  sinks:
    - id: file_sink
      type: file
      config:
        path: "{}"
        format: csv
        include_header: true
        append: false
"#,
        harness.mock_server.uri(),
        output_path.display()
    );

    let (shutdown_tx, engine_handle) = harness.run_pipeline(&yaml).await;

    // Wait for at least 2 lines (header + 1 data row)
    let lines = common::wait_for_lines(&output_path, 2, common::default_test_timeout()).await;

    // Shutdown gracefully
    let _ = shutdown_tx.send(());
    let result = engine_handle.await.expect("Engine task panicked");
    assert!(result.is_ok(), "Engine should shutdown gracefully");

    assert!(
        lines.len() >= 2,
        "File should contain at least 2 lines (header + data), got {}",
        lines.len()
    );

    // First line should be header with column names
    let header = &lines[0];
    assert!(
        header.contains("name") && header.contains("age"),
        "Header should contain column names 'name' and 'age', got: {}",
        header
    );

    // Second line should be data
    let data = &lines[1];
    assert!(
        data.contains("Bob") && data.contains("30"),
        "Data row should contain 'Bob' and '30', got: {}",
        data
    );
}

/// Scenario: TSV Format with Header Row
///
/// Verifies that include_header: true writes column names as the first line for TSV.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_file_sink_tsv_with_header() {
    if common::skip_if_no_network() {
        return;
    }

    let harness = common::TestHarness::new().await;
    let output_path = harness.output_path("output_with_header.tsv");

    Mock::given(method("GET"))
        .and(path("/"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
            "product": "Widget",
            "price": 99
        })))
        .expect(1..)
        .mount(&harness.mock_server)
        .await;

    let yaml = format!(
        r#"
pipeline:
  sources:
    - id: source
      type: http_client
      config:
        url: "{}"
        interval: "100ms"
  transforms:
    - id: pass_through
      inputs: [source]
      outputs: [file_sink]
  sinks:
    - id: file_sink
      type: file
      config:
        path: "{}"
        format: tsv
        include_header: true
        append: false
"#,
        harness.mock_server.uri(),
        output_path.display()
    );

    let (shutdown_tx, engine_handle) = harness.run_pipeline(&yaml).await;

    // Wait for at least 2 lines (header + 1 data row)
    let lines = common::wait_for_lines(&output_path, 2, common::default_test_timeout()).await;

    // Shutdown gracefully
    let _ = shutdown_tx.send(());
    let result = engine_handle.await.expect("Engine task panicked");
    assert!(result.is_ok(), "Engine should shutdown gracefully");

    assert!(
        lines.len() >= 2,
        "File should contain at least 2 lines (header + data), got {}",
        lines.len()
    );

    // First line should be header with column names and tab separators
    let header = &lines[0];
    assert!(
        header.contains("product") && header.contains("price"),
        "Header should contain column names 'product' and 'price', got: {}",
        header
    );
    assert!(
        header.contains('\t'),
        "Header should use tab separators, got: {}",
        header
    );

    // Second line should be data
    let data = &lines[1];
    assert!(
        data.contains("Widget") && data.contains("99"),
        "Data row should contain 'Widget' and '99', got: {}",
        data
    );
}