# retoor import json from devplacepy.services.nodes.shape import INCONCLUSIVE, MATCH, MISMATCH, compare, shape_of JSON = {"content-type": "application/json; charset=utf-8"} HTML = {"content-type": "text/html"} def listing(children): return json.dumps({"kind": "Listing", "data": {"after": None, "children": children}}).encode() def test_two_listings_with_different_content_match(): first = shape_of(200, JSON, listing([{"id": 1}])) second = shape_of(200, JSON, listing([{"id": 2}, {"id": 3}])) assert compare(first, second) == MATCH def test_a_forged_json_shape_mismatches(): first = shape_of(200, JSON, listing([])) forged = shape_of(200, JSON, json.dumps({"message": "buy now"}).encode()) assert compare(first, forged) == MISMATCH def test_html_titles_decide(): page = b" Python : r/python " + b"x" * 600 + b"" same = b"Python : r/python" + b"y" * 700 + b"" other = b"Totally different" + b"x" * 600 + b"" assert compare(shape_of(200, HTML, page), shape_of(200, HTML, same)) == MATCH assert compare(shape_of(200, HTML, page), shape_of(200, HTML, other)) == MISMATCH def test_html_without_a_title_falls_back_to_the_first_heading(): first = shape_of(200, HTML, b"

Hello world

") second = shape_of(200, HTML, b"

Hello world

") assert first.signature == "Hello world" and compare(first, second) == MATCH def test_a_kind_change_mismatches_even_with_the_same_status(): assert compare(shape_of(200, JSON, listing([])), shape_of(200, HTML, b"x")) == MISMATCH def test_blocked_or_error_responses_are_never_conclusive(): ok = shape_of(200, JSON, listing([])) for status in (403, 429, 500, 404): assert compare(ok, shape_of(status, HTML, b"blocked")) == INCONCLUSIVE assert compare(shape_of(status, HTML, b""), ok) == INCONCLUSIVE def test_a_wild_size_difference_mismatches_only_for_real_bodies(): big = shape_of(200, HTML, b"t" + b"a" * 5000) small = shape_of(200, HTML, b"t" + b"a" * 600) tiny = shape_of(200, HTML, b"t") assert compare(big, small) == MISMATCH assert compare(big, tiny) == MATCH def test_invalid_json_is_its_own_kind(): broken = shape_of(200, JSON, b"{not json") assert broken.kind == "json-invalid" assert compare(broken, shape_of(200, JSON, listing([]))) == MISMATCH def test_nested_key_sets_are_bounded_and_unknown_types_compare_by_type(): deep = {"a": {"b": {"c": {"d": 1}}}} assert "a.b" in shape_of(200, JSON, json.dumps(deep).encode()).signature assert "a.b.c.d" not in shape_of(200, JSON, json.dumps(deep).encode()).signature assert compare(shape_of(200, {"content-type": "image/png"}, b"1"), shape_of(200, {"content-type": "image/png"}, b"2")) == MATCH