"""UI history must page backwards while the required API keeps ascending semantics.""" import argparse import json import uuid from compose_helpers import sql from client import Client, check, run, shape def main(): parser = argparse.ArgumentParser() parser.add_argument("--base-url", default="http://localhost:8080") args = parser.parse_args() client = Client(args.base_url) alice, bob, eve, chat = client.bootstrap() previews_path = "/api/ui/chats?limit=1" client.call("GET", previews_path, 401) empty_previews = json.loads(client.call("GET", previews_path, actor=eve)["bytes"]) check(not any(item["id"] == chat["id"] for item in empty_previews["items"]), "UI chat preview leaked a private chat") initial = json.loads(client.call("GET", previews_path, actor=alice)["bytes"]) check(initial["items"][0]["last_message"] is None, "empty chat has a phantom preview") path = "/api/ui/chats/" + chat["id"] + "/messages" client.call("GET", path, 401) client.call("GET", path, 404, actor=eve) members_path = "/api/ui/chats/" + chat["id"] + "/members" client.call("GET", members_path, 401) client.call("GET", members_path, 404, actor=eve) members = json.loads(client.call("GET", members_path, actor=bob)["bytes"])["items"] check({user["id"] for user in members} == {alice["id"], bob["id"]}, "wrong UI member list") for member in members: shape(member, "User") # rejects any password/hash fields in this projection prefix = uuid.uuid4().hex[:20] # Equal timestamps exercise the UUID tie-breaker in both directions. sql(f""" INSERT INTO messages(id, chat_id, sender_id, text, created_at) SELECT ('{prefix}' || lpad(n::text, 12, '0'))::uuid, '{chat['id']}'::uuid, '{alice['id']}'::uuid, 'UI pagination ' || n, '2026-02-01T00:00:00Z'::timestamptz FROM generate_series(1, 75) fixture(n); """) check(int(sql(f"SELECT count(*) FROM messages WHERE chat_id = '{chat['id']}';")) == 75, "UI fixture mismatch") ascending = client.call("GET", "/api/v1/chats/" + chat["id"] + "/messages?limit=50", actor=alice, schema="MessagePage")["json"] recent = client.call("GET", path + "?limit=50", actor=bob, schema="MessagePage")["json"] older = client.call("GET", path + "?limit=50&cursor=" + recent["next_cursor"], actor=bob, schema="MessagePage")["json"] previews = json.loads(client.call("GET", previews_path, actor=alice)["bytes"]) check(previews["items"][0]["last_message"] == recent["items"][0], "preview did not use the latest UUID for equal-time messages") second_chat = client.call("POST", "/api/v1/chats", 201, actor=alice, body={"title": "Preview pagination", "member_ids": [alice["id"]]}, schema="Chat")["json"] first = json.loads(client.call("GET", previews_path, actor=alice)["bytes"]) second = json.loads(client.call("GET", previews_path + "&cursor=" + first["next_cursor"], actor=alice)["bytes"]) check({first["items"][0]["id"], second["items"][0]["id"]} == {chat["id"], second_chat["id"]} and second["next_cursor"] is None, "chat preview pagination lost or repeated chats") client.call("GET", previews_path + "&cursor=" + first["next_cursor"], 400, actor=bob) client.call("GET", "/api/v1/chats?cursor=" + first["next_cursor"], 400, actor=alice) check([item["id"] for item in ascending["items"]] == sorted(item["id"] for item in ascending["items"]), "required history changed order") check([item["id"] for item in recent["items"]] == sorted((item["id"] for item in recent["items"]), reverse=True), "UI history is not newest first") all_items = recent["items"] + older["items"] check(len(all_items) == 75 and len({item["id"] for item in all_items}) == 75 and older["next_cursor"] is None, "UI pagination lost or repeated equal-time messages") client.call("GET", path + "?cursor=" + ascending["next_cursor"], 400, actor=alice) client.call("GET", "/api/v1/chats/" + chat["id"] + "/messages?cursor=" + recent["next_cursor"], 400, actor=alice) print("PASS: UI authentication, privacy, newest page, backwards pagination, cursor isolation; required API stays ascending") if __name__ == "__main__": run(main)