printobserver-server 0.20.2

The printobserver server.
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
//! The served route set, held to the contracts rather than to a list here.
//!
//! The required operation set is **derived from the contracts' own
//! `PrintAction` and `SpoolCommand`** — one operation per variant each declares,
//! read out of the schema that type generates in this process — plus the
//! operations this server is required to serve beside them. Growing the declared list and the routes together
//! cannot satisfy this, because the side it is compared against is another
//! crate's closed type.
//!
//! Coverage is not the whole of the contract, so [`audit`] also rules on the
//! shape every route is served under: one versioned prefix for all of them, a
//! JSON body where a body is taken at all, and a JSON answer. Every one of its
//! refusals is driven against a doctored set beside the served one it accepts,
//! so a check that reported nothing whatever it was handed cannot pass.

use std::collections::BTreeSet;

use printobserver_core::{PrintAction, RecoveryAction, SpoolCommand};
use printobserver_server::{
    BESIDE_THE_ACTIONS, Effect, MEDIA_TYPE, Method, OPERATIONS, Operation, RECOVERY_STATUS,
};
use printobserver_types::serde_json::{Value, json};

use crate::world::{World, manifest_write};

/// One route as the server serves it: the whole path, and its two media types.
#[derive(Debug, Clone, PartialEq, Eq)]
struct Served {
    /// The operation's own name.
    name: String,
    /// The method it is reached by.
    method: Method,
    /// Its whole path, prefix included.
    path: String,
    /// The media type it takes a body in, when it takes one.
    accepts: Option<String>,
    /// The media type it answers in.
    answers: String,
}

impl Served {
    /// The route one declared operation is served as.
    fn of(operation: &Operation) -> Self {
        Self {
            name: operation.name.to_owned(),
            method: operation.method,
            path: operation.full_path(),
            accepts: operation.accepts.map(str::to_owned),
            answers: operation.answers.to_owned(),
        }
    }
}

/// The route set this server serves, read off its own declaration.
fn served() -> Vec<Served> {
    OPERATIONS.iter().map(Served::of).collect()
}

/// One operation per variant the contracts' own `PrintAction`, `SpoolCommand`
/// and `RecoveryAction` declare, the recovery read beside the last, plus the
/// operations this server is required to serve beside them.
///
/// Read out of the schema that type generates rather than out of a list here,
/// so a variant added to the contracts enters this set the moment it is added.
fn required() -> BTreeSet<String> {
    let schema = printobserver_types::contract::schema_of::<PrintAction>();
    let mut found: BTreeSet<String> = schema["oneOf"]
        .as_array()
        .expect("the action vocabulary is a union")
        .iter()
        .map(|variant| {
            variant["properties"]["action"]["const"]
                .as_str()
                .expect("every variant of the vocabulary is tagged with its own name")
                .to_owned()
        })
        .collect();
    assert!(
        found.len() >= 10,
        "the contracts declare only {} actions, which is not the vocabulary",
        found.len()
    );
    let ledger = printobserver_types::contract::schema_of::<SpoolCommand>();
    let commands: Vec<String> = ledger["oneOf"]
        .as_array()
        .expect("the spool vocabulary is a union")
        .iter()
        .map(|variant| {
            variant["properties"]["command"]["const"]
                .as_str()
                .expect("every spool command is tagged with its own name")
                .to_owned()
        })
        .collect();
    assert!(
        commands.len() >= 5,
        "the contracts declare only {} spool commands, which is not the vocabulary",
        commands.len()
    );
    found.extend(commands);
    let recovery = printobserver_types::contract::schema_of::<RecoveryAction>();
    let recoveries: Vec<String> = recovery["oneOf"]
        .as_array()
        .expect("the recovery vocabulary is a union")
        .iter()
        .map(|variant| {
            variant["properties"]["action"]["const"]
                .as_str()
                .expect("every recovery action is tagged with its own name")
                .to_owned()
        })
        .collect();
    assert!(
        recoveries.len() >= 7,
        "the contracts declare only {} recovery actions, which is not the vocabulary",
        recoveries.len()
    );
    found.extend(recoveries);
    found.insert(RECOVERY_STATUS.to_owned());
    found.extend(BESIDE_THE_ACTIONS.iter().map(|name| (*name).to_owned()));
    found
}

/// Every way a served set can disagree with what this server is required to be.
fn audit(served: &[Served], required: &BTreeSet<String>) -> Vec<String> {
    let mut findings = Vec::new();
    let names: BTreeSet<String> = served.iter().map(|route| route.name.clone()).collect();

    findings.extend(
        served
            .iter()
            .filter(|route| !required.contains(&route.name))
            .map(|route| {
                format!(
                    "`{} {}` serves `{}`, which is no operation this server is required \
                     to have",
                    route.method, route.path, route.name
                )
            }),
    );
    findings.extend(
        required
            .iter()
            .filter(|name| !names.contains(*name))
            .map(|name| format!("`{name}` is an operation with no served route")),
    );

    let prefixes: BTreeSet<&str> = served
        .iter()
        .map(|route| route.path.split('/').nth(1).map_or("", |segment| segment))
        .collect();
    if prefixes.len() > 1 {
        let spellings: Vec<String> = prefixes.iter().map(|prefix| format!("/{prefix}")).collect();
        findings.push(format!(
            "two versioned prefixes are in use at once: {}",
            spellings.join(" and ")
        ));
    }
    for route in served {
        if !route.path.starts_with(printobserver_server::VERSION_PREFIX) {
            findings.push(format!(
                "`{}` is served at {}, outside the one versioned prefix {}",
                route.name,
                route.path,
                printobserver_server::VERSION_PREFIX
            ));
        }
        if route
            .accepts
            .as_deref()
            .is_some_and(|media| media != MEDIA_TYPE)
        {
            findings.push(format!(
                "`{}` takes a body as {:?} rather than as {MEDIA_TYPE}",
                route.name, route.accepts
            ));
        }
        if route.answers != MEDIA_TYPE {
            findings.push(format!(
                "`{}` answers as {} rather than as {MEDIA_TYPE}",
                route.name, route.answers
            ));
        }
    }
    findings
}

/// The served set is exactly one route per operation, under one prefix, in JSON.
#[test]
fn the_served_routes_are_exactly_the_operations_the_contracts_declare() {
    assert_eq!(
        audit(&served(), &required()),
        Vec::<String>::new(),
        "the served route set is not the operation set the contracts declare"
    );
    assert_eq!(
        served().len(),
        required().len(),
        "the served set and the required set are the same names in different numbers"
    );
}

/// The audit refuses each way a served set can be wrong.
///
/// Driven against the served set with one thing changed in it, so what refuses
/// the fixture is what reads the real pair.
#[test]
fn the_audit_refuses_each_way_a_served_set_can_be_wrong() {
    let required = required();

    let mut extra = served();
    extra.push(Served {
        name: "reboot".to_owned(),
        method: Method::Post,
        path: "/v1/prints/{print_id}/actions/reboot".to_owned(),
        accepts: Some(MEDIA_TYPE.to_owned()),
        answers: MEDIA_TYPE.to_owned(),
    });
    refuses(
        &audit(&extra, &required),
        "is no operation this server is required",
    );

    let mut missing = served();
    missing.retain(|route| route.name != "pause");
    refuses(
        &audit(&missing, &required),
        "`pause` is an operation with no served route",
    );

    let mut grown = required.clone();
    grown.insert("reboot".to_owned());
    refuses(
        &audit(&served(), &grown),
        "`reboot` is an operation with no served route",
    );

    let mut two_prefixes = served();
    two_prefixes[0].path = two_prefixes[0].path.replace("/v1/", "/v2/");
    refuses(&audit(&two_prefixes, &required), "two versioned prefixes");

    let mut unprefixed = served();
    unprefixed[0].path = unprefixed[0].path.replace("/v1", "");
    refuses(
        &audit(&unprefixed, &required),
        "outside the one versioned prefix",
    );

    let mut form_encoded = served();
    let index = form_encoded
        .iter()
        .position(|route| route.accepts.is_some())
        .expect("some operation takes a body");
    form_encoded[index].accepts = Some("application/x-www-form-urlencoded".to_owned());
    refuses(&audit(&form_encoded, &required), "takes a body as");

    let mut html = served();
    html[0].answers = "text/html".to_owned();
    refuses(&audit(&html, &required), "answers as text/html");
}

/// Fail unless some finding names what was expected.
fn refuses(findings: &[String], naming: &str) {
    assert!(
        findings.iter().any(|finding| finding.contains(naming)),
        "expected a finding naming {naming:?}; got {findings:?}"
    );
}

/// Every declared route is really served, and answers JSON.
///
/// The declaration and the router are one thing by construction — the router is
/// built by folding over the declaration — and this is what proves the fold
/// reached the network: each route is driven over HTTP and answers something
/// other than "there is no such route".
#[tokio::test(flavor = "multi_thread")]
async fn every_declared_route_is_served_and_answers_json() {
    let world = World::open().await;
    let print_id = world.open_print().await;

    for operation in OPERATIONS {
        let url = world.operation_url(&operation.full_path(), print_id);
        // `world.get`, `world.post` and `world.put` refuse an answer that is
        // not JSON, and a path no route is served at answers nothing at all —
        // so reaching a JSON answer at every declared path is what says the
        // fold from the declaration to the router reached the network.
        let url = if operation.name == "preflight" {
            format!("{url}?file_name=benchy.gcode")
        } else {
            url
        };
        let (status, _) = match operation.method {
            Method::Get => world.get(&url.replace("{image_id}", &image_id())).await,
            Method::Post => world.post(&url, &body_for(&operation)).await,
            Method::Put => world.put(&url, &manifest_write(&manifest())).await,
        };
        assert_ne!(
            status,
            reqwest::StatusCode::METHOD_NOT_ALLOWED,
            "`{}` is declared as {} and `{url}` does not take it",
            operation.name,
            operation.method
        );
    }

    // A path this server declares no operation for is served by nothing, which
    // is the other half of "one route per operation and no more": what answers
    // it is the versioned prefix's own refusal, and no operation's answer.
    let (status, media, body) = world.raw_get(&world.url("/prints/reboot")).await;
    assert_eq!(status, reqwest::StatusCode::NOT_FOUND);
    assert!(
        media.starts_with(MEDIA_TYPE)
            && body.contains("no operation of this server is served at that path"),
        "a path this server declares no operation for reached a handler: {media} {body}"
    );
    world.server.stop().await;
}

/// What a running server says about itself carries no secret.
///
/// A long-running host writes down what it came up as, and the things that
/// would be worst to find in that record are the `OctoPrint` key, the ingress
/// secret and the API credential. None is in any of these renderings.
#[tokio::test(flavor = "multi_thread")]
async fn what_a_running_server_says_about_itself_carries_no_secret() {
    let world = World::open().await;
    let state = printobserver_server::ApiState {
        supervisor: std::sync::Arc::clone(world.server.supervisor()),
        prints: std::sync::Arc::clone(&world.server.stores().prints),
        events: std::sync::Arc::clone(&world.server.stores().events),
        images: std::sync::Arc::clone(&world.server.stores().images),
        sessions: std::sync::Arc::clone(&world.server.stores().sessions),
        operator: std::sync::Arc::new(printobserver_server::OperatorCredential::Plaintext(
            printobserver_server::ApiCredential::new(&world.credential)
                .expect("the operator's credential is one"),
        )),
        turns: printobserver_server::TurnCredentials::new(None),
        stored: printobserver_server::Stored::channel().0,
        check_images: None,
        harness: std::sync::Arc::new(printobserver_server::HarnessCheck::new(
            world.server.config(),
            std::sync::Arc::clone(&world.agent)
                as std::sync::Arc<dyn printobserver_supervisor_api::SupervisorPort>,
        )),
    };
    let rendered = format!(
        "{state:?} {:?} {:?}",
        world.server,
        printobserver_server::Ports {
            printer: std::sync::Arc::clone(&world.printer)
                as std::sync::Arc<dyn printobserver_printer_api::PrinterPort>,
            stores: world.server.stores().clone(),
            vision: std::sync::Arc::new(
                printobserver_obico::ObicoVision::new(
                    printobserver_obico::ObicoVisionConfig::default()
                )
                .expect("the adapter is built")
            ),
            agent: std::sync::Arc::clone(&world.agent)
                as std::sync::Arc<dyn printobserver_supervisor_api::SupervisorPort>,
        }
    );

    assert!(
        !rendered.contains(crate::world::SECRET),
        "a rendering of this server carries the ingress secret: {rendered}"
    );
    assert!(
        !rendered.contains(&world.credential),
        "a rendering of this server carries the API credential: {rendered}"
    );
    assert!(
        rendered.contains("ApiState") && rendered.contains("Running") && rendered.contains("Ports"),
        "a rendering of this server says nothing about what it is: {rendered}"
    );
    world.server.stop().await;
}

/// A route that takes a body refuses one that is not JSON.
#[tokio::test(flavor = "multi_thread")]
async fn a_body_that_is_not_json_is_refused() {
    let world = World::open().await;
    let print_id = world.open_print().await;
    let operation = printobserver_server::operation("pause").expect("pause is served");

    let response = world
        .client
        .post(world.operation_url(&operation.full_path(), print_id))
        .header(
            reqwest::header::CONTENT_TYPE,
            "application/x-www-form-urlencoded",
        )
        .body("reason=because")
        .send()
        .await
        .expect("the server answers");

    assert_eq!(
        response.status(),
        reqwest::StatusCode::UNSUPPORTED_MEDIA_TYPE,
        "a body that is not JSON was taken"
    );
    world.server.stop().await;
}

/// An identifier of an image nothing holds, spelled the one way this system
/// spells one.
pub fn image_id() -> String {
    printobserver_types::ImageId::new().to_string()
}

/// A manifest a write can carry.
fn manifest() -> Value {
    printobserver_types::serde_json::to_value(crate::world::start_manifest())
        .expect("a manifest renders")
}

/// A body one mutating operation takes, carrying whatever that action needs.
pub fn body_for(operation: &Operation) -> Value {
    if let Effect::Command(kind) = operation.effect {
        return crate::world::spool_body(kind.action_kind());
    }
    let Effect::Mutating(kind) = operation.effect else {
        return json!({});
    };
    let mut body = json!({
        "reason": "a journey is asking",
        "actor": "operator",
    });
    let object = body.as_object_mut().expect("the body is an object");
    match kind {
        printobserver_core::ActionKind::StartPrint => {
            object.insert("file_name".to_owned(), json!("benchy.gcode"));
            object.insert("manifest".to_owned(), manifest());
        }
        printobserver_core::ActionKind::SetFeedrateFactor
        | printobserver_core::ActionKind::SetFlowrateFactor => {
            object.insert("factor".to_owned(), json!(1.0));
        }
        printobserver_core::ActionKind::SetToolTargetC => {
            object.insert("tool".to_owned(), json!(0));
            object.insert("target_c".to_owned(), json!(215.0));
        }
        printobserver_core::ActionKind::SetBedTargetC => {
            object.insert("target_c".to_owned(), json!(60.0));
        }
        printobserver_core::ActionKind::SetFanPercent => {
            object.insert("percent".to_owned(), json!(40.0));
        }
        printobserver_core::ActionKind::AcknowledgeFailure => {
            object.insert(
                "event_id".to_owned(),
                json!(printobserver_types::EventId::new()),
            );
            object.insert("disposition".to_owned(), json!("continue"));
        }
        _ => {}
    }
    body
}