apr-cli 0.4.17

CLI tool for APR model inspection, debugging, and operations
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
use super::*;
use std::io::Write;
use tempfile::NamedTempFile;

// ========================================================================
// CanaryData and TensorCanary Tests
// ========================================================================

#[test]
fn test_canary_data_serialize_deserialize() {
    let canary = CanaryData {
        model_name: "test-model.safetensors".to_string(),
        tensor_count: 1,
        tensors: BTreeMap::new(),
        created_at: "2024-01-01T00:00:00Z".to_string(),
    };
    let json = serde_json::to_string(&canary).expect("serialize");
    let parsed: CanaryData = serde_json::from_str(&json).expect("deserialize");
    assert_eq!(parsed.model_name, "test-model.safetensors");
    assert_eq!(parsed.tensor_count, 1);
}

#[test]
fn test_tensor_canary_serialize_deserialize() {
    let tensor = TensorCanary {
        shape: vec![768, 768],
        count: 589824,
        mean: 0.0,
        std: 0.02,
        min: -0.1,
        max: 0.1,
    };
    let json = serde_json::to_string(&tensor).expect("serialize");
    let parsed: TensorCanary = serde_json::from_str(&json).expect("deserialize");
    assert_eq!(parsed.shape, vec![768, 768]);
    assert_eq!(parsed.count, 589824);
}

#[test]
fn test_canary_data_with_tensors() {
    let mut tensors = BTreeMap::new();
    tensors.insert(
        "encoder.weight".to_string(),
        TensorCanary {
            shape: vec![768, 768],
            count: 589824,
            mean: 0.0,
            std: 0.02,
            min: -0.1,
            max: 0.1,
        },
    );
    let canary = CanaryData {
        model_name: "test.safetensors".to_string(),
        tensor_count: 1,
        tensors,
        created_at: "2024-01-01T00:00:00Z".to_string(),
    };
    let json = serde_json::to_string_pretty(&canary).expect("serialize");
    assert!(json.contains("encoder.weight"));
    assert!(json.contains("768"));
}

#[test]
fn test_canary_data_clone() {
    let canary = CanaryData {
        model_name: "test.safetensors".to_string(),
        tensor_count: 0,
        tensors: BTreeMap::new(),
        created_at: "2024-01-01T00:00:00Z".to_string(),
    };
    let cloned = canary.clone();
    assert_eq!(cloned.model_name, canary.model_name);
}

#[test]
fn test_tensor_canary_clone() {
    let tensor = TensorCanary {
        shape: vec![768],
        count: 768,
        mean: 0.5,
        std: 0.1,
        min: 0.0,
        max: 1.0,
    };
    let cloned = tensor.clone();
    assert_eq!(cloned.mean, tensor.mean);
}

// ========================================================================
// CanaryCheckResult Tests
// ========================================================================

#[test]
fn test_canary_check_result_passed() {
    let result = CanaryCheckResult {
        tensor_name: "weight".to_string(),
        passed: true,
        mean_drift: 0.01,
        std_drift: 0.02,
        shape_match: true,
        message: None,
    };
    assert!(result.passed);
    assert!(result.message.is_none());
}

#[test]
fn test_canary_check_result_failed() {
    let result = CanaryCheckResult {
        tensor_name: "weight".to_string(),
        passed: false,
        mean_drift: 0.15,
        std_drift: 0.02,
        shape_match: true,
        message: Some("Mean drift exceeded".to_string()),
    };
    assert!(!result.passed);
    assert!(result.message.is_some());
}

#[test]
fn test_canary_check_result_debug() {
    let result = CanaryCheckResult {
        tensor_name: "test".to_string(),
        passed: true,
        mean_drift: 0.0,
        std_drift: 0.0,
        shape_match: true,
        message: None,
    };
    let debug = format!("{result:?}");
    assert!(debug.contains("CanaryCheckResult"));
}

// ========================================================================
// compute_relative_drift Tests
// ========================================================================

#[test]
fn test_compute_relative_drift_normal() {
    // 10% drift: actual=1.1, expected=1.0
    let drift = compute_relative_drift(1.1, 1.0);
    assert!((drift - 0.1).abs() < 0.001);
}

#[test]
fn test_compute_relative_drift_negative() {
    // -10% drift: actual=0.9, expected=1.0
    let drift = compute_relative_drift(0.9, 1.0);
    assert!((drift - 0.1).abs() < 0.001);
}

#[test]
fn test_compute_relative_drift_zero_expected() {
    // When expected is near zero, use absolute difference
    let drift = compute_relative_drift(0.001, 0.0);
    assert!((drift - 0.001).abs() < 0.0001);
}

#[test]
fn test_compute_relative_drift_same_value() {
    let drift = compute_relative_drift(1.0, 1.0);
    assert_eq!(drift, 0.0);
}

#[test]
fn test_compute_relative_drift_large_values() {
    // 50% drift: actual=150, expected=100
    let drift = compute_relative_drift(150.0, 100.0);
    assert!((drift - 0.5).abs() < 0.001);
}

// ========================================================================
// missing_tensor_result Tests
// ========================================================================

#[test]
fn test_missing_tensor_result() {
    let result = missing_tensor_result("missing_weight");
    assert_eq!(result.tensor_name, "missing_weight");
    assert!(!result.passed);
    assert_eq!(result.mean_drift, f32::MAX);
    assert_eq!(result.std_drift, f32::MAX);
    assert!(!result.shape_match);
    assert!(result.message.is_some());
    assert!(result.message.unwrap().contains("not found"));
}

// ========================================================================
// build_failure_message Tests
// ========================================================================

#[test]
fn test_build_failure_message_passed() {
    // Create a minimal TensorCanary for reference
    let _expected = TensorCanary {
        shape: vec![768],
        count: 768,
        mean: 0.0,
        std: 0.02,
        min: -0.1,
        max: 0.1,
    };
    // Since we can't easily create TensorMetadata, we use a helper
    // to test the passed case where message should be None
    let msg = build_failure_message_test_helper(true, true, 0.01, 0.01);
    assert!(msg.is_none());
}

// Helper for testing build_failure_message without needing TensorMetadata
fn build_failure_message_test_helper(
    passed: bool,
    shape_match: bool,
    mean_drift: f32,
    std_drift: f32,
) -> Option<String> {
    if passed {
        return None;
    }
    Some(if !shape_match {
        "Shape mismatch".to_string()
    } else if mean_drift > MEAN_THRESHOLD {
        format!("Mean drift {:.1}% exceeds threshold", mean_drift * 100.0)
    } else {
        format!("Std drift {:.1}% exceeds threshold", std_drift * 100.0)
    })
}

#[test]
fn test_build_failure_message_shape_mismatch() {
    let msg = build_failure_message_test_helper(false, false, 0.01, 0.01);
    assert!(msg.is_some());
    assert!(msg.unwrap().contains("Shape mismatch"));
}

#[test]
fn test_build_failure_message_mean_drift() {
    let msg = build_failure_message_test_helper(false, true, 0.15, 0.01);
    assert!(msg.is_some());
    assert!(msg.unwrap().contains("Mean drift"));
}

#[test]
fn test_build_failure_message_std_drift() {
    let msg = build_failure_message_test_helper(false, true, 0.05, 0.25);
    assert!(msg.is_some());
    assert!(msg.unwrap().contains("Std drift"));
}

// ========================================================================
// CanaryCommands Tests
// ========================================================================

#[test]
fn test_canary_commands_create() {
    let cmd = CanaryCommands::Create {
        file: PathBuf::from("model.safetensors"),
        input: PathBuf::from("input.wav"),
        output: PathBuf::from("canary.json"),
    };
    match cmd {
        CanaryCommands::Create {
            file,
            input,
            output,
        } => {
            assert_eq!(file.to_string_lossy(), "model.safetensors");
            assert_eq!(input.to_string_lossy(), "input.wav");
            assert_eq!(output.to_string_lossy(), "canary.json");
        }
        _ => panic!("Wrong command variant"),
    }
}

#[test]
fn test_canary_commands_check() {
    let cmd = CanaryCommands::Check {
        file: PathBuf::from("model.safetensors"),
        canary: PathBuf::from("canary.json"),
    };
    match cmd {
        CanaryCommands::Check { file, canary } => {
            assert_eq!(file.to_string_lossy(), "model.safetensors");
            assert_eq!(canary.to_string_lossy(), "canary.json");
        }
        _ => panic!("Wrong command variant"),
    }
}

#[test]
fn test_canary_commands_clone() {
    let cmd = CanaryCommands::Create {
        file: PathBuf::from("model.safetensors"),
        input: PathBuf::from("input.wav"),
        output: PathBuf::from("canary.json"),
    };
    let cloned = cmd.clone();
    match cloned {
        CanaryCommands::Create { file, .. } => {
            assert_eq!(file.to_string_lossy(), "model.safetensors");
        }
        _ => panic!("Wrong command variant"),
    }
}

#[test]
fn test_canary_commands_debug() {
    let cmd = CanaryCommands::Check {
        file: PathBuf::from("model.safetensors"),
        canary: PathBuf::from("canary.json"),
    };
    let debug = format!("{cmd:?}");
    assert!(debug.contains("Check"));
}

// ========================================================================
// run Command Tests
// ========================================================================

#[test]
fn test_run_create_model_not_found() {
    let output = NamedTempFile::with_suffix(".json").expect("create output");
    let input = NamedTempFile::with_suffix(".wav").expect("create input");
    let cmd = CanaryCommands::Create {
        file: PathBuf::from("/nonexistent/model.safetensors"),
        input: input.path().to_path_buf(),
        output: output.path().to_path_buf(),
    };
    let result = run(cmd);
    assert!(result.is_err());
}

#[test]
fn test_run_create_invalid_model() {
    let mut model = NamedTempFile::with_suffix(".safetensors").expect("create model");
    model
        .write_all(b"not a valid safetensors file")
        .expect("write");
    let output = NamedTempFile::with_suffix(".json").expect("create output");
    let input = NamedTempFile::with_suffix(".wav").expect("create input");

    let cmd = CanaryCommands::Create {
        file: model.path().to_path_buf(),
        input: input.path().to_path_buf(),
        output: output.path().to_path_buf(),
    };
    let result = run(cmd);
    assert!(result.is_err());
}

#[test]
fn test_run_check_model_not_found() {
    let mut canary = NamedTempFile::with_suffix(".json").expect("create canary");
    canary
        .write_all(br#"{"model_name": "test", "tensor_count": 0, "tensors": {}, "created_at": ""}"#)
        .expect("write");

    let cmd = CanaryCommands::Check {
        file: PathBuf::from("/nonexistent/model.safetensors"),
        canary: canary.path().to_path_buf(),
    };
    let result = run(cmd);
    assert!(result.is_err());
}

#[test]
fn test_run_check_canary_not_found() {
    let mut model = NamedTempFile::with_suffix(".safetensors").expect("create model");
    model.write_all(b"fake model").expect("write");

    let cmd = CanaryCommands::Check {
        file: model.path().to_path_buf(),
        canary: PathBuf::from("/nonexistent/canary.json"),
    };
    let result = run(cmd);
    assert!(result.is_err());
}

#[test]
fn test_run_check_invalid_canary() {
    let mut model = NamedTempFile::with_suffix(".safetensors").expect("create model");
    model.write_all(b"fake model").expect("write");
    let mut canary = NamedTempFile::with_suffix(".json").expect("create canary");
    canary.write_all(b"not valid json").expect("write");

    let cmd = CanaryCommands::Check {
        file: model.path().to_path_buf(),
        canary: canary.path().to_path_buf(),
    };
    let result = run(cmd);
    assert!(result.is_err());
}

// ========================================================================
// validate_paths_exist Tests
// ========================================================================

#[test]
fn test_validate_paths_exist_model_missing() {
    let canary = NamedTempFile::with_suffix(".json").expect("create canary");
    let result = validate_paths_exist(Path::new("/nonexistent/model.safetensors"), canary.path());
    assert!(result.is_err());
}

#[test]
fn test_validate_paths_exist_canary_missing() {
    let model = NamedTempFile::with_suffix(".safetensors").expect("create model");
    let result = validate_paths_exist(model.path(), Path::new("/nonexistent/canary.json"));
    assert!(result.is_err());
}

#[test]
fn test_validate_paths_exist_both_exist() {
    let model = NamedTempFile::with_suffix(".safetensors").expect("create model");
    let canary = NamedTempFile::with_suffix(".json").expect("create canary");
    let result = validate_paths_exist(model.path(), canary.path());
    assert!(result.is_ok());
}

// ========================================================================
// load_canary_data Tests
// ========================================================================

#[test]
fn test_load_canary_data_valid() {
    let mut canary = NamedTempFile::with_suffix(".json").expect("create canary");
    canary.write_all(br#"{"model_name": "test.safetensors", "tensor_count": 0, "tensors": {}, "created_at": "2024-01-01"}"#).expect("write");

    let result = load_canary_data(canary.path());
    assert!(result.is_ok());
    assert_eq!(result.unwrap().model_name, "test.safetensors");
}

#[test]
fn test_load_canary_data_invalid_json() {
    let mut canary = NamedTempFile::with_suffix(".json").expect("create canary");
    canary.write_all(b"not valid json").expect("write");

    let result = load_canary_data(canary.path());
    assert!(result.is_err());
}

#[test]
fn test_load_canary_data_file_not_found() {
    let result = load_canary_data(Path::new("/nonexistent/canary.json"));
    assert!(result.is_err());
}

include!("canary_tests_mean_threshold_std.rs");