from src.core.canonical_command import (
    PUBLIC_COMMANDS,
    STUB_REQUEST_TYPES,
    canonical_json,
    command_hash,
)


def test_sorted_keys():
    assert canonical_json({"b": 1, "a": 2}) == '{"a":2,"b":1}'


def test_one_equals_one_point_zero():
    assert canonical_json(1.0) == "1"
    assert canonical_json(1) == "1"


def test_array_order_preserved():
    assert canonical_json(["z", "a"]) == '["z","a"]'


def test_where_tree_order():
    assert (
        canonical_json({"where": {"and": [{"k": "x"}, {"k": "y"}]}})
        == '{"where":{"and":[{"k":"x"},{"k":"y"}]}}'
    )


def test_hash_stable():
    a = command_hash({"command": "data.delete", "where": {"k": "v"}})
    b = command_hash({"where": {"k": "v"}, "command": "data.delete"})
    assert a == b
    assert len(a) == 64


def test_stub_and_public_catalogs():
    assert "custom.provider.request" not in STUB_REQUEST_TYPES
    assert "data.record.upsert" in PUBLIC_COMMANDS
    assert "content.page.create" in PUBLIC_COMMANDS
    assert "content.publish" in PUBLIC_COMMANDS
    assert "crm.contact.upsert" not in PUBLIC_COMMANDS
