Skip to main content

agentlint_opencode/
lib.rs

1use agentlint_core::{Diagnostic, Difficulty, Validator};
2use std::path::Path;
3
4/// Validates that OpenCode's `AGENTS.md` is non-empty.
5pub struct AgentsMarkdownValidator;
6
7impl Validator for AgentsMarkdownValidator {
8    fn patterns(&self) -> &[&str] {
9        &["AGENTS.md"]
10    }
11
12    fn validate(&self, path: &Path, src: &str) -> Vec<Diagnostic> {
13        if src.trim().is_empty() {
14            return vec![
15                Diagnostic::error(path, 1, 1, "AGENTS.md is empty")
16                    .with_rule("opencode/content/empty", Difficulty::Easy),
17            ];
18        }
19        vec![]
20    }
21}
22
23/// Known top-level keys for `opencode.json`.
24const KNOWN_OPENCODE_KEYS: &[&str] = &[
25    "model",
26    "provider",
27    "providers",
28    "mcpServers",
29    "theme",
30    "keybinds",
31    "disabled_providers",
32    "autoshare",
33];
34
35/// Validates that `opencode.json` is well-formed JSON and has no unknown top-level keys.
36pub struct OpenCodeJsonValidator;
37
38impl Validator for OpenCodeJsonValidator {
39    fn patterns(&self) -> &[&str] {
40        &["opencode.json"]
41    }
42
43    fn validate(&self, path: &Path, src: &str) -> Vec<Diagnostic> {
44        let value: serde_json::Value = match serde_json::from_str(src) {
45            Ok(v) => v,
46            Err(e) => {
47                return vec![
48                    Diagnostic::error(path, e.line(), e.column(), format!("invalid JSON: {e}"))
49                        .with_rule("opencode/config/invalid-json", Difficulty::Easy),
50                ];
51            }
52        };
53
54        let mut diags = Vec::new();
55
56        if let Some(obj) = value.as_object() {
57            for key in obj.keys() {
58                if !KNOWN_OPENCODE_KEYS.contains(&key.as_str()) {
59                    diags.push(
60                        Diagnostic::warning(
61                            path,
62                            1,
63                            1,
64                            format!(
65                                "unknown key '{key}' in opencode.json; \
66                                 it will be silently ignored"
67                            ),
68                        )
69                        .with_rule("opencode/config/unknown-key", Difficulty::Hard),
70                    );
71                }
72            }
73        }
74
75        diags
76    }
77}
78
79#[cfg(test)]
80mod tests {
81    use super::*;
82    use std::path::Path;
83
84    #[test]
85    fn agents_non_empty_is_clean() {
86        let diags =
87            AgentsMarkdownValidator.validate(Path::new("AGENTS.md"), "# OpenCode Instructions\n");
88        assert!(diags.is_empty());
89    }
90
91    #[test]
92    fn agents_empty_is_error() {
93        let diags = AgentsMarkdownValidator.validate(Path::new("AGENTS.md"), "");
94        assert_eq!(diags.len(), 1);
95        assert!(
96            diags[0].message.contains("empty"),
97            "message: {}",
98            diags[0].message
99        );
100    }
101
102    #[test]
103    fn opencode_json_valid_is_clean() {
104        let diags = OpenCodeJsonValidator.validate(
105            Path::new("opencode.json"),
106            r#"{"model": "claude-sonnet-4-6"}"#,
107        );
108        assert!(diags.is_empty());
109    }
110
111    #[test]
112    fn opencode_json_empty_object_is_clean() {
113        let diags = OpenCodeJsonValidator.validate(Path::new("opencode.json"), "{}");
114        assert!(diags.is_empty());
115    }
116
117    #[test]
118    fn opencode_json_invalid_is_error() {
119        let diags = OpenCodeJsonValidator.validate(Path::new("opencode.json"), "{bad json");
120        assert_eq!(diags.len(), 1);
121        assert!(
122            diags[0].message.contains("invalid JSON"),
123            "message: {}",
124            diags[0].message
125        );
126    }
127
128    // #41 — unknown-key
129
130    #[test]
131    fn unknown_key_emits_warning() {
132        let diags = OpenCodeJsonValidator.validate(
133            Path::new("opencode.json"),
134            r#"{"model": "gpt-4", "unknownOption": true}"#,
135        );
136        assert_eq!(diags.len(), 1);
137        assert!(
138            diags[0].rule.contains("unknown-key"),
139            "rule: {}",
140            diags[0].rule
141        );
142        assert!(
143            diags[0].message.contains("unknownOption"),
144            "message: {}",
145            diags[0].message
146        );
147    }
148
149    #[test]
150    fn all_known_keys_are_clean() {
151        let src = r#"{
152            "model": "gpt-4",
153            "provider": "openai",
154            "providers": {},
155            "mcpServers": {},
156            "theme": "dark",
157            "keybinds": {},
158            "disabled_providers": [],
159            "autoshare": false
160        }"#;
161        let diags = OpenCodeJsonValidator.validate(Path::new("opencode.json"), src);
162        assert!(diags.is_empty(), "unexpected diags: {diags:?}");
163    }
164
165    #[test]
166    fn multiple_unknown_keys_each_emit_warning() {
167        let src = r#"{"foo": 1, "bar": 2, "model": "x"}"#;
168        let diags = OpenCodeJsonValidator.validate(Path::new("opencode.json"), src);
169        assert_eq!(diags.len(), 2);
170        assert!(diags.iter().all(|d| d.rule.contains("unknown-key")));
171    }
172}