faucet-cli 1.9.0

Config-driven CLI runner for faucet-stream pipelines (YAML / JSON, Meltano-style)
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
//! Serde types for the `faucet test` spec file.
//!
//! A spec file declares fixture-based, fully-offline test cases for a
//! pipeline's deterministic path (transforms → quality → contract). No real
//! source or sink is ever built: fixture records are streamed through an
//! in-memory source and captured by an in-memory sink + DLQ.

use crate::config::TransformSpec;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use serde_json::Value;

/// Top-level shape of a `faucet test` spec file (YAML or JSON).
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct TestSpecFile {
    /// Spec-format version. Must be `1`.
    pub version: u32,
    /// The test cases, run in declared order.
    pub tests: Vec<TestCase>,
}

/// One fixture-based test case.
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct TestCase {
    /// Human-readable case name. Must be unique within the spec file.
    pub name: String,

    /// Path to a pipeline config file (relative paths resolve against the
    /// spec file's directory). The case runs that config's transforms,
    /// `quality:`, and `contract:` blocks against the fixture input.
    /// Mutually exclusive with `pipeline`.
    #[serde(default)]
    pub config: Option<String>,

    /// Inline pipeline logic (transforms / quality / contract) — no config
    /// file needed. Mutually exclusive with `config`.
    #[serde(default)]
    pub pipeline: Option<InlinePipeline>,

    /// Matrix row id to test when the referenced config expands to more than
    /// one invocation. Defaults to the sole invocation; an error names the
    /// available ids when the config has several and `row` is omitted.
    #[serde(default)]
    pub row: Option<String>,

    /// Fixture input: an inline array of JSON records, or a path (string) to
    /// a `.jsonl` / `.json` / `.yaml` fixture file (relative to the spec
    /// file's directory).
    pub input: InputSpec,

    /// Chunk the fixture input into pages of this many records before feeding
    /// the pipeline. `0` (default) feeds everything as a single page —
    /// matching `batch_size: 0` semantics for per-page checks (batch quality
    /// checks and aggregating SQL transforms see the whole input at once).
    #[serde(default)]
    pub page_size: usize,

    /// Fixed `${now.*}` clock for this case (RFC3339 like
    /// `2026-01-31T00:00:00Z`, or a date `2026-01-31`). Overrides the
    /// command-level `--clock`; defaults to process start (UTC). Set this
    /// whenever a transform stamps `${now.*}` so the case is deterministic.
    #[serde(default)]
    pub clock: Option<String>,

    /// What the case asserts about the run's outcome.
    pub expect: Expectation,
}

/// Inline pipeline logic for a test case that doesn't reference a config file.
#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct InlinePipeline {
    /// Transform chain, identical to `pipeline.transforms` in a config file.
    #[serde(default)]
    pub transforms: Vec<TransformSpec>,

    /// Quality checks, identical to `pipeline.quality` in a config file.
    /// Quarantined records are capturable via `expect.dlq` / `dlq_count`.
    #[cfg(feature = "quality")]
    #[serde(default)]
    pub quality: Option<faucet_core::QualitySpec>,

    /// Data contract, identical to `pipeline.contract` in a config file.
    #[cfg(feature = "contract")]
    #[serde(default)]
    pub contract: Option<faucet_core::ContractSpec>,

    /// PII masking policy, identical to `pipeline.masking` in a config file.
    /// Offline there is no destination sink, so every rule applies regardless
    /// of its `applies_to` scoping.
    #[cfg(feature = "masking")]
    #[serde(default)]
    pub masking: Option<faucet_core::MaskingSpec>,
}

/// Fixture input — inline records or a fixture-file path.
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
#[serde(untagged)]
pub enum InputSpec {
    /// Inline JSON records.
    Inline(Vec<Value>),
    /// Path to a `.jsonl` (one record per line) or `.json` / `.yaml` /
    /// `.yml` (top-level array) fixture file, relative to the spec file.
    Path(String),
}

/// Expected outcome of a test case. Every field is optional but at least one
/// must be set; all set fields are asserted together.
#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct Expectation {
    /// The exact records the sink must receive, in order (set
    /// `unordered: true` to compare as a multiset). Compared per `match`.
    #[serde(default)]
    pub records: Option<Vec<Value>>,

    /// The original record payloads that must land in the DLQ (quality /
    /// contract quarantines), in order. Envelope metadata (timestamps, error
    /// messages) is not compared — only the quarantined payload itself.
    #[serde(default)]
    pub dlq: Option<Vec<Value>>,

    /// Total records the sink must receive (a count-only alternative to
    /// `records`).
    #[serde(default)]
    pub records_written: Option<usize>,

    /// Total DLQ envelopes (a count-only alternative to `dlq`).
    #[serde(default)]
    pub dlq_count: Option<usize>,

    /// The run must FAIL, and the error message must contain this substring
    /// (e.g. a quality `abort` or contract `on_breach: fail`). Without this
    /// field, a failing run fails the case.
    #[serde(default)]
    pub error: Option<String>,

    /// Compare `records` / `dlq` as multisets instead of ordered lists.
    #[serde(default)]
    pub unordered: bool,

    /// How individual records are compared.
    #[serde(default, rename = "match")]
    pub match_mode: MatchMode,
}

/// Record-comparison mode for `expect.records` / `expect.dlq`.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum MatchMode {
    /// Expected and actual records must be deeply equal.
    #[default]
    Exact,
    /// Each expected record must be a recursive subset of the actual record:
    /// every expected object field must be present and match, but the actual
    /// record may carry extra fields. Arrays still compare element-by-element
    /// (same length), applying subset semantics to nested objects.
    Subset,
}

impl Expectation {
    /// True when at least one assertion field is set.
    pub fn has_any(&self) -> bool {
        self.records.is_some()
            || self.dlq.is_some()
            || self.records_written.is_some()
            || self.dlq_count.is_some()
            || self.error.is_some()
    }
}

impl TestSpecFile {
    /// Structural validation, run right after parsing. Fail-fast so a broken
    /// spec never reaches the runner: version, unique non-empty names, the
    /// config/pipeline exclusivity, a non-empty expectation, and page-size
    /// bounds all surface here with the spec path in the message.
    pub fn validate(&self, spec_path: &std::path::Path) -> crate::error::CliResult<()> {
        let at =
            |msg: String| crate::error::CliError::Config(format!("{}: {msg}", spec_path.display()));
        if self.version != 1 {
            return Err(at(format!(
                "unsupported test-spec version {} (expected 1)",
                self.version
            )));
        }
        if self.tests.is_empty() {
            return Err(at("spec declares no tests".to_string()));
        }
        let mut seen = std::collections::HashSet::new();
        for case in &self.tests {
            let name = case.name.trim();
            if name.is_empty() {
                return Err(at("test case with an empty name".to_string()));
            }
            if !seen.insert(name) {
                return Err(at(format!("duplicate test name '{name}'")));
            }
            match (&case.config, &case.pipeline) {
                (Some(_), Some(_)) => {
                    return Err(at(format!(
                        "test '{name}': `config` and `pipeline` are mutually exclusive — pick one"
                    )));
                }
                (None, None) => {
                    return Err(at(format!(
                        "test '{name}': one of `config` (a pipeline config path) or `pipeline` \
                         (inline transforms/quality/contract) is required"
                    )));
                }
                _ => {}
            }
            if case.row.is_some() && case.config.is_none() {
                return Err(at(format!(
                    "test '{name}': `row` selects a matrix row and requires `config`"
                )));
            }
            if !case.expect.has_any() {
                return Err(at(format!(
                    "test '{name}': `expect` must set at least one of records / dlq / \
                     records_written / dlq_count / error"
                )));
            }
            faucet_core::validate_batch_size(case.page_size)
                .map_err(|e| at(format!("test '{name}': page_size: {e}")))?;
        }
        Ok(())
    }
}

/// Parse a spec file (YAML or JSON by extension) and validate it.
pub fn load_spec(path: &std::path::Path) -> crate::error::CliResult<TestSpecFile> {
    use crate::error::CliError;
    let text = std::fs::read_to_string(path).map_err(|source| CliError::ReadConfig {
        path: path.to_path_buf(),
        source,
    })?;
    let ext = path
        .extension()
        .and_then(|e| e.to_str())
        .map(str::to_ascii_lowercase);
    let spec: TestSpecFile = match ext.as_deref() {
        Some("yaml" | "yml") => serde_yaml::from_str(&text).map_err(|e| CliError::ParseConfig {
            path: path.to_path_buf(),
            message: e.to_string(),
        })?,
        Some("json") => serde_json::from_str(&text).map_err(|e| CliError::ParseConfig {
            path: path.to_path_buf(),
            message: e.to_string(),
        })?,
        _ => {
            return Err(CliError::UnknownExtension {
                path: path.to_path_buf(),
            });
        }
    };
    spec.validate(path)?;
    Ok(spec)
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;
    use std::path::Path;

    fn write_spec(dir: &tempfile::TempDir, name: &str, body: &str) -> std::path::PathBuf {
        let p = dir.path().join(name);
        std::fs::write(&p, body).unwrap();
        p
    }

    #[test]
    fn parses_minimal_inline_spec() {
        let dir = tempfile::tempdir().unwrap();
        let p = write_spec(
            &dir,
            "t.yaml",
            r#"
version: 1
tests:
  - name: passthrough
    pipeline: {}
    input: [ { a: 1 } ]
    expect: { records: [ { a: 1 } ] }
"#,
        );
        let spec = load_spec(&p).unwrap();
        assert_eq!(spec.tests.len(), 1);
        assert_eq!(spec.tests[0].name, "passthrough");
        assert!(matches!(spec.tests[0].input, InputSpec::Inline(ref v) if v.len() == 1));
        assert_eq!(spec.tests[0].expect.records, Some(vec![json!({"a": 1})]));
        assert_eq!(spec.tests[0].expect.match_mode, MatchMode::Exact);
        assert!(!spec.tests[0].expect.unordered);
    }

    #[test]
    fn parses_json_spec() {
        let dir = tempfile::tempdir().unwrap();
        let p = write_spec(
            &dir,
            "t.json",
            r#"{ "version": 1, "tests": [ { "name": "n", "pipeline": {},
                 "input": [], "expect": { "records_written": 0 } } ] }"#,
        );
        assert_eq!(load_spec(&p).unwrap().tests.len(), 1);
    }

    #[test]
    fn rejects_unknown_extension_and_missing_file() {
        let dir = tempfile::tempdir().unwrap();
        let p = write_spec(&dir, "t.toml", "version = 1");
        assert!(matches!(
            load_spec(&p),
            Err(crate::error::CliError::UnknownExtension { .. })
        ));
        assert!(matches!(
            load_spec(Path::new("/nonexistent/spec.yaml")),
            Err(crate::error::CliError::ReadConfig { .. })
        ));
    }

    #[test]
    fn rejects_bad_version_empty_tests_and_duplicates() {
        let dir = tempfile::tempdir().unwrap();
        let bad_version = write_spec(
            &dir,
            "v.yaml",
            "version: 2\ntests: [ { name: x, pipeline: {}, input: [], expect: { records_written: 0 } } ]",
        );
        let err = load_spec(&bad_version).unwrap_err().to_string();
        assert!(err.contains("version 2"), "{err}");

        let empty = write_spec(&dir, "e.yaml", "version: 1\ntests: []");
        assert!(
            load_spec(&empty)
                .unwrap_err()
                .to_string()
                .contains("no tests")
        );

        let dup = write_spec(
            &dir,
            "d.yaml",
            r#"
version: 1
tests:
  - { name: same, pipeline: {}, input: [], expect: { records_written: 0 } }
  - { name: same, pipeline: {}, input: [], expect: { records_written: 0 } }
"#,
        );
        assert!(
            load_spec(&dup)
                .unwrap_err()
                .to_string()
                .contains("duplicate")
        );
    }

    #[test]
    fn rejects_config_pipeline_conflicts() {
        let dir = tempfile::tempdir().unwrap();
        let both = write_spec(
            &dir,
            "b.yaml",
            r#"
version: 1
tests:
  - { name: x, config: p.yaml, pipeline: {}, input: [], expect: { records_written: 0 } }
"#,
        );
        assert!(
            load_spec(&both)
                .unwrap_err()
                .to_string()
                .contains("mutually exclusive")
        );

        let neither = write_spec(
            &dir,
            "n.yaml",
            "version: 1\ntests: [ { name: x, input: [], expect: { records_written: 0 } } ]",
        );
        assert!(
            load_spec(&neither)
                .unwrap_err()
                .to_string()
                .contains("is required")
        );
    }

    #[test]
    fn rejects_row_without_config_and_empty_expect() {
        let dir = tempfile::tempdir().unwrap();
        let row = write_spec(
            &dir,
            "r.yaml",
            "version: 1\ntests: [ { name: x, pipeline: {}, row: a, input: [], expect: { records_written: 0 } } ]",
        );
        assert!(
            load_spec(&row)
                .unwrap_err()
                .to_string()
                .contains("requires `config`")
        );

        let empty_expect = write_spec(
            &dir,
            "x.yaml",
            "version: 1\ntests: [ { name: x, pipeline: {}, input: [], expect: {} } ]",
        );
        assert!(
            load_spec(&empty_expect)
                .unwrap_err()
                .to_string()
                .contains("at least one")
        );
    }

    #[test]
    fn rejects_oversized_page_size_and_empty_name() {
        let dir = tempfile::tempdir().unwrap();
        let big = write_spec(
            &dir,
            "p.yaml",
            "version: 1\ntests: [ { name: x, pipeline: {}, input: [], page_size: 2000000, expect: { records_written: 0 } } ]",
        );
        assert!(
            load_spec(&big)
                .unwrap_err()
                .to_string()
                .contains("page_size")
        );

        let unnamed = write_spec(
            &dir,
            "u.yaml",
            "version: 1\ntests: [ { name: '  ', pipeline: {}, input: [], expect: { records_written: 0 } } ]",
        );
        assert!(
            load_spec(&unnamed)
                .unwrap_err()
                .to_string()
                .contains("empty name")
        );
    }

    #[test]
    fn input_path_variant_parses() {
        let dir = tempfile::tempdir().unwrap();
        let p = write_spec(
            &dir,
            "f.yaml",
            r#"
version: 1
tests:
  - name: from-file
    pipeline: {}
    input: fixtures/records.jsonl
    expect: { records_written: 2 }
"#,
        );
        let spec = load_spec(&p).unwrap();
        assert!(
            matches!(spec.tests[0].input, InputSpec::Path(ref s) if s == "fixtures/records.jsonl")
        );
    }

    #[test]
    fn schema_generates() {
        let schema = schemars::schema_for!(TestSpecFile);
        let v = serde_json::to_value(&schema).unwrap();
        assert!(v["properties"]["tests"].is_object());
    }
}