"""POST /v1/text.""" from __future__ import annotations from conftest import error_of POST = "We shipped postkitty 1.0 today. https://postkitty.mertkayacs.com #launch @hermes" def test_short_text_is_counted_with_each_platforms_rule(client): body = client.post("/v1/text", json={"text": POST}).json() assert list(body["platforms"]) == ["instagram", "tiktok", "youtube", "x", "linkedin", "threads", "bluesky", "facebook"] assert body["all_ok"] is True x, instagram = body["platforms"]["x"], body["platforms"]["instagram"] assert (x["unit"], x["limit"], x["ok"], x["split"], x["fold"]) == ("x_weighted", 280, True, False, None) assert x["count"] < len(POST) # X counts the link as 23 whatever its length assert (instagram["unit"], instagram["count"], instagram["hashtags"], instagram["mentions"], instagram["urls"]) == ( "codepoints", len(POST), 1, 1, 1) assert instagram["fold"] == {"limit": 125, "shown": len(POST), "text": POST} assert x["parts"] == [{"text": POST, "count": x["count"]}] assert set(x) == {"label", "ok", "enforcement", "unit", "limit", "count", "overflow", "split", "parts", "hashtags", "mentions", "urls", "fold", "notes"} def test_long_text_splits_into_a_thread_where_the_platform_has_threads(client): text = "This sentence repeats until the post is too long for one post. " * 12 body = client.post("/v1/text", json={"text": text, "platforms": ["x", "threads", "linkedin"]}).json() x = body["platforms"]["x"] assert x["split"] and x["ok"] and len(x["parts"]) > 1 and x["overflow"] == 0 assert all(part["count"] <= 280 for part in x["parts"]) and x["parts"][0]["text"].endswith(f"1/{len(x['parts'])}") assert body["platforms"]["linkedin"]["split"] is False def test_text_over_a_hard_limit_is_reported_with_its_overflow(client): body = client.post("/v1/text", json={"text": "word " * 700, "platforms": ["linkedin"]}).json() linkedin = body["platforms"]["linkedin"] assert (linkedin["ok"], linkedin["enforcement"], linkedin["overflow"]) == (False, "hard", 3500 - 3000) assert body["all_ok"] is False def test_text_over_a_recommended_limit_does_not_fail_the_whole_check(make_client): body = make_client(text_chars=70_000).post("/v1/text", json={"text": "word " * 13_000, "platforms": ["facebook"]}).json() facebook = body["platforms"]["facebook"] assert (facebook["ok"], facebook["enforcement"], facebook["overflow"]) == (False, "recommendation", 65_000 - 63_206) assert body["all_ok"] is True def test_split_can_be_turned_off(client): x = client.post("/v1/text", json={"text": "word " * 100, "platforms": ["x"], "split_threads": False}).json() assert x["platforms"]["x"]["split"] is False and x["platforms"]["x"]["ok"] is False and x["all_ok"] is False def test_platforms_accept_names_target_ids_and_short_names(client): body = client.post("/v1/text", json={"text": POST, "platforms": ["instagram_feed", "Twitter", "x_image"]}).json() assert list(body["platforms"]) == ["instagram", "x"] def test_unknown_platform_empty_text_and_bad_bodies_are_refused(client): error = error_of(client.post("/v1/text", json={"text": POST, "platforms": ["myspace"]}), 422, "unknown_platform") assert "myspace" in error["message"] error_of(client.post("/v1/text", json={"text": " \n "}), 422, "empty_text") error = error_of(client.post("/v1/text", json={}), 422, "invalid_request") assert "'text'" in error["message"] error_of(client.post("/v1/text", json={"text": 5}), 422, "invalid_request") error = error_of(client.post("/v1/text", content=b"{not json", headers={"content-type": "application/json"}), 422, "invalid_request") assert error["message"] == "The request body is missing or is not valid JSON." error_of(client.post("/v1/text"), 422, "invalid_request") def test_text_with_half_an_emoji_is_refused(client): broken = client.post("/v1/text", content=b'{"text": "launch \\ud83d day"}', headers={"content-type": "application/json"}) error_of(broken, 422, "invalid_text") def test_text_over_the_length_limit_is_refused(make_client): error = error_of(make_client(text_chars=100).post("/v1/text", json={"text": "x" * 101}), 422, "text_too_long") assert "101" in error["message"] and "100" in error["message"] def test_text_the_engine_refuses_gets_the_error_shape(client, monkeypatch): from app import engine def refuse(text, names, split_threads): raise ValueError("text holds something no platform can count") monkeypatch.setattr(engine._text, "check_text", refuse) error = error_of(client.post("/v1/text", json={"text": "hello"}), 400, "invalid_text") assert error["message"] == "The text cannot be counted: text holds something no platform can count."