import uuid from datetime import UTC, datetime import pytest from httpx import ASGITransport, AsyncClient from app.main import app async def _create_vehicle(client, auth_headers): """Helper to create a vehicle via sync push and return its id.""" me = await client.get("/api/auth/me", headers=auth_headers) tenant_id = me.json()["tenant_id"] vid = str(uuid.uuid4()) now = datetime.now(UTC).isoformat() await client.post( "/api/sync/push", headers=auth_headers, json={ "operations": [ { "table": "vehicles", "id": vid, "operation": "INSERT", "data": { "id": vid, "tenant_id": tenant_id, "nr_inmatriculare": "CT01TST", "client_nume": "Test Client", "created_at": now, "updated_at": now, }, "timestamp": now, } ] }, ) return vid @pytest.mark.asyncio async def test_order_workflow(client, auth_headers): vid = await _create_vehicle(client, auth_headers) # Create order (DRAFT) r = await client.post( "/api/orders", headers=auth_headers, json={"vehicle_id": vid}, ) assert r.status_code == 200 order_id = r.json()["id"] # Add manopera line: 2h x 150 = 300 r = await client.post( f"/api/orders/{order_id}/lines", headers=auth_headers, json={ "tip": "manopera", "descriere": "Reparatie motor", "ore": 2, "pret_ora": 150, }, ) assert r.status_code == 200 # Add material line: 2 buc x 50 = 100 r = await client.post( f"/api/orders/{order_id}/lines", headers=auth_headers, json={ "tip": "material", "descriere": "Filtru ulei", "cantitate": 2, "pret_unitar": 50, "um": "buc", }, ) assert r.status_code == 200 # Validate order r = await client.post( f"/api/orders/{order_id}/validate", headers=auth_headers, ) assert r.status_code == 200 assert r.json()["status"] == "VALIDAT" # Get order details r = await client.get( f"/api/orders/{order_id}", headers=auth_headers, ) assert r.status_code == 200 data = r.json() assert data["total_manopera"] == 300 assert data["total_materiale"] == 100 assert data["total_general"] == 400 assert data["status"] == "VALIDAT" assert len(data["lines"]) == 2 @pytest.mark.asyncio async def test_cannot_add_line_to_validated_order(client, auth_headers): vid = await _create_vehicle(client, auth_headers) # Create and validate order r = await client.post( "/api/orders", headers=auth_headers, json={"vehicle_id": vid}, ) order_id = r.json()["id"] await client.post( f"/api/orders/{order_id}/validate", headers=auth_headers, ) # Try to add line to validated order r = await client.post( f"/api/orders/{order_id}/lines", headers=auth_headers, json={ "tip": "manopera", "descriere": "Should fail", "ore": 1, "pret_ora": 100, }, ) assert r.status_code == 422 @pytest.mark.asyncio async def test_list_orders(client, auth_headers): vid = await _create_vehicle(client, auth_headers) await client.post( "/api/orders", headers=auth_headers, json={"vehicle_id": vid}, ) r = await client.get("/api/orders", headers=auth_headers) assert r.status_code == 200 assert len(r.json()) == 1