state-hub/tests/test_token_passthrough.py

82 lines
2.9 KiB
Python
Raw Normal View History

"""
Token passthrough test: update_task_status with tokens_in/tokens_out
creates a token event automatically.
Tests the API-level behaviour (the MCP tool delegates to the same endpoints).
"""
from __future__ import annotations
import pytest
async def _create_domain(client, slug="td"):
r = await client.post("/domains/", json={"slug": slug, "name": "D"})
assert r.status_code == 201, r.text
return r.json()
async def _create_topic(client, domain_slug="td"):
r = await client.post("/topics/", json={"slug": "tp", "title": "T", "domain": domain_slug})
assert r.status_code == 201, r.text
return r.json()
async def _create_workstream(client, topic_id):
r = await client.post("/workstreams/", json={"topic_id": topic_id, "slug": "ws", "title": "WS"})
assert r.status_code == 201, r.text
return r.json()
async def _create_task(client, workstream_id):
r = await client.post("/tasks/", json={"workstream_id": workstream_id, "title": "my task"})
assert r.status_code == 201, r.text
return r.json()
@pytest.mark.asyncio
class TestTokenPassthrough:
async def test_update_status_with_tokens_creates_event(self, client):
"""PATCH /tasks/{id} with tokens_in/tokens_out creates a token_event."""
await _create_domain(client)
topic = await _create_topic(client)
ws = await _create_workstream(client, topic["id"])
task = await _create_task(client, ws["id"])
# Update task status with token data
r = await client.patch(f"/tasks/{task['id']}", json={
"status": "done",
"tokens_in": 1200,
"tokens_out": 800,
"model": "claude-sonnet-4-6",
"agent": "custodian",
})
assert r.status_code == 200
assert r.json()["status"] == "done"
# Token event should now exist for this task
r2 = await client.get("/token-events/", params={"task_id": task["id"]})
assert r2.status_code == 200
events = r2.json()
assert len(events) == 1
ev = events[0]
assert ev["tokens_in"] == 1200
assert ev["tokens_out"] == 800
assert ev["tokens_total"] == 2000
assert ev["model"] == "claude-sonnet-4-6"
assert ev["agent"] == "custodian"
assert ev["workstream_id"] == ws["id"]
async def test_update_status_without_tokens_creates_no_event(self, client):
"""PATCH /tasks/{id} without token fields creates no token_event."""
await _create_domain(client)
topic = await _create_topic(client)
ws = await _create_workstream(client, topic["id"])
task = await _create_task(client, ws["id"])
r = await client.patch(f"/tasks/{task['id']}", json={"status": "in_progress"})
assert r.status_code == 200
r2 = await client.get("/token-events/", params={"task_id": task["id"]})
assert r2.status_code == 200
assert r2.json() == []