"""R-11: other people's rows must not push your own out of the overview and the search.""" from __future__ import annotations from tests.support import Actor, case_of, note_on, register def test_own_entries_are_found_behind_many_foreign_ones(client, db, monkeypatch): from fastapi.testclient import TestClient from app.api import routes from app.main import app # A tiny batch size makes the paging visible with few rows. monkeypatch.setattr(routes, "SCAN_BATCH", 2) register(db, "ada-scan@example.test", "Ada") register(db, "ben-scan@example.test", "Ben") ada = Actor(client, "ada-scan@example.test", "Ada") ben = Actor(TestClient(app), "ben-scan@example.test", "Ben") ada.login() ben.login() ada_case = case_of(ada, "Adas Akte") for index in range(7): matter = ada.request("POST", "/api/v1/matters", json={"title": f"Adas Vorgang {index}"}, status=201) ada.request("POST", "/api/v1/tasks", json={"title": f"Frühe Aufgabe {index}", "due_on": "2026-01-0%d" % (index + 1), "matter_id": matter["id"]}, status=201) ada.request("POST", "/api/v1/deadlines", json={"title": f"Frühe Frist {index}", "due_on": "2026-01-0%d" % (index + 1), "matter_id": matter["id"]}, status=201) note_on(ada, f"Gemeinsames Stichwort {index}", "Zebrastreifen", ada_case["id"]) ben_matter = ben.request("POST", "/api/v1/matters", json={"title": "Bens Vorgang"}, status=201) ben.request("POST", "/api/v1/tasks", json={"title": "Bens späte Aufgabe", "due_on": "2027-01-01", "matter_id": ben_matter["id"]}, status=201) ben.request("POST", "/api/v1/deadlines", json={"title": "Bens späte Frist", "due_on": "2027-01-01", "matter_id": ben_matter["id"]}, status=201) ben_case = case_of(ben, "Bens Akte") note_on(ben, "Bens Zebrastreifen-Notiz", "Zebrastreifen", ben_case["id"]) assert [item["title"] for item in ben.request("GET", "/api/v1/views/open-tasks")["items"]] == ["Bens späte Aufgabe"] assert [item["title"] for item in ben.request("GET", "/api/v1/views/open-deadlines")["items"]] == ["Bens späte Frist"] found = ben.request("GET", "/api/v1/search", params={"q": "Zebrastreifen"})["items"] assert [item["title"] for item in found] == ["Bens Zebrastreifen-Notiz"] # The own entries still come in order and the cap on the list holds. monkeypatch.setattr(routes, "VIEW_LIMIT", 3) first = ada.request("GET", "/api/v1/views/open-tasks")["items"] assert [item["title"] for item in first] == ["Frühe Aufgabe 0", "Frühe Aufgabe 1", "Frühe Aufgabe 2"] monkeypatch.setattr(routes, "SEARCH_LIMIT", 4) assert len(ada.request("GET", "/api/v1/search", params={"q": "Zebrastreifen"})["items"]) == 4