import importlib import json import os import re import subprocess import sys import pytest from starlette.applications import Starlette from starlette.routing import Route from starlette.testclient import TestClient @pytest.fixture def client(tmp_path, monkeypatch): import trackio.sqlite_storage import trackio.utils monkeypatch.setattr(trackio.sqlite_storage, "TRACKIO_DIR", tmp_path) monkeypatch.setattr(trackio.utils, "TRACKIO_DIR", tmp_path) monkeypatch.setenv("TRACKIO_INGEST_TOKEN", "test-ingest-token") module = importlib.import_module("ingest") app = Starlette(routes=[Route("/ingest", module.ingest, methods=["POST"])]) with TestClient(app) as test_client: test_client.headers["Authorization"] = "Bearer test-ingest-token" yield test_client def batch(**updates): entry = { "project": "burtenshaw", "run": "arena-run-123", "run_id": "arena-run-123", "step": 1, "log_id": "event-1", "metrics": {"reward/mean": 0.75, "arena/step_seconds": 1.2}, "config": { "participant": "space:burtenshaw", "submission_id": "submission-123", }, } entry.update(updates) return {"logs": [entry]} def alert_batch(**updates): entry = { "project": "burtenshaw", "run": "arena-run-123", "run_id": "arena-run-123", "step": 39, "alert_id": "failure-39", "title": "Sandbox cleanup timed out", "text": "Training ended after 39 updates. Evaluation will use the last checkpoint.", "level": "error", "config": batch()["logs"][0]["config"], } entry.update(updates) return {"alerts": [entry]} def test_missing_or_invalid_auth_cannot_write(client, monkeypatch): for authorization in ("", "Bearer wrong", "test-ingest-token"): assert ( client.post( "/ingest", json=batch(), headers={"Authorization": authorization} ).status_code == 401 ) monkeypatch.delenv("TRACKIO_INGEST_TOKEN") assert client.post("/ingest", json=batch()).status_code == 503 @pytest.mark.parametrize( "updates", [ {"project": "../another-project"}, {"run_id": "another-run"}, {"step": -1}, {"step": True}, {"log_id": ""}, {"metrics": {"loss": "private text"}}, {"metrics": {"loss": True}}, {"metrics": {"loss": 2**80}}, {"metrics": {"loss": {"_type": "html", "html": "