agent-image-diff 0.2.4

Structured image diff with JSON output for agent workflows
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
#![allow(deprecated)]

use assert_cmd::Command;
use predicates::prelude::*;
use serde_json::Value;

fn cmd() -> Command {
    Command::cargo_bin("agent-image-diff").unwrap()
}

#[test]
fn identical_images_match() {
    let output = cmd()
        .args([
            "tests/fixtures/baseline.png",
            "tests/fixtures/identical.png",
            "-v",
        ])
        .output()
        .unwrap();

    assert!(output.status.success());
    let json: Value = serde_json::from_slice(&output.stdout).unwrap();
    assert_eq!(json["match"], true);
    assert_eq!(json["regions"].as_array().unwrap().len(), 0);
    assert_eq!(json["stats"]["changed_pixels"], 0);
}

#[test]
fn different_images_exit_0() {
    cmd()
        .args([
            "tests/fixtures/baseline.png",
            "tests/fixtures/changed_small.png",
        ])
        .assert()
        .code(0);
}

#[test]
fn small_change_detected_with_label() {
    let output = cmd()
        .args([
            "tests/fixtures/baseline.png",
            "tests/fixtures/changed_small.png",
            "--dilate",
            "0",
            "--merge-distance",
            "0",
            "-v",
        ])
        .output()
        .unwrap();

    let json: Value = serde_json::from_slice(&output.stdout).unwrap();
    assert_eq!(json["match"], false);
    assert_eq!(json["stats"]["region_count"], 1);
    assert_eq!(json["stats"]["changed_pixels"], 100);

    let region = &json["regions"][0];
    assert_eq!(region["bounding_box"]["x"], 20);
    assert_eq!(region["bounding_box"]["y"], 30);
    assert_eq!(region["bounding_box"]["width"], 10);
    assert_eq!(region["bounding_box"]["height"], 10);
    assert_eq!(region["pixel_count"], 100);
    // Region should have a label
    assert!(region["label"].is_string());
}

#[test]
fn two_regions_merge_with_default_settings() {
    // With default merge-distance=50, both regions on the
    // 100x100 image should merge into one since they're within 50px
    let output = cmd()
        .args([
            "tests/fixtures/baseline.png",
            "tests/fixtures/changed_large.png",
            "-v",
        ])
        .output()
        .unwrap();

    let json: Value = serde_json::from_slice(&output.stdout).unwrap();
    assert_eq!(json["stats"]["changed_pixels"], 550);
    // With merge-distance=50, the two regions (at (5,5) and (60,70))
    // have a gap of ~45px so they merge into one
    assert_eq!(json["stats"]["region_count"], 1);
}

#[test]
fn two_regions_stay_separate_without_merge() {
    let output = cmd()
        .args([
            "tests/fixtures/baseline.png",
            "tests/fixtures/changed_large.png",
            "--dilate",
            "0",
            "--merge-distance",
            "0",
            "-v",
        ])
        .output()
        .unwrap();

    let json: Value = serde_json::from_slice(&output.stdout).unwrap();
    assert_eq!(json["stats"]["region_count"], 2);
    assert_eq!(json["stats"]["changed_pixels"], 550);

    let regions = json["regions"].as_array().unwrap();
    assert_eq!(regions[0]["pixel_count"], 400);
    assert_eq!(regions[1]["pixel_count"], 150);
}

#[test]
fn dimension_mismatch_reported() {
    let output = cmd()
        .args([
            "tests/fixtures/baseline.png",
            "tests/fixtures/different_size.png",
        ])
        .output()
        .unwrap();

    let json: Value = serde_json::from_slice(&output.stdout).unwrap();
    assert_eq!(json["match"], false);
    assert!(json["dimension_mismatch"].is_object());
    assert_eq!(json["dimension_mismatch"]["baseline"]["width"], 100);
    assert_eq!(json["dimension_mismatch"]["candidate"]["width"], 120);
}

#[test]
fn summary_format_shows_labels() {
    cmd()
        .args([
            "tests/fixtures/baseline.png",
            "tests/fixtures/changed_small.png",
            "-f",
            "summary",
        ])
        .assert()
        .stdout(predicate::str::contains("Images differ"))
        .stdout(predicate::str::contains("Region #1"));
}

#[test]
fn output_flag_creates_diff_image() {
    let dir = tempfile::tempdir().unwrap();
    let diff_path = dir.path().join("diff.png");

    cmd()
        .args([
            "tests/fixtures/baseline.png",
            "tests/fixtures/changed_small.png",
            "-o",
            diff_path.to_str().unwrap(),
        ])
        .assert()
        .code(0);

    assert!(diff_path.exists());
}

#[test]
fn threshold_zero_is_exact_match() {
    let output = cmd()
        .args([
            "tests/fixtures/baseline.png",
            "tests/fixtures/identical.png",
            "-t",
            "0.0",
        ])
        .output()
        .unwrap();

    let json: Value = serde_json::from_slice(&output.stdout).unwrap();
    assert_eq!(json["match"], true);
}

#[test]
fn missing_file_gives_error() {
    cmd()
        .args(["nonexistent.png", "tests/fixtures/baseline.png"])
        .assert()
        .failure()
        .stderr(predicate::str::contains("Failed to open baseline image"));
}

#[test]
fn dilate_zero_and_merge_zero_matches_v1_behavior() {
    // With both disabled, should get strict pixel-level regions
    let output = cmd()
        .args([
            "tests/fixtures/baseline.png",
            "tests/fixtures/changed_large.png",
            "--dilate",
            "0",
            "--merge-distance",
            "0",
            "-v",
        ])
        .output()
        .unwrap();

    let json: Value = serde_json::from_slice(&output.stdout).unwrap();
    // Two separate regions at exact pixel boundaries
    assert_eq!(json["stats"]["region_count"], 2);
    let r0 = &json["regions"][0];
    assert_eq!(r0["bounding_box"]["x"], 5);
    assert_eq!(r0["bounding_box"]["y"], 5);
    assert_eq!(r0["bounding_box"]["width"], 20);
    assert_eq!(r0["bounding_box"]["height"], 20);
}

#[test]
fn regions_have_label_field() {
    let output = cmd()
        .args([
            "tests/fixtures/baseline.png",
            "tests/fixtures/changed_small.png",
        ])
        .output()
        .unwrap();

    let json: Value = serde_json::from_slice(&output.stdout).unwrap();
    let regions = json["regions"].as_array().unwrap();
    for region in regions {
        let label = region["label"].as_str().unwrap();
        assert!(
            ["added", "removed", "color-change", "content-change"].contains(&label),
            "unexpected label: {label}"
        );
    }
}

// --- Tests for compact/verbose/quiet modes ---

#[test]
fn default_json_is_compact() {
    let output = cmd()
        .args([
            "tests/fixtures/baseline.png",
            "tests/fixtures/changed_small.png",
        ])
        .output()
        .unwrap();

    let stdout = String::from_utf8(output.stdout).unwrap();
    // Should be single line (no newlines except trailing)
    assert_eq!(stdout.trim().lines().count(), 1);
    // Should NOT contain verbose-only fields
    assert!(!stdout.contains("\"dimensions\""));
    assert!(!stdout.contains("\"stats\""));
    assert!(!stdout.contains("\"pixel_count\""));
    assert!(!stdout.contains("\"avg_delta\""));
    assert!(!stdout.contains("\"max_delta\""));
    // Should contain required fields
    assert!(stdout.contains("\"match\""));
    assert!(stdout.contains("\"diff_percentage\""));
    assert!(stdout.contains("\"regions\""));
    assert!(stdout.contains("\"bounding_box\""));
    assert!(stdout.contains("\"label\""));
}

#[test]
fn verbose_json_has_all_fields() {
    let output = cmd()
        .args([
            "tests/fixtures/baseline.png",
            "tests/fixtures/changed_small.png",
            "-v",
        ])
        .output()
        .unwrap();

    let stdout = String::from_utf8(output.stdout).unwrap();
    assert!(stdout.contains("\"dimensions\""));
    assert!(stdout.contains("\"stats\""));
    assert!(stdout.contains("\"pixel_count\""));
    assert!(stdout.contains("\"avg_delta\""));
    assert!(stdout.contains("\"max_delta\""));
}

#[test]
fn pretty_flag_adds_whitespace() {
    let output = cmd()
        .args([
            "tests/fixtures/baseline.png",
            "tests/fixtures/changed_small.png",
            "--pretty",
        ])
        .output()
        .unwrap();

    let stdout = String::from_utf8(output.stdout).unwrap();
    // Pretty-printed should have multiple lines
    assert!(stdout.trim().lines().count() > 1);
}

#[test]
fn quiet_mode_no_stdout() {
    let output = cmd()
        .args([
            "tests/fixtures/baseline.png",
            "tests/fixtures/changed_small.png",
            "-q",
        ])
        .output()
        .unwrap();

    assert!(output.status.success());
    assert!(output.stdout.is_empty());
}

#[test]
fn quiet_mode_still_writes_diff_image() {
    let dir = tempfile::tempdir().unwrap();
    let diff_path = dir.path().join("diff.png");

    let output = cmd()
        .args([
            "tests/fixtures/baseline.png",
            "tests/fixtures/changed_small.png",
            "-q",
            "-o",
            diff_path.to_str().unwrap(),
        ])
        .output()
        .unwrap();

    assert!(output.stdout.is_empty());
    assert!(diff_path.exists());
}

#[test]
fn diff_percentage_is_rounded() {
    let output = cmd()
        .args([
            "tests/fixtures/baseline.png",
            "tests/fixtures/changed_small.png",
        ])
        .output()
        .unwrap();

    let json: Value = serde_json::from_slice(&output.stdout).unwrap();
    let pct = json["diff_percentage"].as_f64().unwrap();
    // Should be rounded to 1 decimal place (1.0 for 100 changed out of 10000)
    assert_eq!(pct, 1.0);
}

// --- Tests for --crop mode ---

#[test]
fn crop_extracts_correct_region() {
    let dir = tempfile::tempdir().unwrap();
    let out_path = dir.path().join("cropped.png");

    cmd()
        .args([
            "tests/fixtures/baseline.png",
            "--crop",
            "--x",
            "5",
            "--y",
            "5",
            "--crop-width",
            "20",
            "--crop-height",
            "20",
            "-o",
            out_path.to_str().unwrap(),
        ])
        .assert()
        .code(0);

    assert!(out_path.exists());
    let img = image::open(&out_path).unwrap();
    assert_eq!(img.width(), 20);
    assert_eq!(img.height(), 20);
}

#[test]
fn crop_out_of_bounds_errors() {
    let dir = tempfile::tempdir().unwrap();
    let out_path = dir.path().join("cropped.png");

    cmd()
        .args([
            "tests/fixtures/baseline.png",
            "--crop",
            "--x",
            "90",
            "--y",
            "90",
            "--crop-width",
            "20",
            "--crop-height",
            "20",
            "-o",
            out_path.to_str().unwrap(),
        ])
        .assert()
        .failure()
        .stderr(predicate::str::contains("exceeds"));
}

#[test]
fn diff_without_candidate_errors() {
    cmd()
        .args(["tests/fixtures/baseline.png"])
        .assert()
        .failure()
        .stderr(predicate::str::contains("candidate"));
}