def test_list_topics_empty(client): resp = client.get("/api/topics") assert resp.status_code == 200 assert resp.json()["topics"] == [] def test_create_topic(client): resp = client.post("/api/topics", json={"name": "Finance", "description": "Financial docs", "color": "#ff0000"}) assert resp.status_code == 200 data = resp.json() assert data["name"] == "Finance" assert data["color"] == "#ff0000" assert "id" in data def test_create_topic_deduplication(client): client.post("/api/topics", json={"name": "Finance"}) resp = client.post("/api/topics", json={"name": "finance"}) # case-insensitive assert resp.status_code == 200 topics = client.get("/api/topics").json()["topics"] assert len(topics) == 1 def test_update_topic(client): create = client.post("/api/topics", json={"name": "Old Name"}).json() resp = client.patch(f"/api/topics/{create['id']}", json={"name": "New Name"}) assert resp.status_code == 200 assert resp.json()["name"] == "New Name" def test_update_topic_not_found(client): resp = client.patch("/api/topics/nonexistent", json={"name": "X"}) assert resp.status_code == 404 def test_delete_topic(client): create = client.post("/api/topics", json={"name": "ToDelete"}).json() resp = client.delete(f"/api/topics/{create['id']}") assert resp.status_code == 200 assert resp.json()["success"] is True topics = client.get("/api/topics").json()["topics"] assert not any(t["name"] == "ToDelete" for t in topics) def test_delete_topic_cascades_to_documents(client, sample_txt): # Create a topic topic = client.post("/api/topics", json={"name": "Legal"}).json() # Upload doc (no auto classify to control topics manually) with open(sample_txt, "rb") as f: upload = client.post( "/api/documents/upload", files={"file": ("sample.txt", f, "text/plain")}, data={"auto_classify": "false"}, ).json() # Manually set topic on the document via classify endpoint import services.storage as st st.update_document_topics(upload["id"], ["Legal"]) # Delete topic client.delete(f"/api/topics/{topic['id']}") # Verify document no longer has the topic doc = client.get(f"/api/documents/{upload['id']}").json() assert "Legal" not in doc["topics"] def test_delete_topic_not_found(client): resp = client.delete("/api/topics/nonexistent") assert resp.status_code == 404