42 lines
1.1 KiB
Python
42 lines
1.1 KiB
Python
"""Trace layer: session echo, trace minting, client-event ingest."""
|
|
import uuid
|
|
|
|
import pytest
|
|
from fastapi.testclient import TestClient
|
|
|
|
from src.main import app
|
|
|
|
|
|
@pytest.fixture()
|
|
def client():
|
|
with TestClient(app) as c:
|
|
yield c
|
|
|
|
|
|
def test_trace_headers_echo_session(client):
|
|
r = client.get("/", headers={"X-Session-ID": "sess-1", "X-User-Id": "u1"})
|
|
assert r.status_code == 200
|
|
assert r.headers["X-Session-ID"] == "sess-1"
|
|
assert len(r.headers["X-Trace-ID"]) == 16
|
|
|
|
|
|
def test_trace_headers_mint_session_when_missing(client):
|
|
r = client.get("/")
|
|
assert r.status_code == 200
|
|
assert r.headers["X-Session-ID"]
|
|
assert r.headers["X-Trace-ID"]
|
|
|
|
|
|
def test_events_reject_missing_session(client):
|
|
r = client.post("/api/v1/events", json={"session_id": " ", "events": []})
|
|
assert r.status_code in (400, 422)
|
|
|
|
|
|
def test_events_accept_batch(client):
|
|
sid = f"sess-{uuid.uuid4().hex[:8]}"
|
|
r = client.post("/api/v1/events", json={
|
|
"session_id": sid,
|
|
"events": [{"action": "image.next", "project_id": "p1", "payload": {"dir": 1}}],
|
|
})
|
|
assert r.status_code == 202
|
|
assert r.json() == {"accepted": 1}
|