import pytest
@pytest.mark.asyncio
async def test_document_lifecycle(http_client):
"""
Tests the full lifecycle of a document: add, list, and delete.
This is run as a single, sequential test for a clean state.
"""
print("\n--- Running test_document_lifecycle ---")
# 1. Add a new document
doc_data = {"title": "Lifecycle Test Doc", "text": "This doc will be listed and deleted."}
# Correct the URL to include the trailing slash to avoid the 307 redirect
add_response = await http_client.post("/documents/", json=doc_data)
assert add_response.status_code == 200
try:
message = add_response.json().get("message", "")
document_id = int(message.split(" with ID ")[-1])
except (ValueError, IndexError):
pytest.fail("Could not parse document ID from response message.")
print(f"✅ Document for lifecycle test created with ID: {document_id}")
# 2. List all documents and check if the new document is present
# Correct the URL to include the trailing slash
list_response = await http_client.get("/documents/")
assert list_response.status_code == 200
ids_in_response = {doc["id"] for doc in list_response.json()["documents"]}
assert document_id in ids_in_response
print("✅ Document list test passed.")
# 3. Delete the document
delete_response = await http_client.delete(f"/documents/{document_id}")
assert delete_response.status_code == 200
assert delete_response.json()["document_id"] == document_id
print("✅ Document delete test passed.")