"""The page snapshot comes from the page's own JavaScript world: it is checked in Python before any use.""" import pytest from protocol import build_prompt from runner import MAX_ELEMENTS, MAX_LABEL, MAX_TEXT, SnapshotError, check_snapshot def snapshot(**changes): snap = {"title": "Abmelden", "text": "Bitte bestätigen", "tail": "Bitte bestätigen", "captcha": False, "elements": [{"n": 1, "tag": "input", "type": "email", "label": "E-Mail"}, {"n": 2, "tag": "select", "type": "", "label": "Grund", "options": ["a", "b"]}, {"n": 3, "tag": "input", "type": "checkbox", "label": "Alle", "checked": False}, {"n": 4, "tag": "button", "type": "submit", "label": "Abmelden"}]} snap.update(changes) return snap def element(**changes): e = {"n": 1, "tag": "button", "type": "", "label": "Abmelden"} e.update(changes) return e def test_a_well_formed_snapshot_passes_unchanged(): assert check_snapshot(snapshot()) == snapshot() assert check_snapshot(snapshot(elements=[]))["elements"] == [] @pytest.mark.parametrize("n", ["1", "1] x\n", 1.0, True, None]) def test_element_number_must_be_an_int(n): with pytest.raises(SnapshotError): check_snapshot(snapshot(elements=[element(n=n)])) @pytest.mark.parametrize("numbers", [[1, 3], [2, 1], [0, 1], [1, 1], [2]]) def test_element_numbers_must_be_1_to_k_in_order(numbers): with pytest.raises(SnapshotError): check_snapshot(snapshot(elements=[element(n=n) for n in numbers])) @pytest.mark.parametrize("extra", [{"html": ""}, {"role": "button"}, {"N": 1}]) def test_extra_element_keys_are_refused(extra): with pytest.raises(SnapshotError): check_snapshot(snapshot(elements=[element(**extra)])) @pytest.mark.parametrize("missing", ["n", "tag", "type", "label"]) def test_missing_element_keys_are_refused(missing): e = element() del e[missing] with pytest.raises(SnapshotError): check_snapshot(snapshot(elements=[e])) @pytest.mark.parametrize("changes", [{"label": 5}, {"label": None}, {"label": ["x"]}, {"tag": 1}, {"type": None}, {"options": "a"}, {"options": ["a", 1]}, {"options": ["a"] * 21}, {"checked": "yes"}, {"checked": 1}]) def test_element_values_of_the_wrong_type_are_refused(changes): with pytest.raises(SnapshotError): check_snapshot(snapshot(elements=[element(**changes)])) def test_too_many_elements_are_refused(): with pytest.raises(SnapshotError): check_snapshot(snapshot(elements=[element(n=i) for i in range(1, 501)])) assert len(check_snapshot(snapshot(elements=[element(n=i) for i in range(1, MAX_ELEMENTS + 1)]))["elements"]) \ == MAX_ELEMENTS with pytest.raises(SnapshotError): check_snapshot(snapshot(elements=[element(n=i) for i in range(1, MAX_ELEMENTS + 2)])) @pytest.mark.parametrize("snap", [None, [], "text", 1, {"title": "t"}]) def test_snapshot_must_be_a_dict_with_its_keys(snap): with pytest.raises(SnapshotError): check_snapshot(snap) @pytest.mark.parametrize("changes", [{"title": None}, {"text": 1}, {"tail": ["x"]}, {"captcha": "false"}, {"captcha": 0}, {"elements": None}, {"elements": {"n": 1}}, {"elements": ["[1] button"]}, {"extra": "x"}]) def test_snapshot_values_of_the_wrong_type_are_refused(changes): with pytest.raises(SnapshotError): check_snapshot(snapshot(**changes)) def test_overlong_strings_are_cut_to_the_limits_of_the_javascript(): snap = check_snapshot(snapshot(text="x" * (MAX_TEXT + 500), elements=[element(label="y" * 1000)])) assert len(snap["text"]) == MAX_TEXT and len(snap["elements"][0]["label"]) == MAX_LABEL def test_checked_snapshot_cannot_write_lines_into_the_prompt(): snap = check_snapshot(snapshot()) p = build_prompt(snap["title"], "https://x.example", snap["text"], snap["elements"], [], True) assert sum(line.startswith("[") for line in p.splitlines()) == 4