File size: 4,906 Bytes
afa0cbf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
use std::cell::RefCell;
use std::io;
use std::path::Path;

use pretty_assertions::assert_eq;
use serde_json::json;

use super::CheckStatus;
use super::FAIL_THRESHOLD;
use super::GIB;
use super::WARNING_THRESHOLD;
use super::check;
use super::check_with_paths;

#[test]
fn disk_capacity_is_reported_without_loaded_configuration() {
    let check = check(/*config*/ None, Path::new("."));

    for label in ["CODEX_HOME available: ", "worktree available: "] {
        assert!(
            check
                .details
                .iter()
                .any(|detail| detail.starts_with(label) && !detail.contains("unavailable"))
        );
    }
}

#[test]
fn disk_capacity_thresholds_produce_complete_diagnostics() {
    for (available, status, measured, summary) in [
        (0, "fail", "0.0 MiB", "critically low disk space (0.0 MiB)"),
        (
            FAIL_THRESHOLD - 1,
            "fail",
            "1024.0 MiB",
            "critically low disk space (1024.0 MiB)",
        ),
        (
            FAIL_THRESHOLD,
            "warning",
            "1.0 GiB",
            "low disk space (1.0 GiB)",
        ),
        (
            WARNING_THRESHOLD - 1,
            "warning",
            "5.0 GiB",
            "low disk space (5.0 GiB)",
        ),
        (
            WARNING_THRESHOLD,
            "ok",
            "5.0 GiB",
            "sufficient free disk space (5.0 GiB)",
        ),
        (
            WARNING_THRESHOLD + GIB,
            "ok",
            "6.0 GiB",
            "sufficient free disk space (6.0 GiB)",
        ),
    ] {
        let issues = ["CODEX_HOME", "worktree"]
            .into_iter()
            .filter(|_| status != "ok")
            .map(|label| {
                json!({
                    "severity": status,
                    "cause": format!("{label} has insufficient disk space"),
                    "measured": measured,
                    "expected": "at least 5.0 GiB",
                    "remedy": "Free disk space or move the worktree to a larger volume.",
                    "fields": [format!("{label} available")],
                })
            })
            .collect::<Vec<_>>();

        let check = check_with_paths(Some(Path::new(".")), Path::new("."), |_| Ok(available));

        assert_eq!(
            serde_json::to_value(check).unwrap(),
            json!({
                "id": "system.disk",
                "category": "disk",
                "status": status,
                "summary": summary,
                "details": [
                    "warning threshold: 5.0 GiB",
                    "failure threshold: 1.0 GiB",
                    format!("CODEX_HOME available: {measured}"),
                    format!("worktree available: {measured}"),
                ],
                "issues": issues,
                "remediation": null,
                "durationMs": 0,
            }),
            "available bytes: {available}"
        );
    }
}

#[test]
fn disk_measurement_errors_produce_complete_warning_diagnostics() {
    let check = check_with_paths(Some(Path::new(".")), Path::new("."), |_| {
        Err(io::ErrorKind::PermissionDenied.into())
    });

    let issues = ["CODEX_HOME", "worktree"]
        .into_iter()
        .map(|label| {
            json!({
                "severity": "warning",
                "cause": format!("disk space for {label} could not be checked"),
                "measured": "PermissionDenied",
                "expected": "readable filesystem capacity",
                "remedy": "Check filesystem access and available disk space.",
                "fields": [format!("{label} available")],
            })
        })
        .collect::<Vec<_>>();

    assert_eq!(
        serde_json::to_value(check).unwrap(),
        json!({
            "id": "system.disk",
            "category": "disk",
            "status": "warning",
            "summary": "disk capacity could not be fully verified",
            "details": [
                "warning threshold: 5.0 GiB",
                "failure threshold: 1.0 GiB",
                "CODEX_HOME available: unavailable (PermissionDenied)",
                "worktree available: unavailable (PermissionDenied)",
            ],
            "issues": issues,
            "remediation": null,
            "durationMs": 0,
        })
    );
}

#[test]
fn nonexistent_relative_codex_home_uses_current_directory_capacity() {
    let measured_paths = RefCell::new(Vec::new());
    let check = check_with_paths(
        Some(Path::new("doctor-nonexistent-relative-home/nested")),
        Path::new("."),
        |path| {
            measured_paths.borrow_mut().push(path.to_path_buf());
            Ok(WARNING_THRESHOLD)
        },
    );

    assert_eq!(check.status, CheckStatus::Ok);
    assert_eq!(
        measured_paths.into_inner(),
        [std::env::current_dir().unwrap(), Path::new(".").into()]
    );
}