perfgate-cli 0.17.0

CLI for perfgate performance budgets and baseline diffs
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
//! Integration tests for `perfgate promote` command
//!
//! **Validates: Promote use case requirements**

use predicates::prelude::*;
use std::fs;
use tempfile::tempdir;

mod common;
use common::{fixtures_dir, perfgate_cmd};

/// Test basic promote copies file correctly
///
/// **Validates: Promote creates baseline file**
#[test]
fn test_promote_creates_baseline_file() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let baseline_path = temp_dir.path().join("baseline.json");

    let current = fixtures_dir().join("baseline.json");

    let mut cmd = perfgate_cmd();
    cmd.arg("promote")
        .arg("--current")
        .arg(&current)
        .arg("--to")
        .arg(&baseline_path);

    // Should exit with code 0 (success)
    cmd.assert().success();

    // Verify output file exists
    assert!(baseline_path.exists(), "baseline file should exist");

    // Verify it's valid JSON with correct schema
    let content = fs::read_to_string(&baseline_path).expect("failed to read baseline file");
    let receipt: serde_json::Value =
        serde_json::from_str(&content).expect("output should be valid JSON");

    assert_eq!(
        receipt["schema"].as_str(),
        Some("perfgate.run.v1"),
        "schema should be 'perfgate.run.v1'"
    );
}

/// Test promote preserves receipt data without normalize
///
/// **Validates: Receipt data preservation**
#[test]
fn test_promote_preserves_receipt_data() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let baseline_path = temp_dir.path().join("baseline.json");

    let current = fixtures_dir().join("baseline.json");

    // Read original to get run_id
    let original_content = fs::read_to_string(&current).expect("failed to read original");
    let original: serde_json::Value =
        serde_json::from_str(&original_content).expect("failed to parse original");

    let mut cmd = perfgate_cmd();
    cmd.arg("promote")
        .arg("--current")
        .arg(&current)
        .arg("--to")
        .arg(&baseline_path);

    cmd.assert().success();

    // Read promoted baseline
    let promoted_content = fs::read_to_string(&baseline_path).expect("failed to read promoted");
    let promoted: serde_json::Value =
        serde_json::from_str(&promoted_content).expect("failed to parse promoted");

    // run_id should be preserved without normalize
    assert_eq!(
        original["run"]["id"].as_str(),
        promoted["run"]["id"].as_str(),
        "run_id should be preserved"
    );

    // timestamps should be preserved
    assert_eq!(
        original["run"]["started_at"].as_str(),
        promoted["run"]["started_at"].as_str(),
        "started_at should be preserved"
    );

    // bench metadata should be preserved
    assert_eq!(
        original["bench"]["name"].as_str(),
        promoted["bench"]["name"].as_str(),
        "bench name should be preserved"
    );

    // stats should be preserved
    assert_eq!(
        original["stats"]["wall_ms"]["median"], promoted["stats"]["wall_ms"]["median"],
        "stats should be preserved"
    );
}

/// Test promote with normalize strips run-specific fields
///
/// **Validates: Normalize functionality**
#[test]
fn test_promote_normalize_strips_run_specific_fields() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let baseline_path = temp_dir.path().join("baseline.json");

    let current = fixtures_dir().join("baseline.json");

    let mut cmd = perfgate_cmd();
    cmd.arg("promote")
        .arg("--current")
        .arg(&current)
        .arg("--to")
        .arg(&baseline_path)
        .arg("--normalize");

    cmd.assert().success();

    // Read promoted baseline
    let promoted_content = fs::read_to_string(&baseline_path).expect("failed to read promoted");
    let promoted: serde_json::Value =
        serde_json::from_str(&promoted_content).expect("failed to parse promoted");

    // run_id should be "baseline"
    assert_eq!(
        promoted["run"]["id"].as_str(),
        Some("baseline"),
        "run_id should be 'baseline' after normalize"
    );

    // timestamps should be epoch
    assert_eq!(
        promoted["run"]["started_at"].as_str(),
        Some("1970-01-01T00:00:00Z"),
        "started_at should be epoch after normalize"
    );

    assert_eq!(
        promoted["run"]["ended_at"].as_str(),
        Some("1970-01-01T00:00:00Z"),
        "ended_at should be epoch after normalize"
    );
}

/// Test promote with normalize preserves important data
///
/// **Validates: Normalize preserves bench data**
#[test]
fn test_promote_normalize_preserves_important_data() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let baseline_path = temp_dir.path().join("baseline.json");

    let current = fixtures_dir().join("baseline.json");

    // Read original
    let original_content = fs::read_to_string(&current).expect("failed to read original");
    let original: serde_json::Value =
        serde_json::from_str(&original_content).expect("failed to parse original");

    let mut cmd = perfgate_cmd();
    cmd.arg("promote")
        .arg("--current")
        .arg(&current)
        .arg("--to")
        .arg(&baseline_path)
        .arg("--normalize");

    cmd.assert().success();

    // Read promoted baseline
    let promoted_content = fs::read_to_string(&baseline_path).expect("failed to read promoted");
    let promoted: serde_json::Value =
        serde_json::from_str(&promoted_content).expect("failed to parse promoted");

    // bench metadata should be preserved
    assert_eq!(
        original["bench"]["name"].as_str(),
        promoted["bench"]["name"].as_str(),
        "bench name should be preserved after normalize"
    );

    assert_eq!(
        original["bench"]["command"], promoted["bench"]["command"],
        "bench command should be preserved after normalize"
    );

    // stats should be preserved
    assert_eq!(
        original["stats"]["wall_ms"]["median"], promoted["stats"]["wall_ms"]["median"],
        "stats should be preserved after normalize"
    );

    // samples should be preserved
    assert_eq!(
        original["samples"].as_array().map(|a| a.len()),
        promoted["samples"].as_array().map(|a| a.len()),
        "samples count should be preserved after normalize"
    );

    // host info should be preserved (os/arch)
    assert_eq!(
        original["run"]["host"]["os"].as_str(),
        promoted["run"]["host"]["os"].as_str(),
        "host os should be preserved after normalize"
    );

    assert_eq!(
        original["run"]["host"]["arch"].as_str(),
        promoted["run"]["host"]["arch"].as_str(),
        "host arch should be preserved after normalize"
    );
}

/// Test promote fails gracefully with missing source file
///
/// **Validates: Error handling for missing source**
#[test]
fn test_promote_missing_source_file() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let baseline_path = temp_dir.path().join("baseline.json");

    let nonexistent = temp_dir.path().join("nonexistent.json");

    let mut cmd = perfgate_cmd();
    cmd.arg("promote")
        .arg("--current")
        .arg(&nonexistent)
        .arg("--to")
        .arg(&baseline_path);

    // Should exit with code 1 (error)
    cmd.assert()
        .failure()
        .stderr(predicate::str::contains("read"));
}

/// Test promote fails gracefully with invalid JSON source
///
/// **Validates: Error handling for invalid JSON**
#[test]
fn test_promote_invalid_json_source() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let baseline_path = temp_dir.path().join("baseline.json");

    // Create an invalid JSON file
    let invalid_json = temp_dir.path().join("invalid.json");
    fs::write(&invalid_json, "{ invalid json }").expect("failed to write invalid json");

    let mut cmd = perfgate_cmd();
    cmd.arg("promote")
        .arg("--current")
        .arg(&invalid_json)
        .arg("--to")
        .arg(&baseline_path);

    // Should exit with code 1 (error)
    cmd.assert()
        .failure()
        .stderr(predicate::str::contains("parse"));
}

/// Test promote with pretty flag formats JSON nicely
///
/// **Validates: Pretty-print option**
#[test]
fn test_promote_pretty_flag() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let baseline_path = temp_dir.path().join("baseline.json");

    let current = fixtures_dir().join("baseline.json");

    let mut cmd = perfgate_cmd();
    cmd.arg("promote")
        .arg("--current")
        .arg(&current)
        .arg("--to")
        .arg(&baseline_path)
        .arg("--pretty");

    cmd.assert().success();

    // Read promoted baseline
    let content = fs::read_to_string(&baseline_path).expect("failed to read promoted");

    // Pretty-printed JSON should have newlines and indentation
    assert!(
        content.contains('\n'),
        "pretty-printed JSON should contain newlines"
    );
    assert!(
        content.contains("  "),
        "pretty-printed JSON should have indentation"
    );
}

/// Test atomic write behavior - no temp files should remain
///
/// **Validates: Atomic write behavior**
#[test]
fn test_promote_atomic_write_no_temp_files() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let baseline_path = temp_dir.path().join("baseline.json");

    let current = fixtures_dir().join("baseline.json");

    let mut cmd = perfgate_cmd();
    cmd.arg("promote")
        .arg("--current")
        .arg(&current)
        .arg("--to")
        .arg(&baseline_path);

    cmd.assert().success();

    // Check no .tmp files remain in the directory
    let entries: Vec<_> = fs::read_dir(temp_dir.path())
        .expect("failed to read temp dir")
        .filter_map(|e| e.ok())
        .filter(|e| {
            let name = e.file_name().to_string_lossy().to_string();
            name.starts_with('.') && name.ends_with(".tmp")
        })
        .collect();

    assert!(
        entries.is_empty(),
        "no .tmp files should remain after promote, found: {:?}",
        entries.iter().map(|e| e.path()).collect::<Vec<_>>()
    );
}

/// Test promote --ratchet updates config and emits ratchet artifact.
#[test]
fn test_promote_ratchet_updates_config() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let compare_path = temp_dir.path().join("compare.json");
    let baseline_path = temp_dir.path().join("baseline.json");
    let config_path = temp_dir.path().join("perfgate.toml");
    let ratchet_out = temp_dir.path().join("ratchet.json");

    let baseline = fixtures_dir().join("baseline.json");
    let current = fixtures_dir().join("current_pass.json");

    // Build compare receipt first.
    let mut compare_cmd = perfgate_cmd();
    compare_cmd
        .arg("compare")
        .arg("--baseline")
        .arg(&baseline)
        .arg("--current")
        .arg(&current)
        .arg("--significance-alpha")
        .arg("0.05")
        .arg("--require-significance")
        .arg("--out")
        .arg(&compare_path);
    compare_cmd.assert().success();

    // Config with ratchet enabled and threshold budget for wall_ms.
    let config = r#"[ratchet]
enabled = true
mode = "threshold"
min_improvement = 0.01
max_tightening = 0.10
require_significance = false
allow_metrics = ["wall_ms"]

[[bench]]
name = "test-benchmark"
command = ["echo", "hello"]
[bench.budgets.wall_ms]
threshold = 0.20
"#;
    fs::write(&config_path, config).expect("write config");

    let mut promote_cmd = perfgate_cmd();
    promote_cmd
        .arg("promote")
        .arg("--current")
        .arg(&current)
        .arg("--to")
        .arg(&baseline_path)
        .arg("--ratchet")
        .arg("--compare")
        .arg(&compare_path)
        .arg("--config")
        .arg(&config_path)
        .arg("--ratchet-out")
        .arg(&ratchet_out);

    promote_cmd.assert().success();
    assert!(ratchet_out.exists(), "ratchet artifact should exist");
    let updated_cfg = fs::read_to_string(&config_path).expect("read config");
    assert!(
        updated_cfg.contains("threshold = 0."),
        "threshold should still be present"
    );
}

/// Test promote creates parent directories if needed
///
/// **Validates: Directory creation**
#[test]
fn test_promote_creates_parent_directories() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let baseline_path = temp_dir
        .path()
        .join("subdir")
        .join("nested")
        .join("baseline.json");

    let current = fixtures_dir().join("baseline.json");

    let mut cmd = perfgate_cmd();
    cmd.arg("promote")
        .arg("--current")
        .arg(&current)
        .arg("--to")
        .arg(&baseline_path);

    cmd.assert().success();

    // Verify the file was created in the nested directory
    assert!(
        baseline_path.exists(),
        "baseline file should exist in nested directory"
    );
}