aboutsummaryrefslogtreecommitdiffstats
path: root/test/scripts/compare.py
blob: d6dc85852f1bd506a36fb9f8830ea2af6c206a3b (plain) (blame)
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
#!/usr/bin/env python3
"""Compare captured mitmproxy requests between gemini-cli and gemini-py."""

import json
import sys
from pathlib import Path

CAPTURES_DIR = sys.argv[1] if len(sys.argv) > 1 else "./test/captures"

IGNORE_HEADERS = {
    "authorization",
    "content-length",
    "host",
    "connection",
    "proxy-connection",
    "proxy-authorization",
    "transfer-encoding",
    "accept-encoding",
    "x-capture-source",
    "user-agent",
}

# Session-specific fields inside request body
VOLATILE_REQUEST_KEYS = {"session_id", "contents"}


def load_captures(directory):
    caps = []
    for f in sorted(Path(directory).glob("req_*.json")):
        with open(f) as fh:
            d = json.load(fh)
        # Only compare the main content generation calls
        url = d.get("url", "")
        u = url.lower()
        if "generatecontent" in u and "loadcodeassist" not in u and "counttokens" not in u:
            caps.append(d)
    return caps


def normalize_headers(headers):
    return {k.lower(): v for k, v in headers.items() if k.lower() not in IGNORE_HEADERS}


def normalize_body(body):
    if not isinstance(body, dict):
        return body
    out = {}
    for k, v in body.items():
        if k == "user_prompt_id":
            out[k] = "<redacted>"
        elif k == "request" and isinstance(v, dict):
            out[k] = {
                rk: ("<redacted>" if rk in VOLATILE_REQUEST_KEYS else rv) for rk, rv in v.items()
            }
        else:
            out[k] = v
    return out


def fmt(v):
    if isinstance(v, (dict, list)):
        return json.dumps(v, indent=2, default=str)
    return str(v)


def compare_dicts(label, d1, d2, n1, n2):
    keys = sorted(set(list(d1.keys()) + list(d2.keys())))
    diffs = 0
    for key in keys:
        v1 = d1.get(key, "<missing>")
        v2 = d2.get(key, "<missing>")
        if v1 == v2:
            print(f"    \033[32m✓\033[0m {key}")
        else:
            diffs += 1
            print(f"    \033[31m✗\033[0m {key}")
            print(f"        {n1}: {fmt(v1)}")
            print(f"        {n2}: {fmt(v2)}")
    if diffs == 0:
        print(f"  \033[32m{label}: IDENTICAL\033[0m")
    else:
        print(f"  \033[31m{label}: {diffs} difference(s)\033[0m")
    return diffs


def dump_request(cap):
    src = cap.get("source", "?")
    print(f"  Source: {src}")
    print(f"  URL:    {cap['url']}")
    headers = normalize_headers(cap.get("headers", {}))
    body = cap.get("body", {})
    print(f"  Headers ({len(headers)}):")
    for k in sorted(headers):
        print(f"    {k}: {headers[k]}")
    if isinstance(body, dict):
        print(f"  Body keys: {sorted(body.keys())}")
        print(f"    model:   {body.get('model', 'N/A')}")
        print(f"    project: {body.get('project', 'N/A')}")
        req = body.get("request", {})
        if isinstance(req, dict):
            print(f"    generationConfig: {req.get('generationConfig', 'N/A')}")


def main():
    caps = load_captures(CAPTURES_DIR)
    if not caps:
        print(f"No generateContent captures found in {CAPTURES_DIR}")
        print("Run gemini-cli and gemini-py through the proxy first.")
        sys.exit(1)

    by_source = {}
    for c in caps:
        src = c.get("source", "unknown")
        by_source.setdefault(src, []).append(c)

    sources = {k: len(v) for k, v in by_source.items()}
    print(f"Found {len(caps)} generateContent request(s)")
    print(f"Sources: {sources}\n")

    for i, cap in enumerate(caps):
        print(f"--- Request #{cap.get('capture_id', i + 1)} ---")
        dump_request(cap)
        print()

    sources = list(by_source.keys())
    if len(sources) == 2 and all(len(v) >= 1 for v in by_source.values()):
        n1, n2 = sources

        def pick(caps):
            # Prefer streamGenerateContent (actual response) over routing calls
            streaming = [c for c in caps if "streamgeneratecontent" in c.get("url", "").lower()]
            return streaming[-1] if streaming else caps[-1]

        c1, c2 = pick(by_source[n1]), pick(by_source[n2])

        print("=" * 60)
        print(f"COMPARING: {n1} vs {n2}")
        print("=" * 60)

        h1 = normalize_headers(c1.get("headers", {}))
        h2 = normalize_headers(c2.get("headers", {}))
        total = compare_dicts("Headers", h1, h2, n1, n2)

        b1 = normalize_body(c1.get("body", {}))
        b2 = normalize_body(c2.get("body", {}))
        total += compare_dicts("Body", b1, b2, n1, n2)

        print(f"\n{'=' * 60}")
        if total == 0:
            print("\033[32mRESULT: Requests are identical on the wire.\033[0m")
        else:
            print(f"\033[31mRESULT: {total} total difference(s).\033[0m")
        sys.exit(0 if total == 0 else 1)
    else:
        print("Tip: Clear test/captures/ and run one request from each client to compare.")


if __name__ == "__main__":
    main()