mockforge-cli 0.3.118

CLI interface for MockForge
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
//! Snapshot management commands
//!
//! Provides CLI commands for saving, loading, listing, and managing snapshots.

use clap::Subcommand;
use mockforge_core::snapshots::{SnapshotComponents, SnapshotManager};
use mockforge_core::Result;
use std::path::PathBuf;
use tracing::info;

/// Snapshot management subcommands
#[derive(Subcommand, Debug)]
pub enum SnapshotCommands {
    /// Save current system state
    Save {
        /// Snapshot name
        name: String,
        /// Optional description
        #[arg(long)]
        description: Option<String>,
        /// Workspace ID (defaults to "default")
        #[arg(long, default_value = "default")]
        workspace: String,
        /// Components to include (comma-separated: unified_state,vbr_state,recorder_state,workspace_config)
        /// Default: all
        #[arg(long, value_delimiter = ',')]
        components: Option<Vec<String>>,
    },
    /// Restore system state from snapshot
    Load {
        /// Snapshot name
        name: String,
        /// Workspace ID (defaults to "default")
        #[arg(long, default_value = "default")]
        workspace: String,
        /// Components to restore (comma-separated, default: all)
        #[arg(long, value_delimiter = ',')]
        components: Option<Vec<String>>,
        /// Dry run (validate without restoring)
        #[arg(long)]
        dry_run: bool,
    },
    /// List all snapshots
    List {
        /// Workspace ID (defaults to "default")
        #[arg(long, default_value = "default")]
        workspace: String,
    },
    /// Delete a snapshot
    Delete {
        /// Snapshot name
        name: String,
        /// Workspace ID (defaults to "default")
        #[arg(long, default_value = "default")]
        workspace: String,
    },
    /// Show snapshot information
    Info {
        /// Snapshot name
        name: String,
        /// Workspace ID (defaults to "default")
        #[arg(long, default_value = "default")]
        workspace: String,
    },
    /// Validate snapshot integrity
    Validate {
        /// Snapshot name
        name: String,
        /// Workspace ID (defaults to "default")
        #[arg(long, default_value = "default")]
        workspace: String,
    },
}

/// Parse component list from string vector
fn parse_components(components: Option<Vec<String>>) -> SnapshotComponents {
    if let Some(comp_list) = components {
        let comp_set: std::collections::HashSet<String> =
            comp_list.iter().map(|s| s.to_lowercase()).collect();

        SnapshotComponents {
            unified_state: comp_set.is_empty()
                || comp_set.contains("unified_state")
                || comp_set.contains("unified-state"),
            vbr_state: comp_set.is_empty()
                || comp_set.contains("vbr_state")
                || comp_set.contains("vbr-state"),
            recorder_state: comp_set.is_empty()
                || comp_set.contains("recorder_state")
                || comp_set.contains("recorder-state"),
            workspace_config: comp_set.is_empty()
                || comp_set.contains("workspace_config")
                || comp_set.contains("workspace-config"),
            protocols: Vec::new(), // Empty = all protocols
        }
    } else {
        SnapshotComponents::all()
    }
}

fn strip_unintegrated_components(
    mut components: SnapshotComponents,
) -> (SnapshotComponents, Vec<&'static str>) {
    let mut skipped = Vec::new();

    if components.unified_state {
        components.unified_state = false;
        skipped.push("unified_state");
    }
    if components.vbr_state {
        components.vbr_state = false;
        skipped.push("vbr_state");
    }
    if components.recorder_state {
        components.recorder_state = false;
        skipped.push("recorder_state");
    }
    if components.workspace_config {
        components.workspace_config = false;
        skipped.push("workspace_config");
    }

    (components, skipped)
}

/// Handle snapshot commands
pub async fn handle_snapshot_command(command: SnapshotCommands) -> Result<()> {
    // Initialize snapshot manager
    let snapshot_dir = std::env::var("MOCKFORGE_SNAPSHOT_DIR").ok().map(PathBuf::from);
    let manager = SnapshotManager::new(snapshot_dir);

    match command {
        SnapshotCommands::Save {
            name,
            description,
            workspace,
            components,
        } => {
            info!("Saving snapshot '{}' for workspace '{}'", name, workspace);
            let components = parse_components(components);
            let (components, skipped_components) = strip_unintegrated_components(components);
            if !skipped_components.is_empty() {
                println!(
                    "âš  Snapshot state integration is partial in CLI mode; skipping unsupported components: {}",
                    skipped_components.join(", ")
                );
            }

            // CLI mode does not own a live server state object, so component providers are
            // intentionally absent here and SnapshotManager falls back to empty component payloads.
            let manifest = manager
                .save_snapshot(
                    name.clone(),
                    description,
                    workspace.clone(),
                    components,
                    None,
                    None,
                    None,
                    None,
                )
                .await?;

            println!("✓ Snapshot '{}' saved successfully", name);
            println!("  Workspace: {}", workspace);
            println!("  Size: {} bytes", manifest.size_bytes);
            println!("  Checksum: {}", manifest.checksum);
            if let Some(desc) = &manifest.description {
                println!("  Description: {}", desc);
            }
        }
        SnapshotCommands::Load {
            name,
            workspace,
            components,
            dry_run,
        } => {
            if dry_run {
                info!("Validating snapshot '{}' for workspace '{}' (dry run)", name, workspace);
                let is_valid = manager.validate_snapshot(name.clone(), workspace.clone()).await?;
                if is_valid {
                    println!("✓ Snapshot '{}' is valid", name);
                } else {
                    println!("✗ Snapshot '{}' failed validation", name);
                    return Err(mockforge_core::Error::internal("Snapshot validation failed"));
                }
            } else {
                info!("Loading snapshot '{}' for workspace '{}'", name, workspace);
                let components = components.map(|c| {
                    let requested = parse_components(Some(c));
                    let (filtered, skipped_components) = strip_unintegrated_components(requested);
                    if !skipped_components.is_empty() {
                        println!(
                            "âš  Snapshot restore integration is partial in CLI mode; skipping unsupported components: {}",
                            skipped_components.join(", ")
                        );
                    }
                    filtered
                });

                // CLI mode restores from artifact files only; live consistency/workspace providers
                // are not available in this process.
                let (manifest, vbr_state, recorder_state) = manager
                    .load_snapshot(name.clone(), workspace.clone(), components, None, None)
                    .await?;

                println!("✓ Snapshot '{}' loaded successfully", name);
                println!("  Workspace: {}", workspace);
                println!("  Created: {}", manifest.created_at.format("%Y-%m-%d %H:%M:%S UTC"));
                if let Some(desc) = &manifest.description {
                    println!("  Description: {}", desc);
                }
                if vbr_state.is_some() {
                    println!("  VBR state: loaded (restore manually if needed)");
                }
                if recorder_state.is_some() {
                    println!("  Recorder state: loaded (restore manually if needed)");
                }
            }
        }
        SnapshotCommands::List { workspace } => {
            info!("Listing snapshots for workspace '{}'", workspace);
            let snapshots = manager.list_snapshots(&workspace).await?;

            if snapshots.is_empty() {
                println!("No snapshots found for workspace '{}'", workspace);
            } else {
                println!("Snapshots for workspace '{}':", workspace);
                println!();
                for snapshot in snapshots {
                    println!("  {}", snapshot.name);
                    if let Some(desc) = &snapshot.description {
                        println!("    Description: {}", desc);
                    }
                    println!(
                        "    Created: {}",
                        snapshot.created_at.format("%Y-%m-%d %H:%M:%S UTC")
                    );
                    println!("    Size: {} bytes", snapshot.size_bytes);
                    println!();
                }
            }
        }
        SnapshotCommands::Delete { name, workspace } => {
            info!("Deleting snapshot '{}' for workspace '{}'", name, workspace);
            manager.delete_snapshot(name.clone(), workspace.clone()).await?;
            println!("✓ Snapshot '{}' deleted successfully", name);
        }
        SnapshotCommands::Info { name, workspace } => {
            info!("Getting info for snapshot '{}' in workspace '{}'", name, workspace);
            let manifest = manager.get_snapshot_info(name.clone(), workspace.clone()).await?;

            println!("Snapshot: {}", manifest.name);
            println!("  Workspace: {}", manifest.workspace_id);
            println!("  Created: {}", manifest.created_at.format("%Y-%m-%d %H:%M:%S UTC"));
            println!("  Size: {} bytes", manifest.size_bytes);
            println!("  Checksum: {}", manifest.checksum);
            if let Some(desc) = &manifest.description {
                println!("  Description: {}", desc);
            }
            println!("  Components:");
            println!("    Unified State: {}", manifest.components.unified_state);
            println!("    VBR State: {}", manifest.components.vbr_state);
            println!("    Recorder State: {}", manifest.components.recorder_state);
            println!("    Workspace Config: {}", manifest.components.workspace_config);
            if manifest.components.protocols.is_empty() {
                println!("    Protocols: all");
            } else {
                println!("    Protocols: {}", manifest.components.protocols.join(", "));
            }
        }
        SnapshotCommands::Validate { name, workspace } => {
            info!("Validating snapshot '{}' for workspace '{}'", name, workspace);
            let is_valid = manager.validate_snapshot(name.clone(), workspace.clone()).await?;
            if is_valid {
                println!("✓ Snapshot '{}' is valid", name);
            } else {
                println!("✗ Snapshot '{}' failed validation (checksum mismatch)", name);
                return Err(mockforge_core::Error::internal("Snapshot validation failed"));
            }
        }
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_components_none() {
        let components = parse_components(None);
        assert!(components.unified_state);
        assert!(components.vbr_state);
        assert!(components.recorder_state);
        assert!(components.workspace_config);
        assert!(components.protocols.is_empty());
    }

    #[test]
    fn test_parse_components_empty_vec() {
        let components = parse_components(Some(vec![]));
        // Empty vec means all components
        assert!(components.unified_state);
        assert!(components.vbr_state);
        assert!(components.recorder_state);
        assert!(components.workspace_config);
    }

    #[test]
    fn test_parse_components_single_underscore() {
        let components = parse_components(Some(vec!["unified_state".to_string()]));
        assert!(components.unified_state);
        assert!(!components.vbr_state);
        assert!(!components.recorder_state);
        assert!(!components.workspace_config);
    }

    #[test]
    fn test_parse_components_single_dash() {
        let components = parse_components(Some(vec!["vbr-state".to_string()]));
        assert!(!components.unified_state);
        assert!(components.vbr_state);
        assert!(!components.recorder_state);
        assert!(!components.workspace_config);
    }

    #[test]
    fn test_parse_components_multiple() {
        let components =
            parse_components(Some(vec!["unified_state".to_string(), "recorder-state".to_string()]));
        assert!(components.unified_state);
        assert!(!components.vbr_state);
        assert!(components.recorder_state);
        assert!(!components.workspace_config);
    }

    #[test]
    fn test_parse_components_case_insensitive() {
        let components = parse_components(Some(vec!["UNIFIED_STATE".to_string()]));
        assert!(components.unified_state);
    }

    #[test]
    fn test_parse_components_workspace_config() {
        let components = parse_components(Some(vec!["workspace_config".to_string()]));
        assert!(!components.unified_state);
        assert!(!components.vbr_state);
        assert!(!components.recorder_state);
        assert!(components.workspace_config);
    }

    #[test]
    fn test_parse_components_workspace_config_dash() {
        let components = parse_components(Some(vec!["workspace-config".to_string()]));
        assert!(components.workspace_config);
    }

    #[test]
    fn test_parse_components_all_explicit() {
        let components = parse_components(Some(vec![
            "unified_state".to_string(),
            "vbr_state".to_string(),
            "recorder_state".to_string(),
            "workspace_config".to_string(),
        ]));
        assert!(components.unified_state);
        assert!(components.vbr_state);
        assert!(components.recorder_state);
        assert!(components.workspace_config);
    }

    #[test]
    fn test_snapshot_commands_enum_variants() {
        // Test that all enum variants exist and can be constructed
        let _save = SnapshotCommands::Save {
            name: "test".to_string(),
            description: Some("test description".to_string()),
            workspace: "default".to_string(),
            components: None,
        };

        let _load = SnapshotCommands::Load {
            name: "test".to_string(),
            workspace: "default".to_string(),
            components: None,
            dry_run: false,
        };

        let _list = SnapshotCommands::List {
            workspace: "default".to_string(),
        };

        let _delete = SnapshotCommands::Delete {
            name: "test".to_string(),
            workspace: "default".to_string(),
        };

        let _info = SnapshotCommands::Info {
            name: "test".to_string(),
            workspace: "default".to_string(),
        };

        let _validate = SnapshotCommands::Validate {
            name: "test".to_string(),
            workspace: "default".to_string(),
        };
    }

    #[test]
    fn test_snapshot_commands_debug() {
        let cmd = SnapshotCommands::Save {
            name: "test".to_string(),
            description: None,
            workspace: "default".to_string(),
            components: None,
        };
        let debug_str = format!("{:?}", cmd);
        assert!(debug_str.contains("Save"));
        assert!(debug_str.contains("test"));
    }
}