frostx 0.1.0

frostx monitors project directories for inactivity. Once a configured inactivity threshold elapses (e.g. "90 days since any file was modified"), frostx executes a pipeline of **actions** - e.g., checking git state, creating archives, uploading backups, deleting local copies. Automating the lifecycle of projects, frostx helps users manage disk space and maintain a clean workspace.
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
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
//! End-to-end tests for `frostx projects`.

use std::fs;
use tempfile::tempdir;

fn frostx_bin() -> std::path::PathBuf {
    let mut p = std::env::current_exe().unwrap();
    p.pop();
    p.pop();
    p.push("frostx");
    p
}

fn run(args: &[&str], dir: &std::path::Path) -> std::process::Output {
    std::process::Command::new(frostx_bin())
        .args(args)
        .current_dir(dir)
        .output()
        .expect("failed to run frostx")
}

fn init_project(dir: &std::path::Path, state_dir: &std::path::Path) {
    run(&["init", "."], dir);
    // register in state via check
    run(
        &["--state-dir", state_dir.to_str().unwrap(), "check", "."],
        dir,
    );
}

// ── list ─────────────────────────────────────────────────────────────────────

#[test]
fn projects_list_empty_state_dir() {
    let state_dir = tempdir().unwrap();
    let tmp = tempdir().unwrap();
    let out = run(
        &[
            "--state-dir",
            state_dir.path().to_str().unwrap(),
            "projects",
            "list",
        ],
        tmp.path(),
    );
    assert!(
        out.status.success(),
        "stderr: {}",
        String::from_utf8_lossy(&out.stderr)
    );
}

#[test]
fn projects_list_json_shape() {
    let state_dir = tempdir().unwrap();
    let proj = tempdir().unwrap();
    init_project(proj.path(), state_dir.path());

    let out = run(
        &[
            "--json",
            "--state-dir",
            state_dir.path().to_str().unwrap(),
            "projects",
            "list",
        ],
        proj.path(),
    );
    assert!(out.status.success());
    let v: serde_json::Value = serde_json::from_str(&String::from_utf8_lossy(&out.stdout)).unwrap();
    assert!(v["projects"].is_array());
    assert_eq!(v["projects"].as_array().unwrap().len(), 1);
    assert!(v["projects"][0]["uuid"].is_string());
    assert!(v["projects"][0]["path"].is_string());
}

// ── add ───────────────────────────────────────────────────────────────────────

#[test]
fn projects_add_registers_project() {
    let state_dir = tempdir().unwrap();
    let proj = tempdir().unwrap();
    run(&["init", "."], proj.path());

    let out = run(
        &[
            "--state-dir",
            state_dir.path().to_str().unwrap(),
            "projects",
            "add",
            proj.path().to_str().unwrap(),
        ],
        proj.path(),
    );
    assert!(
        out.status.success(),
        "stderr: {}",
        String::from_utf8_lossy(&out.stderr)
    );

    // Now list should show it.
    let list_out = run(
        &[
            "--json",
            "--state-dir",
            state_dir.path().to_str().unwrap(),
            "projects",
            "list",
        ],
        proj.path(),
    );
    let v: serde_json::Value =
        serde_json::from_str(&String::from_utf8_lossy(&list_out.stdout)).unwrap();
    assert_eq!(v["projects"].as_array().unwrap().len(), 1);
}

#[test]
fn projects_add_json_shape() {
    let state_dir = tempdir().unwrap();
    let proj = tempdir().unwrap();
    run(&["init", "."], proj.path());

    let out = run(
        &[
            "--json",
            "--state-dir",
            state_dir.path().to_str().unwrap(),
            "projects",
            "add",
            proj.path().to_str().unwrap(),
        ],
        proj.path(),
    );
    assert!(out.status.success());
    let v: serde_json::Value = serde_json::from_str(&String::from_utf8_lossy(&out.stdout)).unwrap();
    assert!(v["added"].is_array());
    assert_eq!(v["added"].as_array().unwrap().len(), 1);
    assert!(v["skipped"].is_array());
    assert!(v["added"][0]["uuid"].is_string());
    assert!(v["added"][0]["path"].is_string());
}

#[test]
fn projects_add_scan_finds_nested_projects() {
    let state_dir = tempdir().unwrap();
    let root = tempdir().unwrap();

    // Create two projects inside root.
    let proj_a = root.path().join("a");
    let proj_b = root.path().join("b");
    fs::create_dir(&proj_a).unwrap();
    fs::create_dir(&proj_b).unwrap();
    run(&["init", "."], &proj_a);
    run(&["init", "."], &proj_b);

    let out = run(
        &[
            "--json",
            "--state-dir",
            state_dir.path().to_str().unwrap(),
            "projects",
            "add",
            "--scan",
            root.path().to_str().unwrap(),
        ],
        root.path(),
    );
    assert!(
        out.status.success(),
        "stderr: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    let v: serde_json::Value = serde_json::from_str(&String::from_utf8_lossy(&out.stdout)).unwrap();
    assert_eq!(
        v["added"].as_array().unwrap().len(),
        2,
        "--scan should find both projects"
    );
}

#[test]
fn projects_add_uninitialized_path_is_skipped() {
    let state_dir = tempdir().unwrap();
    let noproject = tempdir().unwrap();

    let out = run(
        &[
            "--json",
            "--state-dir",
            state_dir.path().to_str().unwrap(),
            "projects",
            "add",
            noproject.path().to_str().unwrap(),
        ],
        noproject.path(),
    );
    // Command itself succeeds but reports the path in skipped.
    let v: serde_json::Value = serde_json::from_str(&String::from_utf8_lossy(&out.stdout)).unwrap();
    assert_eq!(v["added"].as_array().unwrap().len(), 0);
    assert_eq!(v["skipped"].as_array().unwrap().len(), 1);
}

// ── rm ────────────────────────────────────────────────────────────────────────

#[test]
fn projects_rm_removes_project() {
    let state_dir = tempdir().unwrap();
    let proj = tempdir().unwrap();
    init_project(proj.path(), state_dir.path());

    let out = run(
        &[
            "--state-dir",
            state_dir.path().to_str().unwrap(),
            "projects",
            "rm",
            proj.path().to_str().unwrap(),
        ],
        proj.path(),
    );
    assert!(
        out.status.success(),
        "stderr: {}",
        String::from_utf8_lossy(&out.stderr)
    );

    // List should now be empty.
    let list_out = run(
        &[
            "--json",
            "--state-dir",
            state_dir.path().to_str().unwrap(),
            "projects",
            "list",
        ],
        proj.path(),
    );
    let v: serde_json::Value =
        serde_json::from_str(&String::from_utf8_lossy(&list_out.stdout)).unwrap();
    assert!(v["projects"].as_array().unwrap().is_empty());
}

#[test]
fn projects_rm_json_shape() {
    let state_dir = tempdir().unwrap();
    let proj = tempdir().unwrap();
    init_project(proj.path(), state_dir.path());

    let out = run(
        &[
            "--json",
            "--state-dir",
            state_dir.path().to_str().unwrap(),
            "projects",
            "rm",
            proj.path().to_str().unwrap(),
        ],
        proj.path(),
    );
    assert!(out.status.success());
    let v: serde_json::Value = serde_json::from_str(&String::from_utf8_lossy(&out.stdout)).unwrap();
    assert!(v["uuid"].is_string());
    assert!(v["path"].is_string());
}

// ── check (all) ───────────────────────────────────────────────────────────────

#[test]
fn projects_check_outputs_each_tracked_project() {
    let state_dir = tempdir().unwrap();
    let proj_a = tempdir().unwrap();
    let proj_b = tempdir().unwrap();
    init_project(proj_a.path(), state_dir.path());
    init_project(proj_b.path(), state_dir.path());

    let out = run(
        &[
            "--json",
            "--state-dir",
            state_dir.path().to_str().unwrap(),
            "projects",
            "check",
        ],
        proj_a.path(),
    );
    assert!(
        out.status.success(),
        "stderr: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    let v: serde_json::Value = serde_json::from_str(&String::from_utf8_lossy(&out.stdout)).unwrap();
    assert!(v.is_array(), "should be a JSON array");
    assert_eq!(
        v.as_array().unwrap().len(),
        2,
        "should report both tracked projects"
    );
}

#[test]
fn projects_check_empty_registry_succeeds() {
    let state_dir = tempdir().unwrap();
    let tmp = tempdir().unwrap();
    let out = run(
        &[
            "--json",
            "--state-dir",
            state_dir.path().to_str().unwrap(),
            "projects",
            "check",
        ],
        tmp.path(),
    );
    assert!(out.status.success());
    let v: serde_json::Value = serde_json::from_str(&String::from_utf8_lossy(&out.stdout)).unwrap();
    assert!(v.is_array());
    assert!(v.as_array().unwrap().is_empty());
}

// ── run (all) ─────────────────────────────────────────────────────────────────

#[test]
fn projects_run_includes_project_field_in_ndjson() {
    let state_dir = tempdir().unwrap();
    let proj = tempdir().unwrap();

    // Write a config with a triggered hook.
    let config = r#"id = "a1b2c3d4-0000-0000-0000-aabbccddeeff"

[config.hook.check_ok]
command = "true"
kind = "check"

[[rule]]
after = "1h"
actions = ["hook.check_ok"]
"#;
    std::fs::write(proj.path().join("frostx.toml"), config).unwrap();

    // Backdate a file so the rule triggers.
    let old_file = proj.path().join("old.txt");
    std::fs::write(&old_file, "x").unwrap();
    std::process::Command::new("touch")
        .args(["-d", "48 hours ago", old_file.to_str().unwrap()])
        .output()
        .unwrap();

    // Register the project.
    run(
        &[
            "--state-dir",
            state_dir.path().to_str().unwrap(),
            "projects",
            "add",
            proj.path().to_str().unwrap(),
        ],
        proj.path(),
    );

    let out = run(
        &[
            "--json",
            "--yes",
            "--state-dir",
            state_dir.path().to_str().unwrap(),
            "projects",
            "run",
        ],
        proj.path(),
    );
    assert!(
        out.status.success(),
        "stderr: {}",
        String::from_utf8_lossy(&out.stderr)
    );

    let stdout = String::from_utf8_lossy(&out.stdout);
    for line in stdout.lines() {
        let v: serde_json::Value =
            serde_json::from_str(line).unwrap_or_else(|e| panic!("invalid NDJSON: {line}: {e}"));
        assert!(
            v["project"].is_string(),
            "each NDJSON line must have 'project' field"
        );
    }
}

// ── --daily (non-TTY behaviour) ───────────────────────────────────────────────
//
// Integration tests run without a TTY, so `--daily` always takes the
// early-exit path.  These tests verify:
//  1. `--daily` (human) is silent; `--daily --json` always emits an envelope.
//  2. The envelope carries `daily_source`: "not-run", "cached", or "fresh".
//  3. The check cache and run cache are fully independent.
//
// Cache helpers store the full envelope JSON (with "cached" source) because
// that is exactly what the binary stores on a real fresh run.

fn daily_check_envelope(
    daily_source: frostx::output::DailySource,
    results: &[frostx::output::CheckOutput],
) -> String {
    serde_json::to_string(&frostx::output::DailyCheckOutput {
        frostx_version: frostx::output::FROSTX_VERSION,
        daily_source,
        results,
    })
    .expect("DailyCheckOutput serialization must not fail")
}

fn daily_run_envelope(
    daily_source: frostx::output::DailySource,
    actions: &[frostx::output::RunActionOutput],
) -> String {
    serde_json::to_string(&frostx::output::DailyRunOutput {
        frostx_version: frostx::output::FROSTX_VERSION,
        daily_source,
        actions,
    })
    .expect("DailyRunOutput serialization must not fail")
}

fn write_daily_check_cache(state_dir: &std::path::Path, envelope: &str) {
    let mut daily = frostx::config::daily::DailyState::load(state_dir).unwrap_or_default();
    daily.record_check(Some(envelope.to_string()));
    daily.save(state_dir).unwrap();
}

fn write_daily_run_cache(state_dir: &std::path::Path, envelope: &str) {
    let mut daily = frostx::config::daily::DailyState::load(state_dir).unwrap_or_default();
    daily.record_run(Some(envelope.to_string()));
    daily.save(state_dir).unwrap();
}

fn parse_daily_json(output: &[u8]) -> serde_json::Value {
    let s = String::from_utf8_lossy(output);
    serde_json::from_str(s.trim())
        .unwrap_or_else(|e| panic!("--daily --json output must be valid JSON: {e}\nGot: {s}"))
}

// Human mode: silent when there is no cache and the TTY check suppresses the run.

#[test]
fn projects_check_daily_human_is_silent_without_cache() {
    let state_dir = tempdir().unwrap();
    let tmp = tempdir().unwrap();
    let out = run(
        &[
            "--state-dir",
            state_dir.path().to_str().unwrap(),
            "projects",
            "check",
            "--daily",
        ],
        tmp.path(),
    );
    assert!(
        out.status.success(),
        "stderr: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    assert!(out.stdout.is_empty(), "human mode should be silent");
}

#[test]
fn projects_run_daily_human_is_silent_without_cache() {
    let state_dir = tempdir().unwrap();
    let tmp = tempdir().unwrap();
    let out = run(
        &[
            "--state-dir",
            state_dir.path().to_str().unwrap(),
            "projects",
            "run",
            "--daily",
        ],
        tmp.path(),
    );
    assert!(
        out.status.success(),
        "stderr: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    assert!(out.stdout.is_empty(), "human mode should be silent");
}

// JSON mode: "not-run" envelope when no cache exists.

#[test]
fn projects_check_daily_json_emits_not_run_when_no_cache() {
    let state_dir = tempdir().unwrap();
    let tmp = tempdir().unwrap();
    let out = run(
        &[
            "--json",
            "--state-dir",
            state_dir.path().to_str().unwrap(),
            "projects",
            "check",
            "--daily",
        ],
        tmp.path(),
    );
    assert!(out.status.success());
    let v = parse_daily_json(&out.stdout);
    assert_eq!(v["daily_source"], "not-run");
    assert_eq!(v["results"], serde_json::json!([]));
}

#[test]
fn projects_run_daily_json_emits_not_run_when_no_cache() {
    let state_dir = tempdir().unwrap();
    let tmp = tempdir().unwrap();
    let out = run(
        &[
            "--json",
            "--state-dir",
            state_dir.path().to_str().unwrap(),
            "projects",
            "run",
            "--daily",
        ],
        tmp.path(),
    );
    assert!(out.status.success());
    let v = parse_daily_json(&out.stdout);
    assert_eq!(v["daily_source"], "not-run");
    assert_eq!(v["actions"], serde_json::json!([]));
}

// JSON mode: cached envelope returned verbatim when cache exists.

#[test]
fn projects_check_daily_json_returns_cached_envelope() {
    let state_dir = tempdir().unwrap();
    let tmp = tempdir().unwrap();
    let envelope = daily_check_envelope(frostx::output::DailySource::Cached, &[]);
    write_daily_check_cache(state_dir.path(), &envelope);

    let out = run(
        &[
            "--json",
            "--state-dir",
            state_dir.path().to_str().unwrap(),
            "projects",
            "check",
            "--daily",
        ],
        tmp.path(),
    );
    assert!(out.status.success());
    assert_eq!(
        String::from_utf8_lossy(&out.stdout),
        format!("{envelope}\n"),
        "cached envelope must be returned verbatim with trailing newline"
    );
}

#[test]
fn projects_run_daily_json_returns_cached_envelope() {
    let state_dir = tempdir().unwrap();
    let tmp = tempdir().unwrap();
    let envelope = daily_run_envelope(frostx::output::DailySource::Cached, &[]);
    write_daily_run_cache(state_dir.path(), &envelope);

    let out = run(
        &[
            "--json",
            "--state-dir",
            state_dir.path().to_str().unwrap(),
            "projects",
            "run",
            "--daily",
        ],
        tmp.path(),
    );
    assert!(out.status.success());
    assert_eq!(
        String::from_utf8_lossy(&out.stdout),
        format!("{envelope}\n"),
        "cached envelope must be returned verbatim with trailing newline"
    );
}

// Separation: check cache must not appear in run output and vice versa.

#[test]
fn projects_daily_check_cache_does_not_bleed_into_run() {
    let state_dir = tempdir().unwrap();
    let tmp = tempdir().unwrap();

    write_daily_check_cache(
        state_dir.path(),
        &daily_check_envelope(frostx::output::DailySource::Cached, &[]),
    );

    // No run cache → run should emit "not-run" envelope.
    let out = run(
        &[
            "--json",
            "--state-dir",
            state_dir.path().to_str().unwrap(),
            "projects",
            "run",
            "--daily",
        ],
        tmp.path(),
    );
    assert!(out.status.success());
    let v = parse_daily_json(&out.stdout);
    assert_eq!(
        v["daily_source"], "not-run",
        "run should not use the check cache"
    );
}

#[test]
fn projects_daily_run_cache_does_not_bleed_into_check() {
    let state_dir = tempdir().unwrap();
    let tmp = tempdir().unwrap();

    write_daily_run_cache(
        state_dir.path(),
        &daily_run_envelope(frostx::output::DailySource::Cached, &[]),
    );

    // No check cache → check should emit "not-run" envelope.
    let out = run(
        &[
            "--json",
            "--state-dir",
            state_dir.path().to_str().unwrap(),
            "projects",
            "check",
            "--daily",
        ],
        tmp.path(),
    );
    assert!(out.status.success());
    let v = parse_daily_json(&out.stdout);
    assert_eq!(
        v["daily_source"], "not-run",
        "check should not use the run cache"
    );
}

#[test]
fn projects_daily_both_caches_are_returned_independently() {
    let state_dir = tempdir().unwrap();
    let tmp = tempdir().unwrap();

    let check_envelope = daily_check_envelope(frostx::output::DailySource::Cached, &[]);
    let run_envelope = daily_run_envelope(frostx::output::DailySource::Cached, &[]);
    {
        let mut daily = frostx::config::daily::DailyState::default();
        daily.record_check(Some(check_envelope.clone()));
        daily.record_run(Some(run_envelope.clone()));
        daily.save(state_dir.path()).unwrap();
    }

    let check_out = run(
        &[
            "--json",
            "--state-dir",
            state_dir.path().to_str().unwrap(),
            "projects",
            "check",
            "--daily",
        ],
        tmp.path(),
    );
    assert_eq!(
        String::from_utf8_lossy(&check_out.stdout),
        format!("{check_envelope}\n")
    );

    let run_out = run(
        &[
            "--json",
            "--state-dir",
            state_dir.path().to_str().unwrap(),
            "projects",
            "run",
            "--daily",
        ],
        tmp.path(),
    );
    assert_eq!(
        String::from_utf8_lossy(&run_out.stdout),
        format!("{run_envelope}\n")
    );
}

// ── daily cache reset on project list change ──────────────────────────────────

#[test]
fn projects_add_resets_daily_cache() {
    let state_dir = tempdir().unwrap();
    let proj = tempdir().unwrap();
    run(&["init", "."], proj.path());

    // Pre-populate both caches.
    write_daily_check_cache(
        state_dir.path(),
        &daily_check_envelope(frostx::output::DailySource::Cached, &[]),
    );
    write_daily_run_cache(
        state_dir.path(),
        &daily_run_envelope(frostx::output::DailySource::Cached, &[]),
    );

    // Adding a project must clear the cache.
    run(
        &[
            "--state-dir",
            state_dir.path().to_str().unwrap(),
            "projects",
            "add",
            proj.path().to_str().unwrap(),
        ],
        proj.path(),
    );

    // Both caches must now be gone → "not-run" envelopes returned.
    let check_out = run(
        &[
            "--json",
            "--state-dir",
            state_dir.path().to_str().unwrap(),
            "projects",
            "check",
            "--daily",
        ],
        proj.path(),
    );
    assert_eq!(
        parse_daily_json(&check_out.stdout)["daily_source"],
        "not-run",
        "check cache should be cleared after projects add"
    );

    let run_out = run(
        &[
            "--json",
            "--state-dir",
            state_dir.path().to_str().unwrap(),
            "projects",
            "run",
            "--daily",
        ],
        proj.path(),
    );
    assert_eq!(
        parse_daily_json(&run_out.stdout)["daily_source"],
        "not-run",
        "run cache should be cleared after projects add"
    );
}

#[test]
fn projects_rm_resets_daily_cache() {
    let state_dir = tempdir().unwrap();
    let proj = tempdir().unwrap();
    init_project(proj.path(), state_dir.path());

    // Pre-populate both caches.
    write_daily_check_cache(
        state_dir.path(),
        &daily_check_envelope(frostx::output::DailySource::Cached, &[]),
    );
    write_daily_run_cache(
        state_dir.path(),
        &daily_run_envelope(frostx::output::DailySource::Cached, &[]),
    );

    // Removing the project must clear the cache.
    run(
        &[
            "--state-dir",
            state_dir.path().to_str().unwrap(),
            "projects",
            "rm",
            proj.path().to_str().unwrap(),
        ],
        proj.path(),
    );

    let tmp = tempdir().unwrap();
    let check_out = run(
        &[
            "--json",
            "--state-dir",
            state_dir.path().to_str().unwrap(),
            "projects",
            "check",
            "--daily",
        ],
        tmp.path(),
    );
    assert_eq!(
        parse_daily_json(&check_out.stdout)["daily_source"],
        "not-run",
        "check cache should be cleared after projects rm"
    );

    let run_out = run(
        &[
            "--json",
            "--state-dir",
            state_dir.path().to_str().unwrap(),
            "projects",
            "run",
            "--daily",
        ],
        tmp.path(),
    );
    assert_eq!(
        parse_daily_json(&run_out.stdout)["daily_source"],
        "not-run",
        "run cache should be cleared after projects rm"
    );
}

#[test]
fn projects_add_with_no_new_projects_does_not_reset_cache() {
    let state_dir = tempdir().unwrap();
    let noproject = tempdir().unwrap();

    // Pre-populate check cache with a known envelope.
    let envelope = daily_check_envelope(frostx::output::DailySource::Cached, &[]);
    write_daily_check_cache(state_dir.path(), &envelope);

    // Attempting to add a non-project path leaves all entries in skipped;
    // the list does not change so the cache must be preserved.
    run(
        &[
            "--state-dir",
            state_dir.path().to_str().unwrap(),
            "projects",
            "add",
            noproject.path().to_str().unwrap(),
        ],
        noproject.path(),
    );

    let out = run(
        &[
            "--json",
            "--state-dir",
            state_dir.path().to_str().unwrap(),
            "projects",
            "check",
            "--daily",
        ],
        noproject.path(),
    );
    assert_eq!(
        String::from_utf8_lossy(&out.stdout),
        format!("{envelope}\n"),
        "cache should be preserved when no project was actually added"
    );
}