File size: 2,533 Bytes
53b2ac9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# SPDX-License-Identifier: MIT
"""Tests for the json_diff plugin."""

import json
import os
import sys

import pytest

sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "plugins"))
from json_diff import run, _flatten


# ── _flatten helper ──

def test_flatten_simple():
    result = _flatten({"a": 1, "b": 2})
    assert result == {"a": 1, "b": 2}


def test_flatten_nested():
    result = _flatten({"a": {"b": {"c": 3}}})
    assert result == {"a.b.c": 3}


def test_flatten_list():
    result = _flatten({"items": [10, 20, 30]})
    assert result == {"items[0]": 10, "items[1]": 20, "items[2]": 30}


def test_flatten_mixed():
    result = _flatten({"a": 1, "b": {"c": [1, 2]}})
    assert result == {"a": 1, "b.c[0]": 1, "b.c[1]": 2}


# ── Inline JSON diffs ──

def test_identical():
    result = run(json_a='{"x": 1}', json_b='{"x": 1}')
    assert "identical" in result.lower() or "no differences" in result.lower()


def test_added_key():
    result = run(json_a='{"a": 1}', json_b='{"a": 1, "b": 2}')
    assert "ADDED" in result
    assert "b" in result


def test_removed_key():
    result = run(json_a='{"a": 1, "b": 2}', json_b='{"a": 1}')
    assert "REMOVED" in result
    assert "b" in result


def test_changed_value():
    result = run(json_a='{"a": 1}', json_b='{"a": 99}')
    assert "CHANGED" in result
    assert "1" in result
    assert "99" in result


def test_nested_diff():
    a = json.dumps({"config": {"debug": True, "port": 8080}})
    b = json.dumps({"config": {"debug": False, "port": 8080}})
    result = run(json_a=a, json_b=b)
    assert "CHANGED" in result
    assert "config.debug" in result


def test_summary_line():
    result = run(json_a='{"a": 1}', json_b='{"b": 2}')
    assert "Summary" in result
    assert "+1" in result
    assert "-1" in result


# ── File-based diffs ──

def test_file_diff(tmp_path):
    fa = tmp_path / "a.json"
    fb = tmp_path / "b.json"
    fa.write_text('{"name": "alice", "age": 30}')
    fb.write_text('{"name": "bob", "age": 30}')

    result = run(file_a=str(fa), file_b=str(fb))
    assert "CHANGED" in result
    assert "name" in result


def test_file_not_found():
    result = run(file_a="/tmp/nonexistent_trashclaw_a.json", json_b='{"a":1}')
    assert "Error" in result and "not found" in result


def test_invalid_json():
    result = run(json_a="not json", json_b='{"a":1}')
    assert "Error" in result and "invalid" in result.lower()


def test_no_input():
    result = run()
    assert "Error" in result