# retoor from devplacepy.database import get_table from tests.api.sync.conftest import comment_record, post_record, sync_ok, unique, user_record def seed_thread(client, admin, origin): sync_ok(client, admin, "users", origin, [user_record("u1", unique("cu1")), user_record("u2", unique("cu2"))]) sync_ok(client, admin, "posts", origin, [post_record("p1", "u1"), post_record("p2", "u1")]) def test_unknown_references_are_rejected_with_specific_reasons(client, admin, origin): seed_thread(client, admin, origin) body = sync_ok( client, admin, "comments", origin, [ comment_record("c1", author="nobody"), comment_record("c2", post="nopost"), comment_record("c3", parent_uid="noparent"), ], ) assert [r["reason"] for r in body["results"]] == ["unknown_author", "unknown_post", "unknown_parent"] def test_create_with_nested_reply_in_one_batch(client, admin, origin): seed_thread(client, admin, origin) body = sync_ok( client, admin, "comments", origin, [comment_record("c1", author="u2"), comment_record("c2", author="u1", parent_uid="c1")], ) assert [r["status"] for r in body["results"]] == ["created", "created"] parent, reply = (get_table("comments").find_one(uid=r["local_uid"]) for r in body["results"]) post = get_table("posts").find_one(uid=reply["post_uid"]) assert reply["parent_uid"] == parent["uid"] assert reply["target_type"] == "post" and reply["target_uid"] == post["uid"] assert parent["deleted_at"] is None def test_parent_on_a_different_post_is_rejected(client, admin, origin): seed_thread(client, admin, origin) sync_ok(client, admin, "comments", origin, [comment_record("c1", post="p1")]) body = sync_ok(client, admin, "comments", origin, [comment_record("c2", post="p2", parent_uid="c1")]) assert body["results"][0]["reason"] == "parent_mismatch" def test_edit_replay_and_tombstone(client, admin, origin): seed_thread(client, admin, origin) created = sync_ok(client, admin, "comments", origin, [comment_record("c1")])["results"][0] assert sync_ok(client, admin, "comments", origin, [comment_record("c1")])["results"][0]["status"] == "unchanged" edited = sync_ok( client, admin, "comments", origin, [comment_record("c1", content="edited comment", updated_at="2025-02-01T00:00:00Z")], )["results"][0] assert edited["status"] == "updated" assert get_table("comments").find_one(uid=created["local_uid"])["content"] == "edited comment" sync_ok( client, admin, "comments", origin, [comment_record("c1", content="edited comment", updated_at="2025-03-01T00:00:00Z", deleted=True)], ) row = get_table("comments").find_one(uid=created["local_uid"]) assert row["deleted_at"] and row["deleted_by"] == "sync" def test_synced_comment_renders_on_the_post_page(client, admin, origin): seed_thread(client, admin, origin) sync_ok(client, admin, "comments", origin, [comment_record("c1", content="comment visible on the page")]) link = get_table("sync_links").find_one(origin=origin, kind="posts", origin_uid="p1") slug = get_table("posts").find_one(uid=link["local_uid"])["slug"] page = client.get(f"/posts/{slug}") assert page.status_code == 200 and "comment visible on the page" in page.text