# retoor from devplacepy.database import get_table from tests.api.sync.conftest import seed_world, sync_ok, vote_record def local(origin, kind, origin_uid): return get_table("sync_links").find_one(origin=origin, kind=kind, origin_uid=origin_uid)["local_uid"] def stars(table, uid): return get_table(table).find_one(uid=uid)["stars"] def test_votes_derive_stars_from_synced_voters_only(client, admin, origin): seed_world(client, admin, origin) post = local(origin, "posts", "p1") body = sync_ok(client, admin, "votes", origin, [vote_record("v1", "u2"), vote_record("v2", "u3")]) assert [item["status"] for item in body["results"]] == ["created", "created"] assert stars("posts", post) == 2 sync_ok(client, admin, "votes", origin, [vote_record("v3", "u1", value=-1)]) assert stars("posts", post) == 1 row = get_table("votes").find_one(uid=local(origin, "votes", "v1")) assert row["target_type"] == "post" and row["user_uid"] == local(origin, "users", "u2") def test_a_changed_and_a_cleared_vote_move_the_total(client, admin, origin): seed_world(client, admin, origin) post = local(origin, "posts", "p1") sync_ok(client, admin, "votes", origin, [vote_record("v1", "u2")]) sync_ok(client, admin, "votes", origin, [vote_record("v1", "u2", value=-1, updated_at="2025-02-01T00:00:00Z")]) assert stars("posts", post) == -1 sync_ok(client, admin, "votes", origin, [vote_record("v1", "u2", value=-1, updated_at="2025-03-01T00:00:00Z", deleted=True)]) assert stars("posts", post) == 0 sync_ok(client, admin, "votes", origin, [vote_record("v1", "u2", value=1, updated_at="2025-04-01T00:00:00Z")]) assert stars("posts", post) == 1 assert get_table("votes").count(user_uid=local(origin, "users", "u2"), target_uid=post) == 1 def test_a_second_origin_id_for_the_same_voter_revives_the_same_row(client, admin, origin): seed_world(client, admin, origin) post = local(origin, "posts", "p1") sync_ok(client, admin, "votes", origin, [vote_record("v1", "u2")]) sync_ok(client, admin, "votes", origin, [vote_record("v9", "u2", value=-1)]) assert get_table("votes").count(user_uid=local(origin, "users", "u2"), target_uid=post, target_type="post") == 1 assert stars("posts", post) == -1 def test_votes_on_comments_and_projects(client, admin, origin): seed_world(client, admin, origin) comment_votes = sync_ok(client, admin, "votes", origin, [vote_record("v1", "u3", "c1", "comment")]) assert comment_votes["results"][0]["status"] == "created" project = local(origin, "projects", "pr1") sync_ok(client, admin, "votes", origin, [vote_record("v2", "u2", "pr1", "project")]) assert stars("projects", project) == 1 def test_value_target_and_voter_validation(client, admin, origin): seed_world(client, admin, origin) body = sync_ok( client, admin, "votes", origin, [vote_record("v1", value=5), vote_record("v2", kind="gist"), vote_record("v3", voter="ghost"), vote_record("v4", target="ghost")], ) statuses = [(item["status"], item["reason"]) for item in body["results"]] assert statuses == [("rejected", "invalid"), ("rejected", "invalid"), ("deferred", "unknown_author"), ("deferred", "unknown_target")] assert body["results"][3]["needs_kind"] == "posts" def test_a_locally_deleted_vote_stays_deleted(client, admin, origin): seed_world(client, admin, origin) created = sync_ok(client, admin, "votes", origin, [vote_record("v1", "u2")])["results"][0] get_table("votes").update({"uid": created["local_uid"], "deleted_at": "2025-06-01T00:00:00+00:00", "deleted_by": "some-moderator"}, ["uid"]) again = sync_ok(client, admin, "votes", origin, [vote_record("v1", "u2", value=-1, updated_at="2025-07-01T00:00:00Z")])["results"][0] assert again["status"] == "unchanged" and again["reason"] == "locally_deleted" def test_dry_run_writes_nothing(client, admin, origin): seed_world(client, admin, origin) body = sync_ok(client, admin, "votes", origin, [vote_record("v1", "u2")], dry_run=True) assert body["results"][0]["status"] == "created" assert get_table("votes").count(target_uid=local(origin, "posts", "p1")) == 0 assert stars("posts", local(origin, "posts", "p1")) == 0