architecturekit 0.10.0

Building blocks for DDD-based applications with CQRS and event sourcing, on top of the EventSourcingDB by the native web.
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
907
908
909
910
911
912
913
// These tests mirror the parts of ArchitectureKit for Go that need no Execute:
// TestDatabaseFailures and TestDatabaseFailuresOfTheV2API through load and
// write rather than through Execute, the tests of Write, of RegisterSchemas, of
// the verification of signatures, of FromLatest and Ignore with a database,
// and "agrees with the database on every character" of TestSubjectScheme. The
// tests of Execute, its conflicts and its retries, of failed writes, which
// write and execute share, and of the state cache are in the modules execute,
// conflict_retries, failed_writes and state_cache.
//
// The answers of the database come either from a real EventSourcingDB in a
// container of each test's own (see database), or from a fake that answers the
// routes a test gives it as the test tells it (see fake), since the client SDK
// does not let a test make the error of an answer itself.

mod commands;
mod conflict_retries;
mod database;
mod execute;
mod failed_writes;
mod fake;
mod projections;
mod read;
mod state_cache;

use std::{collections::HashMap, error::Error};

use eventsourcingdb::error::{ClientError, code};
use serde::{Deserialize, Serialize};

use crate::{
    Categorized, Category, Event, EventOn, Events, LoadError, Permanent, Precondition,
    RegisterSchemasError, State, Store, SubjectScheme, Transient, WriteError,
};

use database::{Database, subject_for};
use fake::{Api, Reply, refusing};

#[derive(Debug, Serialize, Deserialize, Event)]
#[event(type = "io.thenativeweb.test.incremented")]
struct Incremented {
    by: i64,
}

#[derive(Debug, Serialize, Deserialize, Event)]
#[event(type = "io.thenativeweb.test.reset")]
struct Reset {}

#[derive(Debug, Serialize, Deserialize, Event)]
#[event(type = "io.thenativeweb.test.annotated")]
struct Annotated {
    note: String,
}

/// An event of a type the counter has no variant for.
#[derive(Debug, Serialize, Deserialize, Event)]
#[event(type = "io.thenativeweb.test.unrelated")]
struct Unrelated {}

#[derive(Debug, Events)]
enum CounterEvent {
    Incremented(Incremented),
    Reset(Reset),
    Annotated(Annotated),
}

#[derive(Clone, Debug, Default, PartialEq)]
struct Counter {
    total: i64,
}

impl State for Counter {
    type Event = CounterEvent;

    fn evolve(self, event: &CounterEvent) -> Self {
        match event {
            CounterEvent::Incremented(incremented) => Counter {
                total: self.total + incremented.by,
            },
            CounterEvent::Reset(_) => Counter { total: 0 },
            CounterEvent::Annotated(_) => self,
        }
    }
}

#[derive(Clone, Debug, Default, PartialEq)]
struct CounterFromLatest(Counter);

impl State for CounterFromLatest {
    type Event = CounterEvent;

    fn evolve(self, event: &CounterEvent) -> Self {
        CounterFromLatest(self.0.evolve(event))
    }

    fn from_latest() -> Option<&'static str> {
        Some(Reset::EVENT_TYPE)
    }
}

async fn write_unconditionally(store: &Store, events: Vec<EventOn>) {
    let _ = store
        .write(events, vec![Precondition::unconditionally()])
        .await
        .expect("the events are written");
}

// Loading.

#[tokio::test]
async fn loads_the_state_of_a_subject_from_its_events() {
    let database = Database::start().await;
    let store = database.store().await;
    let subject = subject_for("loads");

    write_unconditionally(
        &store,
        vec![
            EventOn::new(&subject, &Incremented { by: 3 }),
            EventOn::new(&subject, &Incremented { by: 4 }),
        ],
    )
    .await;

    let counter: Counter = store.load(&subject).await.expect("the state loads");
    assert_eq!(counter.total, 7);
}

#[tokio::test]
async fn loads_a_pristine_subject_as_the_default_state() {
    let counter: Counter = Database::start()
        .await
        .store()
        .await
        .load(&subject_for("pristine"))
        .await
        .expect("the state loads");

    assert_eq!(counter, Counter::default());
}

#[tokio::test]
async fn loads_a_subject_with_an_ignored_event() {
    let database = Database::start().await;
    let store = database.store().await;
    let subject = subject_for("ignored");

    write_unconditionally(
        &store,
        vec![
            EventOn::new(&subject, &Incremented { by: 3 }),
            EventOn::new(
                &subject,
                &Annotated {
                    note: "a remark".to_owned(),
                },
            ),
        ],
    )
    .await;

    let counter: Counter = store.load(&subject).await.expect("the state loads");
    assert_eq!(counter.total, 3);
}

#[tokio::test]
async fn fails_permanently_on_an_event_the_state_has_no_rule_for() {
    let database = Database::start().await;
    let store = database.store().await;
    let subject = subject_for("no-rule");

    write_unconditionally(&store, vec![EventOn::new(&subject, &Unrelated {})]).await;

    let failure = store
        .load::<Counter>(&subject)
        .await
        .expect_err("the state has no rule for it");
    assert!(matches!(failure, LoadError::Permanent(_)), "{failure:?}");
    assert!(
        failure.to_string().contains("no rule for event type"),
        "{failure}"
    );
}

#[tokio::test]
async fn reads_from_the_latest_event_of_that_type() {
    let database = Database::start().await;
    let store = database.store().await;
    let subject = subject_for("from-latest");

    // The counter has no rule for the first event, so reading it would fail.
    // Starting from the latest reset skips it.
    write_unconditionally(
        &store,
        vec![
            EventOn::new(&subject, &Unrelated {}),
            EventOn::new(&subject, &Reset {}),
            EventOn::new(&subject, &Incremented { by: 2 }),
            EventOn::new(&subject, &Reset {}),
            EventOn::new(&subject, &Incremented { by: 5 }),
        ],
    )
    .await;

    let counter: CounterFromLatest = store
        .load(&subject)
        .await
        .expect("the state loads from the latest reset");
    assert_eq!(counter.0.total, 5);
}

#[tokio::test]
async fn reads_everything_if_the_type_is_missing() {
    let database = Database::start().await;
    let store = database.store().await;
    let subject = subject_for("from-latest-missing");

    write_unconditionally(
        &store,
        vec![
            EventOn::new(&subject, &Incremented { by: 3 }),
            EventOn::new(&subject, &Incremented { by: 4 }),
        ],
    )
    .await;

    let counter: CounterFromLatest = store.load(&subject).await.expect("the state loads");
    assert_eq!(counter.0.total, 7);
}

// Verifying.

#[tokio::test]
async fn verifies_the_signatures_of_the_events_it_reads() {
    let database = Database::start().await;
    let key = database.verification_key();
    let store = database
        .store_with(|builder| builder.signature_verification(key))
        .await;
    let subject = subject_for("signed");

    write_unconditionally(&store, vec![EventOn::new(&subject, &Incremented { by: 1 })]).await;

    let counter: Counter = store.load(&subject).await.expect("the signatures match");
    assert_eq!(counter.total, 1);
}

#[tokio::test]
async fn refuses_an_event_whose_signature_does_not_match_the_key() {
    let other_key = ed25519_dalek::SigningKey::from_bytes(&[7; 32]).verifying_key();
    let database = Database::start().await;
    let store = database
        .store_with(|builder| builder.signature_verification(other_key))
        .await;
    let subject = subject_for("signed-otherwise");

    write_unconditionally(&store, vec![EventOn::new(&subject, &Incremented { by: 1 })]).await;

    let failure = store
        .load::<Counter>(&subject)
        .await
        .expect_err("the signature does not match");
    assert!(
        matches!(failure, LoadError::Permanent(Permanent::Unverified(_))),
        "{failure:?}",
    );
    assert_eq!(failure.category(), Some(Category::Permanent));
}

#[test]
#[should_panic(expected = "without_hash_verification contradicts signature_verification")]
fn refuses_to_skip_hashes_while_checking_signatures() {
    let client =
        eventsourcingdb::Client::new("http://localhost:3000".parse().expect("a URL"), "secret");
    let key = ed25519_dalek::SigningKey::from_bytes(&[7; 32]).verifying_key();

    let _ = Store::builder(client, "https://thenativeweb.io")
        .without_hash_verification()
        .signature_verification(key)
        .build();
}

// Writing.

#[tokio::test]
async fn writes_events_to_several_subjects_at_once() {
    let database = Database::start().await;
    let store = database.store().await;
    let first = subject_for("several-first");
    let second = subject_for("several-second");

    let written = store
        .write(
            vec![
                EventOn::new(&first, &Incremented { by: 1 }),
                EventOn::new(&second, &Incremented { by: 2 }),
            ],
            vec![Precondition::on_pristine_subject(&first)],
        )
        .await
        .expect("the events are written");

    assert_eq!(written.len(), 2);
    assert_eq!(written[0].source(), "https://thenativeweb.io");
    assert_eq!(store.load::<Counter>(&first).await.expect("loads").total, 1);
    assert_eq!(
        store.load::<Counter>(&second).await.expect("loads").total,
        2
    );
}

#[tokio::test]
async fn reports_a_precondition_that_did_not_hold_as_a_conflict_and_writes_nothing() {
    let database = Database::start().await;
    let store = database.store().await;
    let subject = subject_for("conflict");

    write_unconditionally(&store, vec![EventOn::new(&subject, &Incremented { by: 1 })]).await;

    let failure = store
        .write(
            vec![EventOn::new(&subject, &Incremented { by: 2 })],
            vec![Precondition::on_pristine_subject(&subject)],
        )
        .await
        .expect_err("the subject is not pristine");

    assert!(
        matches!(failure, WriteError::Transient(Transient::Conflict(_))),
        "{failure:?}",
    );
    assert_eq!(
        store.load::<Counter>(&subject).await.expect("loads").total,
        1
    );
}

#[tokio::test]
async fn refuses_the_preconditions_of_a_write_before_writing() {
    let database = Database::start().await;
    let store = database.store().await;
    let subject = subject_for("refused-preconditions");

    for (name, preconditions, message) in [
        (
            "none",
            vec![],
            "a write declares no preconditions, use Precondition::unconditionally to write without any",
        ),
        (
            "on_state_read",
            vec![Precondition::on_state_read()],
            "a write reads no state, so Precondition::on_state_read has nothing to guard",
        ),
        (
            "unconditionally with another one",
            vec![
                Precondition::unconditionally(),
                Precondition::on_pristine_subject(&subject),
            ],
            "a write combines Precondition::unconditionally with other preconditions",
        ),
    ] {
        let failure = store
            .write(
                vec![EventOn::new(&subject, &Incremented { by: 1 })],
                preconditions,
            )
            .await
            .expect_err(name);

        assert!(
            matches!(failure, WriteError::Permanent(_)),
            "{name}: {failure:?}"
        );
        assert!(failure.to_string().contains(message), "{name}: {failure}");
    }

    assert_eq!(
        store.load::<Counter>(&subject).await.expect("loads").total,
        0,
        "nothing was written"
    );
}

#[tokio::test]
async fn refuses_an_event_id_that_is_not_a_revision_without_writing() {
    let database = Database::start().await;
    let store = database.store().await;
    let subject = subject_for("not-a-revision");

    let failure = store
        .write(
            vec![EventOn::new(&subject, &Incremented { by: 1 })],
            vec![Precondition::on_event_id(&subject, "latest")],
        )
        .await
        .expect_err("latest is not a revision");

    assert!(
        matches!(failure, WriteError::NotARevision(_)),
        "{failure:?}"
    );
    assert_eq!(failure.category(), None);
    assert_eq!(
        store.load::<Counter>(&subject).await.expect("loads").total,
        0,
        "nothing was written"
    );
}

#[tokio::test]
async fn writes_nothing_for_no_events_but_checks_the_preconditions_first() {
    let database = Database::start().await;
    let store = database.store().await;

    let written = store
        .write(vec![], vec![Precondition::unconditionally()])
        .await
        .expect("no events are nothing to write");
    assert_eq!(written, Vec::new());

    let failure = store
        .write(vec![], vec![])
        .await
        .expect_err("a write declares its preconditions");
    assert!(matches!(failure, WriteError::Permanent(_)), "{failure:?}");
}

#[tokio::test]
async fn refuses_an_event_without_a_subject() {
    let failure = Database::start()
        .await
        .store()
        .await
        .write(
            vec![EventOn::new("", &Incremented { by: 1 })],
            vec![Precondition::unconditionally()],
        )
        .await
        .expect_err("an event needs a subject");

    assert_eq!(
        failure.to_string(),
        "permanent failure: event 0 of a write needs a subject",
    );
}

#[derive(Serialize, Deserialize, Event)]
#[event(type = "io.thenativeweb.test.keyed")]
struct Keyed {
    #[serde(skip_serializing_if = "Option::is_none")]
    by_position: Option<HashMap<(i32, i32), String>>,
}

impl crate::Schema for (i32, i32) {
    fn schema() -> serde_json::Value {
        serde_json::json!({})
    }
}

#[tokio::test]
async fn refuses_an_event_whose_data_can_not_be_encoded_without_writing() {
    let database = Database::start().await;
    let store = database.store().await;
    let subject = subject_for("unencodable");

    // serde_json writes the keys of a map as strings, which a tuple is not.
    let failure = store
        .write(
            vec![EventOn::new(
                &subject,
                &Keyed {
                    by_position: Some(HashMap::from([((1, 2), "x".to_owned())])),
                },
            )],
            vec![Precondition::unconditionally()],
        )
        .await
        .expect_err("the data can not be encoded");

    assert!(matches!(failure, WriteError::Permanent(_)), "{failure:?}");
    assert!(
        failure
            .to_string()
            .contains(r#"refusing to write an event of type "io.thenativeweb.test.keyed""#),
        "{failure}",
    );
    assert!(
        failure.source().is_some(),
        "the error of serde is the cause"
    );
}

#[tokio::test]
async fn agrees_with_the_database_on_every_character() {
    let database = Database::start().await;
    let store = database.store().await;
    let base = subject_for("characters");
    let scheme = SubjectScheme::new(&format!("{base}/{{value}}"));

    // The characters that check accepts go into a single value, since the
    // database takes a subject only if it takes every character in it, and
    // writing an event takes far longer than having one refused.
    let mut accepted = String::new();

    let characters =
        (0..128_u8)
            .map(char::from)
            .chain(['\u{00fc}', '\u{212a}', '\u{ff21}', '\u{1f600}']);
    for character in characters {
        // A slash separates segments, so the database takes it, but the
        // subject would no longer follow the pattern.
        if character == '/' {
            continue;
        }

        let value = format!("a{character}b");
        if scheme.check(&[&value]).is_ok() {
            accepted.push(character);
            continue;
        }

        // One write each, since the database refuses a write as a whole.
        let failure = store
            .write(
                vec![EventOn::new(
                    format!("{base}/{value}"),
                    &Incremented { by: 1 },
                )],
                vec![Precondition::unconditionally()],
            )
            .await
            .expect_err("the database refuses what check refuses");
        assert_malformed_subject(&failure, &value);
    }

    let values = [accepted, "_".to_owned(), "-".to_owned()];
    let events = values
        .iter()
        .map(|value| EventOn::new(scheme.build(&[value]), &Incremented { by: 1 }))
        .collect();
    let _ = store
        .write(events, vec![Precondition::unconditionally()])
        .await
        .expect("the database takes every value that check accepts");

    for value in &values {
        let counter: Counter = store
            .load(&scheme.build(&[value]))
            .await
            .expect("the database reads the subject");
        assert_eq!(counter.total, 1, "{value:?}");
    }
}

/// Asserts that the database refused a write for the given subject as
/// malformed: with the code subject-malformed, or, from a database that offers
/// just the v1 API, which reports it without a code, with a description that
/// says so.
fn assert_malformed_subject(failure: &WriteError, value: &str) {
    assert!(
        matches!(failure, WriteError::Permanent(_)),
        "{value:?}: {failure:?}"
    );

    let Some(ClientError::DbApiError(answer)) = cause_of(failure) else {
        panic!("{value:?}: the cause has to be the answer of the database, got {failure:?}");
    };
    if answer.code.is_empty() {
        assert!(
            answer.description.contains("malformed subject"),
            "{value:?}: {answer:?}"
        );
    } else {
        assert_eq!(answer.code, code::SUBJECT_MALFORMED, "{value:?}");
    }
}

/// Returns the error of the client that a failure wraps, if there is one.
fn cause_of(failure: &dyn Error) -> Option<&ClientError> {
    failure.source()?.downcast_ref::<ClientError>()
}

// Registering schemas.

#[derive(Serialize, Deserialize, Event)]
#[event(type = "io.thenativeweb.test.titled")]
struct Titled {
    title: String,
}

/// The same event type as Titled, with another shape.
#[derive(Serialize, Deserialize, Event)]
#[event(type = "io.thenativeweb.test.titled")]
struct TitledOtherwise {
    headline: String,
}

#[derive(Serialize, Deserialize, Event)]
#[event(type = "io.thenativeweb.test.registered")]
struct Registered {
    name: String,
}

#[tokio::test]
async fn registers_the_schemas_of_events_and_checks_them_on_every_start() {
    let database = Database::start().await;
    let store = database.store().await;
    let schemas = || [crate::EventSchema::of::<Registered>()];

    store
        .register_schemas(schemas())
        .await
        .expect("the schemas are registered");
    store
        .register_schemas(schemas())
        .await
        .expect("registering them again checks them");
}

#[tokio::test]
async fn fails_permanently_for_a_schema_that_differs_from_the_registered_one() {
    let database = Database::start().await;
    let store = database.store().await;

    store
        .register_schemas([crate::EventSchema::of::<Titled>()])
        .await
        .expect("the first schema is registered");

    let failure = store
        .register_schemas([crate::EventSchema::of::<TitledOtherwise>()])
        .await
        .expect_err("a registered schema can not change");

    assert!(
        matches!(failure, RegisterSchemasError::Permanent(_)),
        "{failure:?}"
    );
    assert!(
        failure
            .to_string()
            .contains("differs from the registered one, which cannot change"),
        "{failure}",
    );
}

#[tokio::test]
async fn refuses_two_different_schemas_for_the_same_event_type() {
    let failure = Database::start()
        .await
        .store()
        .await
        .register_schemas([
            crate::EventSchema::of::<Titled>(),
            crate::EventSchema::of::<TitledOtherwise>(),
        ])
        .await
        .expect_err("one event type has one schema");

    assert_eq!(
        failure.to_string(),
        r#"permanent failure: event type "io.thenativeweb.test.titled" has two different schemas"#,
    );
}

#[tokio::test]
async fn registers_the_same_schema_given_twice_once() {
    Database::start()
        .await
        .store()
        .await
        .register_schemas([
            crate::EventSchema::of::<Registered>(),
            crate::EventSchema::of::<Registered>(),
        ])
        .await
        .expect("the same schema twice is one schema");
}

// Sorting failures, with a fake database.

const READ_EVENTS: &str = "/api/v1/read-events";
const WRITE_EVENTS: &str = "/api/v1/write-events";

async fn load_from(reply: Reply) -> LoadError {
    let store = Store::builder(
        refusing(Api::V1, READ_EVENTS, reply).await,
        "https://thenativeweb.io",
    )
    .build();
    store
        .load::<Counter>("/test")
        .await
        .expect_err("the fake refuses")
}

async fn write_to(api: Api, path: &'static str, reply: Reply) -> WriteError {
    let store = Store::builder(refusing(api, path, reply).await, "https://thenativeweb.io").build();
    store
        .write(
            vec![EventOn::new("/test", &Incremented { by: 1 })],
            vec![Precondition::on_pristine_subject("/test")],
        )
        .await
        .expect_err("the fake refuses")
}

#[tokio::test]
async fn sorts_failures_by_status_when_reading_and_writing() {
    for (name, status, category) in [
        ("a bad request is permanent", 400, Category::Permanent),
        (
            "a rejected API token is permanent",
            401,
            Category::Permanent,
        ),
        (
            "a request that is too large is permanent",
            413,
            Category::Permanent,
        ),
        ("too many requests are transient", 429, Category::Transient),
        (
            "an unavailable database is transient",
            503,
            Category::Transient,
        ),
    ] {
        let read = load_from(Reply::status(status, "refused")).await;
        assert_eq!(
            read.category(),
            Some(category),
            "{name}, when reading: {read:?}"
        );

        let written = write_to(Api::V1, WRITE_EVENTS, Reply::status(status, "refused")).await;
        assert_eq!(
            written.category(),
            Some(category),
            "{name}, when writing: {written:?}"
        );
        assert!(
            !matches!(written, WriteError::Transient(Transient::Conflict(_))),
            "{name}: only 409 is a conflict",
        );
    }
}

#[tokio::test]
async fn sorts_any_other_status_of_500_and_above_as_transient_when_reading() {
    for status in [500, 502, 504] {
        let read = load_from(Reply::status(status, "failed")).await;
        assert_eq!(
            read.category(),
            Some(Category::Transient),
            "{status}: {read:?}"
        );
    }
}

#[tokio::test]
async fn sorts_409_as_permanent_on_every_read_since_reading_has_no_preconditions() {
    let read = load_from(Reply::status(409, "state conflict: precondition failed")).await;

    assert!(matches!(read, LoadError::Permanent(_)), "{read:?}");
}

#[tokio::test]
async fn sorts_409_by_its_reason_when_writing() {
    let conflict = write_to(
        Api::V1,
        WRITE_EVENTS,
        Reply::status(409, "state conflict: precondition failed"),
    )
    .await;
    assert!(
        matches!(conflict, WriteError::Transient(Transient::Conflict(_))),
        "{conflict:?}",
    );

    let schema = write_to(
        Api::V1,
        WRITE_EVENTS,
        Reply::status(409, "schema conflict: event is invalid"),
    )
    .await;
    assert!(matches!(schema, WriteError::Permanent(_)), "{schema:?}");
}

#[tokio::test]
async fn names_a_rejected_api_token() {
    let read = load_from(Reply::status(401, "unauthorized")).await;

    assert!(
        read.to_string()
            .contains("the database rejected the API token"),
        "{read}"
    );
}

#[tokio::test]
async fn treats_an_answer_that_does_not_come_from_an_eventsourcingdb_as_transient_when_reading() {
    let read = load_from(Reply::foreign(502)).await;

    assert_eq!(read.category(), Some(Category::Transient), "{read:?}");
}

#[tokio::test]
async fn leaves_the_outcome_of_a_write_open_that_failed_after_it_may_have_been_stored() {
    // A 500, or an answer of a proxy in front of the database, may come after
    // the events were stored, so the client leaves the outcome open.
    for reply in [Reply::status(500, "failed"), Reply::foreign(502)] {
        let written = write_to(Api::V1, WRITE_EVENTS, reply.clone()).await;

        assert!(
            matches!(written, WriteError::OutcomeUnknown(_)),
            "{reply:?}: {written:?}"
        );
        assert_eq!(written.category(), None, "{reply:?}");
        assert!(
            written.to_string().starts_with("outcome unknown: "),
            "{written}"
        );
    }
}

#[tokio::test]
async fn wraps_the_error_of_the_client_so_that_source_reaches_the_answer_of_the_database() {
    let read = load_from(Reply::status(503, "shutting down")).await;

    let Some(ClientError::DbApiError(answer)) = cause_of(&read) else {
        panic!("the cause has to be the answer of the database, got {read:?}");
    };
    assert_eq!(answer.status, 503);
    assert_eq!(answer.description, "shutting down");
}

#[tokio::test]
async fn sorts_failures_of_the_v2_api_by_their_code() {
    const V2_WRITE_EVENTS: &str = "/api/v2/data/write-events";

    let conflict = write_to(
        Api::V2,
        V2_WRITE_EVENTS,
        Reply::error_with_code(code::PRECONDITION_VIOLATED, "precondition does not hold"),
    )
    .await;
    assert!(
        matches!(conflict, WriteError::Transient(Transient::Conflict(_))),
        "{conflict:?}",
    );

    for transient in [
        code::SERVER_SHUTTING_DOWN,
        code::STORE_READ_ONLY,
        code::INDEXES_REBUILDING,
        code::RESTORE_IN_PROGRESS,
        code::DISK_FULL,
        code::STORE_FAILED,
        code::LICENSE_INVALID,
        code::CAPACITY_EXCEEDED,
    ] {
        let written = write_to(
            Api::V2,
            V2_WRITE_EVENTS,
            Reply::error_with_code(transient, "not now"),
        )
        .await;
        assert!(
            matches!(written, WriteError::Transient(Transient::Other(_))),
            "{transient}: {written:?}"
        );
    }

    for permanent in [
        code::SUBJECT_MALFORMED,
        code::EVENT_SCHEMA_VIOLATED,
        "https://docs.eventsourcingdb.io/errors/from-the-future",
    ] {
        let written = write_to(
            Api::V2,
            V2_WRITE_EVENTS,
            Reply::error_with_code(permanent, "wrong"),
        )
        .await;
        assert!(
            matches!(written, WriteError::Permanent(_)),
            "{permanent}: {written:?}"
        );
    }
}

#[tokio::test]
async fn sorts_failures_by_status_when_reading_the_registered_schemas() {
    let store = Store::builder(
        refusing(
            Api::V1,
            "/api/v1/read-event-types",
            Reply::status(503, "unavailable"),
        )
        .await,
        "https://thenativeweb.io",
    )
    .build();

    let failure = store
        .register_schemas([crate::EventSchema::of::<Registered>()])
        .await
        .expect_err("the fake refuses");

    assert_eq!(failure.category(), Some(Category::Transient), "{failure:?}");
}