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
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
//! Every way this server will not come up, and what it says about each.
//!
//! The configuration walk covers the values a *field* can carry wrongly. These
//! are the refusals that are about the host rather than about a field: a port
//! already taken, a state directory that will not hold a store, an instance
//! that refuses the key it was given. Each of them is a supervisor that would
//! otherwise have come up half-working, and what each answers is the thing an
//! operator has to change.

use printobserver_oneharness::ModelName;
use printobserver_server::{CLIENT_CONFIG_FILE, ConfigField, Server, StartError};
use tempfile::TempDir;

use crate::probes::{base_url, refusing_host, silent_host};
use crate::world::{document, set, write};

/// One configuration, with one value changed, started.
async fn started_with(
    root: &std::path::Path,
    octoprint: &str,
    change: impl FnOnce(&mut toml::Value),
) -> Result<printobserver_server::Running, StartError> {
    let mut document = document(root, octoprint);
    change(&mut document);
    let path = write(root, &document);
    Server::start(&path).await
}

/// An address this adapter does not speak to is refused where it is configured.
#[tokio::test(flavor = "multi_thread")]
async fn an_address_this_adapter_does_not_speak_to_is_refused() {
    let root = TempDir::new().expect("a journey's own root");
    let started = started_with(root.path(), "http://127.0.0.1:1", |document| {
        set(
            document,
            "octoprint.url",
            toml::Value::String("https://octoprint.example".to_owned()),
        );
    })
    .await;

    let Err(refusal) = started else {
        panic!("a URL this adapter does not speak was accepted");
    };
    assert_eq!(refusal.field(), Some(ConfigField::OctoprintUrl));
    assert!(
        refusal.to_string().contains("https://octoprint.example"),
        "the refusal does not name the URL: {refusal}"
    );
}

/// An instance that refuses the key refuses the start, naming the key.
#[tokio::test(flavor = "multi_thread")]
async fn an_instance_that_refuses_the_key_refuses_the_start() {
    let refusing = refusing_host().await;
    let root = TempDir::new().expect("a journey's own root");

    let started = started_with(root.path(), &base_url(&refusing), |_| {}).await;

    let Err(refusal) = started else {
        panic!("an instance that refuses the key let the server start");
    };
    assert_eq!(refusal.field(), Some(ConfigField::OctoprintApiKey));
    assert!(
        refusal.to_string().contains("refused it"),
        "the refusal does not say the instance refused the key: {refusal}"
    );
}

/// An address already taken is refused naming it.
#[tokio::test(flavor = "multi_thread")]
async fn an_address_already_taken_is_refused_naming_it() {
    let answering = silent_host().await;
    let taken = tokio::net::TcpListener::bind("127.0.0.1:0")
        .await
        .expect("a loopback port");
    let address = taken.local_addr().expect("the bound address");
    let root = TempDir::new().expect("a journey's own root");

    let started = started_with(root.path(), &base_url(&answering), |document| {
        set(document, "listen", toml::Value::String(address.to_string()));
    })
    .await;

    let Err(refusal) = started else {
        panic!("a server came up on an address already taken");
    };
    assert!(
        matches!(refusal, StartError::Listen { .. }),
        "the refusal is not about the address: {refusal}"
    );
    assert!(
        refusal.to_string().contains(&address.to_string()),
        "the refusal does not name the address: {refusal}"
    );
}

/// A state directory that will not hold a store is refused naming the store.
///
/// Made unwritable through its mode, which Unix alone has; the journey after it
/// refuses the same start on every platform.
#[cfg(unix)]
#[tokio::test(flavor = "multi_thread")]
async fn a_state_directory_that_will_not_hold_a_store_is_refused() {
    use std::os::unix::fs::PermissionsExt as _;

    let answering = silent_host().await;
    let root = TempDir::new().expect("a journey's own root");
    let state = root.path().join("state");
    std::fs::create_dir_all(&state).expect("a state directory");
    std::fs::set_permissions(&state, std::fs::Permissions::from_mode(0o500))
        .expect("the state directory is unwritable");

    let started = started_with(root.path(), &base_url(&answering), |_| {}).await;

    // Put it back before asserting, so a failing journey still leaves a
    // directory its own scratch root can remove.
    std::fs::set_permissions(&state, std::fs::Permissions::from_mode(0o700))
        .expect("the state directory is writable again");

    let Err(refusal) = started else {
        panic!("a server came up over a state directory that cannot hold a store");
    };
    assert!(
        matches!(refusal, StartError::Store { .. }),
        "the refusal is not about the store: {refusal}"
    );
}

/// A state directory with a directory where its database belongs cannot hold a
/// store either, and is refused naming the store — on every platform, modes or
/// none.
#[tokio::test(flavor = "multi_thread")]
async fn a_state_directory_whose_database_path_is_a_directory_is_refused() {
    let answering = silent_host().await;
    let root = TempDir::new().expect("a journey's own root");
    std::fs::create_dir_all(
        root.path()
            .join("state")
            .join(printobserver_store_sqlite::DATABASE_FILE_NAME),
    )
    .expect("a directory where the database belongs");

    let started = started_with(root.path(), &base_url(&answering), |_| {}).await;

    let Err(refusal) = started else {
        panic!("a server came up over a state directory whose database is a directory");
    };
    assert!(
        matches!(refusal, StartError::Store { .. }),
        "the refusal is not about the store: {refusal}"
    );
}

/// A state directory that will not hold the agent's assets is refused.
#[tokio::test(flavor = "multi_thread")]
async fn a_state_directory_that_will_not_hold_the_agents_assets_is_refused() {
    let answering = silent_host().await;
    let root = TempDir::new().expect("a journey's own root");
    let state = root.path().join("state");
    std::fs::create_dir_all(&state).expect("a state directory");
    // The agent's skill, template and schema are materialized under here, and
    // a file where that directory belongs is a host that cannot hold them.
    std::fs::write(
        state.join(printobserver_server::ASSETS_DIRECTORY),
        b"not a directory",
    )
    .expect("a file is writable");

    let started = started_with(root.path(), &base_url(&answering), |_| {}).await;

    let Err(refusal) = started else {
        panic!("a server came up with nowhere to put the agent's own assets");
    };
    assert!(
        matches!(refusal, StartError::State { .. }),
        "the refusal is not about the state directory: {refusal}"
    );
    assert!(
        refusal
            .to_string()
            .contains(printobserver_server::ASSETS_DIRECTORY),
        "the refusal does not name what could not be put in place: {refusal}"
    );
}

/// Every refusal says which it is, in its own words.
#[test]
fn every_refusal_says_which_it_is_in_its_own_words() {
    let refusals = [
        StartError::State {
            detail: "the disk is full".to_owned(),
        },
        StartError::Listen {
            address: "127.0.0.1:8420".parse().expect("an address"),
            detail: "already in use".to_owned(),
        },
        StartError::Store {
            detail: "the disk is full".to_owned(),
        },
        StartError::Supervisor {
            detail: "no such harness".to_owned(),
        },
        StartError::Reconciliation {
            detail: "the disk is full".to_owned(),
        },
    ];
    for refusal in refusals {
        assert!(
            !refusal.to_string().is_empty(),
            "a refusal said nothing: {refusal:?}"
        );
        assert_eq!(
            refusal.field(),
            None,
            "a refusal that is not about a field named one: {refusal}"
        );
    }

    let unreadable = printobserver_server::ConfigError::Unreadable {
        path: "/etc/printobserver/config.toml".into(),
        detail: "no such file".to_owned(),
    };
    assert!(unreadable.to_string().contains("config.toml"));
    assert_eq!(unreadable.field(), None);
    let unparsable = printobserver_server::ConfigError::Unparsable {
        path: "/etc/printobserver/config.toml".into(),
        detail: "expected a value".to_owned(),
    };
    assert!(unparsable.to_string().contains("config.toml"));
    assert_eq!(unparsable.field(), None);
    assert_eq!(
        StartError::from(unparsable).field(),
        None,
        "a document that is not this configuration is about no one field"
    );
}

/// Every field this program takes is spelled once, and spells itself.
#[test]
fn every_field_is_spelled_once_and_spells_itself() {
    let mut spellings: Vec<&str> = ConfigField::ALL.iter().map(|field| field.key()).collect();
    let declared = spellings.len();
    spellings.sort_unstable();
    spellings.dedup();
    assert_eq!(
        spellings.len(),
        declared,
        "two fields are spelled the same way"
    );
    for field in ConfigField::ALL {
        assert_eq!(field.to_string(), field.key());
    }
}

/// A server nobody stopped stops when the handle to it goes.
///
/// A supervisor whose serving task outlived its handle would be a process that
/// went on answering the API after the thing that owned it had gone — and a
/// tier that leaked one per journey would eventually be answering on a hundred
/// ports at once.
#[tokio::test(flavor = "multi_thread")]
async fn a_server_nobody_stopped_stops_when_its_handle_goes() {
    let answering = silent_host().await;
    let root = TempDir::new().expect("a journey's own root");
    let running = started_with(root.path(), &base_url(&answering), |_| {})
        .await
        .expect("the server starts");
    let address = running.address();
    assert!(
        tokio::net::TcpStream::connect(address).await.is_ok(),
        "the server is not answering on the address it took"
    );

    drop(running);

    let deadline = std::time::Instant::now() + core::time::Duration::from_secs(5);
    while std::time::Instant::now() < deadline {
        if tokio::net::TcpStream::connect(address).await.is_err() {
            return;
        }
        tokio::time::sleep(core::time::Duration::from_millis(50)).await;
    }
    panic!("{address} was still answering after the handle to the server had gone");
}

/// A skill and a template the operator supplied are what the agent is built
/// with.
///
/// Editing either on a running host is a restart rather than a rebuild, which
/// is why they are paths. The skill has no fallback — this program carries
/// none — and a configuration naming a template runs the operator's rather
/// than the one this program carries.
#[tokio::test(flavor = "multi_thread")]
async fn a_skill_and_a_template_the_operator_supplied_are_what_is_used() {
    let answering = silent_host().await;
    let root = TempDir::new().expect("a journey's own root");
    let skill = root.path().join("our-own-skill.md");
    let template = root.path().join("our-own-prompt.md");
    std::fs::write(&skill, b"# Our own skill\n").expect("a skill is writable");
    // A template declares the three slots one turn fills, and this is the
    // committed one, so what is changed is where it is read from.
    std::fs::write(&template, printobserver_server::TURN_PROMPT).expect("a template is writable");

    let running = started_with(root.path(), &base_url(&answering), |document| {
        set(
            document,
            "supervisor.skill_path",
            toml::Value::String(skill.display().to_string()),
        );
        set(
            document,
            "supervisor.prompt_template_path",
            toml::Value::String(template.display().to_string()),
        );
        set(
            document,
            "supervisor.model",
            toml::Value::String("claude-opus-5".to_owned()),
        );
    })
    .await
    .expect("a server runs on the skill and the template the operator supplied");

    assert_eq!(running.config().skill_path, skill);
    assert_eq!(
        running.config().prompt_template_path.as_deref(),
        Some(template.as_path())
    );
    assert_eq!(
        running.config().model.as_ref().map(ModelName::as_str),
        Some("claude-opus-5")
    );
    // The template this program carries is written into the state directory
    // only when the operator supplied none.
    assert!(
        !running
            .config()
            .assets_dir()
            .join(printobserver_server::PROMPT_FILE)
            .exists(),
        "a template the operator supplied was replaced by the one this program carries"
    );
    running.stop().await;
}

/// The command every refusal of the skill names, which is how an operator gets
/// one: this program carries none.
const INSTALL: &str = "gh skill install nickderobertis/printobserver printobserver";

/// A configuration naming no skill is refused, naming the field and how to
/// install the skill it should name.
///
/// Every install made before the skill stopped shipping inside this program
/// carries such a configuration, so the refusal is the whole of the upgrade
/// instruction its operator is given.
#[tokio::test(flavor = "multi_thread")]
async fn a_configuration_naming_no_skill_is_refused_saying_how_to_install_one() {
    let answering = silent_host().await;
    let root = TempDir::new().expect("a journey's own root");

    let started = started_with(root.path(), &base_url(&answering), |document| {
        crate::world::remove(document, "supervisor.skill_path");
    })
    .await;

    let Err(refusal) = started else {
        panic!("a server came up with no skill for its agent");
    };
    assert_eq!(refusal.field(), Some(ConfigField::SkillPath));
    let said = refusal.to_string();
    assert!(
        said.contains("supervisor.skill_path") && said.contains(INSTALL),
        "the refusal does not name the field and how to install the skill: {said}"
    );
    assert!(
        !root.path().join("state").join(CLIENT_CONFIG_FILE).exists(),
        "a server refused for its skill still wrote a client configuration"
    );
}

/// A skill path naming nothing readable is refused the same way.
#[tokio::test(flavor = "multi_thread")]
async fn an_unreadable_skill_is_refused_saying_how_to_install_one() {
    let answering = silent_host().await;
    let root = TempDir::new().expect("a journey's own root");
    let absent = root
        .path()
        .join("skills")
        .join("printobserver")
        .join("SKILL.md");

    let started = started_with(root.path(), &base_url(&answering), |document| {
        set(
            document,
            "supervisor.skill_path",
            toml::Value::String(absent.display().to_string()),
        );
    })
    .await;

    let Err(refusal) = started else {
        panic!("a server came up on a skill that is not there");
    };
    assert_eq!(refusal.field(), Some(ConfigField::SkillPath));
    let said = refusal.to_string();
    assert!(
        said.contains("supervisor.skill_path")
            && said.contains("cannot be read")
            && said.contains(INSTALL),
        "the refusal does not name the field, the file and how to install the skill: {said}"
    );
}

/// A template that is not a template refuses the start, in the harness's words.
#[tokio::test(flavor = "multi_thread")]
async fn a_template_that_is_not_a_template_refuses_the_start() {
    let answering = silent_host().await;
    let root = TempDir::new().expect("a journey's own root");
    let template = root.path().join("not-a-template.md");
    std::fs::write(&template, b"this names none of the slots a turn fills\n")
        .expect("a file is writable");

    let started = started_with(root.path(), &base_url(&answering), |document| {
        set(
            document,
            "supervisor.prompt_template_path",
            toml::Value::String(template.display().to_string()),
        );
    })
    .await;

    let Err(refusal) = started else {
        panic!("a server came up on a file that fills no turn");
    };
    assert!(
        matches!(refusal, StartError::Supervisor { .. }),
        "the refusal is not about the supervising agent: {refusal}"
    );
    assert!(
        refusal.to_string().contains("not-a-template.md"),
        "the refusal does not name the file: {refusal}"
    );
}

/// An assets directory nothing can be written into refuses the start.
///
/// Made unwritable through its mode, which Unix alone has; the journey after it
/// refuses the same start on every platform.
#[cfg(unix)]
#[tokio::test(flavor = "multi_thread")]
async fn an_assets_directory_nothing_can_be_written_into_refuses_the_start() {
    use std::os::unix::fs::PermissionsExt as _;

    let answering = silent_host().await;
    let root = TempDir::new().expect("a journey's own root");
    let assets = root
        .path()
        .join("state")
        .join(printobserver_server::ASSETS_DIRECTORY);
    std::fs::create_dir_all(&assets).expect("an assets directory");
    std::fs::set_permissions(&assets, std::fs::Permissions::from_mode(0o500))
        .expect("the assets directory is unwritable");

    let started = started_with(root.path(), &base_url(&answering), |_| {}).await;

    std::fs::set_permissions(&assets, std::fs::Permissions::from_mode(0o700))
        .expect("the assets directory is writable again");

    let Err(refusal) = started else {
        panic!("a server came up with nowhere to write the agent's own assets");
    };
    assert!(
        matches!(refusal, StartError::State { .. }),
        "the refusal is not about the state directory: {refusal}"
    );
}

/// An assets path holding a file refuses the start naming it — on every
/// platform, modes or none.
#[tokio::test(flavor = "multi_thread")]
async fn an_assets_path_holding_a_file_refuses_the_start() {
    let answering = silent_host().await;
    let root = TempDir::new().expect("a journey's own root");
    let state = root.path().join("state");
    std::fs::create_dir_all(&state).expect("a state directory");
    let assets = state.join(printobserver_server::ASSETS_DIRECTORY);
    std::fs::write(&assets, "not a directory").expect("a file where the assets belong");

    let started = started_with(root.path(), &base_url(&answering), |_| {}).await;

    let Err(refusal) = started else {
        panic!("a server came up with a file where the agent's own assets belong");
    };
    assert!(
        matches!(refusal, StartError::State { .. }),
        "the refusal is not about the state directory: {refusal}"
    );
    assert!(
        refusal
            .to_string()
            .contains(printobserver_server::ASSETS_DIRECTORY),
        "the refusal does not name the assets path: {refusal}"
    );
}

/// One way a value can be written wrongly: what it is, the field it is about,
/// what the refusal has to say, and the change that makes it so.
struct Wrongly {
    /// What the case is, for a reader of a failure message.
    described: &'static str,
    /// The field the refusal must name.
    field: ConfigField,
    /// What the refusal must say.
    saying: &'static str,
    /// The one value it changes.
    change: Box<dyn Fn(&mut toml::Value)>,
}

/// Every value the safety envelope and the ingress bound refuse is refused
/// where it is configured.
///
/// The field walk gives each field one unacceptable value of its own kind;
/// these are the other ways the same two fields can be written wrongly, and
/// each of them is a supervisor that would otherwise have come up granting
/// nothing, or answering an alert Obico had already abandoned.
#[tokio::test(flavor = "multi_thread")]
async fn every_other_way_the_envelope_and_the_bound_can_be_wrong_is_refused() {
    let answering = silent_host().await;
    let base = base_url(&answering);
    let cases = [
        Wrongly {
            described: "an envelope allowing nothing",
            field: ConfigField::SafetyEnvelope,
            saying: "no adjustable at all",
            change: Box::new(|document: &mut toml::Value| {
                set(
                    document,
                    "safety.allowed",
                    toml::Value::Table(toml::Table::new()),
                );
            }),
        },
        Wrongly {
            described: "an agent interval that is not a count of seconds",
            field: ConfigField::SafetyEnvelope,
            saying: "count of seconds",
            change: Box::new(|document: &mut toml::Value| {
                set(
                    document,
                    "safety.agent_min_interval_s",
                    toml::Value::Integer(-30),
                );
            }),
        },
        Wrongly {
            described: "an answer bound of zero",
            field: ConfigField::IngressAnswerBoundMs,
            saying: "run out of time before it starts",
            change: Box::new(|document: &mut toml::Value| {
                set(document, "ingress.answer_bound_ms", toml::Value::Integer(0));
            }),
        },
    ];

    for case in cases {
        let root = TempDir::new().expect("a journey's own root");
        let started = started_with(root.path(), &base, case.change).await;

        let Err(refusal) = started else {
            panic!("{} was accepted", case.described);
        };
        assert_eq!(
            refusal.field(),
            Some(case.field),
            "{} was refused naming {:?}: {refusal}",
            case.described,
            refusal.field()
        );
        assert!(
            refusal.to_string().contains(case.saying),
            "{} was refused without saying why: {refusal}",
            case.described
        );
    }
}

/// An asset of the agent's that cannot be written refuses the start naming it
/// — on every platform, modes or none: a directory where the file belongs is
/// a path nothing can write a file to.
#[tokio::test(flavor = "multi_thread")]
async fn an_asset_that_cannot_be_written_refuses_the_start_naming_it() {
    let answering = silent_host().await;
    let root = TempDir::new().expect("a journey's own root");
    let assets = root
        .path()
        .join("state")
        .join(printobserver_server::ASSETS_DIRECTORY);
    std::fs::create_dir_all(assets.join(printobserver_server::SCHEMA_FILE))
        .expect("a directory where the schema belongs");

    let started = started_with(root.path(), &base_url(&answering), |_| {}).await;

    let Err(refusal) = started else {
        panic!("a server came up with no schema for the agent to answer in");
    };
    assert!(
        matches!(refusal, StartError::State { .. }),
        "the refusal is not about the state directory: {refusal}"
    );
    assert!(
        refusal.to_string().contains(&format!(
            "{} could not be written",
            printobserver_server::SCHEMA_FILE
        )),
        "the refusal does not name the asset: {refusal}"
    );
}

/// A state directory that cannot hold the configured harness's sign-in refuses
/// the start naming the directory, rather than starting turns that could never
/// find a sign-in.
#[tokio::test(flavor = "multi_thread")]
async fn a_state_directory_that_cannot_hold_the_harnesss_sign_in_refuses_the_start() {
    let answering = silent_host().await;
    let root = TempDir::new().expect("a journey's own root");
    let state = root.path().join("state");
    std::fs::create_dir_all(&state).expect("a state directory");
    // A file where the directory every harness's own directory goes in has to be.
    std::fs::write(
        state.join(printobserver_server::HARNESS_DIRECTORY),
        "not a directory",
    )
    .expect("a file is writable");

    let started = started_with(root.path(), &base_url(&answering), |_| {}).await;

    let Err(refusal) = started else {
        panic!("a server came up with nowhere to keep its harness's sign-in");
    };
    assert!(
        matches!(refusal, StartError::State { .. }),
        "the refusal is not about the state directory: {refusal}"
    );
    let named = std::path::Path::new(printobserver_server::HARNESS_DIRECTORY).join("claude-code");
    assert!(
        refusal
            .to_string()
            .contains(&format!("{} could not be created", named.display())),
        "the refusal does not name the sign-in's directory: {refusal}"
    );
}

/// A secret and a state directory that name nothing are each refused where
/// they are configured, saying why.
///
/// Neither is a value of the wrong kind, which the field walk covers: each is
/// text that parses and still leaves a supervisor that anything could pause, or
/// one with nowhere to keep its state.
#[tokio::test(flavor = "multi_thread")]
async fn a_secret_or_a_state_directory_that_names_nothing_is_refused() {
    let answering = silent_host().await;
    let base = base_url(&answering);
    let cases = [
        Wrongly {
            described: "a shared secret that is only whitespace",
            field: ConfigField::IngressSharedSecret,
            saying: "anything that can post to the ingress can pause a printer",
            change: Box::new(|document: &mut toml::Value| {
                set(
                    document,
                    "ingress.shared_secret",
                    toml::Value::String("   ".to_owned()),
                );
            }),
        },
        Wrongly {
            described: "an empty state directory",
            field: ConfigField::StateDir,
            saying: "names no directory",
            change: Box::new(|document: &mut toml::Value| {
                set(document, "state_dir", toml::Value::String(String::new()));
            }),
        },
    ];

    for case in cases {
        let root = TempDir::new().expect("a journey's own root");
        let started = started_with(root.path(), &base, case.change).await;

        let Err(refusal) = started else {
            panic!("{} was accepted", case.described);
        };
        assert_eq!(
            refusal.field(),
            Some(case.field),
            "{} was refused naming {:?}: {refusal}",
            case.described,
            refusal.field()
        );
        assert!(
            refusal.to_string().contains(case.saying),
            "{} was refused without saying why: {refusal}",
            case.described
        );
    }
}