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
//! Integration tests for `perfgate md` command
//!
//! **Validates: Requirements 7.1, 7.6**

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

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

fn tradeoff_fixture_path() -> PathBuf {
    PathBuf::from(env!("CARGO_MANIFEST_DIR"))
        .join("..")
        .join("..")
        .join("fixtures")
        .join("schema")
        .join("v0.16")
        .join("perfgate.tradeoff.v1.json")
}

/// Test markdown generation from compare receipt with pass verdict
/// Verify output contains expected table structure and verdict emoji
///
/// **Validates: Requirements 7.1, 7.6**
#[test]
fn test_md_pass_verdict_stdout() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let compare_receipt_path = temp_dir.path().join("compare.json");

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

    // First, generate a compare receipt
    generate_compare_receipt(&baseline, &current, &compare_receipt_path)
        .expect("failed to generate compare receipt");

    assert!(
        compare_receipt_path.exists(),
        "compare receipt should exist"
    );

    // Run perfgate md command
    let mut cmd = perfgate_cmd();
    cmd.arg("md").arg("--compare").arg(&compare_receipt_path);

    cmd.assert()
        .success()
        // Verify verdict emoji for pass (Requirement 7.2)
        .stdout(predicate::str::contains(""))
        // Verify benchmark name is present (Requirement 7.3)
        .stdout(predicate::str::contains("test-benchmark"))
        // Verify table header with columns (Requirement 7.4)
        .stdout(predicate::str::contains("| metric |"))
        .stdout(predicate::str::contains("baseline"))
        .stdout(predicate::str::contains("current"))
        .stdout(predicate::str::contains("delta"))
        .stdout(predicate::str::contains("budget"))
        .stdout(predicate::str::contains("status"))
        // Verify metric row exists
        .stdout(predicate::str::contains("wall_ms"));
}

/// Test markdown generation from compare receipt with warn verdict
/// Verify output contains warning emoji
///
/// **Validates: Requirements 7.1, 7.6**
#[test]
fn test_md_warn_verdict_stdout() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let compare_receipt_path = temp_dir.path().join("compare.json");

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

    // Generate a compare receipt with warn verdict
    generate_compare_receipt(&baseline, &current, &compare_receipt_path)
        .expect("failed to generate compare receipt");

    assert!(
        compare_receipt_path.exists(),
        "compare receipt should exist"
    );

    // Run perfgate md command
    let mut cmd = perfgate_cmd();
    cmd.arg("md").arg("--compare").arg(&compare_receipt_path);

    cmd.assert()
        .success()
        // Verify verdict emoji for warn (Requirement 7.2)
        .stdout(predicate::str::contains("⚠️"))
        // Verify benchmark name is present (Requirement 7.3)
        .stdout(predicate::str::contains("test-benchmark"))
        // Verify table structure (Requirement 7.4)
        .stdout(predicate::str::contains("| metric |"));
}

/// Test markdown generation from compare receipt with fail verdict
/// Verify output contains fail emoji
///
/// **Validates: Requirements 7.1, 7.6**
#[test]
fn test_md_fail_verdict_stdout() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let compare_receipt_path = temp_dir.path().join("compare.json");

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

    // Generate a compare receipt with fail verdict
    generate_compare_receipt(&baseline, &current, &compare_receipt_path)
        .expect("failed to generate compare receipt");

    assert!(
        compare_receipt_path.exists(),
        "compare receipt should exist"
    );

    // Run perfgate md command
    let mut cmd = perfgate_cmd();
    cmd.arg("md").arg("--compare").arg(&compare_receipt_path);

    cmd.assert()
        .success()
        // Verify verdict emoji for fail (Requirement 7.2)
        .stdout(predicate::str::contains(""))
        // Verify benchmark name is present (Requirement 7.3)
        .stdout(predicate::str::contains("test-benchmark"))
        // Verify table structure (Requirement 7.4)
        .stdout(predicate::str::contains("| metric |"));
}

/// Test markdown output to file with --out flag
/// Verify file is created with expected content
///
/// **Validates: Requirements 7.1, 7.6**
#[test]
fn test_md_output_to_file() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let compare_receipt_path = temp_dir.path().join("compare.json");
    let md_output_path = temp_dir.path().join("output.md");

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

    // Generate a compare receipt
    generate_compare_receipt(&baseline, &current, &compare_receipt_path)
        .expect("failed to generate compare receipt");

    assert!(
        compare_receipt_path.exists(),
        "compare receipt should exist"
    );

    // Run perfgate md command with --out flag
    let mut cmd = perfgate_cmd();
    cmd.arg("md")
        .arg("--compare")
        .arg(&compare_receipt_path)
        .arg("--out")
        .arg(&md_output_path);

    cmd.assert().success();

    // Verify output file exists
    assert!(md_output_path.exists(), "markdown output file should exist");

    // Read and verify content
    let content = fs::read_to_string(&md_output_path).expect("failed to read markdown file");

    // Verify verdict emoji (Requirement 7.2)
    assert!(content.contains(""), "markdown should contain pass emoji");

    // Verify benchmark name (Requirement 7.3)
    assert!(
        content.contains("test-benchmark"),
        "markdown should contain benchmark name"
    );

    // Verify table header columns (Requirement 7.4)
    assert!(
        content.contains("| metric |"),
        "markdown should contain table header"
    );
    assert!(
        content.contains("baseline"),
        "markdown should contain baseline column"
    );
    assert!(
        content.contains("current"),
        "markdown should contain current column"
    );
    assert!(
        content.contains("delta"),
        "markdown should contain delta column"
    );
    assert!(
        content.contains("budget"),
        "markdown should contain budget column"
    );
    assert!(
        content.contains("status"),
        "markdown should contain status column"
    );

    // Verify metric row
    assert!(
        content.contains("wall_ms"),
        "markdown should contain wall_ms metric"
    );
}

/// Test markdown command with missing compare file
/// Should exit with error code 1
///
/// **Validates: Requirements 7.1**
#[test]
fn test_md_missing_compare_file() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let nonexistent_path = temp_dir.path().join("nonexistent.json");

    let mut cmd = perfgate_cmd();
    cmd.arg("md").arg("--compare").arg(&nonexistent_path);

    cmd.assert()
        .failure()
        .stderr(predicate::str::contains("read"));
}

/// Test markdown command without required --compare argument
/// Should fail with missing argument error
///
/// **Validates: Requirements 7.1**
#[test]
fn test_md_missing_compare_argument() {
    let mut cmd = perfgate_cmd();
    cmd.arg("md");

    cmd.assert().failure().stderr(predicate::str::contains(
        "Either --compare or --tradeoff is required",
    ));
}

#[test]
fn test_md_tradeoff_receipt_stdout() {
    let mut cmd = perfgate_cmd();
    cmd.arg("md").arg("--tradeoff").arg(tradeoff_fixture_path());

    cmd.assert()
        .success()
        .stdout(predicate::str::contains("perfgate tradeoff"))
        .stdout(predicate::str::contains("large_file_parse"))
        .stdout(predicate::str::contains("accepted"))
        .stdout(predicate::str::contains(
            "tokenizer-slower-if-parser-faster",
        ))
        .stdout(predicate::str::contains("Summary"))
        .stdout(predicate::str::contains("Weighted Workload"))
        .stdout(predicate::str::contains("Probe Evidence"))
        .stdout(predicate::str::contains("Accepted / Rejected Tradeoffs"))
        .stdout(predicate::str::contains("Local Reproduction"));
}

/// Test markdown output contains verdict reasons when present
/// Uses fail scenario which should have reasons
///
/// **Validates: Requirements 7.5**
#[test]
fn test_md_contains_verdict_reasons() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let compare_receipt_path = temp_dir.path().join("compare.json");

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

    // Generate a compare receipt with fail verdict (should have reasons)
    generate_compare_receipt(&baseline, &current, &compare_receipt_path)
        .expect("failed to generate compare receipt");

    assert!(
        compare_receipt_path.exists(),
        "compare receipt should exist"
    );

    // Read the compare receipt to check if it has reasons
    let content =
        fs::read_to_string(&compare_receipt_path).expect("failed to read compare receipt");
    let receipt: serde_json::Value =
        serde_json::from_str(&content).expect("compare receipt should be valid JSON");

    // Run perfgate md command
    let mut cmd = perfgate_cmd();
    cmd.arg("md").arg("--compare").arg(&compare_receipt_path);

    let output = cmd.assert().success();

    // If the receipt has reasons, verify they appear in the markdown
    if receipt["verdict"]["reasons"]
        .as_array()
        .is_some_and(|reasons| !reasons.is_empty())
    {
        output.stdout(predicate::str::contains("Notes:"));
    }
}

/// Test markdown template rendering with Handlebars.
#[test]
fn test_md_template_renders_custom_output() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let compare_receipt_path = temp_dir.path().join("compare.json");
    let template_path = temp_dir.path().join("comment.hbs");

    let baseline = fixtures_dir().join("baseline.json");
    let current = fixtures_dir().join("current_pass.json");
    generate_compare_receipt(&baseline, &current, &compare_receipt_path)
        .expect("failed to generate compare receipt");

    fs::write(
        &template_path,
        r#"{{header}}
bench={{bench.name}}
{{#each rows}}
- {{metric}} {{status}} {{delta_pct}}
{{/each}}
"#,
    )
    .expect("write template");

    let mut cmd = perfgate_cmd();
    cmd.arg("md")
        .arg("--compare")
        .arg(&compare_receipt_path)
        .arg("--template")
        .arg(&template_path);

    cmd.assert()
        .success()
        .stdout(predicate::str::contains("bench=test-benchmark"))
        .stdout(predicate::str::contains("- wall_ms"));
}

/// Test markdown table contains all expected metrics
/// Verifies both wall_ms and max_rss_kb are present when available
///
/// **Validates: Requirements 7.4**
#[test]
fn test_md_contains_all_metrics() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let compare_receipt_path = temp_dir.path().join("compare.json");

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

    // Generate a compare receipt
    generate_compare_receipt(&baseline, &current, &compare_receipt_path)
        .expect("failed to generate compare receipt");

    // Run perfgate md command
    let mut cmd = perfgate_cmd();
    cmd.arg("md").arg("--compare").arg(&compare_receipt_path);

    cmd.assert()
        .success()
        // Verify wall_ms metric is present
        .stdout(predicate::str::contains("wall_ms"))
        // Verify max_rss_kb metric is present (fixtures have this metric)
        .stdout(predicate::str::contains("max_rss_kb"));
}

/// Test markdown stdout is deterministic for the same compare receipt.
#[test]
fn test_md_stdout_deterministic() {
    let temp_dir = tempdir().expect("failed to create temp dir");
    let compare_receipt_path = temp_dir.path().join("compare.json");

    let baseline = fixtures_dir().join("baseline.json");
    let current = fixtures_dir().join("current_warn.json");
    generate_compare_receipt(&baseline, &current, &compare_receipt_path)
        .expect("failed to generate compare receipt");

    let output1 = perfgate_cmd()
        .arg("md")
        .arg("--compare")
        .arg(&compare_receipt_path)
        .output()
        .expect("first md run");
    assert!(output1.status.success(), "first md run should succeed");

    let output2 = perfgate_cmd()
        .arg("md")
        .arg("--compare")
        .arg(&compare_receipt_path)
        .output()
        .expect("second md run");
    assert!(output2.status.success(), "second md run should succeed");

    assert_eq!(
        output1.stdout, output2.stdout,
        "markdown stdout should be byte-for-byte deterministic"
    );
}