sofie-testing 0.4.0

The test harness: a database per test, and the app served on it.
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
#[cfg(feature = "flags")]
use std::sync::OnceLock;
use std::sync::{Arc, Mutex};
use std::time::Duration;

use sofie::db::{Db, File, Migrations};
#[cfg(feature = "flags")]
use sofie::flags::{Flags, Rule};
use sofie::prelude::*;
use sofie::values::Fixture;
use sofie::worker::Worker;
use sofie_core::unpoisoned;
use sofie_testing::{
    TestApp, TestPrincipal, assert_boundary, assert_moves, assert_raised, assert_round_trips,
    assert_store, assert_unique, assert_value, drain_failing, fixture_input,
};

const TABLES: &str = "
CREATE TABLE product (
    id uuid PRIMARY KEY, version bigint NOT NULL,
    created_at timestamptz NOT NULL, updated_at timestamptz NOT NULL,
    seller uuid NOT NULL, name text NOT NULL, sku text NOT NULL UNIQUE,
    price bigint NOT NULL, price_currency text NOT NULL, stock bigint NOT NULL,
    status text NOT NULL
);
CREATE TABLE note (
    id uuid PRIMARY KEY, version bigint NOT NULL,
    created_at timestamptz NOT NULL, updated_at timestamptz NOT NULL,
    text text NOT NULL
);
";

pub struct Seller;

impl sofie::http::AreaPrincipal for Seller {
    const ACCESS: sofie::http::Access = sofie::http::Access::Owned;
    type Record = Self;
}

#[derive(Aggregate)]
pub struct Product {
    meta: Meta,
    #[owner]
    seller: Id<Seller>,
    #[required]
    #[max(12)]
    name: String,
    #[unique]
    sku: String,
    price: Money,
    #[range(min = 0, max = 500)]
    stock: i64,
    status: ProductStatus,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Enum)]
pub enum ProductStatus {
    #[to(Listed)]
    Draft,
    #[to(Retired)]
    Listed,
    Retired,
}

#[derive(Event)]
pub struct NoteWritten {
    pub text: String,
}

#[derive(Aggregate)]
pub struct Note {
    meta: Meta,
    text: String,
}

impl Note {
    fn write(text: &str) -> Self {
        let mut note = Self {
            meta: Meta::new().unwrap(),
            text: text.to_owned(),
        };
        note.meta
            .raise(NoteWritten {
                text: text.to_owned(),
            })
            .unwrap();
        note
    }
}

pub struct Sellers;

impl Area for Sellers {
    const NAME: &'static str = "sellers";
    const PREFIX: &'static str = "/sellers";
    const SIGN_IN: &'static str = "/sign-in";
    type Principal = Seller;
}

async fn signed_in(_seller: Principal<Sellers>) -> Result<()> {
    Ok(())
}

#[cfg(feature = "contracts")]
async fn reserve(_seller: Principal<Sellers>, stock: Dep<dyn Stock>) -> Result<()> {
    stock.reserve("MUG-1", 1).await.map(drop)
}

fn routes(routes: &mut Routes<Sellers>) {
    routes.get("/me", signed_in);
    #[cfg(feature = "contracts")]
    routes.post("/reserve", reserve);
}

async fn harness(db: Db) -> Result<App> {
    db.schema()
        .migrate(&[Migrations::new(
            "harness",
            const { &[File::new("0001_tables.sql", TABLES.as_bytes())] },
        )])
        .await?;
    Ok(App::new(db).area::<Sellers>("harness", routes))
}

async fn started() -> TestApp {
    sofie_testing::start(harness, Worker::new).await
}

#[tokio::test]
async fn a_fixture_is_made_by_the_default_factory_held_to_its_constraints_and_saved() {
    let app = started().await;
    let seller = TestPrincipal::<Sellers>::fresh(&app);

    let product: Product = app.fixture().owned_by(seller.id()).await;

    let rows = app
        .query(
            "SELECT seller, name, status, stock FROM harness.product",
            &[],
        )
        .await;
    assert_eq!(rows.len(), 1);
    assert_eq!(rows[0][0], sofie::core::SqlValue::Uuid(seller.id().uuid()));
    let sofie::core::SqlValue::Text(name) = &rows[0][1] else {
        panic!("{rows:?}");
    };
    assert!(!name.is_empty() && name.chars().count() <= 12, "{name}");
    assert_eq!(rows[0][2], sofie::core::SqlValue::Text("DRAFT".to_owned()));
    assert_eq!(product.meta().version(), 1);
}

#[tokio::test]
async fn fixtures_are_the_same_in_every_run() {
    let first = started().await;
    let second = started().await;

    let inputs = |app: &TestApp| {
        [
            fixture_input::<<Product as sofie::db::Create>::Input>(app),
            fixture_input::<<Product as sofie::db::Create>::Input>(app),
        ]
    };

    let made = inputs(&first);
    assert_eq!(made, inputs(&second));
    assert_ne!(made[0], made[1]);
}

#[tokio::test]
async fn a_record_loads_as_saved_and_a_stale_save_conflicts() {
    let app = started().await;
    let first: Product = app.fixture().await;
    let second: Product = app.fixture().await;

    assert_store(&app, first.id(), second.id()).await;
}

#[tokio::test]
async fn a_unique_field_refuses_a_second_record_with_the_same_value() {
    let app = started().await;

    assert_unique::<Product>(&app, "sku").await;
}

#[tokio::test]
async fn values_round_trip_through_json_and_the_database() {
    let app = started().await;

    assert_value::<Money>(&app).await;
    assert_value::<Email>(&app).await;
    assert_value::<Instant>(&app).await;
}

/// An app's own value, with neither `PartialEq` nor `Debug`.
#[derive(Value)]
struct Shelf(String);

impl Shelf {
    #[expect(
        clippy::unnecessary_wraps,
        reason = "derive(Value) builds a value through a fallible try_new"
    )]
    fn try_new(text: String) -> Result<Self, &'static str> {
        Ok(Self(text))
    }
}

impl Fixture for Shelf {
    fn fixture(seed: &mut sofie::values::Seed) -> Self {
        Self(format!("shelf {}", seed.draw()))
    }
}

#[tokio::test]
async fn an_app_s_own_value_round_trips_through_json_and_the_database() {
    let app = started().await;

    assert_round_trips::<Shelf>(&app).await;
}

#[test]
fn each_constraint_is_refused_past_its_bound() {
    assert_boundary("name", Product::__NAME, "required");
    assert_boundary("name", Product::__NAME, "max 12");
    assert_boundary("stock", Product::__STOCK, "min 0");
    assert_boundary("stock", Product::__STOCK, "max 500");
}

#[test]
fn a_status_moves_only_along_its_transitions() {
    assert_moves::<ProductStatus>(&[("DRAFT", &["LISTED"]), ("LISTED", &["RETIRED"])]);
}

#[test]
#[should_panic(expected = "transitions changed")]
fn a_changed_transition_fails_the_test_of_the_old_one() {
    assert_moves::<ProductStatus>(&[("DRAFT", &["LISTED", "RETIRED"]), ("LISTED", &["RETIRED"])]);
}

#[tokio::test]
async fn a_test_calls_as_a_principal_without_signing_in() {
    let app = started().await;
    let seller = TestPrincipal::<Sellers>::fresh(&app);

    let anonymous = app.get("/sellers/me").accept_json().await;
    let signed_in = app.as_(&seller).get("/sellers/me").accept_json().await;

    anonymous.assert_status(401);
    signed_in.assert_ok();
}

async fn deliver(_event: NoteWritten) -> Result<()> {
    Ok(())
}

#[tokio::test]
async fn a_raised_event_is_found_before_and_after_it_is_delivered() {
    let app = sofie_testing::start(harness, |db| {
        Worker::new(db).on::<NoteWritten, _>("harness.deliver", deliver)
    })
    .await;
    let mut note = Note::write("restock on Monday");
    app.db().commit(&mut note).await.unwrap();

    let before = assert_raised::<NoteWritten>(&app);
    assert_eq!(app.drain().await, 1);
    let after = assert_raised::<NoteWritten>(&app);

    assert_eq!(before.len(), 1);
    assert_eq!(after[0].text, "restock on Monday");
}

async fn refuse(_event: NoteWritten) -> Result<()> {
    Err(Error::internal_message("the shelf is full"))
}

async fn started_refusing() -> TestApp {
    let app = sofie_testing::start(harness, |db| {
        Worker::new(db).on::<NoteWritten, _>("harness.refuse", refuse)
    })
    .await;
    let mut note = Note::write("restock on Monday");
    app.db().commit(&mut note).await.unwrap();
    app
}

#[tokio::test]
#[should_panic(expected = "the shelf is full")]
async fn a_failing_job_fails_the_drain() {
    started_refusing().await.drain().await;
}

#[tokio::test]
async fn a_drain_expecting_failures_names_them() {
    let app = started_refusing().await;

    let failures = drain_failing(&app).await;

    assert_eq!(failures.len(), 1);
    assert_eq!(failures[0].job, "harness.refuse");
}

static FIRED: Mutex<Vec<(&str, i64)>> = Mutex::new(Vec::new());

fn fired(name: &'static str, clock: &Clock) {
    let second = clock.now().timestamp().as_second();
    unpoisoned::lock(&FIRED).push((name, second));
}

async fn hourly(clock: Clock) -> Result<()> {
    fired("hourly", &clock);
    Ok(())
}

async fn every_ninety_minutes(clock: Clock) -> Result<()> {
    fired("ninety", &clock);
    Ok(())
}

#[tokio::test]
async fn advancing_the_clock_runs_each_schedule_when_due_in_order() {
    let app = sofie_testing::start(harness, |db| {
        Worker::new(db)
            .every("harness.hourly", Duration::from_hours(1), hourly)
            .every(
                "harness.ninety",
                Duration::from_mins(90),
                every_ninety_minutes,
            )
    })
    .await;
    let start = app.clock().clock().now().timestamp().as_second();

    let completed = app.advance(Duration::from_hours(3)).await;

    let fired: Vec<(&str, i64)> = unpoisoned::lock(&FIRED)
        .iter()
        .map(|(name, second)| (*name, second - start))
        .collect();
    let mut first = fired[..2].to_vec();
    first.sort_unstable();
    assert_eq!(first, [("hourly", 0), ("ninety", 0)]);
    assert_eq!(
        fired[2..5],
        [("hourly", 3_600), ("ninety", 5_400), ("hourly", 7_200)]
    );
    let mut last = fired[5..].to_vec();
    last.sort_unstable();
    assert_eq!(last, [("hourly", 10_800), ("ninety", 10_800)]);
    assert_eq!(completed, 7);
}

#[cfg(feature = "flags")]
static WORKER_FLAGS: OnceLock<Flags> = OnceLock::new();

#[cfg(feature = "flags")]
#[tokio::test]
async fn the_app_and_its_worker_share_one_set_of_flags() {
    let app = sofie_testing::start_with_flags(
        |db, _flags| harness(db),
        |db, flags| {
            WORKER_FLAGS.set(flags.clone()).ok();
            Worker::new(db)
        },
    )
    .await;

    app.flags()
        .set("harness.beta", Rule::everyone())
        .await
        .unwrap();

    let seen = WORKER_FLAGS.get().unwrap().rules();
    assert_eq!(seen, [("harness.beta".to_owned(), Rule::everyone())]);
}

#[sofie::contract]
pub trait Stock: Send + Sync {
    async fn reserve(&self, sku: &str, count: i64) -> Result<i64>;
}

#[tokio::test]
async fn a_contract_s_fake_records_calls_and_fails_when_told() {
    let fake = Arc::new(FakeStock::default());
    let stock: Arc<dyn Stock> = fake.clone();

    let reserved = stock.reserve("MUG-1", 2).await;
    fake.calls.fail("reserve");
    let failed = stock.reserve("MUG-1", 2).await;

    assert_eq!(
        reserved.unwrap(),
        i64::fixture(&mut sofie::values::Seed::new(0))
    );
    assert!(matches!(failed, Err(Error::Unavailable(_))), "{failed:?}");
    assert_eq!(fake.calls.made(), ["reserve", "reserve"]);
}

/// As `sofie gen` writes a DTO into a contract.
#[derive(sofie::serde::Deserialize, sofie::serde::Serialize, sofie::Fixture)]
#[serde(crate = "sofie::serde")]
pub struct Shelved {
    pub id: sofie::db::Uuid,
    pub sku: String,
    pub count: i64,
}

#[sofie::contract]
pub trait Shelves: Send + Sync {
    async fn shelved(&self, sku: &str) -> Result<Shelved>;
}

#[tokio::test]
async fn a_contract_answering_a_dto_gets_a_fake_drawing_it() {
    let fake = FakeShelves::default();

    let shelved = fake.shelved("MUG-1").await.unwrap();

    let drawn = Shelved::fixture(&mut sofie::values::Seed::new(0));
    assert_eq!(
        (shelved.id, shelved.sku, shelved.count),
        (drawn.id, drawn.sku, drawn.count)
    );
}

#[cfg(feature = "contracts")]
#[tokio::test]
async fn a_test_answers_a_dependency_of_the_app_with_its_fake() {
    let app = started().await;
    let seller = TestPrincipal::<Sellers>::fresh(&app);
    let fake = Arc::new(FakeStock::default());

    sofie_testing::fake::<dyn Stock>(&app, fake.clone());
    let reserved = app
        .as_(&seller)
        .post("/sellers/reserve")
        .accept_json()
        .await;

    reserved.assert_ok();
    assert_eq!(fake.calls.made(), ["reserve"]);
}

async fn listed() -> Result<()> {
    Ok(())
}

#[tokio::test]
#[should_panic(expected = "are served at one path")]
async fn a_test_server_whose_app_cannot_start_says_why() {
    let app = sofie::http::App::new(Db::new(sofie_testing::FakeDriver::default())).area(
        "shop",
        |routes: &mut sofie::http::Routes<sofie::http::Web>| {
            routes.get("/orders/{order}", listed);
            routes.get("/orders/{id}", listed);
        },
    );

    sofie_testing::TestServer::start(app).await;
}