smith-config 0.1.2

Unified configuration management for agent services
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
//! Comprehensive test coverage for behavior pack configuration management
//!
//! NUCLEAR COVERAGE TARGET: 100% CODE OBLITERATION

use crate::behavior::{
    AtomGuards, BehaviorMode, BehaviorPack, BehaviorPackManager, CapabilityParams,
    EnabledCapabilities, GuardConfig, MacroGuards, PlaybookGuards, ValidationLevel,
};
use serde_json::json;
use std::fs;
use std::thread;
use std::time::Duration;
use tempfile::TempDir;

#[test]
fn test_behavior_mode_default() {
    assert_eq!(BehaviorMode::default(), BehaviorMode::Strict);
}

#[test]
fn test_behavior_mode_serialization() {
    let modes = vec![
        (BehaviorMode::Strict, "strict"),
        (BehaviorMode::Explore, "explore"),
        (BehaviorMode::Shadow, "shadow"),
    ];

    for (mode, expected) in modes {
        let serialized = serde_json::to_string(&mode).unwrap();
        assert_eq!(serialized, format!("\"{}\"", expected));

        let deserialized: BehaviorMode = serde_json::from_str(&serialized).unwrap();
        assert_eq!(deserialized, mode);
    }
}

#[test]
fn test_enabled_capabilities_default() {
    let capabilities = EnabledCapabilities::default();
    assert!(capabilities.atoms.is_empty());
    assert!(capabilities.macros.is_empty());
    assert!(capabilities.playbooks.is_empty());
}

#[test]
fn test_guard_config_default() {
    let guard_config = GuardConfig::default();

    assert!(guard_config.atoms.is_some());
    assert!(guard_config.macros.is_some());
    assert!(guard_config.playbooks.is_some());
}

#[test]
fn test_atom_guards_default() {
    let atom_guards = AtomGuards::default();
    assert_eq!(atom_guards.default_max_bytes, 1048576); // 1MB
    assert!(atom_guards.require_justification);
}

#[test]
fn test_macro_guards_default() {
    let macro_guards = MacroGuards::default();
    assert!(matches!(
        macro_guards.template_validation,
        ValidationLevel::Strict
    ));
}

#[test]
fn test_playbook_guards_default() {
    let playbook_guards = PlaybookGuards::default();
    assert!(!playbook_guards.parallel_execution);
    assert_eq!(playbook_guards.max_steps, 10);
}

#[test]
fn test_validation_level_serialization() {
    let levels = vec![
        (ValidationLevel::Strict, "strict"),
        (ValidationLevel::Permissive, "permissive"),
    ];

    for (level, expected) in levels {
        let serialized = serde_json::to_string(&level).unwrap();
        assert_eq!(serialized, format!("\"{}\"", expected));

        let deserialized: ValidationLevel = serde_json::from_str(&serialized).unwrap();
        match (level, deserialized) {
            (ValidationLevel::Strict, ValidationLevel::Strict)
            | (ValidationLevel::Permissive, ValidationLevel::Permissive) => {}
            _ => panic!("Deserialization mismatch"),
        }
    }
}

#[test]
fn test_behavior_pack_creation() {
    let mut pack = BehaviorPack {
        name: "test-pack".to_string(),
        mode: BehaviorMode::Explore,
        enable: EnabledCapabilities {
            atoms: vec!["fs.read.v1".to_string()],
            macros: vec!["data.process".to_string()],
            playbooks: vec!["deploy.sequence".to_string()],
        },
        params: std::collections::HashMap::new(),
        guards: GuardConfig::default(),
    };

    pack.params
        .insert("fs.read.v1".to_string(), json!({"max_bytes": 2048}));

    assert_eq!(pack.name, "test-pack");
    assert_eq!(pack.mode, BehaviorMode::Explore);
    assert_eq!(pack.enable.atoms.len(), 1);
    assert_eq!(pack.enable.macros.len(), 1);
    assert_eq!(pack.enable.playbooks.len(), 1);
    assert_eq!(pack.params.len(), 1);
}

#[test]
fn test_behavior_pack_serialization() {
    let pack = BehaviorPack {
        name: "serialize-test".to_string(),
        mode: BehaviorMode::Shadow,
        enable: EnabledCapabilities {
            atoms: vec!["http.fetch.v1".to_string()],
            macros: vec![],
            playbooks: vec![],
        },
        params: {
            let mut params = std::collections::HashMap::new();
            params.insert("http.fetch.v1".to_string(), json!({"timeout_ms": 5000}));
            params
        },
        guards: GuardConfig::default(),
    };

    let serialized = serde_json::to_string(&pack).unwrap();
    let deserialized: BehaviorPack = serde_json::from_str(&serialized).unwrap();

    assert_eq!(deserialized.name, pack.name);
    assert_eq!(deserialized.mode, pack.mode);
    assert_eq!(deserialized.enable.atoms, pack.enable.atoms);
    assert_eq!(deserialized.params.len(), pack.params.len());
}

#[test]
fn test_behavior_pack_validation() -> anyhow::Result<()> {
    // Test valid pack - Strict mode cannot have atoms
    let valid_pack = BehaviorPack {
        name: "valid-pack".to_string(),
        mode: BehaviorMode::Strict,
        enable: EnabledCapabilities {
            atoms: vec![], // Empty for strict mode
            macros: vec!["macro.v1".to_string()],
            playbooks: vec!["playbook.v1".to_string()],
        },
        params: std::collections::HashMap::new(),
        guards: GuardConfig::default(),
    };

    assert!(valid_pack.validate().is_ok());

    // Test pack with empty name
    let empty_name_pack = BehaviorPack {
        name: "".to_string(),
        mode: BehaviorMode::Strict,
        enable: EnabledCapabilities::default(),
        params: std::collections::HashMap::new(),
        guards: GuardConfig::default(),
    };

    // Empty name should be invalid
    assert!(empty_name_pack.validate().is_err());

    Ok(())
}

#[test]
fn test_behavior_pack_manager_new() -> anyhow::Result<()> {
    let temp_dir = TempDir::new()?;
    let manager = BehaviorPackManager::new(temp_dir.path());

    // Manager should start with empty pack collection
    assert!(manager.list_packs().is_empty());
    Ok(())
}

#[test]
fn test_behavior_pack_manager_load_pack() -> anyhow::Result<()> {
    let temp_dir = TempDir::new()?;

    // Create a test behavior pack file
    let pack = BehaviorPack {
        name: "test-pack".to_string(),
        mode: BehaviorMode::Strict,
        enable: EnabledCapabilities {
            atoms: vec![], // Empty for strict mode
            macros: vec!["macro.v1".to_string()],
            playbooks: vec![],
        },
        params: std::collections::HashMap::new(),
        guards: GuardConfig::default(),
    };

    let pack_config = serde_yaml::to_string(&pack)?;
    let pack_file = temp_dir.path().join("test-pack.yaml");
    fs::write(&pack_file, pack_config)?;

    let mut manager = BehaviorPackManager::new(temp_dir.path());
    manager.load_pack(&pack_file)?;

    let pack_names = manager.list_packs();
    assert_eq!(pack_names.len(), 1);
    assert!(pack_names.contains(&"test-pack".to_string()));

    let loaded_pack = manager.get_pack("test-pack").unwrap();
    assert_eq!(loaded_pack.name, "test-pack");
    assert_eq!(loaded_pack.mode, BehaviorMode::Strict);

    Ok(())
}

#[test]
fn test_behavior_pack_manager_hot_reload() -> anyhow::Result<()> {
    let temp_dir = TempDir::new()?;
    let mut manager = BehaviorPackManager::new(temp_dir.path());

    // Create initial pack file
    let pack = BehaviorPack {
        name: "hot-reload-test".to_string(),
        mode: BehaviorMode::Strict,
        enable: EnabledCapabilities {
            atoms: vec![], // Empty for strict mode
            macros: vec!["macro.v1".to_string()],
            playbooks: vec![],
        },
        params: std::collections::HashMap::new(),
        guards: GuardConfig::default(),
    };

    let pack_config = serde_yaml::to_string(&pack)?;
    let pack_file = temp_dir.path().join("hot-reload-test.yaml");
    fs::write(&pack_file, pack_config)?;

    // Load initial pack
    manager.load_pack(&pack_file)?;

    // Wait a bit then modify the file
    thread::sleep(Duration::from_millis(10));

    let modified_pack = BehaviorPack {
        name: "hot-reload-test".to_string(),
        mode: BehaviorMode::Explore, // Changed mode
        enable: EnabledCapabilities {
            atoms: vec!["fs.read.v1".to_string(), "http.fetch.v1".to_string()], // Added atoms (allowed in Explore mode)
            macros: vec!["macro.v1".to_string()],
            playbooks: vec![],
        },
        params: std::collections::HashMap::new(),
        guards: GuardConfig::default(),
    };

    let modified_config = serde_yaml::to_string(&modified_pack)?;
    fs::write(&pack_file, modified_config)?;

    // Check for changes and reload
    let reloaded = manager.check_and_reload()?;
    assert!(!reloaded.is_empty());
    assert!(reloaded.contains(&"hot-reload-test".to_string()));

    // Verify changes were loaded
    let reloaded_pack = manager.get_pack("hot-reload-test").unwrap();
    assert_eq!(reloaded_pack.mode, BehaviorMode::Explore);
    assert_eq!(reloaded_pack.enable.atoms.len(), 2);

    Ok(())
}

#[test]
fn test_behavior_pack_manager_polling_config() -> anyhow::Result<()> {
    let temp_dir = TempDir::new()?;
    let mut manager = BehaviorPackManager::new(temp_dir.path());

    // Test default polling interval
    let default_interval = manager.poll_interval();
    assert!(default_interval > Duration::from_secs(0));

    // Test setting custom polling interval
    let custom_interval = Duration::from_secs(5);
    manager.set_poll_interval(custom_interval);
    assert_eq!(manager.poll_interval(), custom_interval);

    Ok(())
}

#[test]
fn test_behavior_pack_manager_all_packs() -> anyhow::Result<()> {
    let temp_dir = TempDir::new()?;
    let mut manager = BehaviorPackManager::new(temp_dir.path());

    // Initially empty
    assert!(manager.all_packs().is_empty());

    // Load a pack
    let pack = BehaviorPack {
        name: "test-pack".to_string(),
        mode: BehaviorMode::Strict,
        enable: EnabledCapabilities::default(),
        params: std::collections::HashMap::new(),
        guards: GuardConfig::default(),
    };

    let pack_config = serde_yaml::to_string(&pack)?;
    let pack_file = temp_dir.path().join("test-pack.yaml");
    fs::write(&pack_file, pack_config)?;

    manager.load_pack(&pack_file)?;

    // Should have one pack
    assert_eq!(manager.all_packs().len(), 1);
    assert!(manager.all_packs().contains_key("test-pack"));

    Ok(())
}

#[test]
fn test_capability_params_usage() {
    let mut params: CapabilityParams = std::collections::HashMap::new();

    params.insert(
        "fs.read.v1".to_string(),
        json!({"max_bytes": 1024, "timeout_ms": 5000}),
    );
    params.insert(
        "http.fetch.v1".to_string(),
        json!({"headers": {"User-Agent": "smith-executor"}}),
    );

    assert_eq!(params.len(), 2);
    assert!(params.contains_key("fs.read.v1"));
    assert!(params.contains_key("http.fetch.v1"));

    // Test complex parameter nesting
    let fs_params = params.get("fs.read.v1").unwrap();
    assert_eq!(fs_params["max_bytes"], 1024);
    assert_eq!(fs_params["timeout_ms"], 5000);
}

#[test]
fn test_guard_config_customization() {
    let custom_guards = GuardConfig {
        atoms: Some(AtomGuards {
            default_max_bytes: 2048,
            require_justification: false,
        }),
        macros: Some(MacroGuards {
            template_validation: ValidationLevel::Permissive,
        }),
        playbooks: Some(PlaybookGuards {
            parallel_execution: true,
            max_steps: 20,
        }),
    };

    assert_eq!(
        custom_guards.atoms.as_ref().unwrap().default_max_bytes,
        2048
    );
    assert!(!custom_guards.atoms.as_ref().unwrap().require_justification);
    assert!(matches!(
        custom_guards.macros.as_ref().unwrap().template_validation,
        ValidationLevel::Permissive
    ));
    assert!(custom_guards.playbooks.as_ref().unwrap().parallel_execution);
    assert_eq!(custom_guards.playbooks.as_ref().unwrap().max_steps, 20);
}

#[test]
fn test_behavior_pack_edge_cases() -> anyhow::Result<()> {
    // Test serialization with empty capabilities
    let no_caps_pack = BehaviorPack {
        name: "no-caps".to_string(),
        mode: BehaviorMode::Shadow,
        enable: EnabledCapabilities::default(),
        params: std::collections::HashMap::new(),
        guards: GuardConfig::default(),
    };

    let serialized = serde_json::to_string(&no_caps_pack)?;
    let deserialized: BehaviorPack = serde_json::from_str(&serialized)?;

    assert_eq!(deserialized.name, "no-caps");
    assert!(deserialized.enable.atoms.is_empty());
    assert!(deserialized.enable.macros.is_empty());
    assert!(deserialized.enable.playbooks.is_empty());

    Ok(())
}

#[test]
fn test_behavior_pack_manager_error_handling() -> anyhow::Result<()> {
    let temp_dir = TempDir::new()?;
    let manager = BehaviorPackManager::new(temp_dir.path());

    // Test getting non-existent pack
    assert!(manager.get_pack("nonexistent").is_none());

    // Test loading non-existent file
    let mut mutable_manager = manager;
    let nonexistent_file = temp_dir.path().join("nonexistent.yaml");
    let load_result = mutable_manager.load_pack(&nonexistent_file);
    assert!(load_result.is_err());

    Ok(())
}

#[test]
fn test_behavior_pack_manager_invalid_yaml() -> anyhow::Result<()> {
    let temp_dir = TempDir::new()?;
    let mut manager = BehaviorPackManager::new(temp_dir.path());

    // Create invalid YAML file
    let invalid_file = temp_dir.path().join("invalid.yaml");
    fs::write(&invalid_file, "invalid: yaml: content: [")?;

    // Loading should fail gracefully
    let load_result = manager.load_pack(&invalid_file);
    assert!(load_result.is_err());

    Ok(())
}