cortexkit-bus-testkit 0.9.2

Private, binary-backed NATS and subc daemons for CortexKit module tests.
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
#![cfg(unix)]

use cortexkit_bus_testkit::{
    skip_or_fail, BusOptions, BusServer, DaemonOptions, Module, PrivateDaemon, RigError,
};
use futures_util::StreamExt;
use serde_json::{json, Value};
use std::{
    io::{self, Read},
    path::{Path, PathBuf},
    process::Command,
    time::{Duration, Instant, SystemTime},
};
use subc_control::{ClientControlRequest, ClientControlResponse};

const LIMIT: Duration = Duration::from_secs(30);

fn prerequisites() -> bool {
    for result in [
        cortexkit_bus_testkit::ck_subc_binary(),
        cortexkit_bus_testkit::ck_bus_binary(),
        cortexkit_bus_testkit::nats_server_bin().map(|(path, _)| path),
    ] {
        if let Err(error) = result {
            skip_or_fail(error);
            return false;
        }
    }
    true
}

async fn bus() -> BusServer {
    BusServer::start(BusOptions::default()).await.unwrap()
}

async fn wait_file(path: &Path, daemon: Option<&PrivateDaemon>) {
    let deadline = Instant::now() + LIMIT;
    while !path.is_file() {
        assert!(
            Instant::now() < deadline,
            "file never appeared: {}; {}",
            path.display(),
            daemon.map(PrivateDaemon::log_text).unwrap_or_default()
        );
        tokio::time::sleep(Duration::from_millis(50)).await;
    }
}

#[test]
fn binary_policy_probe() {
    let Ok(which) = std::env::var("TESTKIT_BINARY_PROBE") else {
        return;
    };
    let error = match which.as_str() {
        "nats" => cortexkit_bus_testkit::nats_server_bin().err().unwrap(),
        "bus" => cortexkit_bus_testkit::ck_bus_binary().unwrap_err(),
        "subc" => cortexkit_bus_testkit::ck_subc_binary().unwrap_err(),
        _ => panic!("unknown probe"),
    };
    let variable = match which.as_str() {
        "nats" => "CK_NATS_SERVER_BIN",
        "bus" => "CK_BUS_BIN",
        _ => "CK_SUBC_BIN",
    };
    assert!(
        matches!(&error, RigError::MissingBinary { env_var, how_to_fix, .. }
        if *env_var == variable && how_to_fix.contains(variable))
    );
    assert!(error.to_string().contains(variable));
    let required = std::env::var("CK_BUS_TESTKIT_REQUIRE").as_deref() == Ok("1");
    assert_eq!(
        std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| skip_or_fail(error))).is_err(),
        required
    );
    assert!(
        std::panic::catch_unwind(|| skip_or_fail(RigError::Invalid("broken rig".into()))).is_err()
    );
}

#[test]
fn missing_binaries_and_require_policy() {
    let executable = cortexkit_test_support::ckdev_binary(std::env::current_exe().unwrap());
    for which in ["nats", "bus", "subc"] {
        for required in [false, true] {
            let mut command = Command::new(&executable);
            command
                .args(["--exact", "binary_policy_probe", "--nocapture"])
                .env("TESTKIT_BINARY_PROBE", which)
                .env("CK_NATS_SERVER_BIN", "/nonexistent/testkit/nats-server")
                .env_remove("CK_BUS_BIN")
                .env_remove("CK_SUBC_BIN")
                .env_remove("CK_BUS_TESTKIT_REQUIRE");
            if required {
                command.env("CK_BUS_TESTKIT_REQUIRE", "1");
            }
            let output = command.output().unwrap();
            assert!(
                output.status.success(),
                "{which} require={required}: {} {}",
                String::from_utf8_lossy(&output.stdout),
                String::from_utf8_lossy(&output.stderr)
            );
            assert!(String::from_utf8_lossy(&output.stdout).contains("1 passed"));
        }
    }
}

fn read_optional(path: &Path) -> Option<Vec<u8>> {
    match std::fs::read(path) {
        Ok(bytes) => Some(bytes),
        Err(error) if error.kind() == io::ErrorKind::NotFound => None,
        Err(error) => panic!("read {}: {error}", path.display()),
    }
}

struct ProtectedFile {
    path: PathBuf,
    bytes: Option<Vec<u8>>,
    modified: Option<SystemTime>,
}

impl ProtectedFile {
    fn capture(path: PathBuf, check_mtime: bool) -> Self {
        let (bytes, modified) = match std::fs::File::open(&path) {
            Ok(mut file) => {
                let modified = check_mtime.then(|| file.metadata().unwrap().modified().unwrap());
                let mut bytes = Vec::new();
                file.read_to_end(&mut bytes).unwrap();
                (Some(bytes), modified)
            }
            Err(error) if error.kind() == io::ErrorKind::NotFound => (None, None),
            Err(error) => panic!("open {}: {error}", path.display()),
        };
        Self {
            path,
            bytes,
            modified,
        }
    }

    fn assert_unchanged(&self) {
        let after = Self::capture(self.path.clone(), self.modified.is_some());
        // Connection files contain authentication keys; never include their bytes
        // in assertion diagnostics.
        assert!(
            self.bytes == after.bytes,
            "production file bytes changed: {}",
            self.path.display()
        );
        assert!(
            self.modified == after.modified,
            "production file mtime changed: {}",
            self.path.display()
        );
    }
}

fn inherited_home(variable: &str, fallback: PathBuf) -> PathBuf {
    std::env::var_os(variable)
        .filter(|value| !value.is_empty())
        .map(PathBuf::from)
        .unwrap_or(fallback)
}

fn production_connection_files() -> Vec<PathBuf> {
    if let Some(runtime) = std::env::var_os("XDG_RUNTIME_DIR").filter(|value| !value.is_empty()) {
        return vec![PathBuf::from(runtime).join(subc_transport::CONNECTION_FILE_NAME)];
    }
    vec![
        std::env::temp_dir().join(subc_transport::CONNECTION_FILE_NAME),
        // Current daemons use a per-user fallback when XDG_RUNTIME_DIR is absent.
        std::env::temp_dir().join(format!(
            "subc-{}.connection.json",
            subc_transport::connection_file::user_connection_token()
        )),
    ]
}

fn assert_no_rig_pids(path: &Path, rig_pids: &[u32]) {
    let Some(bytes) = read_optional(path) else {
        return;
    };
    let record: Value = serde_json::from_slice(&bytes).expect("live-children record is JSON");
    for child in record["children"]
        .as_array()
        .expect("live-children has a children array")
    {
        let pid = u32::try_from(child["pid"].as_u64().expect("live child has a pid")).unwrap();
        assert!(
            !rig_pids.contains(&pid),
            "production live-children record {} contains rig pid {pid}",
            path.display()
        );
    }
}

fn assert_no_rig_paths(root: &Path, markers: &[&str]) {
    let mut directories = vec![root.to_path_buf()];
    while let Some(directory) = directories.pop() {
        let entries = match std::fs::read_dir(&directory) {
            Ok(entries) => entries,
            Err(error) if error.kind() == io::ErrorKind::NotFound => continue,
            Err(error) => panic!("scan {}: {error}", directory.display()),
        };
        for entry in entries {
            let entry = match entry {
                Ok(entry) => entry,
                Err(error) if error.kind() == io::ErrorKind::NotFound => continue,
                Err(error) => panic!("scan {}: {error}", directory.display()),
            };
            let path = entry.path();
            for marker in markers {
                assert!(
                    !path.to_string_lossy().contains(marker),
                    "rig marker {marker:?} in production path {}",
                    path.display()
                );
            }
            match entry.file_type() {
                Ok(kind) if kind.is_dir() => directories.push(path),
                // Live modules may remove unrelated files while the scan runs.
                Ok(_) => {}
                Err(error) if error.kind() == io::ErrorKind::NotFound => {}
                Err(error) => panic!("inspect {}: {error}", path.display()),
            }
        }
    }
}

async fn wait_private_child_record(daemon: &PrivateDaemon, module_id: &str, pid: u32) {
    let path = daemon.data_home.join("cortexkit/run/live-children.json");
    let deadline = Instant::now() + LIMIT;
    loop {
        if let Some(bytes) = read_optional(&path) {
            let record: Value = serde_json::from_slice(&bytes).unwrap();
            if record["children"]
                .as_array()
                .unwrap()
                .iter()
                .any(|child| child["module_id"] == module_id && child["pid"] == pid)
            {
                return;
            }
        }
        assert!(
            Instant::now() < deadline,
            "private record never listed {module_id} pid {pid}: {}",
            daemon.log_text()
        );
        tokio::time::sleep(Duration::from_millis(50)).await;
    }
}

#[test]
fn production_record_rejects_each_rig_pid() {
    let root = tempfile::tempdir().unwrap();
    let record = root.path().join("live-children.json");
    let rig_pids = [101, 102, 103, 104];
    let write_record = |pid| {
        std::fs::write(
            &record,
            serde_json::to_vec(&json!({
                "version": 1, "children": [{"module_id": "unrelated", "pid": pid}],
            }))
            .unwrap(),
        )
        .unwrap();
    };
    assert_no_rig_pids(&record, &rig_pids);
    write_record(999);
    assert_no_rig_pids(&record, &rig_pids);
    for pid in rig_pids {
        write_record(pid);
        assert!(
            std::panic::catch_unwind(|| assert_no_rig_pids(&record, &rig_pids)).is_err(),
            "production record must reject rig pid {pid}"
        );
    }
}

#[test]
fn production_path_scan_ignores_unrelated_writes_and_rejects_rig_markers() {
    let root = tempfile::tempdir().unwrap();
    let module_id = "bustest-unique-fixture";
    let root_name = ".tmp-unique-fixture";
    let markers = [module_id, root_name];
    assert_no_rig_paths(&root.path().join("absent"), &markers);
    std::fs::create_dir(root.path().join("aft")).unwrap();
    std::fs::write(root.path().join("aft/live.log"), b"first write").unwrap();
    assert_no_rig_paths(root.path(), &markers);
    std::fs::write(root.path().join("aft/another.log"), b"unrelated new file").unwrap();
    std::fs::write(root.path().join("aft/live.log"), b"later write").unwrap();
    assert_no_rig_paths(root.path(), &markers);
    for marker in markers {
        let leaked = root
            .path()
            .join("aft")
            .join(format!("prefix-{marker}-suffix"));
        std::fs::write(&leaked, b"leak").unwrap();
        assert!(
            std::panic::catch_unwind(|| assert_no_rig_paths(root.path(), &markers)).is_err(),
            "production scan must reject marker {marker}"
        );
        std::fs::remove_file(leaked).unwrap();
    }
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn private_daemon_isolates_and_cleans_up() {
    if !prerequisites() {
        return;
    }
    let home = PathBuf::from(std::env::var_os("HOME").unwrap());
    let real_data = inherited_home("XDG_DATA_HOME", home.join(".local/share")).join("cortexkit");
    let real_config =
        inherited_home("XDG_CONFIG_HOME", home.join(".config")).join("cortexkit/subc.jsonc");
    let real_children = real_data.join("run/live-children.json");
    let mut protected = vec![
        ProtectedFile::capture(real_data.join("machine-id"), true),
        ProtectedFile::capture(real_config, false),
    ];
    protected.extend(
        production_connection_files()
            .into_iter()
            .map(|path| ProtectedFile::capture(path, false)),
    );
    let module_id = format!("bustest-{}", cortexkit_bus_testkit::new_machine_id());
    let bus = bus().await;
    let bus_root = bus.dir.clone();
    let bus_pid = bus.pid();
    let daemon = PrivateDaemon::start(
        &bus,
        DaemonOptions {
            modules: vec![test_module(&module_id)],
            ..Default::default()
        },
    )
    .await
    .unwrap();
    wait_file(&daemon.runtime_dir.join("module-homes.json"), Some(&daemon)).await;
    let homes: Value = serde_json::from_slice(
        &std::fs::read(daemon.runtime_dir.join("module-homes.json")).unwrap(),
    )
    .unwrap();
    assert_eq!(homes["data"], daemon.data_home.display().to_string());
    assert_eq!(homes["runtime"], daemon.runtime_dir.display().to_string());
    assert_eq!(homes["config"], daemon.config_home.display().to_string());
    // The daemon allocated a real per-module SQLite descriptor under the private
    // data home, as production does, so a module opening a leased store can run.
    wait_file(
        &daemon.runtime_dir.join("module-storage.json"),
        Some(&daemon),
    )
    .await;
    let storage: Value = serde_json::from_slice(
        &std::fs::read(daemon.runtime_dir.join("module-storage.json")).unwrap(),
    )
    .unwrap();
    assert_eq!(
        storage["backend"]["backend"], "sqlite",
        "storage descriptor: {storage}"
    );
    assert_eq!(
        storage["backend"]["path"],
        format!(
            "{}/cortexkit/{module_id}/store.db",
            daemon.data_home.display()
        ),
        "storage descriptor: {storage}"
    );
    let root = daemon.root().to_path_buf();
    let pid = daemon.pid();
    let module_pids = [
        supervised_pid(&daemon, "ckbus").await,
        homes["pid"].as_u64().unwrap() as u32,
    ];
    wait_private_child_record(&daemon, &module_id, module_pids[1]).await;
    let rig_pids = [pid, module_pids[0], module_pids[1], bus_pid];
    let root_name = root.file_name().unwrap().to_str().unwrap();
    assert!(
        subc_os::Process::open(module_pids[1]).unwrap().is_some(),
        "probe exited: {}",
        daemon.log_text()
    );
    // Positive evidence matters: clean production paths alone would also pass if
    // the daemon had never started or the identity had been written in the wrong tree.
    assert!(daemon.data_home.join("cortexkit/machine-id").is_file());
    assert!(daemon
        .data_home
        .join("cortexkit/run/live-children.json")
        .is_file());
    assert!(daemon.connection_file.is_file());
    assert!(daemon.connection_file.starts_with(&daemon.runtime_dir));
    assert!(daemon.config_file.is_file());
    assert!(daemon.config_file.starts_with(&daemon.config_home));
    assert!(daemon.runtime_dir.is_dir());
    assert!(!daemon.data_home.starts_with(&real_data));
    // The live fleet owns these production paths too. Only immutable file contents
    // and this rig's unique markers/PIDs identify a leak; unrelated writes do not.
    for file in &protected {
        file.assert_unchanged();
    }
    assert_no_rig_pids(&real_children, &rig_pids);
    assert_no_rig_paths(&real_data, &[&module_id, root_name]);
    drop(daemon);
    assert!(!root.exists(), "daemon tree survives Drop");
    assert!(
        subc_os::Process::open(pid).unwrap().is_none(),
        "daemon survives Drop"
    );
    for pid in module_pids {
        assert!(
            subc_os::Process::open(pid).unwrap().is_none(),
            "supervised module survives Drop"
        );
    }
    drop(bus);
    assert!(!bus_root.exists(), "broker tree survives Drop");
    assert!(
        subc_os::Process::open(bus_pid).unwrap().is_none(),
        "NATS survives Drop"
    );
    for file in &protected {
        file.assert_unchanged();
    }
    assert_no_rig_pids(&real_children, &rig_pids);
    assert_no_rig_paths(&real_data, &[&module_id, root_name]);
}

#[cfg(target_os = "linux")]
fn executable_path(pid: u32) -> PathBuf {
    std::fs::read_link(format!("/proc/{pid}/exe")).unwrap()
}

#[cfg(target_os = "macos")]
fn executable_path(pid: u32) -> PathBuf {
    let output = Command::new(cortexkit_test_support::ckdev_binary("/bin/ps"))
        .args(["-ww", "-p", &pid.to_string(), "-o", "comm="])
        .output()
        .unwrap();
    assert!(output.status.success());
    PathBuf::from(String::from_utf8(output.stdout).unwrap().trim())
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn spawned_executables_are_ckdev_copies() {
    if !prerequisites() {
        return;
    }
    let bus = bus().await;
    let daemon = PrivateDaemon::start(&bus, DaemonOptions::default())
        .await
        .unwrap();
    let bus_pid = supervised_pid(&daemon, "ckbus").await;
    for (pid, staged) in [
        (
            bus.pid(),
            cortexkit_bus_testkit::nats_server_bin().unwrap().0,
        ),
        (
            daemon.pid(),
            cortexkit_bus_testkit::ck_subc_binary().unwrap(),
        ),
        (bus_pid, cortexkit_bus_testkit::ck_bus_binary().unwrap()),
    ] {
        let actual = executable_path(pid);
        assert_eq!(
            actual.canonicalize().unwrap(),
            staged.canonicalize().unwrap()
        );
        assert!(actual
            .file_name()
            .unwrap()
            .to_string_lossy()
            .starts_with("ckdev-"));
        // Cross-check the kernel's executable inode, not argv or the configured path.
        let observed = subc_os::Process::open(pid)
            .unwrap()
            .unwrap()
            .observe()
            .unwrap();
        assert_eq!(observed.executable, subc_os::file_identity(&actual));
        assert!(observed.executable.is_some());
    }
}

fn test_module(id: &str) -> Module {
    // Cargo test builds examples in the profile folder's examples/, even with a
    // custom target directory or a release profile. A direct executable
    // preserves the image identity checked by macOS's privacy trampoline.
    let executable = cortexkit_test_support::cargo_profile_dir()
        .join("examples")
        .join(format!("bus-test-module{}", std::env::consts::EXE_SUFFIX));
    assert!(
        executable.is_file(),
        "Cargo must build the bus-test-module example"
    );
    Module::new(id, executable)
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn module_publishes_with_delivered_credential_and_nonce_sign() {
    if !prerequisites() {
        return;
    }
    let result = tempfile::tempdir().unwrap();
    let mut module = test_module("testkit_probe");
    module
        .env
        .insert("TESTKIT_RESULT".into(), result.path().display().to_string());
    let bus = bus().await;
    let daemon = PrivateDaemon::start(
        &bus,
        DaemonOptions {
            modules: vec![module],
            ..Default::default()
        },
    )
    .await
    .unwrap();
    wait_file(&result.path().join("credential.json"), Some(&daemon)).await;
    let credential: Value =
        serde_json::from_slice(&std::fs::read(result.path().join("credential.json")).unwrap())
            .unwrap();
    assert_eq!(credential["server_url"], bus.url);
    let observer_key = nkeys::KeyPair::new_user();
    let jwt = bus.trust.user_jwt(
        &cortexkit_bus_testkit::trust::box_root_id(),
        credential["account_public"].as_str().unwrap(),
        &observer_key,
        cortexkit_bus_testkit::trust::unix_now() - 60,
    );
    let observer = async_nats::ConnectOptions::with_jwt(jwt, move |nonce| {
        let signature = observer_key.sign(&nonce);
        async move { signature.map_err(async_nats::AuthError::new) }
    })
    .connect(&bus.url)
    .await
    .unwrap();
    let subject = format!(
        "ck.{}.event.testkit_probe.smoke.v1",
        credential["acct"].as_str().unwrap()
    );
    let mut subscription = observer.subscribe(subject.clone()).await.unwrap();
    observer.flush().await.unwrap();
    std::fs::write(result.path().join("publish-now"), b"ready").unwrap();
    let message = tokio::time::timeout(LIMIT, subscription.next())
        .await
        .unwrap_or_else(|_| panic!("publisher did not deliver: {}", daemon.log_text()))
        .unwrap();
    assert_eq!(message.subject.as_str(), subject);
    assert_eq!(message.payload.as_ref(), b"private smoke");
    drop(daemon);
}

#[test]
fn config_declares_reserved_ckbus_and_caller_modules() {
    let executable = std::env::current_exe().unwrap();
    let mut module = Module::new("publisher", &executable);
    module.args = vec!["argument with spaces".into()];
    module
        .env
        .insert("FIXTURE_VALUE".into(), "quoted \"value\"".into());
    module.reserved = true;
    let text = cortexkit_bus_testkit::subc_config(
        &executable,
        &[("CKBUS_NATS_URL".into(), "nats://127.0.0.1:1234".into())],
        &[module],
    )
    .unwrap();
    let config: Value = serde_json::from_str(&text).unwrap();
    assert_eq!(config["port"], 0);
    assert_eq!(config["modules"]["ckbus"]["reserved"], true);
    assert_eq!(config["modules"]["ckbus"]["protocol"], "subc");
    assert_eq!(
        config["modules"]["ckbus"]["env"]["CKBUS_NATS_URL"],
        "nats://127.0.0.1:1234"
    );
    assert_eq!(
        config["modules"]["publisher"]["args"],
        json!(["argument with spaces"])
    );
    assert_eq!(
        config["modules"]["publisher"]["env"]["FIXTURE_VALUE"],
        "quoted \"value\""
    );
    assert_eq!(config["modules"]["publisher"]["reserved"], true);
    assert_eq!(
        config["modules"]["publisher"]["program"],
        cortexkit_test_support::ckdev_binary(&executable)
            .display()
            .to_string()
    );
    assert!(cortexkit_bus_testkit::subc_config(
        &executable,
        &[],
        &[Module::new("ckbus", &executable)]
    )
    .is_err());
}

#[test]
fn machine_id_is_validated_and_written_under_the_given_home() {
    let root = tempfile::tempdir().unwrap();
    let machine_id = cortexkit_bus_testkit::new_machine_id();
    let path = cortexkit_bus_testkit::write_machine_id(root.path(), &machine_id).unwrap();
    assert_eq!(path, root.path().join("cortexkit/machine-id"));
    assert_eq!(
        std::fs::read_to_string(&path).unwrap(),
        format!("{machine_id}\n")
    );
    assert!(cortexkit_bus_testkit::write_machine_id(root.path(), "not an id").is_err());
    assert_eq!(
        std::fs::read_to_string(&path).unwrap(),
        format!("{machine_id}\n")
    );
}

async fn supervised_pid(daemon: &PrivateDaemon, module_id: &str) -> u32 {
    let deadline = Instant::now() + LIMIT;
    loop {
        let response = daemon
            .control(ClientControlRequest::SupervisorProvenance {
                module_id: Some(module_id.into()),
            })
            .await
            .unwrap();
        let ClientControlResponse::SupervisorProvenance { modules, .. } = response else {
            panic!("provenance response");
        };
        if let Some(pid) = modules
            .iter()
            .find(|module| module.module_id == module_id)
            .and_then(|module| module.daemon_observed.pid)
        {
            return pid;
        }
        assert!(
            Instant::now() < deadline,
            "no supervised pid for {module_id}"
        );
        tokio::time::sleep(Duration::from_millis(50)).await;
    }
}