faucet-cli 1.13.0

Config-driven CLI runner for faucet-stream pipelines (YAML / JSON, Meltano-style)
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
//! End-to-end wiring for the top-level `profiling:` block (#708): the
//! expand-time gates, the executor's per-run profile + baseline persistence,
//! drift detection across runs, the `on_drift` policies, the masked-values
//! guarantee, the `faucet profiling show|reset` verbs, the `faucet doctor`
//! probes, and the catalog copy of each run's profile.
#![cfg(all(feature = "source-csv", feature = "sink-jsonl"))]

use faucet_cli::cli::{
    ProfilingArgs, ProfilingCommand, ProfilingConfigArgs, ProfilingResetArgs, ProfilingShowArgs,
};
use faucet_cli::config::PipelineConfig;
use faucet_cli::error::CliError;
use faucet_cli::expand::expand;
use faucet_cli::profiling::{ProfileHistory, profiling_state_key};
use faucet_core::{DriftMetric, StateStore};
use std::path::{Path, PathBuf};

fn yaml(dir: &Path, extra_top: &str, extra_pipeline: &str) -> String {
    format!(
        r#"version: 1
name: proftest
pipeline:
  source: {{ type: csv, config: {{ path: {input} }} }}
  sink: {{ type: jsonl, config: {{ path: {output}, append: false }} }}
  state: {{ type: file, config: {{ path: {state} }} }}
  transforms:
    - type: cast
      config: {{ fields: {{ amount: float }}, on_error: "null" }}
{extra_pipeline}
profiling:
  min_history: 3
  window: 5
{extra_top}
"#,
        input = dir.join("in.csv").display(),
        output = dir.join("out.jsonl").display(),
        state = dir.join("state").display(),
    )
}

fn parse(text: &str) -> PipelineConfig {
    PipelineConfig::from_text(text, Path::new("prof.yaml")).expect("config parses")
}

/// A stable dataset: `amount` numeric, `region` categorical, `email` PII.
fn stable_csv(seed: u64) -> String {
    let mut s = String::from("id,amount,region,email\n");
    for i in 0..120u64 {
        let k = (i * 7 + seed) % 1000;
        let region = match k % 3 {
            0 => "eu",
            1 => "us",
            _ => "apac",
        };
        s.push_str(&format!(
            "{i},{}.{},{region},user{k}@example.com\n",
            k % 90 + 10,
            k % 10
        ));
    }
    s
}

/// The same shape with 40 % of `amount` empty (null after CSV typing) and a
/// region the baseline never held.
fn drifted_csv() -> String {
    let mut s = String::from("id,amount,region,email\n");
    for i in 0..120u64 {
        let amount = if i % 5 < 2 {
            String::new()
        } else {
            format!("{}.5", i % 90 + 10)
        };
        let region = if i % 2 == 0 { "eu" } else { "latam" };
        s.push_str(&format!("{i},{amount},{region},user{i}@example.com\n"));
    }
    s
}

async fn history(dir: &Path) -> ProfileHistory {
    let store = faucet_core::FileStateStore::new(dir.join("state"));
    let key = profiling_state_key("proftest::row-0");
    store
        .get(&key)
        .await
        .unwrap()
        .map(ProfileHistory::from_value)
        .unwrap_or_default()
}

// ── expand-time gates ────────────────────────────────────────────────────────

#[test]
fn expand_requires_a_state_block_and_a_valid_spec() {
    let cfg = parse(
        r#"version: 1
pipeline:
  source: { type: csv, config: { path: in.csv } }
  sink: { type: jsonl, config: { path: out.jsonl } }
profiling: {}
"#,
    );
    let err = expand(&cfg).unwrap_err();
    assert!(
        matches!(&err, CliError::Config(m) if m.contains("profiling") && m.contains("state")),
        "expected the needs-state gate, got: {err}"
    );

    let cfg = parse(
        r#"version: 1
pipeline:
  source: { type: csv, config: { path: in.csv } }
  sink: { type: jsonl, config: { path: out.jsonl } }
  state: { type: memory, config: {} }
profiling: { min_history: 1 }
"#,
    );
    let err = expand(&cfg).unwrap_err();
    assert!(
        matches!(&err, CliError::Config(m) if m.contains("min_history")),
        "expected the validation error, got: {err}"
    );

    // A per-row override is validated and resolved onto the node.
    let cfg = parse(
        r#"version: 1
pipeline:
  source: { type: csv, config: { path: in.csv } }
  sink: { type: jsonl, config: { path: out.jsonl } }
  state: { type: memory, config: {} }
profiling: { window: 30 }
matrix:
  - id: a
  - id: b
    profiling: { window: 10, on_drift: fail }
"#,
    );
    let nodes = expand(&cfg).unwrap();
    assert_eq!(nodes[0].profiling.as_ref().unwrap().window, 30);
    assert_eq!(nodes[1].profiling.as_ref().unwrap().window, 10);
    assert_eq!(
        nodes[1].profiling.as_ref().unwrap().on_drift,
        faucet_core::OnProfileDrift::Fail
    );
    let without = parse(
        r#"version: 1
pipeline:
  source: { type: csv, config: { path: in.csv } }
  sink: { type: jsonl, config: { path: out.jsonl } }
"#,
    );
    assert!(expand(&without).unwrap()[0].profiling.is_none());
}

// ── executor: baseline, drift, on_drift ──────────────────────────────────────

#[tokio::test]
async fn runs_learn_a_baseline_then_flag_drift_without_failing_under_warn() {
    let dir = tempfile::tempdir().unwrap();
    let cfg = yaml(dir.path(), "", "");
    for seed in 0..3u64 {
        std::fs::write(dir.path().join("in.csv"), stable_csv(seed)).unwrap();
        let summary = faucet_cli::run_from_yaml_str(&cfg).await.unwrap();
        assert!(!summary.had_failures(), "{summary:?}");
        let h = history(dir.path()).await;
        assert_eq!(h.runs.len(), seed as usize + 1);
        assert!(h.latest().unwrap().drift.is_empty(), "warming up");
    }
    let h = history(dir.path()).await;
    let amount = &h.latest().unwrap().profile.columns["amount"];
    assert_eq!(amount.rows, 120);
    assert!(amount.numeric.is_some(), "csv typed the column numeric");
    assert!(amount.null_rate < 0.01);
    let region = &h.latest().unwrap().profile.columns["region"];
    assert_eq!(region.top_values.as_ref().unwrap().len(), 3);
    assert!(
        !h.latest()
            .unwrap()
            .profile
            .columns
            .contains_key("_faucet_run_id")
    );

    // A stable fourth run compares against the baseline and stays clean.
    std::fs::write(dir.path().join("in.csv"), stable_csv(9)).unwrap();
    faucet_cli::run_from_yaml_str(&cfg).await.unwrap();
    assert!(history(dir.path()).await.latest().unwrap().drift.is_empty());

    // The drifted run: null-rate jump on `amount`, a new `region` value —
    // recorded, not failed (`on_drift: warn`).
    std::fs::write(dir.path().join("in.csv"), drifted_csv()).unwrap();
    let summary = faucet_cli::run_from_yaml_str(&cfg).await.unwrap();
    assert!(!summary.had_failures(), "{summary:?}");
    let h = history(dir.path()).await;
    assert_eq!(h.runs.len(), 5, "windowed to 5");
    let drift = &h.latest().unwrap().drift;
    assert!(
        drift.iter().any(|d| d.column == "amount"
            && d.metric == DriftMetric::NullRate
            && d.observed >= 0.39),
        "{drift:?}"
    );
    assert!(
        drift.iter().any(|d| d.column == "region"
            && d.metric == DriftMetric::NewValue
            && d.value.as_deref() == Some("latam")),
        "{drift:?}"
    );
    assert!(
        drift
            .iter()
            .all(|d| d.column != "id" || d.metric != DriftMetric::NullRate),
        "the untouched id column raised no null-rate drift: {drift:?}"
    );
}

#[tokio::test]
async fn on_drift_fail_marks_the_run_failed_after_writing() {
    let dir = tempfile::tempdir().unwrap();
    let cfg = yaml(dir.path(), "  on_drift: fail", "");
    for seed in 0..3u64 {
        std::fs::write(dir.path().join("in.csv"), stable_csv(seed)).unwrap();
        assert!(
            !faucet_cli::run_from_yaml_str(&cfg)
                .await
                .unwrap()
                .had_failures()
        );
    }
    std::fs::write(dir.path().join("in.csv"), drifted_csv()).unwrap();
    let summary = faucet_cli::run_from_yaml_str(&cfg).await.unwrap();
    assert!(summary.had_failures(), "{summary:?}");
    let err = summary.invocations[0].error.clone().unwrap();
    assert!(err.contains("Profile drift on columns"), "{err}");
    assert!(err.contains("amount"), "{err}");
    // The data was written before the verdict, and the run joined the baseline.
    let written = std::fs::read_to_string(dir.path().join("out.jsonl")).unwrap();
    assert_eq!(written.lines().count(), 120);
    assert_eq!(history(dir.path()).await.runs.len(), 4);
}

#[cfg(feature = "masking")]
#[tokio::test]
async fn profiles_never_hold_unmasked_values() {
    let dir = tempfile::tempdir().unwrap();
    let cfg = yaml(
        dir.path(),
        "",
        "  masking:\n    rules:\n      - name: emails\n        match: { value_detector: email }\n        action: { type: redact }\n",
    );
    std::fs::write(dir.path().join("in.csv"), stable_csv(1)).unwrap();
    faucet_cli::run_from_yaml_str(&cfg).await.unwrap();
    let h = history(dir.path()).await;
    let email = &h.latest().unwrap().profile.columns["email"];
    assert_eq!(email.distinct, 1, "every value is the mask");
    let top = email.top_values.as_ref().unwrap();
    assert_eq!(top.len(), 1);
    assert_eq!(top[0].value, "***");
    assert!(!serde_json::to_string(&h).unwrap().contains("@example.com"));
}

#[tokio::test]
async fn dry_run_and_limit_do_not_touch_the_baseline() {
    let dir = tempfile::tempdir().unwrap();
    let text = yaml(dir.path(), "", "");
    std::fs::write(dir.path().join("in.csv"), stable_csv(1)).unwrap();
    let cfg = parse(&text);
    let nodes = expand(&cfg).unwrap();
    let opts = |dry_run: bool, limit: Option<usize>| faucet_cli::executor::ExecuteOptions {
        legacy_state_writes: false,
        pipeline_name: "proftest".into(),
        run_id: None,
        execution: None,
        concurrency: None,
        dry_run,
        limit,
        state_path_override: None,
        state_scope: Default::default(),
        shard: None,
        auth: Default::default(),
        clock: chrono::Utc::now().fixed_offset(),
        cancel: None,
        resilience: None,
        sla: None,
        reconcile: None,
        verify: None,
        rollback: None,
        #[cfg(feature = "lineage")]
        lineage: None,
        #[cfg(feature = "lineage")]
        lineage_cfg: None,
        #[cfg(feature = "notify")]
        notifier: None,
        #[cfg(feature = "catalog")]
        catalog: None,
        usage: Default::default(),
        budget: None,
    };
    faucet_cli::executor::run_expanded(nodes.clone(), opts(true, None))
        .await
        .unwrap();
    faucet_cli::executor::run_expanded(nodes, opts(false, Some(10)))
        .await
        .unwrap();
    assert!(history(dir.path()).await.runs.is_empty());
}

// ── `faucet profiling show|reset` ────────────────────────────────────────────

fn common(config: PathBuf, row: Option<&str>, json: bool) -> ProfilingConfigArgs {
    ProfilingConfigArgs {
        config: Some(config),
        row: row.map(String::from),
        json,
        env_file: None,
        no_env_file: true,
        profile: None,
    }
}

#[tokio::test]
async fn show_and_reset_verbs_drive_the_state_store() {
    let dir = tempfile::tempdir().unwrap();
    let text = yaml(dir.path(), "", "");
    let path = dir.path().join("prof.yaml");
    std::fs::write(&path, &text).unwrap();
    for seed in 0..2u64 {
        std::fs::write(dir.path().join("in.csv"), stable_csv(seed)).unwrap();
        faucet_cli::run_from_yaml_str(&text).await.unwrap();
    }
    for json in [false, true] {
        faucet_cli::commands::profiling::run(ProfilingArgs {
            command: ProfilingCommand::Show(ProfilingShowArgs {
                common: common(path.clone(), None, json),
                full: json,
            }),
        })
        .await
        .expect("show");
    }
    faucet_cli::commands::profiling::run(ProfilingArgs {
        command: ProfilingCommand::Reset(ProfilingResetArgs {
            common: common(path.clone(), Some("row-0"), false),
            column: Some("amount".into()),
        }),
    })
    .await
    .expect("column reset");
    let h = history(dir.path()).await;
    assert_eq!(h.runs.len(), 2);
    assert!(
        h.runs
            .iter()
            .all(|r| !r.profile.columns.contains_key("amount"))
    );
    faucet_cli::commands::profiling::run(ProfilingArgs {
        command: ProfilingCommand::Reset(ProfilingResetArgs {
            common: common(path.clone(), None, true),
            column: None,
        }),
    })
    .await
    .expect("full reset");
    assert!(history(dir.path()).await.runs.is_empty());

    // Unknown row / no profiling block are user errors, not panics.
    let err = faucet_cli::commands::profiling::run(ProfilingArgs {
        command: ProfilingCommand::Show(ProfilingShowArgs {
            common: common(path.clone(), Some("nope"), false),
            full: false,
        }),
    })
    .await
    .unwrap_err()
    .to_string();
    assert!(err.contains("not a root row"), "{err}");
    let plain = dir.path().join("plain.yaml");
    std::fs::write(
        &plain,
        "version: 1\npipeline:\n  source: { type: csv, config: { path: in.csv } }\n  sink: { type: jsonl, config: { path: out.jsonl } }\n",
    )
    .unwrap();
    let err = faucet_cli::commands::profiling::run(ProfilingArgs {
        command: ProfilingCommand::Show(ProfilingShowArgs {
            common: common(plain, None, false),
            full: false,
        }),
    })
    .await
    .unwrap_err()
    .to_string();
    assert!(err.contains("no `profiling:` block"), "{err}");
}

// ── `faucet doctor` probes ───────────────────────────────────────────────────

#[tokio::test]
async fn doctor_reports_the_baseline_depth() {
    use faucet_cli::commands::doctor::probe_roots;
    let dir = tempfile::tempdir().unwrap();
    let text = yaml(dir.path(), "", "");
    let cfg = parse(&text);
    let nodes = expand(&cfg).unwrap();
    let auth = faucet_cli::auth_catalog::build_auth_catalog(None).unwrap();
    let ctx = faucet_core::CheckContext::default();
    std::fs::write(dir.path().join("in.csv"), stable_csv(0)).unwrap();
    let invs = probe_roots(
        &nodes,
        &auth,
        &ctx,
        None,
        cfg.profiling.as_ref(),
        "proftest",
    )
    .await;
    let probe = invs[0]
        .probes
        .iter()
        .find(|p| p.role == "profiling")
        .expect("profiling probe");
    assert!(
        probe.hint.as_deref().unwrap_or("").contains("warming up"),
        "{probe:?}"
    );
    faucet_cli::run_from_yaml_str(&text).await.unwrap();
    let invs = probe_roots(
        &nodes,
        &auth,
        &ctx,
        None,
        cfg.profiling.as_ref(),
        "proftest",
    )
    .await;
    let probe = invs[0]
        .probes
        .iter()
        .find(|p| p.role == "profiling")
        .unwrap();
    assert!(
        probe.hint.as_deref().unwrap_or("").contains("1 of 3"),
        "{probe:?}"
    );
    let none = probe_roots(&nodes, &auth, &ctx, None, None, "proftest").await;
    assert!(
        none[0].probes.iter().any(|p| p.role == "profiling"),
        "the node's own spec applies"
    );
}

// ── catalog copy ─────────────────────────────────────────────────────────────

#[cfg(all(feature = "catalog", feature = "serve-history-sqlite"))]
#[tokio::test]
async fn each_run_profile_is_recorded_on_the_sink_dataset() {
    use faucet_cli::serve::history::catalog::CatalogListFilter;
    let dir = tempfile::tempdir().unwrap();
    let cfg = yaml(
        dir.path(),
        &format!(
            "catalog:\n  url: \"sqlite:{}/cat.db\"",
            dir.path().display()
        ),
        "",
    );
    for seed in 0..2u64 {
        std::fs::write(dir.path().join("in.csv"), stable_csv(seed)).unwrap();
        faucet_cli::run_from_yaml_str(&cfg).await.unwrap();
    }
    let handle = faucet_cli::catalog::connect_from_spec(&faucet_cli::catalog::CatalogSpec {
        url: format!("sqlite:{}/cat.db", dir.path().display()),
        sample_records: 10,
        datasets: Vec::new(),
    })
    .await
    .unwrap();
    let page = handle
        .store
        .catalog_list_datasets(&CatalogListFilter {
            limit: 10,
            ..Default::default()
        })
        .await
        .unwrap();
    let sink = page.datasets.iter().find(|d| d.kind == "jsonl").unwrap();
    let detail = handle
        .store
        .catalog_get_dataset(&sink.id)
        .await
        .unwrap()
        .unwrap();
    let profile = detail
        .profile
        .expect("profile recorded on the sink dataset");
    assert_eq!(profile.history.len(), 2);
    assert_eq!(profile.latest.baseline_runs, 1);
    assert!(profile.latest.profile.columns.contains_key("amount"));
    let source = page.datasets.iter().find(|d| d.kind == "csv").unwrap();
    let source_detail = handle
        .store
        .catalog_get_dataset(&source.id)
        .await
        .unwrap()
        .unwrap();
    assert!(
        source_detail.profile.is_none(),
        "profiles describe the destination"
    );
    let history = handle
        .store
        .catalog_profile_history(&sink.id, 1)
        .await
        .unwrap();
    assert_eq!(history.len(), 1);
    assert_eq!(history[0].run_id, profile.latest.run_id);
}