faucet-conformance 1.5.1

Reusable connector conformance test battery for the faucet-stream ecosystem
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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
//! Capability checks 15–19 (RFC 0013, #878): each proves a sink that declares a
//! capability behaves as declared. [`crate::required`] names which of them a
//! connector must run, and `scripts/conformance-coverage.py` enforces it.
//!
//! Every check writes rows keyed on `"id"` with a non-key `"v"` column, from an
//! id range no other check uses, so several checks can share one destination.
//! `distinct_count` reports the destination's current row count (distinct by
//! key for a keyed sink).

use faucet_core::{BatchAtomicity, Sink, Value};
use std::future::Future;

fn rows(ids: std::ops::RangeInclusive<i64>) -> Vec<Value> {
    ids.map(|i| serde_json::json!({ "id": i, "v": format!("v{i}") }))
        .collect()
}

async fn flush<S: Sink + ?Sized>(sink: &S, what: &str) {
    let label = sink.connector_name();
    sink.flush()
        .await
        .unwrap_or_else(|e| panic!("[{label}] flush after {what} errored: {e}"));
}

/// **Check 15.** A sink that reports per-row outcomes
/// ([`Sink::write_batch_partial`]) reports them truthfully: one outcome per
/// row offered, every row of a valid page `Ok`, and exactly the `Ok` rows
/// land in the destination. A sink that answers `Ok` for a row it dropped
/// would let the pipeline advance a bookmark past a lost row.
pub async fn assert_partial_outcomes_truthful<S, F, Fut>(sink: &S, distinct_count: F)
where
    S: Sink + ?Sized,
    F: Fn() -> Fut,
    Fut: Future<Output = usize>,
{
    let label = sink.connector_name();
    let page = rows(901..=905);
    let before = distinct_count().await;
    let outcomes = sink
        .write_batch_partial(&page)
        .await
        .unwrap_or_else(|e| panic!("[{label}] write_batch_partial of a valid page errored: {e}"));
    flush(sink, "write_batch_partial").await;
    assert_eq!(
        outcomes.len(),
        page.len(),
        "[{label}] write_batch_partial returned {} outcomes for {} rows — one per row offered",
        outcomes.len(),
        page.len()
    );
    let ok = outcomes.iter().filter(|o| o.is_ok()).count();
    let landed = distinct_count().await - before;
    assert_eq!(
        landed, ok,
        "[{label}] write_batch_partial reported {ok} rows Ok but {landed} landed — an Ok \
         outcome must mean the row is in the destination"
    );
    assert_eq!(
        ok,
        page.len(),
        "[{label}] a valid page reported {} failed rows",
        page.len() - ok
    );
}

/// **Check 16.** A sink's [`Sink::write_batch_reported`] accounts for every
/// row offered (`accepted + coalesced + skipped` equals the page) and its
/// `accepted` count is the number of rows that landed (#879).
pub async fn assert_reported_rows_truthful<S, F, Fut>(sink: &S, distinct_count: F)
where
    S: Sink + ?Sized,
    F: Fn() -> Fut,
    Fut: Future<Output = usize>,
{
    let label = sink.connector_name();
    let page = rows(911..=915);
    let before = distinct_count().await;
    let report = sink
        .write_batch_reported(&page)
        .await
        .unwrap_or_else(|e| panic!("[{label}] write_batch_reported of a valid page errored: {e}"));
    flush(sink, "write_batch_reported").await;
    let accounted = report.accepted + report.coalesced + report.skipped;
    assert_eq!(
        accounted,
        page.len(),
        "[{label}] write_batch_reported accounts for {accounted} of {} rows offered ({report:?})",
        page.len()
    );
    let landed = distinct_count().await - before;
    assert_eq!(
        report.accepted, landed,
        "[{label}] write_batch_reported says {} rows were accepted but {landed} landed",
        report.accepted
    );
}

/// **Check 17.** A sink whose [`Sink::write_batch_is_replay_safe`] is `true`
/// converges when the same page is written twice — the resilience layer
/// retries such a write after a lost response, so a duplicate here is a
/// duplicate in production. Configure the sink as it declares replay safety
/// (for most sinks: `write_mode: upsert` with `key: ["id"]`).
pub async fn assert_replay_safe<S, F, Fut>(sink: &S, distinct_count: F)
where
    S: Sink + ?Sized,
    F: Fn() -> Fut,
    Fut: Future<Output = usize>,
{
    let label = sink.connector_name();
    assert!(
        sink.write_batch_is_replay_safe(),
        "[{label}] declares its plain write replay-unsafe — configure it as it declares \
         replay safety (e.g. a keyed upsert) before calling this check"
    );
    let page = rows(921..=923);
    let before = distinct_count().await;
    for attempt in ["first write", "replayed write"] {
        sink.write_batch(&page)
            .await
            .unwrap_or_else(|e| panic!("[{label}] {attempt} errored: {e}"));
        flush(sink, attempt).await;
    }
    let landed = distinct_count().await - before;
    assert_eq!(
        landed,
        page.len(),
        "[{label}] replaying a page duplicated rows: {landed} landed for {} distinct keys — \
         the sink is not replay-safe",
        page.len()
    );
}

/// **Check 18.** A sink configured for `write_mode: overwrite` keeps the
/// begin → stage → commit lifecycle: staged rows are invisible until
/// [`commit_overwrite`](Sink::commit_overwrite), an
/// [`abort_overwrite`](Sink::abort_overwrite) leaves the destination as it
/// was, and a commit replaces the destination with exactly the run's rows.
///
/// Seed the destination with a few rows first so "untouched" is observable.
#[allow(deprecated)] // drives the 1.x lifecycle hook until faucet-core 2.0 (RFC 0014)
pub async fn assert_overwrite_lifecycle<S, F, Fut>(sink: &S, distinct_count: F)
where
    S: Sink + ?Sized,
    F: Fn() -> Fut,
    Fut: Future<Output = usize>,
{
    let label = sink.connector_name();
    assert!(
        sink.is_overwrite(),
        "[{label}] is not configured for write_mode: overwrite — nothing to verify"
    );
    let before = distinct_count().await;

    sink.begin_overwrite()
        .await
        .unwrap_or_else(|e| panic!("[{label}] begin_overwrite (aborted run) errored: {e}"));
    sink.write_batch(&rows(931..=933))
        .await
        .unwrap_or_else(|e| panic!("[{label}] write in the aborted run errored: {e}"));
    flush(sink, "the aborted run's write").await;
    assert_eq!(
        distinct_count().await,
        before,
        "[{label}] staged rows are visible in the destination before commit"
    );
    sink.abort_overwrite()
        .await
        .unwrap_or_else(|e| panic!("[{label}] abort_overwrite errored: {e}"));
    assert_eq!(
        distinct_count().await,
        before,
        "[{label}] abort_overwrite changed the destination — a failed run must leave it as it was"
    );

    sink.begin_overwrite()
        .await
        .unwrap_or_else(|e| panic!("[{label}] begin_overwrite (committed run) errored: {e}"));
    let page = rows(941..=942);
    sink.write_batch(&page)
        .await
        .unwrap_or_else(|e| panic!("[{label}] write in the committed run errored: {e}"));
    flush(sink, "the committed run's write").await;
    assert_eq!(
        distinct_count().await,
        before,
        "[{label}] staged rows are visible in the destination before commit"
    );
    sink.commit_overwrite()
        .await
        .unwrap_or_else(|e| panic!("[{label}] commit_overwrite errored: {e}"));
    assert_eq!(
        distinct_count().await,
        page.len(),
        "[{label}] commit_overwrite did not replace the destination with exactly the run's rows"
    );
}

/// **Check 19.** A sink's declared [`BatchAtomicity`] holds when a write
/// fails. `poison_page` must hold at least one row the sink accepts and at
/// least one it rejects (a missing required column, a value of the wrong
/// type).
///
/// - `Atomic`: the write fails and no row of the page lands.
/// - `PerRow`: an outer `Err` lands no row; otherwise the poison row is
///   reported `Err` and exactly the `Ok` rows land.
/// - `BestEffort` declares nothing to verify, so the check fails rather than
///   pass vacuously: configure the sink as it declares atomicity.
pub async fn assert_batch_atomicity_truthful<S, F, Fut>(
    sink: &S,
    distinct_count: F,
    poison_page: &[Value],
) where
    S: Sink + ?Sized,
    F: Fn() -> Fut,
    Fut: Future<Output = usize>,
{
    let label = sink.connector_name();
    let declared = crate::assert_batch_atomicity_declared(sink);
    let before = distinct_count().await;
    match declared {
        BatchAtomicity::Atomic => {
            // A buffering sink reports the failure when it flushes.
            let outcome = match sink.write_batch(poison_page).await {
                Ok(_) => sink.flush().await,
                Err(e) => {
                    let _ = sink.flush().await;
                    Err(e)
                }
            };
            assert!(
                outcome.is_err(),
                "[{label}] the poison page was written without error — it must hold a row \
                 the sink rejects"
            );
            let landed = distinct_count().await - before;
            assert_eq!(
                landed, 0,
                "[{label}] declares atomic batches but {landed} rows of a failed write landed"
            );
        }
        BatchAtomicity::PerRow => match sink.write_batch_partial(poison_page).await {
            Err(_) => {
                let _ = sink.flush().await;
                let landed = distinct_count().await - before;
                assert_eq!(
                    landed, 0,
                    "[{label}] declares per-row outcomes but an outer Err left {landed} rows \
                     behind — an outer Err must mean no row committed"
                );
            }
            Ok(outcomes) => {
                flush(sink, "the poison page").await;
                assert_eq!(
                    outcomes.len(),
                    poison_page.len(),
                    "[{label}] write_batch_partial returned {} outcomes for {} rows",
                    outcomes.len(),
                    poison_page.len()
                );
                let ok = outcomes.iter().filter(|o| o.is_ok()).count();
                assert!(
                    ok < poison_page.len(),
                    "[{label}] every row of the poison page was reported Ok — it must hold a \
                     row the sink rejects"
                );
                let landed = distinct_count().await - before;
                assert_eq!(
                    landed, ok,
                    "[{label}] reported {ok} rows Ok but {landed} landed"
                );
            }
        },
        _ => panic!(
            "[{label}] declares {declared} batches — nothing to verify; configure the sink as \
             it declares atomic or per-row batches"
        ),
    }
}

/// **Check 20 (RFC 0014).** A sink's declared capabilities match the
/// lifecycle hooks that answer: every capability it does not declare —
/// overwrite, exactly-once, cleanup, schema evolution, rollback — is refused by
/// its hook rather than silently accepted, the non-destructive hooks of the
/// ones it does declare answer, and keyed dedup comes with a keyed write mode.
/// A sink built on a family driver ([`faucet_core::driver`]) passes by
/// construction; the check guards that construction for every connector.
#[allow(deprecated)] // probes the lifecycle hooks the family drivers replace
pub async fn assert_driver_declarations_truthful<S: Sink + ?Sized>(sink: &S) {
    use faucet_core::WriteMode;
    let label = sink.connector_name();
    let caps = sink.capabilities();
    let modes = sink.supported_write_modes();
    assert_eq!(
        caps.overwrite(),
        modes.contains(&WriteMode::Overwrite),
        "[{label}] the overwrite capability disagrees with supported_write_modes"
    );
    if caps.keyed_dedup() {
        assert!(
            modes
                .iter()
                .any(|m| matches!(m, WriteMode::Upsert | WriteMode::Delete)),
            "[{label}] dedups by key but declares no keyed write mode"
        );
    }
    let scope = "conformance::driver";
    if caps.overwrite() {
        assert!(
            sink.overwrite_staging_exists().await.is_ok(),
            "[{label}] declares overwrite but its staging probe errored"
        );
    } else {
        assert!(
            !sink.is_overwrite() && sink.begin_overwrite().await.is_err(),
            "[{label}] does not declare overwrite but begin_overwrite answered"
        );
    }
    let token = sink.last_committed_token(scope).await;
    if caps.idempotent_writes() {
        assert!(
            token.is_ok(),
            "[{label}] declares exactly-once but its watermark read errored: {:?}",
            token.err()
        );
    } else {
        assert!(
            matches!(token, Ok(None)),
            "[{label}] does not declare exactly-once but reported a watermark"
        );
    }
    if !caps.cleanup() {
        assert!(
            sink.cleanup_scope(
                &std::collections::BTreeMap::new(),
                &faucet_core::SeenKeys::new()
            )
            .await
            .is_err(),
            "[{label}] does not declare cleanup but cleanup_scope answered"
        );
    }
    let evolved = sink
        .evolve_schema(&faucet_core::SchemaEvolution::default())
        .await;
    assert_eq!(
        evolved.is_ok(),
        caps.schema_evolution(),
        "[{label}] schema evolution is declared {} but an empty evolution {}",
        caps.schema_evolution(),
        if evolved.is_ok() {
            "succeeded"
        } else {
            "failed"
        }
    );
    if !caps.rollback() {
        let opts = faucet_core::rollback::RollbackOptions {
            run_id_column: "_faucet_run_id".into(),
            mode: faucet_core::rollback::RollbackMode::Append,
            force: false,
            dry_run: true,
            later_runs: false,
        };
        assert!(
            sink.rollback_run("conformance", &opts).await.is_err(),
            "[{label}] does not declare rollback but rollback_run answered"
        );
    }
}

/// **Check 21 (RFC 0014).** The four ways the engine writes a valid page —
/// plain, reported, per-row and governed — agree: each accepts every row, the
/// report accounts for the page, and every per-row outcome is `Ok`. A family
/// driver serves all four from one write primitive; this proves the
/// connector's primitive behaves the same under each.
pub async fn assert_driver_row_paths_agree<S: Sink + ?Sized>(sink: &S) {
    let label = sink.connector_name();
    let n = sink
        .write_batch(&rows(941..=943))
        .await
        .unwrap_or_else(|e| panic!("[{label}] plain write errored: {e}"));
    assert_eq!(n, 3, "[{label}] plain write accepted {n} of 3 rows");
    let report = sink
        .write_batch_reported(&rows(944..=946))
        .await
        .unwrap_or_else(|e| panic!("[{label}] reported write errored: {e}"));
    assert!(
        report.check(3).is_ok(),
        "[{label}] reported write left rows unaccounted: {report:?}"
    );
    let outcomes = sink
        .write_batch_partial(&rows(947..=949))
        .await
        .unwrap_or_else(|e| panic!("[{label}] per-row write errored: {e}"));
    assert_eq!(outcomes.len(), 3, "[{label}] one outcome per row");
    assert!(
        outcomes.iter().all(Result::is_ok),
        "[{label}] a valid row failed on the per-row path: {outcomes:?}"
    );
    let governed = faucet_core::governance::Governance::new()
        .govern(rows(950..=952), sink)
        .await
        .unwrap_or_else(|e| panic!("[{label}] governing a page errored: {e}"));
    let report = sink
        .write_governed(&governed.page)
        .await
        .unwrap_or_else(|e| panic!("[{label}] governed write errored: {e}"));
    assert!(
        report.check(3).is_ok(),
        "[{label}] governed write left rows unaccounted: {report:?}"
    );
    flush(sink, "the row-path writes").await;
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::doubles::{
        AtomicitySink, LyingKeyedSink, MisreportingSink, OverwriteSink, PartialOutcomeSink,
        TestSink,
    };
    use serde_json::json;

    fn counter<T: Clone + Send + 'static>(
        sink: &T,
        len: fn(&T) -> usize,
    ) -> impl Fn() -> std::future::Ready<usize> {
        let s = sink.clone();
        move || std::future::ready(len(&s))
    }

    fn poison() -> Vec<Value> {
        vec![json!({ "id": 1, "v": "a" }), json!({ "v": "no id" })]
    }

    #[tokio::test]
    async fn check15_passes_for_an_honest_per_row_sink() {
        let sink = PartialOutcomeSink::honest();
        assert_partial_outcomes_truthful(&sink, counter(&sink, PartialOutcomeSink::len)).await;
        assert_eq!(sink.len(), 5);
    }

    #[tokio::test]
    async fn check15_passes_for_the_default_impl() {
        let sink = TestSink::new();
        assert_partial_outcomes_truthful(&sink, counter(&sink, TestSink::len)).await;
    }

    #[tokio::test]
    #[should_panic(expected = "an Ok outcome must mean the row is in the destination")]
    async fn check15_fails_when_ok_rows_are_dropped() {
        let sink = PartialOutcomeSink::drops_silently();
        assert_partial_outcomes_truthful(&sink, counter(&sink, PartialOutcomeSink::len)).await;
    }

    #[tokio::test]
    #[should_panic(expected = "one per row offered")]
    async fn check15_fails_on_a_short_outcome_vector() {
        let sink = PartialOutcomeSink::short();
        assert_partial_outcomes_truthful(&sink, counter(&sink, PartialOutcomeSink::len)).await;
    }

    #[tokio::test]
    #[should_panic(expected = "a valid page reported")]
    async fn check15_fails_when_a_valid_page_reports_failures() {
        let sink = PartialOutcomeSink::rejects_all();
        assert_partial_outcomes_truthful(&sink, counter(&sink, PartialOutcomeSink::len)).await;
    }

    #[tokio::test]
    async fn check16_passes_for_the_default_report() {
        let sink = TestSink::new();
        assert_reported_rows_truthful(&sink, counter(&sink, TestSink::len)).await;
    }

    #[tokio::test]
    async fn check16_passes_for_an_honest_skipping_sink() {
        let sink = MisreportingSink::honest_skip();
        assert_reported_rows_truthful(&sink, counter(&sink, MisreportingSink::len)).await;
    }

    #[tokio::test]
    #[should_panic(expected = "rows were accepted but")]
    async fn check16_fails_when_accepted_overstates_what_landed() {
        let sink = MisreportingSink::over_reports();
        assert_reported_rows_truthful(&sink, counter(&sink, MisreportingSink::len)).await;
    }

    #[tokio::test]
    #[should_panic(expected = "accounts for 4 of 5 rows offered")]
    async fn check16_fails_when_rows_go_unaccounted() {
        let sink = MisreportingSink::under_reports();
        assert_reported_rows_truthful(&sink, counter(&sink, MisreportingSink::len)).await;
    }

    #[tokio::test]
    async fn check17_passes_for_a_keyed_sink() {
        let sink = TestSink::keyed("id");
        assert_replay_safe(&sink, counter(&sink, TestSink::len)).await;
    }

    #[tokio::test]
    #[should_panic(expected = "replaying a page duplicated rows")]
    async fn check17_fails_for_a_sink_that_lies_about_dedup() {
        let sink = LyingKeyedSink::new();
        assert_replay_safe(&sink, counter(&sink, LyingKeyedSink::len)).await;
    }

    #[tokio::test]
    #[should_panic(expected = "declares its plain write replay-unsafe")]
    async fn check17_fails_for_an_append_sink() {
        let sink = TestSink::new();
        assert_replay_safe(&sink, counter(&sink, TestSink::len)).await;
    }

    #[tokio::test]
    async fn check18_passes_for_an_honest_overwrite_sink() {
        let sink = OverwriteSink::honest();
        sink.seed(4);
        assert_overwrite_lifecycle(&sink, counter(&sink, OverwriteSink::len)).await;
        assert_eq!(sink.len(), 2);
    }

    #[tokio::test]
    #[should_panic(expected = "visible in the destination before commit")]
    async fn check18_fails_when_writes_bypass_staging() {
        let sink = OverwriteSink::eager();
        sink.seed(4);
        assert_overwrite_lifecycle(&sink, counter(&sink, OverwriteSink::len)).await;
    }

    #[tokio::test]
    #[should_panic(expected = "abort_overwrite changed the destination")]
    async fn check18_fails_when_abort_truncates() {
        let sink = OverwriteSink::truncating_abort();
        sink.seed(4);
        assert_overwrite_lifecycle(&sink, counter(&sink, OverwriteSink::len)).await;
    }

    #[tokio::test]
    #[should_panic(expected = "did not replace the destination")]
    async fn check18_fails_when_commit_appends() {
        let sink = OverwriteSink::appending_commit();
        sink.seed(4);
        assert_overwrite_lifecycle(&sink, counter(&sink, OverwriteSink::len)).await;
    }

    #[tokio::test]
    #[should_panic(expected = "is not configured for write_mode: overwrite")]
    async fn check18_fails_for_a_sink_not_in_overwrite_mode() {
        let sink = TestSink::new();
        assert_overwrite_lifecycle(&sink, counter(&sink, TestSink::len)).await;
    }

    #[tokio::test]
    async fn check19_passes_for_an_atomic_sink() {
        let sink = AtomicitySink::atomic();
        assert_batch_atomicity_truthful(&sink, counter(&sink, AtomicitySink::len), &poison()).await;
    }

    #[tokio::test]
    #[should_panic(expected = "declares atomic batches but 1 rows of a failed write landed")]
    async fn check19_fails_for_a_torn_atomic_sink() {
        let sink = AtomicitySink::torn();
        assert_batch_atomicity_truthful(&sink, counter(&sink, AtomicitySink::len), &poison()).await;
    }

    #[tokio::test]
    #[should_panic(expected = "the poison page was written without error")]
    async fn check19_fails_for_a_harmless_poison_page() {
        let sink = AtomicitySink::atomic();
        let page = vec![json!({ "id": 1, "v": "a" })];
        assert_batch_atomicity_truthful(&sink, counter(&sink, AtomicitySink::len), &page).await;
    }

    #[tokio::test]
    async fn check19_passes_for_a_per_row_sink() {
        let sink = AtomicitySink::per_row();
        assert_batch_atomicity_truthful(&sink, counter(&sink, AtomicitySink::len), &poison()).await;
        assert_eq!(sink.len(), 1);
    }

    #[tokio::test]
    async fn check19_passes_for_a_per_row_sink_failing_the_whole_call() {
        let sink = AtomicitySink::per_row_outer_err();
        assert_batch_atomicity_truthful(&sink, counter(&sink, AtomicitySink::len), &poison()).await;
    }

    #[tokio::test]
    #[should_panic(expected = "an outer Err left 1 rows behind")]
    async fn check19_fails_for_a_per_row_sink_whose_outer_err_committed() {
        let sink = AtomicitySink::per_row_torn();
        assert_batch_atomicity_truthful(&sink, counter(&sink, AtomicitySink::len), &poison()).await;
    }

    #[tokio::test]
    #[should_panic(expected = "every row of the poison page was reported Ok")]
    async fn check19_fails_for_a_harmless_per_row_poison_page() {
        let sink = AtomicitySink::per_row();
        let page = vec![json!({ "id": 1, "v": "a" })];
        assert_batch_atomicity_truthful(&sink, counter(&sink, AtomicitySink::len), &page).await;
    }

    #[tokio::test]
    #[should_panic(expected = "declares best_effort batches")]
    async fn check19_fails_for_a_best_effort_sink() {
        let sink = TestSink::new();
        assert_batch_atomicity_truthful(&sink, counter(&sink, TestSink::len), &poison()).await;
    }

    /// An append-only driver: every lifecycle it lacks is refused by the adapter.
    #[derive(Default)]
    struct Mem(std::sync::Mutex<Vec<Value>>);

    #[faucet_core::async_trait]
    impl faucet_core::driver::SinkDriver for Mem {
        fn connector_name(&self) -> &'static str {
            "mem"
        }
        async fn write(
            &self,
            _: &faucet_core::CallSlot,
            rows: &[Value],
            _: faucet_core::driver::RowPolicy,
        ) -> Result<faucet_core::driver::PageWrite, faucet_core::FaucetError> {
            self.0.lock().unwrap().extend_from_slice(rows);
            Ok(faucet_core::driver::PageWrite::accepted(rows.len()))
        }
    }

    #[tokio::test]
    async fn check20_and_21_pass_for_a_driver_sink() {
        let sink = faucet_core::driver::DriverSink::new(Mem::default());
        assert_driver_declarations_truthful(&sink).await;
        assert_driver_row_paths_agree(&sink).await;
        assert_eq!(sink.driver().0.lock().unwrap().len(), 12);
    }

    /// A hand-written sink whose hooks answer what its capabilities deny.
    #[derive(Default)]
    struct Liar {
        lie: &'static str,
    }

    #[faucet_core::async_trait]
    #[allow(deprecated)]
    impl Sink for Liar {
        async fn write_batch(&self, records: &[Value]) -> Result<usize, faucet_core::FaucetError> {
            Ok(match self.lie {
                "short_plain" => records.len() - 1,
                _ => records.len(),
            })
        }
        async fn write_batch_partial(
            &self,
            records: &[Value],
        ) -> Result<Vec<faucet_core::RowOutcome>, faucet_core::FaucetError> {
            Ok(records
                .iter()
                .map(|_| match self.lie {
                    "bad_row" => Err(faucet_core::FaucetError::Sink("x".into())),
                    _ => Ok(()),
                })
                .collect())
        }
        async fn write_governed(
            &self,
            page: &faucet_core::governance::GovernedPage<'_>,
        ) -> Result<faucet_core::WriteReport, faucet_core::FaucetError> {
            Ok(faucet_core::WriteReport::accepted(match self.lie {
                "short_governed" => 0,
                _ => page.len(),
            }))
        }
        async fn write_batch_reported(
            &self,
            records: &[Value],
        ) -> Result<faucet_core::WriteReport, faucet_core::FaucetError> {
            Ok(faucet_core::WriteReport::accepted(match self.lie {
                "short_reported" => 1,
                _ => records.len(),
            }))
        }
        fn dedups_by_key(&self) -> bool {
            self.lie == "dedup"
        }
        fn supported_write_modes(&self) -> &'static [faucet_core::WriteMode] {
            match self.lie {
                "modes" => &[
                    faucet_core::WriteMode::Append,
                    faucet_core::WriteMode::Overwrite,
                ],
                _ => &[faucet_core::WriteMode::Append],
            }
        }
        fn capabilities(&self) -> faucet_core::Capabilities {
            let base = faucet_core::Capabilities::of_sink(self);
            match self.lie {
                "modes" => base
                    .to_builder()
                    .write_modes([faucet_core::WriteMode::Append])
                    .build(),
                "idem_broken" => base.to_builder().idempotent_writes(true).build(),
                "evolve_broken" => base.to_builder().schema_evolution(true).build(),
                _ => base,
            }
        }
        async fn begin_overwrite(&self) -> Result<(), faucet_core::FaucetError> {
            if self.lie == "overwrite" {
                Ok(())
            } else {
                Err(faucet_core::FaucetError::Sink("no".into()))
            }
        }
        async fn last_committed_token(
            &self,
            _: &str,
        ) -> Result<Option<String>, faucet_core::FaucetError> {
            match self.lie {
                "token" => Ok(Some("t".into())),
                "idem_broken" => Err(faucet_core::FaucetError::Sink("down".into())),
                _ => Ok(None),
            }
        }
        async fn cleanup_scope(
            &self,
            _: &std::collections::BTreeMap<String, Value>,
            _: &faucet_core::SeenKeys,
        ) -> Result<u64, faucet_core::FaucetError> {
            if self.lie == "cleanup" {
                Ok(0)
            } else {
                Err(faucet_core::FaucetError::Sink("no".into()))
            }
        }
        async fn evolve_schema(
            &self,
            _: &faucet_core::SchemaEvolution,
        ) -> Result<(), faucet_core::FaucetError> {
            if self.lie == "evolve" {
                Ok(())
            } else {
                Err(faucet_core::FaucetError::Sink("no".into()))
            }
        }
        async fn rollback_run(
            &self,
            _: &str,
            _: &faucet_core::rollback::RollbackOptions,
        ) -> Result<faucet_core::rollback::RollbackOutcome, faucet_core::FaucetError> {
            if self.lie == "rollback" {
                Ok(Default::default())
            } else {
                Err(faucet_core::FaucetError::Sink("no".into()))
            }
        }
    }

    #[tokio::test]
    async fn check20_passes_for_an_honest_hand_written_sink() {
        assert_driver_declarations_truthful(&Liar::default()).await;
        assert_driver_row_paths_agree(&Liar::default()).await;
    }

    macro_rules! lies {
        ($($name:ident: $lie:literal => $msg:literal;)*) => {$(
            #[tokio::test]
            #[should_panic(expected = $msg)]
            async fn $name() {
                let sink = Liar { lie: $lie };
                assert_driver_declarations_truthful(&sink).await;
                assert_driver_row_paths_agree(&sink).await;
            }
        )*};
    }

    lies! {
        check20_fails_on_disagreeing_modes: "modes" => "disagrees with supported_write_modes";
        check20_fails_on_dedup_without_a_keyed_mode: "dedup" => "declares no keyed write mode";
        check20_fails_on_an_undeclared_overwrite: "overwrite" => "begin_overwrite answered";
        check20_fails_on_an_undeclared_watermark: "token" => "reported a watermark";
        check20_fails_on_a_broken_declared_watermark: "idem_broken" => "watermark read errored";
        check20_fails_on_an_undeclared_cleanup: "cleanup" => "cleanup_scope answered";
        check20_fails_on_an_undeclared_evolution: "evolve" => "an empty evolution succeeded";
        check20_fails_on_a_broken_declared_evolution: "evolve_broken" => "an empty evolution failed";
        check20_fails_on_an_undeclared_rollback: "rollback" => "rollback_run answered";
        check21_fails_on_a_short_plain_write: "short_plain" => "plain write accepted 2 of 3";
        check21_fails_on_a_short_report: "short_reported" => "reported write left rows";
        check21_fails_on_a_failed_valid_row: "bad_row" => "a valid row failed";
        check21_fails_on_a_short_governed_write: "short_governed" => "governed write left rows";
    }

    /// A driver with staging, a watermark and an evolvable schema.
    #[derive(Default)]
    struct Lifecycle(Mem);

    #[faucet_core::async_trait]
    impl faucet_core::driver::SinkDriver for Lifecycle {
        fn connector_name(&self) -> &'static str {
            "lifecycle"
        }
        async fn write(
            &self,
            calls: &faucet_core::CallSlot,
            rows: &[Value],
            policy: faucet_core::driver::RowPolicy,
        ) -> Result<faucet_core::driver::PageWrite, faucet_core::FaucetError> {
            self.0.write(calls, rows, policy).await
        }
        fn write_modes(&self) -> &'static [faucet_core::WriteMode] {
            &[
                faucet_core::WriteMode::Append,
                faucet_core::WriteMode::Overwrite,
            ]
        }
        fn staging(&self) -> Option<&dyn faucet_core::driver::StagingDriver> {
            Some(self)
        }
        fn commit_tokens(&self) -> Option<&dyn faucet_core::driver::CommitTokenDriver> {
            Some(self)
        }
        fn schema(&self) -> Option<&dyn faucet_core::driver::SchemaDriver> {
            Some(self)
        }
    }

    #[faucet_core::async_trait]
    impl faucet_core::driver::StagingDriver for Lifecycle {
        fn active(&self) -> bool {
            false
        }
        async fn stage(&self, _: &faucet_core::CallSlot) -> Result<(), faucet_core::FaucetError> {
            Ok(())
        }
        async fn publish(&self, _: &faucet_core::CallSlot) -> Result<(), faucet_core::FaucetError> {
            Ok(())
        }
        async fn discard(&self, _: &faucet_core::CallSlot) -> Result<(), faucet_core::FaucetError> {
            Ok(())
        }
        async fn staged(
            &self,
            _: &faucet_core::CallSlot,
        ) -> Result<bool, faucet_core::FaucetError> {
            Ok(false)
        }
    }

    #[faucet_core::async_trait]
    impl faucet_core::driver::CommitTokenDriver for Lifecycle {
        async fn last_token(
            &self,
            _: &faucet_core::CallSlot,
            _: &str,
        ) -> Result<Option<String>, faucet_core::FaucetError> {
            Ok(None)
        }
        async fn write_with_token(
            &self,
            _: &faucet_core::CallSlot,
            rows: &[Value],
            _: &str,
            _: &str,
        ) -> Result<faucet_core::WriteReport, faucet_core::FaucetError> {
            Ok(faucet_core::WriteReport::accepted(rows.len()))
        }
    }

    #[faucet_core::async_trait]
    impl faucet_core::driver::SchemaDriver for Lifecycle {
        async fn current_schema(
            &self,
            _: &faucet_core::CallSlot,
        ) -> Result<Option<Value>, faucet_core::FaucetError> {
            Ok(None)
        }
        fn evolvable(&self) -> bool {
            true
        }
        async fn evolve(
            &self,
            _: &faucet_core::CallSlot,
            _: &faucet_core::SchemaEvolution,
        ) -> Result<(), faucet_core::FaucetError> {
            Ok(())
        }
    }

    #[tokio::test]
    async fn check20_probes_a_declared_overwrite_watermark_and_evolution() {
        let sink = faucet_core::driver::DriverSink::new(Lifecycle::default());
        assert_driver_declarations_truthful(&sink).await;
        assert_driver_row_paths_agree(&sink).await;
        #[allow(deprecated)] // the lifecycle hooks the double implements
        {
            sink.begin_overwrite().await.unwrap();
            sink.commit_overwrite().await.unwrap();
            sink.abort_overwrite().await.unwrap();
            let r = sink
                .write_batch_idempotent_reported(&rows(1..=2), "s", "t")
                .await
                .unwrap();
            assert_eq!(r.accepted, 2);
        }
    }
}