homeboy 0.132.0

CLI for multi-component deployment and development workflow automation
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
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
//! Tests for `src/core/rig/runner.rs`.
//!
//! Two layers:
//!
//! - Report shape tests (originally authored in #1468) — verify the JSON
//!   envelope contract that CLI JSON output and scheduled jobs depend on.
//! - End-to-end tests for `run_up` / `run_check` / `run_down` / `run_status`
//!   against a minimal spec with no pipeline and no services. These exercise
//!   the bookkeeping path (state file write, report assembly) without
//!   spinning up real services. Richer integration is still smoke-tested
//!   manually per #1468's README.
//!
//! Each end-to-end test isolates `HOME` to a tempdir so the shared rig state
//! file doesn't bleed across tests or the developer's real `~/.config`.

use std::collections::HashMap;

use crate::rig::pipeline::PipelineOutcome;
use crate::rig::runner::{
    run_check, run_check_groups, run_down, run_repair, run_status, run_up, snapshot_state,
    CheckReport, RigStatusReport, ServiceStatusReport, SymlinkStatusState, UpReport,
};
use crate::rig::spec::{
    ComponentSpec, PipelineStep, RigResourcesSpec, RigSpec, ServiceKind, ServiceSpec, SharedPathOp,
    SharedPathSpec, SymlinkSpec,
};
use crate::rig::state::RigState;
use crate::test_support::with_isolated_home;

fn empty_pipeline(name: &str) -> PipelineOutcome {
    PipelineOutcome {
        name: name.to_string(),
        steps: Vec::new(),
        passed: 0,
        failed: 0,
    }
}

fn minimal_spec(id: &str) -> RigSpec {
    RigSpec {
        id: id.to_string(),
        description: format!("{} fixture", id),
        components: HashMap::new(),
        services: HashMap::new(),
        symlinks: Vec::new(),
        shared_paths: Vec::new(),
        resources: Default::default(),
        pipeline: HashMap::new(),
        bench: None,
        bench_workloads: HashMap::new(),
        trace_workloads: HashMap::new(),
        bench_profiles: HashMap::new(),
        app_launcher: None,
    }
}

fn symlink_spec(id: &str, link: String, target: String) -> RigSpec {
    RigSpec {
        symlinks: vec![SymlinkSpec { link, target }],
        ..minimal_spec(id)
    }
}

#[cfg(unix)]
fn unix_symlink(target: &std::path::Path, link: &std::path::Path) {
    std::os::unix::fs::symlink(target, link).expect("create symlink");
}

// ------------------------------------------------------------------
// Report shape tests (preserved from #1468)
// ------------------------------------------------------------------

#[test]
fn test_up_report_serializes_success_flag() {
    let report = UpReport {
        rig_id: "test".to_string(),
        pipeline: empty_pipeline("up"),
        success: true,
    };
    let json = serde_json::to_string(&report).expect("serialize");
    assert!(json.contains("\"rig_id\":\"test\""));
    assert!(json.contains("\"success\":true"));
}

#[test]
fn test_check_report_serializes_success_flag() {
    let report = CheckReport {
        rig_id: "test".to_string(),
        pipeline: empty_pipeline("check"),
        success: false,
    };
    let json = serde_json::to_string(&report).expect("serialize");
    assert!(json.contains("\"success\":false"));
}

#[test]
fn test_status_report_empty_services_serializes() {
    let report = RigStatusReport {
        rig_id: "test".to_string(),
        description: "empty rig".to_string(),
        services: Vec::new(),
        symlinks: Vec::new(),
        last_up: None,
        last_check: None,
        last_check_result: None,
        materialized: None,
    };
    let json = serde_json::to_string(&report).expect("serialize");
    assert!(json.contains("\"services\":[]"));
    // last_up is None -> serialized as null (not skipped, to aid tooling).
    assert!(json.contains("\"last_up\":null"));
}

#[test]
fn test_service_status_report_omits_optional_fields_when_stopped() {
    let report = ServiceStatusReport {
        id: "svc".to_string(),
        kind: "command".to_string(),
        status: "stopped".to_string(),
        pid: None,
        port: None,
        log_path: "/tmp/svc.log".to_string(),
        started_at: None,
    };
    let json = serde_json::to_string(&report).expect("serialize");
    assert!(!json.contains("\"pid\""));
    assert!(!json.contains("\"started_at\""));
}

#[test]
fn test_service_status_report_emits_pid_when_running() {
    let report = ServiceStatusReport {
        id: "svc".to_string(),
        kind: "http-static".to_string(),
        status: "running".to_string(),
        pid: Some(4321),
        port: Some(9724),
        log_path: "/tmp/svc.log".to_string(),
        started_at: Some("2026-04-24T13:00:00Z".to_string()),
    };
    let json = serde_json::to_string(&report).expect("serialize");
    assert!(json.contains("\"pid\":4321"));
    assert!(json.contains("\"kind\":\"http-static\""));
    assert!(json.contains("\"port\":9724"));
    assert!(json.contains("\"log_path\":\"/tmp/svc.log\""));
    assert!(json.contains("\"started_at\":\"2026-04-24T13:00:00Z\""));
}

// ------------------------------------------------------------------
// End-to-end tests: each top-level runner entry point
// ------------------------------------------------------------------

#[test]
fn test_run_up() {
    with_isolated_home(|_dir| {
        let mut rig = minimal_spec("run-up-fixture");
        rig.resources = RigResourcesSpec {
            exclusive: vec!["run-up-exclusive".to_string()],
            paths: vec!["/tmp/run-up-path".to_string()],
            ports: vec![9981],
            process_patterns: vec!["run-up-process".to_string()],
        };
        let report = run_up(&rig).expect("run_up succeeds with empty pipeline");
        assert_eq!(report.rig_id, "run-up-fixture");
        assert!(report.success, "empty pipeline should report success");
        assert_eq!(report.pipeline.passed, 0);
        assert_eq!(report.pipeline.failed, 0);

        let state = RigState::load(&rig.id).expect("state loads");
        let materialized = state.materialized.expect("materialized ownership");
        assert_eq!(materialized.rig_id, "run-up-fixture");
        assert_eq!(materialized.resources.exclusive, vec!["run-up-exclusive"]);
        assert_eq!(materialized.resources.ports, vec![9981]);
        assert!(
            materialized.components.is_empty(),
            "minimal spec has no component snapshot entries"
        );
    });
}

#[test]
fn test_failed_run_up_does_not_write_materialized_ownership() {
    with_isolated_home(|_dir| {
        let mut rig = minimal_spec("run-up-failure-fixture");
        let mut pipeline = HashMap::new();
        pipeline.insert(
            "up".to_string(),
            vec![PipelineStep::Command {
                step_id: None,
                depends_on: Vec::new(),
                cmd: "false".to_string(),
                cwd: None,
                env: HashMap::new(),
                label: Some("intentional failure".to_string()),
            }],
        );
        rig.pipeline = pipeline;

        let report = run_up(&rig).expect("run_up returns a failed report");
        assert!(!report.success, "failing step should fail up");

        let state = RigState::load(&rig.id).expect("state loads");
        assert!(
            state.materialized.is_none(),
            "failed up must not persist materialized ownership"
        );
    });
}

#[test]
fn test_run_check() {
    with_isolated_home(|_dir| {
        let rig = minimal_spec("run-check-fixture");
        let report = run_check(&rig).expect("run_check succeeds with empty pipeline");
        assert_eq!(report.rig_id, "run-check-fixture");
        assert!(report.success, "empty pipeline should pass check");
        assert_eq!(report.pipeline.failed, 0);

        // Side effect: check writes last_check + last_check_result to state.
        let status = run_status(&rig).expect("run_status reads back state");
        assert_eq!(status.last_check_result.as_deref(), Some("pass"));
        assert!(status.last_check.is_some(), "last_check timestamp recorded");
    });
}

#[test]
fn test_run_check_groups_runs_only_matching_check_steps() {
    with_isolated_home(|_dir| {
        let rig: RigSpec = serde_json::from_str(
            r#"{
                "id": "scoped-check-fixture",
                "pipeline": {
                    "check": [
                        {
                            "kind": "check",
                            "label": "desktop app exists",
                            "groups": ["desktop-app"],
                            "command": "true"
                        },
                        {
                            "kind": "check",
                            "label": "unrelated cli symlink",
                            "groups": ["cli-dev-copy"],
                            "command": "false"
                        },
                        {
                            "kind": "symlink",
                            "op": "verify"
                        }
                    ]
                }
            }"#,
        )
        .expect("parse rig");

        let report =
            run_check_groups(&rig, &["desktop-app".to_string()]).expect("scoped check runs");

        assert!(report.success, "unselected failing checks are skipped");
        assert_eq!(report.pipeline.steps.len(), 1);
        assert_eq!(report.pipeline.steps[0].label, "desktop app exists");
        assert_eq!(report.pipeline.passed, 1);
        assert_eq!(report.pipeline.failed, 0);

        let full = run_check(&rig).expect("full check returns failed report");
        assert!(!full.success, "full rig check remains strict");
        assert!(full.pipeline.failed >= 1);
    });
}

#[test]
fn test_run_down() {
    with_isolated_home(|_dir| {
        let rig = minimal_spec("run-down-fixture");
        run_up(&rig).expect("seed materialized ownership");
        assert!(
            RigState::load(&rig.id)
                .expect("state loads")
                .materialized
                .is_some(),
            "precondition: up writes ownership"
        );

        let report = run_down(&rig).expect("run_down succeeds with no services");
        assert_eq!(report.rig_id, "run-down-fixture");
        assert!(
            report.stopped.is_empty(),
            "no services declared, nothing to stop"
        );
        assert!(
            report.pipeline.is_none(),
            "no `down` pipeline declared, so no outcome reported"
        );
        assert!(report.success, "empty teardown is trivially successful");
        assert!(
            RigState::load(&rig.id)
                .expect("state loads")
                .materialized
                .is_none(),
            "down clears materialized ownership"
        );
    });
}

#[cfg(unix)]
#[test]
fn test_run_repair_repairs_drifted_symlink() {
    with_isolated_home(|_dir| {
        let tmp = tempfile::tempdir().expect("tmpdir");
        let old_target = tmp.path().join("old-target");
        let expected_target = tmp.path().join("expected-target");
        let link = tmp.path().join("tool");
        std::fs::write(&old_target, "old").expect("old target");
        std::fs::write(&expected_target, "expected").expect("expected target");
        unix_symlink(&old_target, &link);

        let rig = symlink_spec(
            "repair-drifted-symlink-fixture",
            link.to_string_lossy().into_owned(),
            expected_target.to_string_lossy().into_owned(),
        );

        let report = run_repair(&rig).expect("repair succeeds");
        assert!(report.success);
        assert_eq!(report.repaired, 1);
        assert_eq!(report.unchanged, 0);
        assert_eq!(report.blocked, 0);
        assert_eq!(report.resources[0].status, "repaired");
        assert_eq!(
            report.resources[0].previous_target.as_deref(),
            Some(old_target.to_string_lossy().as_ref())
        );
        assert_eq!(
            std::fs::read_link(&link).expect("read link"),
            expected_target
        );
    });
}

#[cfg(unix)]
#[test]
fn test_run_repair_creates_missing_symlink() {
    with_isolated_home(|_dir| {
        let tmp = tempfile::tempdir().expect("tmpdir");
        let expected_target = tmp.path().join("expected-target");
        let link = tmp.path().join("nested").join("tool");
        std::fs::write(&expected_target, "expected").expect("expected target");

        let rig = symlink_spec(
            "repair-missing-symlink-fixture",
            link.to_string_lossy().into_owned(),
            expected_target.to_string_lossy().into_owned(),
        );

        let report = run_repair(&rig).expect("repair succeeds");
        assert!(report.success);
        assert_eq!(report.repaired, 1);
        assert_eq!(report.resources[0].status, "repaired");
        assert_eq!(report.resources[0].previous_target, None);
        assert_eq!(
            std::fs::read_link(&link).expect("read link"),
            expected_target
        );
    });
}

#[cfg(unix)]
#[test]
fn test_run_repair_noops_ok_symlink() {
    with_isolated_home(|_dir| {
        let tmp = tempfile::tempdir().expect("tmpdir");
        let expected_target = tmp.path().join("expected-target");
        let link = tmp.path().join("tool");
        std::fs::write(&expected_target, "expected").expect("expected target");
        unix_symlink(&expected_target, &link);

        let rig = symlink_spec(
            "repair-ok-symlink-fixture",
            link.to_string_lossy().into_owned(),
            expected_target.to_string_lossy().into_owned(),
        );

        let report = run_repair(&rig).expect("repair succeeds");
        assert!(report.success);
        assert_eq!(report.repaired, 0);
        assert_eq!(report.unchanged, 1);
        assert_eq!(report.blocked, 0);
        assert_eq!(report.resources[0].status, "unchanged");
        assert_eq!(
            std::fs::read_link(&link).expect("read link"),
            expected_target
        );
    });
}

#[cfg(unix)]
#[test]
fn test_run_repair_blocks_non_symlink() {
    with_isolated_home(|_dir| {
        let tmp = tempfile::tempdir().expect("tmpdir");
        let expected_target = tmp.path().join("expected-target");
        let link = tmp.path().join("tool");
        std::fs::write(&expected_target, "expected").expect("expected target");
        std::fs::write(&link, "real file").expect("real file at link path");

        let rig = symlink_spec(
            "repair-blocked-symlink-fixture",
            link.to_string_lossy().into_owned(),
            expected_target.to_string_lossy().into_owned(),
        );

        let report = run_repair(&rig).expect("repair reports blocked resource");
        assert!(!report.success);
        assert_eq!(report.repaired, 0);
        assert_eq!(report.unchanged, 0);
        assert_eq!(report.blocked, 1);
        assert_eq!(report.resources[0].status, "blocked");
        assert!(report.resources[0]
            .error
            .as_deref()
            .unwrap_or_default()
            .contains("not a symlink"));
        assert_eq!(
            std::fs::read_to_string(&link).expect("read file"),
            "real file"
        );
    });
}

#[cfg(unix)]
#[test]
fn test_run_repair_does_not_run_pipeline_commands() {
    with_isolated_home(|_dir| {
        let tmp = tempfile::tempdir().expect("tmpdir");
        let expected_target = tmp.path().join("expected-target");
        let link = tmp.path().join("tool");
        let command_marker = tmp.path().join("command-ran");
        std::fs::write(&expected_target, "expected").expect("expected target");

        let mut rig = symlink_spec(
            "repair-skip-pipeline-fixture",
            link.to_string_lossy().into_owned(),
            expected_target.to_string_lossy().into_owned(),
        );
        rig.pipeline.insert(
            "up".to_string(),
            vec![PipelineStep::Command {
                step_id: None,
                depends_on: Vec::new(),
                cmd: format!("printf ran > {}", command_marker.to_string_lossy()),
                cwd: None,
                env: HashMap::new(),
                label: Some("must-not-run".to_string()),
            }],
        );

        let report = run_repair(&rig).expect("repair succeeds");
        assert!(report.success);
        assert_eq!(report.repaired, 1);
        assert!(link.is_symlink(), "repair still handled declared symlink");
        assert!(
            !command_marker.exists(),
            "repair must not run heavy or arbitrary pipeline commands"
        );
    });
}

#[cfg(unix)]
#[test]
fn test_run_down_cleans_state_owned_shared_paths() {
    with_isolated_home(|_dir| {
        let tmp = tempfile::tempdir().expect("tmpdir");
        let target = tmp.path().join("primary-node_modules");
        let link = tmp.path().join("worktree-node_modules");
        std::fs::create_dir(&target).expect("target dir");

        let mut pipeline = HashMap::new();
        pipeline.insert(
            "up".to_string(),
            vec![PipelineStep::SharedPath {
                step_id: None,
                depends_on: Vec::new(),
                op: SharedPathOp::Ensure,
            }],
        );
        let rig = RigSpec {
            id: "run-down-shared-path-fixture".to_string(),
            description: String::new(),
            components: HashMap::new(),
            services: HashMap::new(),
            symlinks: Vec::new(),
            shared_paths: vec![SharedPathSpec {
                link: link.to_string_lossy().into_owned(),
                target: target.to_string_lossy().into_owned(),
            }],
            resources: Default::default(),
            pipeline,
            bench: None,
            bench_workloads: HashMap::new(),
            trace_workloads: HashMap::new(),
            bench_profiles: HashMap::new(),
            app_launcher: None,
        };

        let up = crate::rig::pipeline::run_pipeline(&rig, "up", true).expect("up pipeline");
        assert!(up.is_success());
        assert!(link.is_symlink());

        let down = run_down(&rig).expect("run_down");
        assert!(down.success);
        assert!(!link.exists(), "run_down removes owned shared-path symlink");
    });
}

#[test]
fn test_run_status() {
    with_isolated_home(|_dir| {
        let rig = minimal_spec("run-status-fixture");
        let status = run_status(&rig).expect("run_status succeeds with empty state");
        assert_eq!(status.rig_id, "run-status-fixture");
        assert_eq!(status.description, "run-status-fixture fixture");
        assert!(status.services.is_empty(), "no services declared");
        assert!(
            status.last_up.is_none(),
            "never brought up, so no timestamp"
        );
        assert!(status.last_check.is_none());
        assert!(status.last_check_result.is_none());
        assert!(status.materialized.is_none());

        run_up(&rig).expect("seed materialized ownership");
        let status = run_status(&rig).expect("run_status reports materialized state");
        let materialized = status.materialized.expect("materialized ownership");
        assert_eq!(materialized.rig_id, "run-status-fixture");
        assert!(!materialized.materialized_at.is_empty());

        let mut services = HashMap::new();
        services.insert(
            "assets".to_string(),
            ServiceSpec {
                kind: ServiceKind::HttpStatic,
                cwd: Some("/tmp".to_string()),
                port: Some(9724),
                command: None,
                env: HashMap::new(),
                health: None,
                discover: None,
            },
        );
        let rig = RigSpec {
            id: "run-status-service-fixture".to_string(),
            description: "service status".to_string(),
            components: HashMap::new(),
            services,
            symlinks: Vec::new(),
            shared_paths: Vec::new(),
            resources: Default::default(),
            pipeline: HashMap::new(),
            bench: None,
            app_launcher: None,
            bench_workloads: HashMap::new(),
            trace_workloads: HashMap::new(),
            bench_profiles: HashMap::new(),
        };

        let status = run_status(&rig).expect("run_status with service");
        assert_eq!(status.services.len(), 1);
        let service = &status.services[0];
        assert_eq!(service.id, "assets");
        assert_eq!(service.kind, "http-static");
        assert_eq!(service.status, "stopped");
        assert_eq!(service.port, Some(9724));
        assert!(
            service
                .log_path
                .ends_with("run-status-service-fixture.state/logs/assets.log"),
            "unexpected log path: {}",
            service.log_path
        );
    });
}

#[cfg(unix)]
#[test]
fn test_run_status_reports_declared_symlink_states() {
    with_isolated_home(|_dir| {
        let tmp = tempfile::tempdir().expect("tmpdir");
        let expected = tmp.path().join("expected-target");
        let other = tmp.path().join("other-target");
        let ok = tmp.path().join("ok-link");
        let missing = tmp.path().join("missing-link");
        let drifted = tmp.path().join("drifted-link");
        let blocked = tmp.path().join("blocked-link");

        std::fs::create_dir(&expected).expect("expected target");
        std::fs::create_dir(&other).expect("other target");
        std::os::unix::fs::symlink(&expected, &ok).expect("ok symlink");
        std::os::unix::fs::symlink(&other, &drifted).expect("drifted symlink");
        std::fs::write(&blocked, "not a symlink").expect("blocked file");

        let mut rig = minimal_spec("run-status-symlink-fixture");
        rig.symlinks = vec![
            SymlinkSpec {
                link: ok.to_string_lossy().into_owned(),
                target: expected.to_string_lossy().into_owned(),
            },
            SymlinkSpec {
                link: missing.to_string_lossy().into_owned(),
                target: expected.to_string_lossy().into_owned(),
            },
            SymlinkSpec {
                link: drifted.to_string_lossy().into_owned(),
                target: expected.to_string_lossy().into_owned(),
            },
            SymlinkSpec {
                link: blocked.to_string_lossy().into_owned(),
                target: expected.to_string_lossy().into_owned(),
            },
        ];

        let status = run_status(&rig).expect("run_status with symlinks");
        assert_eq!(status.symlinks.len(), 4);

        let by_link = status
            .symlinks
            .iter()
            .map(|entry| (entry.link.as_str(), entry))
            .collect::<HashMap<_, _>>();

        let ok_report = by_link
            .get(ok.to_string_lossy().as_ref())
            .expect("ok report");
        assert_eq!(ok_report.state, SymlinkStatusState::Ok);
        assert_eq!(
            ok_report.actual_target.as_deref(),
            Some(expected.to_string_lossy().as_ref())
        );
        assert_eq!(
            ok_report.expected_target,
            expected.to_string_lossy().as_ref()
        );

        let missing_report = by_link
            .get(missing.to_string_lossy().as_ref())
            .expect("missing report");
        assert_eq!(missing_report.state, SymlinkStatusState::Missing);
        assert!(missing_report.actual_target.is_none());

        let drifted_report = by_link
            .get(drifted.to_string_lossy().as_ref())
            .expect("drifted report");
        assert_eq!(drifted_report.state, SymlinkStatusState::Drifted);
        assert_eq!(
            drifted_report.actual_target.as_deref(),
            Some(other.to_string_lossy().as_ref())
        );

        let blocked_report = by_link
            .get(blocked.to_string_lossy().as_ref())
            .expect("blocked report");
        assert_eq!(
            blocked_report.state,
            SymlinkStatusState::BlockedByNonSymlink
        );
        assert!(blocked_report.actual_target.is_none());

        let json = serde_json::to_string(&status).expect("serialize status");
        assert!(json.contains("\"symlinks\""));
        assert!(json.contains("\"state\":\"ok\""));
        assert!(json.contains("\"state\":\"missing\""));
        assert!(json.contains("\"state\":\"drifted\""));
        assert!(json.contains("\"state\":\"blocked_by_non_symlink\""));
    });
}

#[test]
fn test_snapshot_state() {
    // Spec carries two components with non-existent paths. `snapshot_state`
    // should still emit one entry per component (sorted by ID) with the
    // expanded path captured and `sha`/`branch` left as `None` because
    // git won't resolve in a non-repo directory. This is the contract for
    // `homeboy bench --rig` against rigs that include components on
    // ephemeral or read-only paths.
    let mut components = HashMap::new();
    components.insert(
        "studio".to_string(),
        ComponentSpec {
            path: "/tmp/homeboy-snapshot-test-not-a-repo-z".to_string(),
            remote_url: None,
            triage_remote_url: None,
            stack: None,
            branch: None,
            extensions: None,
        },
    );
    components.insert(
        "playground".to_string(),
        ComponentSpec {
            path: "/tmp/homeboy-snapshot-test-not-a-repo-a".to_string(),
            remote_url: None,
            triage_remote_url: None,
            stack: None,
            branch: None,
            extensions: None,
        },
    );
    let rig = RigSpec {
        id: "snapshot-fixture".to_string(),
        description: String::new(),
        components,
        services: HashMap::new(),
        symlinks: Vec::new(),
        shared_paths: Vec::new(),
        resources: Default::default(),
        pipeline: HashMap::new(),
        bench: None,
        bench_workloads: HashMap::new(),
        trace_workloads: HashMap::new(),
        bench_profiles: HashMap::new(),
        app_launcher: None,
    };

    let snapshot = snapshot_state(&rig);
    assert_eq!(snapshot.rig_id, "snapshot-fixture");
    assert!(!snapshot.captured_at.is_empty(), "timestamp populated");
    let ids: Vec<&str> = snapshot.components.keys().map(|s| s.as_str()).collect();
    assert_eq!(
        ids,
        vec!["playground", "studio"],
        "BTreeMap key order is alphabetical, independent of HashMap insertion order"
    );
    for entry in snapshot.components.values() {
        assert!(entry.sha.is_none(), "non-repo path has no HEAD SHA");
        assert!(entry.branch.is_none(), "non-repo path has no branch");
    }
}