#!/usr/bin/env python3
"""json_array_guard — check that a JSON array-of-objects file loads with the
same columns and rows regardless of whitespace padding after "[" and of the
reader's chunksize. Earned on datasets #9122 (HF-815): on shipped datasets
5.2.0, 120 spaces of padding or chunksize=1 wraps every record in an extra
"text" field, in both normal and streaming mode, while a string array must
keep its "text" column.

Usage: python json_array_guard.py [path.json]   (default: generates its own)
Exit 0 when every cell agrees; 1 when any cell diverges.
"""
import json, sys
from pathlib import Path
from tempfile import TemporaryDirectory
from datasets import load_dataset

def cells(path):
    out = {}
    for streaming in (False, True):
        ds = load_dataset("json", data_files=str(path), split="train", streaming=streaming)
        out[f"stream={streaming}"] = list(ds)
    for cs in (1, 10 << 20):
        ds = load_dataset("json", data_files=str(path), split="train", chunksize=cs)
        out[f"chunksize={cs}"] = list(ds)
    return out

def main():
    records = [{"id": 1, "text": "hello"}, {"id": 2, "text": "world"}]
    bad = 0
    with TemporaryDirectory() as folder:
        if len(sys.argv) > 1:
            paths = [Path(sys.argv[1])]
            expected = json.loads(paths[0].read_text())
        else:
            paths = []
            for padding in (0, 120):
                p = Path(folder) / f"records-{padding}.json"
                p.write_text("[" + " " * padding + json.dumps(records)[1:], encoding="utf-8")
                paths.append(p)
            expected = records
        for p in paths:
            for name, rows in cells(p).items():
                ok = rows == expected
                bad += not ok
                print(f"{p.name} {name}: {'AGREE' if ok else 'DIVERGE -> ' + str(rows)}")
    print("VERDICT:", "divergence found" if bad else "all cells agree")
    raise SystemExit(1 if bad else 0)

if __name__ == "__main__":
    main()
