"""Document model, operations, HTML profile and colour parsing: no pack needed.""" from __future__ import annotations import pytest from designgym.core import ops from designgym.core.colors import delta_e, parse_color from designgym.core.doc import DesignDoc, Element from designgym.core.fonts import FontRegistry from designgym.core.html_profile import HTMLProfileError, doc_to_html, html_to_doc @pytest.fixture def ctx(): return ops.OpContext(FontRegistry(None), {"a1": {"aspect": 2.0, "kind": "photo"}, "a2": {"aspect": 0.5, "kind": "graphic"}}) @pytest.fixture def doc(): return DesignDoc(width=1000, height=800) def test_colors(): assert parse_color("rgba(255, 102, 51, 1)") == "#ff6633" assert parse_color("rgba(0,0,0,0.5)") == "#00000080" assert parse_color("#FFF") == "#ffffff" assert parse_color("white") == "#ffffff" assert parse_color("#11223344") == "#11223344" assert parse_color("#112233ff") == "#112233" with pytest.raises(ValueError): parse_color("notacolor") assert delta_e("#000000", "#000000") == 0 assert delta_e("#000000", "#ffffff") > 99 def test_ops_do_not_mutate_input(doc, ctx): new, res = ops.apply(doc, "add_image", {"asset_id": "a1", "x": 10, "y": 20, "width": 200}, ctx) assert doc.elements == [] el = new.find(res["element_id"]) assert (el.width, el.height) == (200, 100) # aspect kept def test_add_image_defaults_and_errors(doc, ctx): new, res = ops.apply(doc, "add_image", {"asset_id": "a2"}, ctx) el = new.find(res["element_id"]) assert el.width / el.height == pytest.approx(0.5) assert el.x == pytest.approx((1000 - el.width) / 2) with pytest.raises(ops.OpError, match="unknown asset"): ops.apply(doc, "add_image", {"asset_id": "zz"}, ctx) with pytest.raises(ops.OpError, match="width"): ops.apply(doc, "add_image", {"asset_id": "a1", "width": -5}, ctx) def test_text_height_follows_lines(doc, ctx): new, res = ops.apply(doc, "add_text", {"text": "a\nb\nc", "font_size": 40, "line_height": 1.5}, ctx) el = new.find(res["element_id"]) assert el.height == pytest.approx(3 * 40 * 1.5) assert el.auto_width new2, _ = ops.apply(new, "update_element", {"element_id": el.id, "text": "one line"}, ctx) assert new2.find(el.id).height == pytest.approx(60) def test_update_rejects_wrong_props(doc, ctx): new, res = ops.apply(doc, "add_image", {"asset_id": "a1"}, ctx) with pytest.raises(ops.OpError, match="image elements have no font_size"): ops.apply(new, "update_element", {"element_id": res["element_id"], "font_size": 3}, ctx) with pytest.raises(ops.OpError, match="no element"): ops.apply(new, "update_element", {"element_id": "e99", "x": 3}, ctx) def test_reorder_align_duplicate_delete(doc, ctx): d = doc ids = [] for x in (0, 100, 300): d, r = ops.apply(d, "add_shape", {"x": x, "y": 0, "width": 50, "height": 50}, ctx) ids.append(r["element_id"]) d, _ = ops.apply(d, "reorder_element", {"element_id": ids[0], "to": "front"}, ctx) assert [e.id for e in d.elements] == [ids[1], ids[2], ids[0]] d, _ = ops.apply(d, "reorder_element", {"element_id": ids[0], "to": "0"}, ctx) assert d.elements[0].id == ids[0] d, _ = ops.apply(d, "align_elements", {"element_ids": [ids[1]], "alignment": "center"}, ctx) assert d.find(ids[1]).x == pytest.approx(475) d, _ = ops.apply(d, "align_elements", {"element_ids": ids, "alignment": "top", "relative_to": "selection"}, ctx) d, r = ops.apply(d, "duplicate_element", {"element_id": ids[2]}, ctx) assert d.index_of(r["element_id"]) == d.index_of(ids[2]) + 1 d, _ = ops.apply(d, "delete_element", {"element_id": ids[2]}, ctx) assert len(d.elements) == 3 def test_rotated_align_uses_bounds(doc, ctx): d, r = ops.apply(doc, "add_shape", {"x": 100, "y": 100, "width": 200, "height": 100, "angle": 90}, ctx) d, _ = ops.apply(d, "align_elements", {"element_ids": [r["element_id"]], "alignment": "left"}, ctx) el = d.find(r["element_id"]) # A 200x100 box rotated 90 degrees is 100 wide; its visual left edge is at x+50. assert el.x + 50 == pytest.approx(0) def test_font_validation(doc): reg = FontRegistry(None) ctx = ops.OpContext(reg, {}) # Without a font pack any family is accepted (tests run anywhere). d, r = ops.apply(doc, "add_text", {"text": "x", "font_family": "Whatever"}, ctx) assert d.find(r["element_id"]).font_family == "Whatever" def test_runs_join_to_text(doc, ctx): d, r = ops.apply(doc, "add_text", {"text": "ignored", "runs": [ {"text": "BUY "}, {"text": "NOW", "color": "#ff0000", "bold": True}]}, ctx) el = d.find(r["element_id"]) assert el.text == "BUY NOW" assert el.styled_runs()[1].color == "#ff0000" d2, _ = ops.apply(d, "update_element", {"element_id": el.id, "text": "plain"}, ctx) assert d2.find(el.id).runs is None def test_html_round_trip(doc, ctx): d = doc d, _ = ops.apply(d, "set_background", {"color": "#f4efe6"}, ctx) d, _ = ops.apply(d, "add_image", {"asset_id": "a1", "x": 0, "y": 0, "width": 1000, "angle": 5}, ctx) d, _ = ops.apply(d, "add_shape", {"shape": "ellipse", "x": 10, "y": 10, "width": 80, "height": 80, "fill": "#e63946", "stroke": "#000000", "stroke_width": 3}, ctx) d, _ = ops.apply(d, "add_text", {"text": " Summer\nSale ", "x": 50, "y": 600, "width": 900, "font_size": 72, "color": "#1d1d1f", "align": "center", "letter_spacing": 2, "uppercase": True, "line_height": 1.2, "runs": [{"text": " Summer\n"}, {"text": "Sale ", "color": "#ff0000"}]}, ctx) html = doc_to_html(d) parsed, warnings = html_to_doc(html, 1000, 800) assert warnings == [] back, _ = ops.apply(DesignDoc(width=1000, height=800), "replace_document", {"document": parsed}, ctx) assert back.public() == d.public() def test_html_hand_written(ctx): page = """

Caption