Skip to main content

coven/
readable_objects.rs

1//! Coven's objects that app SQL may read, made outside any store for an
2//! app's test of its own schema (api.md E4), with sync's computation of the
3//! sync state, which the database crate can't reach.
4
5/// Make on `connection`, a connection an app's test opened itself outside
6/// any store, every object of coven's that app SQL may read, and register
7/// coven's SQL functions there, with `device` as this device's id, as
8/// `coven_database::test_utils::make_readable_objects` says. The sync state
9/// function is sync's own, as every store registers it.
10pub fn make_readable_objects(
11    connection: &coven_database::test_utils::Connection,
12    device: coven_foundation::id_source::DeviceId,
13) -> coven_database::SqliteResult<()> {
14    coven_database::test_utils::make_readable_objects(
15        connection,
16        device,
17        &coven_sync::sync_functions(),
18    )
19}
20
21/// Journal `invite` on `connection`, where `make_readable_objects` made
22/// coven's objects, as a store journals an invite at its stage, so that
23/// `coven_invites` shows it. Returns the invite's operation.
24pub fn record_invite(
25    connection: &coven_database::test_utils::Connection,
26    invite: coven_sync::RecordedInvite,
27) -> Result<coven_database::OperationId, crate::CovenError> {
28    let new = coven_sync::invite_operation(invite)?;
29    Ok(coven_database::test_utils::journal_outside_store(connection, &new, None)?)
30}
31
32/// End the invite `invite` on `connection` as a store ends one that the
33/// provider refused to share with `account` for `refusal`, a reason only a
34/// person can fix: `coven_operations` lists it as refused, and it leaves
35/// `coven_invites` (§13.2).
36pub fn refuse_invite(
37    connection: &coven_database::test_utils::Connection,
38    invite: coven_database::OperationId,
39    account: &str,
40    refusal: coven_foundation::sharing::SharingRefusal,
41) -> Result<(), crate::CovenError> {
42    let outcome =
43        coven_database::ListedOutcome::Invite(coven_database::InviteOutcome::SharingRefused {
44            account: account.to_owned(),
45            refusal,
46        });
47    let kind = coven_sync::OperationKind::Invite.listed();
48    Ok(coven_database::test_utils::finish_outside_store(
49        connection,
50        invite,
51        coven_database::test_utils::OutcomeListing::Unlisted { kind: &kind, outcome: &outcome },
52    )?)
53}
54
55/// Journal `change` on `connection`, where `make_readable_objects` made
56/// coven's objects, as the app's write journals it, so that
57/// `coven_operations` lists it as running. Returns its operation.
58pub fn record_change(
59    connection: &coven_database::test_utils::Connection,
60    change: coven_format::admin::StoreChange,
61) -> Result<coven_database::OperationId, crate::CovenError> {
62    let (new, kind) = coven_sync::change_operation(change)?;
63    Ok(coven_database::test_utils::journal_outside_store(connection, &new, Some(&kind))?)
64}
65
66/// End the operation `id`, which the app's write journaled, on `connection`
67/// as a store ends it with `outcome`: `coven_operations` lists the outcome
68/// until the app acknowledges it (§11.4). An outcome not in the outcome type
69/// of the operation's kind fails with `Invariant`, as coven never writes one.
70pub fn finish_operation(
71    connection: &coven_database::test_utils::Connection,
72    id: coven_database::OperationId,
73    outcome: impl Into<coven_database::ListedOutcome>,
74) -> Result<(), crate::CovenError> {
75    let outcome = outcome.into();
76    let kind: Option<coven_sync::OperationKind> = connection
77        .query_row("SELECT (SELECT kind FROM coven_operations WHERE operation=?1)", [id.0], |row| {
78            row.get(0)
79        })
80        .map_err(coven_database::DbError::from)?;
81    if kind.is_some_and(|kind| !kind.lists(&outcome)) {
82        return Err(coven_database::DbError::Invariant {
83            what: "an operation ends in the outcome type of its kind",
84        }
85        .into());
86    }
87    Ok(coven_database::test_utils::finish_outside_store(
88        connection,
89        id,
90        coven_database::test_utils::OutcomeListing::Listed(&outcome),
91    )?)
92}
93
94/// Save `record` on `connection`, where `make_readable_objects` made
95/// coven's objects, as a store saves a pending record, so that
96/// `coven_pending` shows it (§19.1).
97pub fn record_pending(
98    connection: &coven_database::test_utils::Connection,
99    record: &coven_sync::PendingRecord,
100) -> Result<(), crate::CovenError> {
101    Ok(coven_database::test_utils::save_outside_store(connection, record)?)
102}
103
104/// Record on `connection`, where `make_readable_objects` made coven's
105/// objects, that `location` is the store's storage and this device holds
106/// its credentials, as opening a connected store records it. Until a test
107/// does, `coven_sync_state` shows not synced, as for a store with no
108/// storage (§19.1).
109pub fn record_connected_storage(
110    connection: &coven_database::test_utils::Connection,
111    location: coven_storage::StorageConfig,
112) -> Result<(), crate::CovenError> {
113    Ok(coven_sync::record_connected_outside_store(connection, location)?)
114}
115
116/// Save `pass` on `connection`, where `make_readable_objects` made coven's
117/// objects, as how the last pass ended, as a store's loop writes it when a
118/// pass ends, so that `coven_sync_status.last_pass` holds it and
119/// `coven_sync_state` computes its state from it beside the pending records
120/// the test saves with `record_pending` (§19.1).
121pub fn record_last_pass(
122    connection: &coven_database::test_utils::Connection,
123    pass: &coven_sync::LastPass,
124) -> Result<(), crate::CovenError> {
125    Ok(pass.record_outside_store(connection)?)
126}
127
128#[cfg(test)]
129#[path = "readable_objects_tests.rs"]
130mod tests;