import json
from pathlib import Path

root = Path(__file__).parent
read = lambda name: json.loads((root / f"{name}.json").read_text())
b, c, old_b, old_c = [read(name) for name in ("green-B", "green-C", "baseline-B", "baseline-C")]
assert len(b["routes"]) == len(c["routes"]) == 24
assert b["server_version"].startswith("5.6.")
assert c["dm_statement_clock_probe"] == {"samples": 20, "native_equal_samples": 20, "seconds_equal_samples": 20}
assert old_b["source_update_time_advanced"] == {"area": True, "shop": True}
assert old_c["source_update_time_advanced"] == {"area": False, "shop": False}
assert old_c["timestamp_wire_differences"] == {
    "create_time": {"type": "string", "length": 26, "fraction_digits": 6},
    "update_time": {"type": "string", "length": 26, "fraction_digits": 6},
}
for result in (b, c):
    assert result["compatibility_pass"] and result["execution_failure"] == ""
    assert result["cleanup_zero"] and result["own_after"] == {"areas": 0, "shops": 0}
    assert result["cleanup_delete_rows"] == 2
    assert result["fixture_timestamp_updates"] == 10
    assert result["natural_unchanged"]
    assert result["non_target_counts_before"] == result["non_target_counts_after"]
    assert result["source_update_time_advanced"] == {"area": True, "shop": True}
    assert result["source_noop_time_preserved"] == {"area": True, "shop": True}
    assert all(result["source_status_timestamp_semantics"].values())
    assert result["boundary"]["business_constant"] == "BUSINESS_VALUE"
    assert result["boundary"]["business"] == "" and not result["boundary"]["school_feature"]
    assert not result.get("timestamp_wire_differences")
    assert all(n == 0 for n in result["final_own_states"]["external_reference_counts"].values())
    for kind in ("area", "shop"):
        first = result["source_created_times"][kind]
        assert first["same_raw_value"] and first["create_no_nonzero_fraction"] and first["update_no_nonzero_fraction"]
    for table in result["schema"].values():
        for col in table:
            if col["column_name"].lower() in ("create_time", "update_time"):
                assert int(col["datetime_precision"]) == (0 if result["lane"] == "B" else 6)
for name, left in b["routes"].items():
    right, original = c["routes"][name], old_b["routes"][name]
    for field in ("action", "status", "code", "expected_code", "wire_shape", "message_sha256"):
        assert left[field] == right[field], (name, field)
        assert left[field] == original[field], (name, field, "MySQL regression")
    assert left.get("normalized_own_response") == right.get("normalized_own_response"), name
    assert left.get("normalized_own_response") == original.get("normalized_own_response"), name
    if left["expected_code"] == 0:
        assert left["source_roundtrip"] and right["source_roundtrip"]
for lane in ("B", "C"):
    before, after = read(f"reads-baseline-{lane}"), read(f"reads-green-{lane}")
    assert before["natural_unchanged"] and after["natural_unchanged"]
    assert before["database_writes"] == after["database_writes"] == 0
    for action, result in after["routes"].items():
        assert result["status"] == 200 and result["code"] == 0
        if lane == "B":
            assert result["wire_sha256"] == before["routes"][action]["wire_sha256"], action
read_b, read_c = read("reads-green-B"), read("reads-green-C")
for action in read_b["routes"]:
    assert read_b["routes"][action]["wire_shape"] == read_c["routes"][action]["wire_shape"]
print(json.dumps({"primary_write_method_keys": 6, "http_scenarios_each_engine": 24,
                  "existing_read_regressions_each_engine": 4,
                  "mysql_raw_read_wire_bodies_unchanged": 4,
                  "mysql_normalized_own_write_responses_unchanged": 24,
                  "wire_and_normalized_own_value_differences": 0,
                  "on_update_changed_and_noop_parity": True,
                  "created_two_times_same_value_and_no_nonzero_fraction": True,
                  "own_rows_after_cleanup": 0,
                  "natural_rows_and_order_device_counts_unchanged": True,
                  "real_customer_business_or_school_branch_proven": False}))
