Section 14 of 25

Testing by layer

Contents
Layer How to test What counts as proof
logic.rs, update.rs a plain #[test], no tokio an assertion on a value or on the list of effects
store.rs a database in a temporary folder the row that comes back from a read
service.rs #[tokio::test] with a real store and a data seam (14.2) the state of files or the database after the call
actor/ a Handle plus a script of messages; time with tokio::time::pause the sequence of Snapshots
drivers a real process or a recorded stream the process is dead (ESRCH or an empty job), the lines from the wire
app/commands no unit test; an end-to-end or integration test through the entry, for what a person sees the text a person sees

A test for a message shown to a person belongs where the person sees it, not only in logic.rs. A function that produces the right sentence proves nothing if nobody calls it.

14.1. One integration binary

Every file directly in tests/ is a separate binary, and each binary is linked separately. On a large workspace that is a minute per file. Give each crate one integration target, tests/it/main.rs, with one module per topic, and run a single topic with a filter:

tests/it/
  main.rs          mod recording_stops_on_time_limit; mod transcript_survives_restart; …
  recording_stops_on_time_limit.rs
  transcript_survives_restart.rs
cargo nextest run --test it recording_stops_on_time_limit::

Run with --no-tests=fail, so that a filter that matches nothing is red instead of quietly green.

Helpers shared by the tests are a module of the same target, tests/it/support/. A helper file placed directly in tests/ is one more binary to link.

14.2. A fake driver without a trait

A test needs a driver that does not start real processes. SLAT forbids a trait with one implementation, and a fake does not count as a second real one. Two options keep the rule. Prefer the first: a feature changes how drivers and every crate above it compile, so a test build with it and an application build without it are two separate sets of artifacts (section 22.3). A data seam costs no compile time at all.

  • A data seam. The service takes the driver's output as a value or a channel, not the driver itself. The test sends lines into the channel.
  • A test build. drivers exposes #[cfg(any(test, feature = "fake"))] pub mod fake; with the same functions as the real module, and the test crate enables the fake feature.

When a second real implementation appears (a second provider, a second platform), that is the moment for a trait.

14.3. Mutants in the diff

A test that passes proves something only if it would fail on wrong code. That is section 16.2 applied to tests. cargo-mutants changes the code (it flips a condition, replaces a return value, deletes a statement) and reports every change that no test noticed. Run it on the lines a pull request changed, not on the whole workspace:

git diff origin/main...HEAD > changes.diff
cargo mutants --in-diff changes.diff --package quayside-core

logic.rs and update.rs are where this pays off most. They are pure, so their tests run in microseconds, and a surviving mutant there is a business rule nobody checks. A surviving mutant is fixed with a test, never with an exclusion.

14.4. Property tests for update

An update test checks one script of messages. A property test checks thousands of random scripts against rules that must always hold. With proptest, generate a sequence of Msg values and assert the invariants of the state after every step:

proptest! {
    #[test]
    fn a_stale_generation_never_changes_the_phase(script in messages(64)) {
        let mut state = State::default();
        for msg in script {
            let generation_before = state.generation;
            let phase_before = state.phase.clone();
            let stale = msg.generation().is_some_and(|g| g != generation_before);
            update(&mut state, msg);
            prop_assert!(state.generation >= generation_before);
            if stale {
                prop_assert_eq!(&state.phase, &phase_before);
            }
        }
    }
}

messages(n) is a strategy of the test module that builds random Msg values, and Msg::generation() is a small helper that returns the generation a message carries. The leak from section 20.2 is exactly the kind of bug such a test finds on its own: no one has to think of the order of messages that causes it.

14.5. A test through the entry

The sentence a person sees is proved through the entry: the real command and a real AppState, without a window. Tauri's test feature has a mock runtime that runs a command the way the window does. The test is a module of the app crate's tests/it and registers the commands it calls:

#[test]
fn reading_a_transcript_that_is_gone_says_so() {
    let app = tauri::test::mock_builder()
        .manage(support::app_state())
        .invoke_handler(tauri::generate_handler![transcript::commands::transcript_read])
        .build(tauri::test::mock_context(tauri::test::noop_assets()))
        .expect("the test app could not start");
    let window = tauri::WebviewWindowBuilder::new(&app, "main", Default::default())
        .build()
        .expect("the test window could not open");

    let reply = tauri::test::get_ipc_response(
        &window,
        support::invoke("transcript_read", serde_json::json!({ "id": "gone" })),
    );

    let error = reply.expect_err("a missing transcript must be refused");
    assert_eq!(support::english(&error["messageKey"]), "This transcript no longer exists.");
}

support::app_state() assembles AppState on a temporary folder, support::invoke builds the tauri::webview::InvokeRequest with tauri::test::INVOKE_KEY, and support::english looks the key up in the same translation file the front-end uses. A test that stops at messageKey proves only the mapping; the sentence is the proof.