rcman 0.1.9

Framework-agnostic settings management with schema, backup/restore, secrets and derive macro support
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
//! Profiles Feature Integration Tests
//!
//! Tests for profile management in sub-settings including:
//! - Profile CRUD operations
//! - Profile switching and cache invalidation
//! - Multi-file and single-file mode with profiles
//! - Profile events

mod common;

use rcman::{SettingsConfig, SettingsManager, SubSettingsConfig};
use serde_json::json;
use std::sync::{Arc, Mutex};
use tempfile::TempDir;

// =============================================================================
// Profile CRUD Operations
// =============================================================================

#[test]
fn test_create_profile() {
    let temp_dir = TempDir::new().unwrap();

    let manager = SettingsManager::builder("test-app", "1.0.0")
        .with_config_dir(temp_dir.path())
        .with_sub_settings(SubSettingsConfig::new("remotes").with_profiles())
        .build()
        .unwrap();

    let remotes = manager.sub_settings("remotes").unwrap();
    let profiles = remotes.profiles().unwrap();

    // Create a new profile
    profiles.create("work").unwrap();

    // Verify it exists
    let list = profiles.list().unwrap();
    assert!(list.contains(&"default".to_string()));
    assert!(list.contains(&"work".to_string()));
}

#[test]
fn test_switch_profile() {
    let temp_dir = TempDir::new().unwrap();

    let manager = SettingsManager::builder("test-app", "1.0.0")
        .with_config_dir(temp_dir.path())
        .with_sub_settings(SubSettingsConfig::new("remotes").with_profiles())
        .build()
        .unwrap();

    let remotes = manager.sub_settings("remotes").unwrap();
    let profiles = remotes.profiles().unwrap();

    profiles.create("work").unwrap();
    profiles.switch("work").unwrap();

    assert_eq!(profiles.active().unwrap(), "work");
}

#[test]
fn test_seamless_profile_switching() {
    let temp_dir = TempDir::new().unwrap();

    let manager = SettingsManager::builder("test-app", "1.0.0")
        .with_config_dir(temp_dir.path())
        .with_sub_settings(SubSettingsConfig::new("remotes").with_profiles())
        .build()
        .unwrap();

    let remotes = manager.sub_settings("remotes").unwrap();

    // Add data to default profile
    remotes
        .set("personal-gdrive", &json!({"type": "drive"}))
        .unwrap();
    assert!(remotes.exists("personal-gdrive").unwrap());

    // Create work profile and switch to it using the convenience method
    remotes.profiles().unwrap().create("work").unwrap();
    remotes.switch_profile("work").unwrap();

    // In work profile, personal-gdrive should not exist (different directory)
    assert!(!remotes.exists("personal-gdrive").unwrap());

    // Add work-specific data
    remotes
        .set("company-drive", &json!({"type": "sharepoint"}))
        .unwrap();
    assert!(remotes.exists("company-drive").unwrap());

    // Switch back to default
    remotes.switch_profile("default").unwrap();

    // Back in default, personal-gdrive exists, company-drive does not
    assert!(remotes.exists("personal-gdrive").unwrap());
    assert!(!remotes.exists("company-drive").unwrap());
}

#[test]
fn test_delete_profile() {
    let temp_dir = TempDir::new().unwrap();

    let manager = SettingsManager::builder("test-app", "1.0.0")
        .with_config_dir(temp_dir.path())
        .with_sub_settings(SubSettingsConfig::new("remotes").with_profiles())
        .build()
        .unwrap();

    let remotes = manager.sub_settings("remotes").unwrap();
    let profiles = remotes.profiles().unwrap();

    profiles.create("temp").unwrap();
    assert!(profiles.exists("temp").unwrap());

    profiles.delete("temp").unwrap();
    assert!(!profiles.exists("temp").unwrap());
}

#[test]
fn test_cannot_delete_active_profile() {
    let temp_dir = TempDir::new().unwrap();

    let manager = SettingsManager::builder("test-app", "1.0.0")
        .with_config_dir(temp_dir.path())
        .with_sub_settings(SubSettingsConfig::new("remotes").with_profiles())
        .build()
        .unwrap();

    let remotes = manager.sub_settings("remotes").unwrap();
    let profiles = remotes.profiles().unwrap();

    // default is active, can't delete it
    let result = profiles.delete("default");
    assert!(result.is_err());
}

#[test]
fn test_rename_profile() {
    let temp_dir = TempDir::new().unwrap();

    let manager = SettingsManager::builder("test-app", "1.0.0")
        .with_config_dir(temp_dir.path())
        .with_sub_settings(SubSettingsConfig::new("remotes").with_profiles())
        .build()
        .unwrap();

    let remotes = manager.sub_settings("remotes").unwrap();
    let profiles = remotes.profiles().unwrap();

    profiles.create("old-name").unwrap();
    profiles.rename("old-name", "new-name").unwrap();

    let list = profiles.list().unwrap();
    assert!(!list.contains(&"old-name".to_string()));
    assert!(list.contains(&"new-name".to_string()));
}

#[test]
fn test_duplicate_profile() {
    let temp_dir = TempDir::new().unwrap();

    let manager = SettingsManager::builder("test-app", "1.0.0")
        .with_config_dir(temp_dir.path())
        .with_sub_settings(SubSettingsConfig::new("remotes").with_profiles())
        .build()
        .unwrap();

    let remotes = manager.sub_settings("remotes").unwrap();

    // Add some data to default profile
    remotes.set("gdrive", &json!({"type": "drive"})).unwrap();

    let profiles = remotes.profiles().unwrap();
    profiles.duplicate("default", "backup").unwrap();

    // Verify both profiles exist
    let list = profiles.list().unwrap();
    assert!(list.contains(&"default".to_string()));
    assert!(list.contains(&"backup".to_string()));

    // Verify the backup profile has the data
    let backup_dir = temp_dir
        .path()
        .join("remotes")
        .join("profiles")
        .join("backup");
    assert!(backup_dir.join("gdrive.json").exists());
}

// =============================================================================
// Profile Events
// =============================================================================

#[test]
fn test_profile_events() {
    let temp_dir = TempDir::new().unwrap();

    let manager = SettingsManager::builder("test-app", "1.0.0")
        .with_config_dir(temp_dir.path())
        .with_sub_settings(SubSettingsConfig::new("remotes").with_profiles())
        .build()
        .unwrap();

    let remotes = manager.sub_settings("remotes").unwrap();
    let profiles = remotes.profiles().unwrap();

    let events = Arc::new(Mutex::new(Vec::new()));
    let events_clone = events.clone();

    profiles.set_on_event(move |event| {
        events_clone.lock().unwrap().push(format!("{event:?}"));
    });

    profiles.create("work").unwrap();
    profiles.switch("work").unwrap();
    profiles.rename("work", "job").unwrap();

    let recorded = events.lock().unwrap();
    assert_eq!(recorded.len(), 3);
}

// =============================================================================
// Profiles Not Enabled
// =============================================================================

#[test]
fn test_profiles_not_enabled_error() {
    let temp_dir = TempDir::new().unwrap();

    let manager = SettingsManager::builder("test-app", "1.0.0")
        .with_config_dir(temp_dir.path())
        .with_sub_settings(SubSettingsConfig::new("remotes")) // No .with_profiles()
        .build()
        .unwrap();

    let remotes = manager.sub_settings("remotes").unwrap();
    let result = remotes.profiles();

    assert!(result.is_err());
}

// =============================================================================
// Profile Directory Structure
// =============================================================================

#[test]
fn test_profile_directory_structure() {
    let temp_dir = TempDir::new().unwrap();

    let manager = SettingsManager::builder("test-app", "1.0.0")
        .with_config_dir(temp_dir.path())
        .with_sub_settings(SubSettingsConfig::new("remotes").with_profiles())
        .build()
        .unwrap();

    let remotes = manager.sub_settings("remotes").unwrap();

    // Add data to default profile
    remotes.set("gdrive", &json!({"type": "drive"})).unwrap();

    // Create and switch to work profile
    let profiles = remotes.profiles().unwrap();
    profiles.create("work").unwrap();

    // Verify directory structure
    let remotes_dir = temp_dir.path().join("remotes");
    assert!(remotes_dir.join(".profiles.json").exists());
    assert!(remotes_dir.join("profiles").join("default").is_dir());
    assert!(remotes_dir.join("profiles").join("work").is_dir());
    assert!(
        remotes_dir
            .join("profiles")
            .join("default")
            .join("gdrive.json")
            .exists()
    );
}

// =============================================================================
// Single-File Mode with Profiles
// =============================================================================

#[test]
fn test_single_file_mode_with_profiles() {
    let temp_dir = TempDir::new().unwrap();

    let manager = SettingsManager::builder("test-app", "1.0.0")
        .with_config_dir(temp_dir.path())
        .with_sub_settings(SubSettingsConfig::singlefile("backends").with_profiles())
        .build()
        .unwrap();

    let backends = manager.sub_settings("backends").unwrap();

    // Add data
    backends
        .set("local", &json!({"host": "localhost"}))
        .unwrap();

    let profiles = backends.profiles().unwrap();
    profiles.create("work").unwrap();

    // Verify single file in profile directory
    let backends_dir = temp_dir.path().join("backends");
    assert!(backends_dir.join(".profiles.json").exists());
    assert!(
        backends_dir
            .join("profiles")
            .join("default")
            .join("backends.json")
            .exists()
    );
}

#[test]
fn test_single_file_profile_migration() {
    let temp_dir = TempDir::new().unwrap();
    let backends_file = temp_dir.path().join("backends.json");

    // 1. Setup Legacy Flat Structure
    std::fs::write(&backends_file, r#"{ "local": { "type": "local" } }"#).unwrap();

    // 2. Initialize Manager with Profiles Enabled for this single-file sub-setting
    let _manager = SettingsManager::builder("test-app", "1.0.0")
        .with_config_dir(temp_dir.path())
        .with_sub_settings(SubSettingsConfig::singlefile("backends").with_profiles())
        .build()
        .unwrap();

    // 3. Verify Migration
    // The original file should be gone (or rather, moved)
    assert!(
        !backends_file.exists(),
        "Legacy file should have been moved"
    );

    // The single-file container directory should exist
    let backends_dir = temp_dir.path().join("backends");
    assert!(backends_dir.is_dir());

    // Manifest should exist
    assert!(backends_dir.join(".profiles.json").exists());

    // The file should be inside the default profile
    let migrated_file = backends_dir
        .join("profiles")
        .join("default")
        .join("backends.json");

    assert!(
        migrated_file.exists(),
        "File should be migrated to default profile"
    );

    // Check content
    let content = std::fs::read_to_string(migrated_file).unwrap();
    assert!(content.contains("local"));
}

// =============================================================================
// Main Settings Profiles
// =============================================================================

#[test]
fn test_main_settings_profiles() {
    use rcman::{SettingMetadata, SettingsSchema};
    use serde::{Deserialize, Serialize};
    use std::collections::HashMap;

    #[derive(Serialize, Deserialize, Default)]
    struct TestSettings {
        #[serde(default)]
        general: GeneralSettings,
    }

    #[derive(Serialize, Deserialize)]
    struct GeneralSettings {
        #[serde(default = "default_theme")]
        theme: String,
    }

    fn default_theme() -> String {
        "light".to_string()
    }

    impl Default for GeneralSettings {
        fn default() -> Self {
            Self {
                theme: default_theme(),
            }
        }
    }

    impl SettingsSchema for TestSettings {
        fn get_metadata() -> HashMap<String, SettingMetadata> {
            let mut map = HashMap::new();
            map.insert(
                "general.theme".to_string(),
                SettingMetadata::text("light").meta_str("label", "Theme"),
            );
            map
        }
    }

    let temp_dir = TempDir::new().unwrap();

    let config = SettingsConfig::builder("test-app", "1.0.0")
        .with_config_dir(temp_dir.path())
        .with_schema::<TestSettings>()
        .with_profiles() // Enable profiles for main settings
        .build();
    let manager = SettingsManager::new(config).unwrap();

    // Verify profiles are enabled
    assert!(manager.is_profiles_enabled());

    // Check default profile
    assert_eq!(manager.active_profile().unwrap(), "default");

    // Save setting in default profile
    manager
        .save_setting("general", "theme", &json!("dark"))
        .unwrap();

    // Create and switch to work profile
    manager.create_profile("work").unwrap();
    manager.switch_profile("work").unwrap();

    assert_eq!(manager.active_profile().unwrap(), "work");

    // Work profile should have default theme (light)
    let settings: TestSettings = manager.get_all().unwrap();
    assert_eq!(settings.general.theme, "light");

    // Save a different theme in work profile
    manager
        .save_setting("general", "theme", &json!("ocean"))
        .unwrap();

    // Switch back to default - should have dark theme
    manager.switch_profile("default").unwrap();
    let settings: TestSettings = manager.get_all().unwrap();
    assert_eq!(settings.general.theme, "dark");

    // Switch to work - should have ocean theme
    manager.switch_profile("work").unwrap();
    let settings: TestSettings = manager.get_all().unwrap();
    assert_eq!(settings.general.theme, "ocean");
}

#[test]
fn test_main_profile_switch_emits_changed_setting_callbacks() {
    use rcman::{SettingMetadata, SettingsSchema};
    use serde::{Deserialize, Serialize};
    use std::collections::HashMap;

    #[derive(Default, Serialize, Deserialize)]
    struct TestSettings {
        general: GeneralSettings,
    }

    #[derive(Default, Serialize, Deserialize)]
    struct GeneralSettings {
        theme: String,
    }

    impl SettingsSchema for TestSettings {
        fn get_metadata() -> HashMap<String, SettingMetadata> {
            let mut map = HashMap::new();
            map.insert(
                "general.theme".to_string(),
                SettingMetadata::text("light").meta_str("label", "Theme"),
            );
            map
        }
    }

    let temp_dir = TempDir::new().unwrap();

    let config = SettingsConfig::builder("test-app", "1.0.0")
        .with_config_dir(temp_dir.path())
        .with_schema::<TestSettings>()
        .with_profiles()
        .build();
    let manager = SettingsManager::new(config).unwrap();

    manager
        .save_setting("general", "theme", &json!("dark"))
        .unwrap();
    manager.create_profile("work").unwrap();
    manager.switch_profile("work").unwrap();
    manager
        .save_setting("general", "theme", &json!("ocean"))
        .unwrap();

    let events = Arc::new(Mutex::new(Vec::new()));
    let events_clone = Arc::clone(&events);
    manager.events().on_change(move |key, old, new| {
        events_clone
            .lock()
            .unwrap()
            .push((key.to_string(), old.clone(), new.clone()));
    });

    manager.switch_profile("default").unwrap();
    manager.switch_profile("work").unwrap();

    let recorded = events.lock().unwrap();
    assert_eq!(recorded.len(), 2);
    assert!(recorded.iter().any(|(key, old, new)| key == "general.theme"
        && *old == json!("ocean")
        && *new == json!("dark")));
    assert!(recorded.iter().any(|(key, old, new)| key == "general.theme"
        && *old == json!("dark")
        && *new == json!("ocean")));
}

#[test]
fn test_main_profile_switch_without_value_change_emits_no_callbacks() {
    use rcman::{SettingMetadata, SettingsSchema};
    use serde::{Deserialize, Serialize};
    use std::collections::HashMap;

    #[derive(Default, Serialize, Deserialize)]
    struct TestSettings {
        general: GeneralSettings,
    }

    #[derive(Default, Serialize, Deserialize)]
    struct GeneralSettings {
        theme: String,
    }

    impl SettingsSchema for TestSettings {
        fn get_metadata() -> HashMap<String, SettingMetadata> {
            let mut map = HashMap::new();
            map.insert(
                "general.theme".to_string(),
                SettingMetadata::text("light").meta_str("label", "Theme"),
            );
            map
        }
    }

    let temp_dir = TempDir::new().unwrap();

    let config = SettingsConfig::builder("test-app", "1.0.0")
        .with_config_dir(temp_dir.path())
        .with_schema::<TestSettings>()
        .with_profiles()
        .build();
    let manager = SettingsManager::new(config).unwrap();

    manager.create_profile("work").unwrap();

    let callback_count = Arc::new(Mutex::new(0usize));
    let callback_count_clone = Arc::clone(&callback_count);
    manager.events().on_change(move |_key, _old, _new| {
        let mut guard = callback_count_clone.lock().unwrap();
        *guard += 1;
    });

    manager.switch_profile("work").unwrap();
    manager.switch_profile("default").unwrap();

    assert_eq!(*callback_count.lock().unwrap(), 0);
}

#[test]
fn test_main_settings_profiles_directory_structure() {
    use rcman::{SettingMetadata, SettingsSchema};
    use serde::{Deserialize, Serialize};
    use std::collections::HashMap;

    #[derive(Default, Serialize, Deserialize)]
    struct TestSettings {
        ui: UiSettings,
    }

    #[derive(Default, Serialize, Deserialize)]
    struct UiSettings {
        theme: String,
    }

    impl SettingsSchema for TestSettings {
        fn get_metadata() -> HashMap<String, SettingMetadata> {
            let mut map = HashMap::new();
            map.insert(
                "ui.theme".to_string(),
                SettingMetadata::text("light").meta_str("label", "Theme"),
            );
            map
        }
    }

    let temp_dir = TempDir::new().unwrap();

    let config = SettingsConfig::builder("test-app", "1.0.0")
        .with_config_dir(temp_dir.path())
        .with_schema::<TestSettings>()
        .with_profiles()
        .build();
    let manager = SettingsManager::new(config).unwrap();

    // Save something to create the default profile directory
    manager.save_setting("ui", "theme", &json!("dark")).unwrap();

    manager.create_profile("work").unwrap();

    // Check directory structure
    // .profiles.json should exist at config root
    assert!(
        temp_dir.path().join(".profiles.json").exists(),
        ".profiles.json should exist"
    );

    // profiles/ directory should exist
    let profiles_dir = temp_dir.path().join("profiles");
    assert!(profiles_dir.exists(), "profiles/ directory should exist");

    // work profile dir exists because we created it
    assert!(
        profiles_dir.join("work").exists(),
        "work profile directory should exist"
    );

    // default profile dir should exist after saving
    assert!(
        profiles_dir.join("default").exists(),
        "default profile directory should exist after saving"
    );
    assert!(
        profiles_dir.join("default").join("settings.json").exists(),
        "settings.json should exist in default profile"
    );
}

#[test]
fn test_main_profile_propagation_with_mixed_sub_settings() {
    let temp_dir = TempDir::new().unwrap();

    let manager = SettingsManager::builder("test-app", "1.0.0")
        .with_config_dir(temp_dir.path())
        .with_profiles()
        .with_sub_settings(SubSettingsConfig::new("remotes").with_profiles())
        .with_sub_settings(SubSettingsConfig::new("shared"))
        .build()
        .unwrap();

    manager.create_profile("work").unwrap();
    manager.switch_profile("work").unwrap();

    let remotes = manager.sub_settings("remotes").unwrap();
    assert_eq!(remotes.profiles().unwrap().active().unwrap(), "work");

    let shared = manager.sub_settings("shared").unwrap();
    assert!(matches!(
        shared.profiles(),
        Err(rcman::Error::ProfilesNotEnabled)
    ));
}

#[cfg(any(feature = "keychain", feature = "encrypted-file"))]
#[test]
#[cfg_attr(
    feature = "keychain",
    ignore = "Requires Secret Service daemon (not available in CI)"
)]
fn test_secret_reset_is_profile_scoped() {
    use rcman::{SettingMetadata, SettingsSchema};
    use serde::{Deserialize, Serialize};
    use std::collections::HashMap;

    #[derive(Default, Serialize, Deserialize)]
    struct TestSettings {
        api: ApiSettings,
    }

    #[derive(Default, Serialize, Deserialize)]
    struct ApiSettings {
        key: String,
    }

    impl SettingsSchema for TestSettings {
        fn get_metadata() -> HashMap<String, SettingMetadata> {
            let mut map = HashMap::new();
            map.insert(
                "api.key".to_string(),
                SettingMetadata::text("")
                    .meta_str("label", "API Key")
                    .secret(),
            );
            map
        }
    }

    let temp_dir = TempDir::new().unwrap();

    let config = SettingsConfig::builder("test-app", "1.0.0")
        .with_config_dir(temp_dir.path())
        .with_schema::<TestSettings>()
        .with_profiles()
        .with_credentials()
        .build();
    let manager = SettingsManager::new(config).unwrap();

    manager
        .save_setting("api", "key", &json!("default-secret"))
        .unwrap();

    manager.create_profile("work").unwrap();
    manager.switch_profile("work").unwrap();
    manager
        .save_setting("api", "key", &json!("work-secret"))
        .unwrap();
    manager.reset_setting("api", "key").unwrap();

    let work_metadata = manager.metadata().unwrap();
    assert_eq!(work_metadata.get("api.key").unwrap().value, Some(json!("")));

    manager.switch_profile("default").unwrap();
    let default_metadata = manager.metadata().unwrap();
    assert_eq!(
        default_metadata.get("api.key").unwrap().value,
        Some(json!("default-secret"))
    );
}