Skip to main content

faucet_core/
traits.rs

1//! Shared traits for faucet sources and sinks.
2
3use crate::error::FaucetError;
4use crate::pipeline::StreamPage;
5use async_trait::async_trait;
6use futures_core::Stream;
7use serde_json::Value;
8use std::pin::Pin;
9
10/// A source fetches records from an external system.
11#[async_trait]
12pub trait Source: Send + Sync {
13    /// Primary fetch method. Receives context from a parent source's records.
14    ///
15    /// An empty context map means this is a root source (no parent).
16    /// Connectors that support being a child should use
17    /// [`substitute_context()`](crate::util::substitute_context) to resolve
18    /// `{placeholder}` tokens in their URL path, query parameters, headers,
19    /// or body. Connectors that don't need parent context ignore the map.
20    async fn fetch_with_context(
21        &self,
22        context: &std::collections::HashMap<String, Value>,
23    ) -> Result<Vec<Value>, FaucetError>;
24
25    /// Convenience: fetch with no parent context.
26    async fn fetch_all(&self) -> Result<Vec<Value>, FaucetError> {
27        self.fetch_with_context(&std::collections::HashMap::new())
28            .await
29    }
30
31    /// Incremental fetch with parent context support.
32    ///
33    /// Returns the records and an optional bookmark value for incremental
34    /// replication. The default delegates to `fetch_with_context` and
35    /// returns `None` for the bookmark.
36    async fn fetch_with_context_incremental(
37        &self,
38        context: &std::collections::HashMap<String, Value>,
39    ) -> Result<(Vec<Value>, Option<Value>), FaucetError> {
40        let records = self.fetch_with_context(context).await?;
41        Ok((records, None))
42    }
43
44    /// Convenience: incremental fetch with no parent context.
45    async fn fetch_all_incremental(&self) -> Result<(Vec<Value>, Option<Value>), FaucetError> {
46        self.fetch_with_context_incremental(&std::collections::HashMap::new())
47            .await
48    }
49
50    /// Stream records page-by-page so the pipeline can write to the sink as
51    /// pages arrive instead of buffering the full result set.
52    ///
53    /// `batch_size` is the *hint* the pipeline passes down; sources are free
54    /// to use a larger or smaller native chunk (e.g. one page per HTTP
55    /// response, one row-group per Parquet file) but should approximate it
56    /// where feasible. The special value `batch_size = 0` means "do not
57    /// batch — emit the entire result set in a single page." Sources that
58    /// stream natively should treat `0` as "skip the chunking layer and
59    /// yield one page after the underlying read completes" (useful for
60    /// small lookup tables or for sinks like SQL `COPY` / BigQuery load
61    /// jobs that prefer one large request).
62    ///
63    /// The default implementation fetches the full result set via
64    /// [`fetch_with_context_incremental`](Self::fetch_with_context_incremental)
65    /// and chunks it in memory by `batch_size`. The bookmark (when present)
66    /// is attached to the *final* page so the pipeline only persists after
67    /// the entire fetch has been written. Sources that can stream natively
68    /// override this method and may emit per-page bookmarks (e.g. CDC).
69    ///
70    /// An empty result with a `Some(bookmark)` still yields one empty page
71    /// carrying the bookmark, so incremental runs that produce no records
72    /// still advance their checkpoint.
73    fn stream_pages<'a>(
74        &'a self,
75        context: &'a std::collections::HashMap<String, Value>,
76        batch_size: usize,
77    ) -> Pin<Box<dyn Stream<Item = Result<StreamPage, FaucetError>> + Send + 'a>> {
78        Box::pin(async_stream::try_stream! {
79            let (records, bookmark) = self
80                .fetch_with_context_incremental(context)
81                .await?;
82            let total = records.len();
83            // batch_size == 0 means "no batching" — emit all records as one
84            // page. Otherwise chunk into pages of size `batch_size`.
85            let chunk = if batch_size == 0 { usize::MAX } else { batch_size };
86
87            if total == 0 {
88                if bookmark.is_some() {
89                    yield StreamPage {
90                        records: Vec::new(),
91                        bookmark,
92                    };
93                }
94                return;
95            }
96
97            let mut iter = records.into_iter();
98            let mut consumed = 0usize;
99            loop {
100                let batch: Vec<Value> = iter.by_ref().take(chunk).collect();
101                if batch.is_empty() {
102                    break;
103                }
104                consumed += batch.len();
105                let page_bookmark = if consumed >= total {
106                    bookmark.clone()
107                } else {
108                    None
109                };
110                yield StreamPage {
111                    records: batch,
112                    bookmark: page_bookmark,
113                };
114            }
115        })
116    }
117
118    /// Return a JSON Schema describing the configuration this source accepts.
119    fn config_schema(&self) -> Value {
120        serde_json::json!({"type": "object", "properties": {}})
121    }
122
123    /// Stable key under which this source's incremental-replication bookmark
124    /// should be persisted in a [`StateStore`](crate::state::StateStore).
125    ///
126    /// Returning `Some(key)` opts this source into resumable runs: when the
127    /// pipeline is configured with a state store via
128    /// [`Pipeline::with_state_store`](crate::Pipeline::with_state_store), it
129    /// reads the bookmark at `key` before fetching and writes the new
130    /// bookmark back only after the sink confirms the batch was written.
131    ///
132    /// The default returns `None`, meaning the source is not persisted.
133    /// Keys must satisfy [`validate_state_key`](crate::state::validate_state_key).
134    fn state_key(&self) -> Option<String> {
135        None
136    }
137
138    /// Apply a bookmark loaded from a [`StateStore`](crate::state::StateStore)
139    /// as this run's starting point.
140    ///
141    /// The default implementation ignores the value, which keeps existing
142    /// sources backwards-compatible. Sources that support incremental
143    /// replication override this — typically by storing the value behind
144    /// interior mutability and consulting it inside
145    /// `fetch_with_context_incremental`.
146    async fn apply_start_bookmark(&self, _bookmark: Value) -> Result<(), FaucetError> {
147        Ok(())
148    }
149
150    /// Capture the source's current replication position **without consuming
151    /// any changes**, ensuring any server-side resource (e.g. a logical
152    /// replication slot) needed to later resume from that position exists.
153    ///
154    /// Returns the position as a bookmark [`Value`] — the same shape
155    /// [`apply_start_bookmark`](Self::apply_start_bookmark) accepts — or `None`
156    /// if this source does not support position capture.
157    ///
158    /// Used by the snapshot→CDC replication orchestrator (`faucet replicate`)
159    /// to anchor the CDC stream at-or-before the bulk snapshot's read point so
160    /// the handoff has no gap. The default returns `None`.
161    async fn capture_resume_position(&self) -> Result<Option<Value>, FaucetError> {
162        Ok(None)
163    }
164
165    /// Whether this source **deterministically replays** the same page sequence
166    /// from a given bookmark — the requirement for exactly-once delivery (a
167    /// non-deterministic replay could cause the pipeline to skip a page whose
168    /// contents differ from the one already committed). Default: `false`.
169    ///
170    /// Sources with a durable monotonic position and per-page bookmarks (CDC)
171    /// override this to return `true`. The pipeline rejects
172    /// `DeliveryMode::ExactlyOnce` against a source that returns `false`.
173    fn supports_exactly_once(&self) -> bool {
174        false
175    }
176
177    /// The typed replay capability this source advertises — see
178    /// [`ReplayGuarantee`](crate::ReplayGuarantee).
179    ///
180    /// The default derives from [`supports_exactly_once`](Self::supports_exactly_once)
181    /// (the boolean stays the back-compat primitive: existing connectors that
182    /// override only the boolean automatically advertise `Deterministic`
183    /// here). Override this directly only to *diverge* from the boolean —
184    /// there is currently no reason to.
185    fn replay_guarantee(&self) -> crate::idempotency::ReplayGuarantee {
186        if self.supports_exactly_once() {
187            crate::idempotency::ReplayGuarantee::Deterministic
188        } else {
189            crate::idempotency::ReplayGuarantee::NonDeterministic
190        }
191    }
192
193    /// Whether this source can split its work into independent shards for
194    /// clustered (Mode B) execution. Default: `false` (single whole-dataset
195    /// shard). Sources with a natural partition (object-store prefixes, table
196    /// primary-key ranges) override this to `true` and implement
197    /// [`enumerate_shards`](Self::enumerate_shards) +
198    /// [`apply_shard`](Self::apply_shard).
199    fn is_shardable(&self) -> bool {
200        false
201    }
202
203    /// Enumerate the shards this source splits into, aiming for roughly `target`
204    /// of them (a hint — the source may return fewer, e.g. when the dataset is
205    /// small, or one per natural partition regardless of `target`).
206    ///
207    /// Called **once per run** by the cluster coordinator; enumeration must be
208    /// deterministic enough that re-enumeration yields a compatible set (stable
209    /// shard ids), since it may run on more than one instance and is reconciled
210    /// by idempotent insert. May perform read-only I/O (a `LIST`, a `MIN/MAX`
211    /// query). The default returns a single whole-dataset shard
212    /// ([`ShardSpec::whole`](crate::ShardSpec::whole)), preserving today's
213    /// single-worker behavior.
214    async fn enumerate_shards(
215        &self,
216        _target: usize,
217    ) -> Result<Vec<crate::shard::ShardSpec>, FaucetError> {
218        Ok(vec![crate::shard::ShardSpec::whole()])
219    }
220
221    /// Narrow this source instance to a single shard before streaming.
222    ///
223    /// Called on the worker that claims `shard`, after construction and before
224    /// any `stream_pages` call. Like [`apply_start_bookmark`](Self::apply_start_bookmark)
225    /// this takes `&self` and is expected to record the shard behind interior
226    /// mutability (the source consults it when building its query / listing).
227    /// The default ignores the shard — a non-shardable source only ever receives
228    /// [`ShardSpec::whole`](crate::ShardSpec::whole), so ignoring it streams the
229    /// whole dataset. Implementations should accept the whole shard as a no-op.
230    async fn apply_shard(&self, _shard: &crate::shard::ShardSpec) -> Result<(), FaucetError> {
231        Ok(())
232    }
233
234    /// Stable identifier used as the `connector` label on metrics and the
235    /// `connector` attribute on spans. Defaults to the final segment of
236    /// `std::any::type_name::<Self>()`, e.g. `"RestSource"`. Built-in
237    /// connectors override with a short, friendly snake_case name (e.g.
238    /// `"rest"`). Must return a non-empty string; observability decorators
239    /// fall back to `"unknown"` in release builds if it is empty (and
240    /// `debug_assert!` in debug builds).
241    fn connector_name(&self) -> &'static str {
242        crate::observability::strip_type_name(std::any::type_name::<Self>())
243    }
244
245    /// Logical dataset identity for lineage emission, following OpenLineage
246    /// naming conventions (<https://openlineage.io/docs/spec/naming>).
247    ///
248    /// The default returns `"<connector_name>://unknown"`. Built-in connectors
249    /// override with a credential-free URI derived from their config. Strip any
250    /// credentials with [`redact_uri_credentials`](crate::redact_uri_credentials).
251    /// Informational metadata only — never used for I/O.
252    fn dataset_uri(&self) -> String {
253        format!("{}://unknown", self.connector_name())
254    }
255
256    /// Run a fast, non-mutating preflight probe (used by `faucet doctor`).
257    ///
258    /// The default pulls a **single page** via
259    /// [`stream_pages`](Self::stream_pages) and reports success/failure — it
260    /// exercises the real read path (DNS, TLS, auth, the first request, the
261    /// first-record decode) but never paginates the full dataset and never
262    /// repeats. The page stream is dropped immediately after the first page.
263    ///
264    /// Sources whose first page *blocks* waiting for inbound data (webhook,
265    /// websocket) or has *side effects* (CDC consuming WAL) override this with a
266    /// cheaper, side-effect-free probe. Probe-level failures are returned as a
267    /// [`ProbeStatus::Fail`](crate::check::ProbeStatus) inside `Ok(report)`.
268    async fn check(
269        &self,
270        ctx: &crate::check::CheckContext,
271    ) -> Result<crate::check::CheckReport, FaucetError> {
272        use crate::check::{CheckReport, Probe};
273        use futures::StreamExt;
274
275        let empty = std::collections::HashMap::new();
276        let start = std::time::Instant::now();
277        let mut pages = self.stream_pages(&empty, 1);
278        let probe = match tokio::time::timeout(ctx.timeout, pages.next()).await {
279            Err(_) => Probe::fail("read", start.elapsed(), "timed out fetching first page"),
280            Ok(None) | Ok(Some(Ok(_))) => Probe::pass("read", start.elapsed()),
281            Ok(Some(Err(e))) => Probe::fail("read", start.elapsed(), e.to_string()),
282        };
283        Ok(CheckReport::single(probe))
284    }
285}
286
287/// Per-row outcome from [`Sink::write_batch_partial`].
288///
289/// `Ok(())` — the row was durably written to the sink.
290/// `Err(_)` — the row failed; the pipeline will route it to the DLQ when
291/// one is configured.
292pub type RowOutcome = Result<(), FaucetError>;
293
294/// A sink writes records to an external system.
295#[async_trait]
296pub trait Sink: Send + Sync {
297    /// Write a batch of records to the destination.
298    ///
299    /// Returns the number of records successfully written.
300    async fn write_batch(&self, records: &[Value]) -> Result<usize, FaucetError>;
301
302    /// Flush any buffered data to the destination.
303    ///
304    /// The default implementation is a no-op (suitable for sinks that
305    /// write immediately in `write_batch`).
306    async fn flush(&self) -> Result<(), FaucetError> {
307        Ok(())
308    }
309
310    /// Write a batch and report per-row outcomes.
311    ///
312    /// Sinks whose underlying API exposes per-row results (BigQuery
313    /// `insertAll`, Elasticsearch `_bulk`) override this. The default
314    /// implementation delegates to [`Self::write_batch`] and maps a single success
315    /// onto a uniform all-`Ok(())` vector. An outer failure is bubbled up
316    /// unchanged so the pipeline's DLQ router can apply its `on_batch_error`
317    /// policy at a single decision point.
318    async fn write_batch_partial(&self, records: &[Value]) -> Result<Vec<RowOutcome>, FaucetError> {
319        self.write_batch(records).await?;
320        Ok(records.iter().map(|_| Ok(())).collect())
321    }
322
323    /// Whether this sink can durably commit a page's rows **and** a commit token
324    /// in a single atomic transaction. Default: `false` (at-least-once only).
325    ///
326    /// Only return `true` from a sink that genuinely commits both atomically —
327    /// see [`write_batch_idempotent`](Self::write_batch_idempotent). The pipeline
328    /// rejects `DeliveryMode::ExactlyOnce` against a sink that returns `false`.
329    fn supports_idempotent_writes(&self) -> bool {
330        false
331    }
332
333    /// The strongest delivery guarantee this sink can uphold — see
334    /// [`SinkGuarantee`](crate::SinkGuarantee).
335    ///
336    /// The default derives from the two back-compat primitives:
337    /// [`supports_idempotent_writes`](Self::supports_idempotent_writes) →
338    /// `AtomicWatermark`, else an upsert-capable
339    /// [`supported_write_modes`](Self::supported_write_modes) → `KeyedUpsert`,
340    /// else `AtLeastOnce`. Existing connectors that override only the
341    /// primitives automatically advertise the right capability here.
342    fn sink_guarantee(&self) -> crate::idempotency::SinkGuarantee {
343        if self.supports_idempotent_writes() {
344            crate::idempotency::SinkGuarantee::AtomicWatermark
345        } else if self
346            .supported_write_modes()
347            .contains(&crate::write_mode::WriteMode::Upsert)
348        {
349            crate::idempotency::SinkGuarantee::KeyedUpsert
350        } else {
351            crate::idempotency::SinkGuarantee::AtLeastOnce
352        }
353    }
354
355    /// Whether this sink instance is **configured** to dedup by key — i.e.
356    /// `write_mode: upsert` (or `delete`) with a non-empty `key`, so
357    /// re-applying a record with the same key converges instead of
358    /// duplicating. Default: `false`.
359    ///
360    /// Distinct from [`sink_guarantee`](Self::sink_guarantee) (capability):
361    /// this reflects the *live config*. Sinks that flatten a
362    /// [`WriteSpec`](crate::write_mode::WriteSpec) into their config override
363    /// it as `self.config.write.dedups_by_key()`. The pipeline consults it to
364    /// derive the keyed-upsert effectively-once mechanism at run time.
365    fn dedups_by_key(&self) -> bool {
366        false
367    }
368
369    /// Write modes this sink can apply. Default: append-only. Sinks that
370    /// implement key-based merge override this to include
371    /// [`WriteMode::Upsert`](crate::write_mode::WriteMode) /
372    /// [`WriteMode::Delete`](crate::write_mode::WriteMode). The CLI rejects a
373    /// configured mode that is not in this set at config-load time.
374    fn supported_write_modes(&self) -> &'static [crate::write_mode::WriteMode] {
375        &[crate::write_mode::WriteMode::Append]
376    }
377
378    /// The sink's live destination schema as an `infer_schema`-shaped object
379    /// (`{"type":"object","properties":{ <col>: <type-fragment>, … }}`), or
380    /// `None` for a schemaless sink or a target that does not exist yet.
381    ///
382    /// Used by the schema-drift policy to diff each page's shape against the
383    /// real destination. Default: `Ok(None)` (drift handling is inert).
384    async fn current_schema(&self) -> Result<Option<Value>, FaucetError> {
385        Ok(None)
386    }
387
388    /// Whether this sink can apply additive/widening DDL via
389    /// [`evolve_schema`](Self::evolve_schema). Default: `false`. The CLI rejects
390    /// `on_drift: evolve` against a sink that returns `false` at config-load.
391    fn supports_schema_evolution(&self) -> bool {
392        false
393    }
394
395    /// Apply an additive schema evolution (new columns, lossless widenings,
396    /// nullability relaxations) to the destination. MUST be idempotent
397    /// (`ADD COLUMN IF NOT EXISTS` semantics) so concurrent runs converge.
398    ///
399    /// Default: a typed "unsupported" error. Override only when the backend
400    /// supports in-place additive DDL (and return `true` from
401    /// `supports_schema_evolution`).
402    async fn evolve_schema(
403        &self,
404        evolution: &crate::drift::SchemaEvolution,
405    ) -> Result<(), FaucetError> {
406        let _ = evolution;
407        Err(FaucetError::Sink(format!(
408            "sink '{}' does not support schema evolution",
409            self.connector_name()
410        )))
411    }
412
413    /// Write `records` AND durably record `token` for `scope`, atomically.
414    ///
415    /// `scope` namespaces the watermark (the pipeline passes the per-row state
416    /// key, e.g. `"{name}::{row_id}"`). `token` is a monotonic, fixed-width
417    /// string (see [`format_token`](crate::format_token)).
418    ///
419    /// The default is **not** idempotent: it ignores the token and delegates to
420    /// [`write_batch`](Self::write_batch). Override only when the commit is
421    /// genuinely atomic (and return `true` from `supports_idempotent_writes`).
422    async fn write_batch_idempotent(
423        &self,
424        records: &[Value],
425        scope: &str,
426        token: &str,
427    ) -> Result<usize, FaucetError> {
428        let _ = (scope, token);
429        self.write_batch(records).await
430    }
431
432    /// The last token durably committed for `scope`, or `None` if this sink has
433    /// never committed under that scope. Default: `None`.
434    async fn last_committed_token(&self, scope: &str) -> Result<Option<String>, FaucetError> {
435        let _ = scope;
436        Ok(None)
437    }
438
439    /// Return a JSON Schema describing the configuration this sink accepts.
440    ///
441    /// The schema is auto-generated from the config struct using `schemars`.
442    /// Callers can inspect it to discover required fields, types, defaults,
443    /// and descriptions before constructing the sink.
444    ///
445    /// The default returns an empty object schema.
446    fn config_schema(&self) -> Value {
447        serde_json::json!({"type": "object", "properties": {}})
448    }
449
450    /// Stable identifier used as the `connector` label on metrics and the
451    /// `connector` attribute on spans. See `Source::connector_name`.
452    fn connector_name(&self) -> &'static str {
453        crate::observability::strip_type_name(std::any::type_name::<Self>())
454    }
455
456    /// Logical dataset identity for lineage emission, following OpenLineage
457    /// naming conventions (<https://openlineage.io/docs/spec/naming>).
458    ///
459    /// The default returns `"<connector_name>://unknown"`. Built-in connectors
460    /// override with a credential-free URI derived from their config. Strip any
461    /// credentials with [`redact_uri_credentials`](crate::redact_uri_credentials).
462    /// Informational metadata only — never used for I/O.
463    fn dataset_uri(&self) -> String {
464        format!("{}://unknown", self.connector_name())
465    }
466
467    /// Run a fast, non-mutating preflight probe (used by `faucet doctor`).
468    ///
469    /// Unlike sources, a sink has no non-mutating "first page" equivalent
470    /// (`write_batch` mutates the destination), so the default returns
471    /// [`CheckReport::not_implemented`](crate::check::CheckReport::not_implemented).
472    /// Built-in sinks override this with a connect / auth / metadata probe.
473    ///
474    /// The probe **MUST be idempotent and side-effect-free** — no inserts, no
475    /// residual rows or objects — and must never put credentials or connection
476    /// strings in a probe `reason`/`hint`.
477    async fn check(
478        &self,
479        _ctx: &crate::check::CheckContext,
480    ) -> Result<crate::check::CheckReport, FaucetError> {
481        Ok(crate::check::CheckReport::not_implemented())
482    }
483}
484
485#[cfg(test)]
486mod tests {
487    use super::*;
488    use serde_json::json;
489
490    // ── Mock Source ──────────────────────────────────────────────────────────
491
492    struct MockSource {
493        records: Vec<Value>,
494    }
495
496    #[async_trait]
497    impl Source for MockSource {
498        async fn fetch_with_context(
499            &self,
500            _context: &std::collections::HashMap<String, Value>,
501        ) -> Result<Vec<Value>, FaucetError> {
502            Ok(self.records.clone())
503        }
504    }
505
506    struct IncrementalSource {
507        records: Vec<Value>,
508        bookmark: Value,
509    }
510
511    #[async_trait]
512    impl Source for IncrementalSource {
513        async fn fetch_with_context(
514            &self,
515            _context: &std::collections::HashMap<String, Value>,
516        ) -> Result<Vec<Value>, FaucetError> {
517            Ok(self.records.clone())
518        }
519
520        async fn fetch_with_context_incremental(
521            &self,
522            _context: &std::collections::HashMap<String, Value>,
523        ) -> Result<(Vec<Value>, Option<Value>), FaucetError> {
524            Ok((self.records.clone(), Some(self.bookmark.clone())))
525        }
526    }
527
528    struct FailingSource;
529
530    #[async_trait]
531    impl Source for FailingSource {
532        async fn fetch_with_context(
533            &self,
534            _context: &std::collections::HashMap<String, Value>,
535        ) -> Result<Vec<Value>, FaucetError> {
536            Err(FaucetError::Auth("no credentials".into()))
537        }
538    }
539
540    // ── Mock Sink ───────────────────────────────────────────────────────────
541
542    struct MockSink {
543        written: std::sync::Mutex<Vec<Value>>,
544    }
545
546    impl MockSink {
547        fn new() -> Self {
548            Self {
549                written: std::sync::Mutex::new(Vec::new()),
550            }
551        }
552    }
553
554    #[async_trait]
555    impl Sink for MockSink {
556        async fn write_batch(&self, records: &[Value]) -> Result<usize, FaucetError> {
557            let mut w = self.written.lock().unwrap();
558            w.extend(records.iter().cloned());
559            Ok(records.len())
560        }
561    }
562
563    struct FailingSink;
564
565    #[async_trait]
566    impl Sink for FailingSink {
567        async fn write_batch(&self, _records: &[Value]) -> Result<usize, FaucetError> {
568            Err(FaucetError::Sink("write failed".into()))
569        }
570    }
571
572    // ── Source tests ────────────────────────────────────────────────────────
573
574    #[tokio::test]
575    async fn source_fetch_all_returns_records() {
576        let source = MockSource {
577            records: vec![json!({"id": 1}), json!({"id": 2})],
578        };
579        let records = source.fetch_all().await.unwrap();
580        assert_eq!(records.len(), 2);
581        assert_eq!(records[0]["id"], 1);
582    }
583
584    #[tokio::test]
585    async fn source_fetch_all_empty() {
586        let source = MockSource { records: vec![] };
587        let records = source.fetch_all().await.unwrap();
588        assert!(records.is_empty());
589    }
590
591    #[tokio::test]
592    async fn source_default_incremental_returns_none_bookmark() {
593        let source = MockSource {
594            records: vec![json!({"id": 1})],
595        };
596        let (records, bookmark) = source.fetch_all_incremental().await.unwrap();
597        assert_eq!(records.len(), 1);
598        assert!(bookmark.is_none());
599    }
600
601    #[tokio::test]
602    async fn source_custom_incremental_returns_bookmark() {
603        let source = IncrementalSource {
604            records: vec![json!({"id": 1})],
605            bookmark: json!("2024-12-01"),
606        };
607        let (records, bookmark) = source.fetch_all_incremental().await.unwrap();
608        assert_eq!(records.len(), 1);
609        assert_eq!(bookmark, Some(json!("2024-12-01")));
610    }
611
612    #[tokio::test]
613    async fn source_error_propagates() {
614        let source = FailingSource;
615        let result = source.fetch_all().await;
616        assert!(result.is_err());
617        assert!(matches!(result, Err(FaucetError::Auth(_))));
618    }
619
620    #[tokio::test]
621    async fn source_as_trait_object() {
622        let source: Box<dyn Source> = Box::new(MockSource {
623            records: vec![json!({"id": 42})],
624        });
625        let records = source.fetch_all().await.unwrap();
626        assert_eq!(records[0]["id"], 42);
627    }
628
629    // ── Sink tests ──────────────────────────────────────────────────────────
630
631    #[tokio::test]
632    async fn sink_write_batch_returns_count() {
633        let sink = MockSink::new();
634        let records = vec![json!({"id": 1}), json!({"id": 2}), json!({"id": 3})];
635        let count = sink.write_batch(&records).await.unwrap();
636        assert_eq!(count, 3);
637    }
638
639    #[tokio::test]
640    async fn sink_write_batch_empty() {
641        let sink = MockSink::new();
642        let count = sink.write_batch(&[]).await.unwrap();
643        assert_eq!(count, 0);
644    }
645
646    #[tokio::test]
647    async fn sink_accumulates_records() {
648        let sink = MockSink::new();
649        sink.write_batch(&[json!({"a": 1})]).await.unwrap();
650        sink.write_batch(&[json!({"b": 2})]).await.unwrap();
651        let written = sink.written.lock().unwrap();
652        assert_eq!(written.len(), 2);
653    }
654
655    #[tokio::test]
656    async fn sink_default_flush_is_noop() {
657        let sink = MockSink::new();
658        assert!(sink.flush().await.is_ok());
659    }
660
661    #[tokio::test]
662    async fn sink_error_propagates() {
663        let sink = FailingSink;
664        let result = sink.write_batch(&[json!({"id": 1})]).await;
665        assert!(result.is_err());
666        assert!(matches!(result, Err(FaucetError::Sink(_))));
667    }
668
669    #[tokio::test]
670    async fn sink_as_trait_object() {
671        let sink: Box<dyn Sink> = Box::new(MockSink::new());
672        let count = sink.write_batch(&[json!({"id": 1})]).await.unwrap();
673        assert_eq!(count, 1);
674    }
675
676    // ── stream_pages tests ──────────────────────────────────────────────────
677
678    use crate::pipeline::DEFAULT_BATCH_SIZE;
679    use futures::StreamExt;
680
681    #[tokio::test]
682    async fn default_stream_pages_chunks_records() {
683        let source = MockSource {
684            records: (0..5).map(|i| json!({"i": i})).collect(),
685        };
686        let ctx = std::collections::HashMap::new();
687        let mut pages = source.stream_pages(&ctx, 2);
688        let mut all = Vec::new();
689        while let Some(page) = pages.next().await {
690            all.push(page.unwrap());
691        }
692        // 5 records, batch_size=2 → pages of [2, 2, 1]
693        assert_eq!(all.len(), 3);
694        assert_eq!(all[0].records.len(), 2);
695        assert_eq!(all[1].records.len(), 2);
696        assert_eq!(all[2].records.len(), 1);
697    }
698
699    #[tokio::test]
700    async fn default_stream_pages_attaches_bookmark_to_final_page_only() {
701        let source = IncrementalSource {
702            records: (0..5).map(|i| json!({"i": i})).collect(),
703            bookmark: json!("v1"),
704        };
705        let ctx = std::collections::HashMap::new();
706        let mut pages = source.stream_pages(&ctx, 2);
707        let mut collected = Vec::new();
708        while let Some(page) = pages.next().await {
709            collected.push(page.unwrap());
710        }
711        assert_eq!(collected.len(), 3);
712        assert!(collected[0].bookmark.is_none());
713        assert!(collected[1].bookmark.is_none());
714        assert_eq!(collected[2].bookmark, Some(json!("v1")));
715    }
716
717    #[tokio::test]
718    async fn default_stream_pages_single_page_when_batch_size_exceeds_total() {
719        let source = MockSource {
720            records: vec![json!({"id": 1}), json!({"id": 2})],
721        };
722        let ctx = std::collections::HashMap::new();
723        let mut pages = source.stream_pages(&ctx, 100);
724        let mut collected = Vec::new();
725        while let Some(page) = pages.next().await {
726            collected.push(page.unwrap());
727        }
728        assert_eq!(collected.len(), 1);
729        assert_eq!(collected[0].records.len(), 2);
730    }
731
732    #[tokio::test]
733    async fn default_stream_pages_batch_size_zero_emits_single_page() {
734        // batch_size = 0 is the "no batching" sentinel — yields every record
735        // in one page regardless of total count.
736        let source = MockSource {
737            records: (0..50_000).map(|i| json!({"i": i})).collect(),
738        };
739        let ctx = std::collections::HashMap::new();
740        let mut pages = source.stream_pages(&ctx, 0);
741        let mut collected = Vec::new();
742        while let Some(page) = pages.next().await {
743            collected.push(page.unwrap());
744        }
745        assert_eq!(
746            collected.len(),
747            1,
748            "batch_size=0 must emit exactly one page"
749        );
750        assert_eq!(collected[0].records.len(), 50_000);
751    }
752
753    #[tokio::test]
754    async fn default_stream_pages_batch_size_zero_attaches_bookmark_to_sole_page() {
755        let source = IncrementalSource {
756            records: (0..3).map(|i| json!({"i": i})).collect(),
757            bookmark: json!("v1"),
758        };
759        let ctx = std::collections::HashMap::new();
760        let mut pages = source.stream_pages(&ctx, 0);
761        let page = pages.next().await.unwrap().unwrap();
762        assert_eq!(page.records.len(), 3);
763        assert_eq!(page.bookmark, Some(json!("v1")));
764        assert!(pages.next().await.is_none());
765    }
766
767    #[tokio::test]
768    async fn default_stream_pages_empty_source_yields_no_pages() {
769        let source = MockSource { records: vec![] };
770        let ctx = std::collections::HashMap::new();
771        let mut pages = source.stream_pages(&ctx, DEFAULT_BATCH_SIZE);
772        assert!(pages.next().await.is_none());
773    }
774
775    #[tokio::test]
776    async fn default_stream_pages_empty_source_with_bookmark_yields_single_empty_page() {
777        let source = IncrementalSource {
778            records: vec![],
779            bookmark: json!("v0"),
780        };
781        let ctx = std::collections::HashMap::new();
782        let mut pages = source.stream_pages(&ctx, DEFAULT_BATCH_SIZE);
783        let mut collected = Vec::new();
784        while let Some(page) = pages.next().await {
785            collected.push(page.unwrap());
786        }
787        // One empty-records page that carries the bookmark, so the pipeline
788        // still persists progress on otherwise-empty incremental runs.
789        assert_eq!(collected.len(), 1);
790        assert!(collected[0].records.is_empty());
791        assert_eq!(collected[0].bookmark, Some(json!("v0")));
792    }
793
794    #[tokio::test]
795    async fn default_stream_pages_propagates_fetch_errors() {
796        let source = FailingSource;
797        let ctx = std::collections::HashMap::new();
798        let mut pages = source.stream_pages(&ctx, DEFAULT_BATCH_SIZE);
799        let first = pages.next().await.unwrap();
800        assert!(matches!(first, Err(FaucetError::Auth(_))));
801    }
802
803    #[test]
804    fn source_default_connector_name_is_stripped_type_name() {
805        // MockSource lives at `faucet_core::traits::tests::MockSource`; the
806        // stripped type_name yields the trailing segment.
807        let source = MockSource { records: vec![] };
808        assert_eq!(source.connector_name(), "MockSource");
809    }
810
811    #[test]
812    fn sink_default_connector_name_is_stripped_type_name() {
813        let sink = MockSink::new();
814        assert_eq!(sink.connector_name(), "MockSink");
815    }
816
817    #[test]
818    fn source_default_dataset_uri_uses_connector_name() {
819        let source = MockSource { records: vec![] };
820        assert_eq!(source.dataset_uri(), "MockSource://unknown");
821    }
822
823    #[test]
824    fn sink_default_dataset_uri_uses_connector_name() {
825        let sink = MockSink::new();
826        assert_eq!(sink.dataset_uri(), "MockSink://unknown");
827    }
828
829    // ── write_batch_partial tests ───────────────────────────────────────────
830
831    #[tokio::test]
832    async fn default_write_batch_partial_success_returns_all_ok() {
833        let sink = MockSink::new();
834        let records = vec![json!({"id": 1}), json!({"id": 2}), json!({"id": 3})];
835        let outcomes = sink.write_batch_partial(&records).await.unwrap();
836        assert_eq!(outcomes.len(), 3);
837        assert!(outcomes.iter().all(|o| o.is_ok()));
838        assert_eq!(sink.written.lock().unwrap().len(), 3);
839    }
840
841    #[tokio::test]
842    async fn default_write_batch_partial_bubbles_outer_err() {
843        let sink = FailingSink;
844        let records = vec![json!({"id": 1}), json!({"id": 2})];
845        let result = sink.write_batch_partial(&records).await;
846        assert!(matches!(result, Err(FaucetError::Sink(_))));
847    }
848
849    #[tokio::test]
850    async fn default_write_batch_partial_empty_returns_empty_vec() {
851        let sink = MockSink::new();
852        let outcomes = sink.write_batch_partial(&[]).await.unwrap();
853        assert!(outcomes.is_empty());
854    }
855
856    #[tokio::test]
857    async fn default_write_batch_partial_callable_through_trait_object() {
858        let sink: Box<dyn Sink> = Box::new(MockSink::new());
859        let records = vec![json!({"id": 1}), json!({"id": 2})];
860        let outcomes = sink.write_batch_partial(&records).await.unwrap();
861        assert_eq!(outcomes.len(), 2);
862        assert!(outcomes.iter().all(|o| o.is_ok()));
863    }
864
865    // ── check() tests ─────────────────────────────────────────────────────────
866
867    #[tokio::test]
868    async fn source_default_check_pulls_first_page_and_passes() {
869        let source = MockSource {
870            records: vec![json!({"id": 1}), json!({"id": 2})],
871        };
872        let report = source
873            .check(&crate::check::CheckContext::default())
874            .await
875            .unwrap();
876        assert_eq!(report.failed_count(), 0);
877        assert!(
878            report
879                .probes
880                .iter()
881                .any(|p| p.name == "read" && matches!(p.status, crate::check::ProbeStatus::Pass))
882        );
883    }
884
885    #[tokio::test]
886    async fn source_default_check_passes_on_empty_source() {
887        let source = MockSource { records: vec![] };
888        let report = source
889            .check(&crate::check::CheckContext::default())
890            .await
891            .unwrap();
892        // Reachable but empty is still a healthy source.
893        assert_eq!(report.failed_count(), 0);
894    }
895
896    #[tokio::test]
897    async fn source_default_check_fails_when_fetch_errors() {
898        let source = FailingSource;
899        let report = source
900            .check(&crate::check::CheckContext::default())
901            .await
902            .unwrap();
903        assert_eq!(report.failed_count(), 1);
904        assert!(report.probes.iter().any(
905            |p| p.name == "read" && matches!(p.status, crate::check::ProbeStatus::Fail { .. })
906        ));
907    }
908
909    #[tokio::test]
910    async fn sink_default_check_is_not_implemented_skip() {
911        let sink = MockSink::new();
912        let report = sink
913            .check(&crate::check::CheckContext::default())
914            .await
915            .unwrap();
916        assert_eq!(report.probes.len(), 1);
917        assert!(matches!(
918            report.probes[0].status,
919            crate::check::ProbeStatus::Skip { .. }
920        ));
921    }
922
923    #[tokio::test]
924    async fn source_check_callable_through_trait_object() {
925        let source: Box<dyn Source> = Box::new(MockSource {
926            records: vec![json!({"id": 1})],
927        });
928        let report = source
929            .check(&crate::check::CheckContext::default())
930            .await
931            .unwrap();
932        assert_eq!(report.failed_count(), 0);
933    }
934
935    // ── idempotent-write / exactly-once capability tests ──────────────────────
936
937    #[tokio::test]
938    async fn sink_default_is_not_idempotent() {
939        let sink = MockSink::new();
940        assert!(!sink.supports_idempotent_writes());
941        // Default write_batch_idempotent ignores the token and delegates.
942        let n = sink
943            .write_batch_idempotent(&[json!({"id": 1})], "scope::a", "00000000000000000001")
944            .await
945            .unwrap();
946        assert_eq!(n, 1);
947        assert_eq!(sink.last_committed_token("scope::a").await.unwrap(), None);
948        assert_eq!(sink.written.lock().unwrap().len(), 1);
949    }
950
951    #[test]
952    fn source_default_does_not_support_exactly_once() {
953        let source = MockSource { records: vec![] };
954        assert!(!source.supports_exactly_once());
955    }
956
957    #[test]
958    fn sink_default_supported_write_modes_is_append_only() {
959        use crate::write_mode::WriteMode;
960        let sink = MockSink::new();
961        assert_eq!(sink.supported_write_modes(), &[WriteMode::Append]);
962    }
963
964    #[test]
965    fn supported_write_modes_callable_through_trait_object() {
966        use crate::write_mode::WriteMode;
967        let sink: Box<dyn Sink> = Box::new(MockSink::new());
968        assert!(sink.supported_write_modes().contains(&WriteMode::Append));
969    }
970
971    #[tokio::test]
972    async fn sink_default_current_schema_is_none() {
973        let sink = MockSink::new();
974        assert_eq!(sink.current_schema().await.unwrap(), None);
975    }
976
977    #[test]
978    fn sink_default_does_not_support_schema_evolution() {
979        let sink = MockSink::new();
980        assert!(!sink.supports_schema_evolution());
981    }
982
983    #[tokio::test]
984    async fn sink_default_evolve_schema_is_unsupported_error() {
985        let sink = MockSink::new();
986        let evo = crate::drift::SchemaEvolution::default();
987        let err = sink.evolve_schema(&evo).await.unwrap_err();
988        assert!(matches!(err, FaucetError::Sink(_)));
989        assert!(err.to_string().contains("schema evolution"));
990    }
991
992    #[tokio::test]
993    async fn source_default_capture_resume_position_is_none() {
994        let source = MockSource { records: vec![] };
995        assert_eq!(source.capture_resume_position().await.unwrap(), None);
996    }
997
998    #[tokio::test]
999    async fn capture_resume_position_callable_through_trait_object() {
1000        let source: Box<dyn Source> = Box::new(MockSource { records: vec![] });
1001        assert!(source.capture_resume_position().await.unwrap().is_none());
1002    }
1003
1004    #[tokio::test]
1005    async fn source_default_is_not_shardable() {
1006        let source: Box<dyn Source> = Box::new(MockSource { records: vec![] });
1007        assert!(!source.is_shardable());
1008    }
1009
1010    #[tokio::test]
1011    async fn source_default_enumerates_single_whole_shard() {
1012        // A non-shardable source enumerates to exactly one whole-dataset shard,
1013        // regardless of the requested target — preserving single-worker behavior.
1014        let source: Box<dyn Source> = Box::new(MockSource { records: vec![] });
1015        let shards = source.enumerate_shards(8).await.unwrap();
1016        assert_eq!(shards.len(), 1);
1017        assert!(shards[0].is_whole());
1018    }
1019
1020    #[tokio::test]
1021    async fn source_default_apply_shard_is_noop() {
1022        let source: Box<dyn Source> = Box::new(MockSource { records: vec![] });
1023        // Applying the whole shard is a no-op and must not error.
1024        source
1025            .apply_shard(&crate::shard::ShardSpec::whole())
1026            .await
1027            .unwrap();
1028    }
1029}