Download tests/test_admin_console_router_utils.py from muzissss/mum: direct link, hf CLI and curl.
- Browser
- Download file 10.8 kB
-
https://huggingface.co/spaces/muzissss/mum/resolve/main/tests/test_admin_console_router_utils.py
- Command line
-
hf download hf://spaces/muzissss/mum/tests/test_admin_console_router_utils.py
-
curl -L -o test_admin_console_router_utils.py https://huggingface.co/spaces/muzissss/mum/resolve/main/tests/test_admin_console_router_utils.py
10.8 kB
| import json | |
| from datetime import datetime, timedelta, timezone | |
| from api_server.admin_console_router import ( | |
| _build_cpa_usage_payload, | |
| _build_bootstrap_config_payload, | |
| _build_usage_snapshot, | |
| _collect_usage_events, | |
| _extract_usage_rows_from_cpa_payload, | |
| _merge_runtime_workspace_rows, | |
| _parse_emails_text, | |
| _parse_items, | |
| ) | |
| def test_parse_emails_text_parses_and_deduplicates(): | |
| text = """ | |
| # comment | |
| a@example.com | |
| b@example.com,p2 | |
| A@example.com,p3 | |
| """ | |
| out = _parse_emails_text(text) | |
| assert out == [ | |
| {"email": "a@example.com", "password": ""}, | |
| {"email": "b@example.com", "password": "p2"}, | |
| ] | |
| def test_parse_items_prefers_emails_text_when_present(): | |
| payload = { | |
| "emails_text": "x@example.com,px", | |
| "items": [{"email": "y@example.com", "password": "py"}], | |
| } | |
| out = _parse_items(payload) | |
| assert out == [{"email": "x@example.com", "password": "px"}] | |
| def test_parse_items_supports_mixed_item_types_and_deduplicates(): | |
| payload = { | |
| "items": [ | |
| "a@example.com", | |
| {"email": "b@example.com", "password": "p2"}, | |
| {"email": "A@example.com", "password": "p3"}, | |
| ] | |
| } | |
| out = _parse_items(payload) | |
| assert out == [ | |
| {"email": "a@example.com", "password": ""}, | |
| {"email": "b@example.com", "password": "p2"}, | |
| ] | |
| def test_collect_usage_events_prefers_usage_event_and_builds_snapshot(tmp_path, monkeypatch): | |
| monkeypatch.setenv("JSONL_LOG_DIR", str(tmp_path)) | |
| now = datetime.now(timezone.utc) | |
| rows = [ | |
| { | |
| "ts": (now - timedelta(hours=2)).isoformat().replace("+00:00", "Z"), | |
| "event": "usage_event", | |
| "model": "gpt-4.1-mini", | |
| "success": True, | |
| "status": "success", | |
| "prompt_tokens": 100, | |
| "completion_tokens": 50, | |
| "total_tokens": 150, | |
| }, | |
| { | |
| "ts": (now - timedelta(hours=1)).isoformat().replace("+00:00", "Z"), | |
| "event": "usage_event", | |
| "model": "gpt-4.1-mini", | |
| "success": False, | |
| "status": "failed", | |
| "prompt_tokens": 80, | |
| "completion_tokens": 0, | |
| "total_tokens": 80, | |
| }, | |
| ] | |
| log_path = tmp_path / "openai_adapter_run.jsonl" | |
| with log_path.open("w", encoding="utf-8") as f: | |
| for row in rows: | |
| f.write(json.dumps(row, ensure_ascii=False) + "\n") | |
| collected = _collect_usage_events(max_lines=1000) | |
| assert collected["source"] == "usage_event" | |
| assert len(collected["events"]) == 2 | |
| snap = _build_usage_snapshot( | |
| collected["events"], | |
| source=collected["source"], | |
| log_path=collected["path"], | |
| days=2, | |
| hours=24, | |
| model_limit=10, | |
| events_limit=10, | |
| ) | |
| summary = snap["summary"] | |
| assert summary["total_requests"] == 2 | |
| assert summary["success_count"] == 1 | |
| assert summary["failure_count"] == 1 | |
| assert summary["total_tokens"] == 230 | |
| assert summary["prompt_tokens"] == 180 | |
| assert summary["completion_tokens"] == 50 | |
| assert summary["unique_models"] == 1 | |
| def test_collect_usage_events_falls_back_to_legacy_logs(tmp_path, monkeypatch): | |
| monkeypatch.setenv("JSONL_LOG_DIR", str(tmp_path)) | |
| now = datetime.now(timezone.utc) | |
| rows = [ | |
| { | |
| "ts": (now - timedelta(hours=1)).isoformat().replace("+00:00", "Z"), | |
| "event": "app_reused_for_conversation", | |
| "conversation_id": "c1", | |
| "app_id": 123, | |
| }, | |
| { | |
| "ts": (now - timedelta(minutes=30)).isoformat().replace("+00:00", "Z"), | |
| "event": "account_chat_failed", | |
| "conversation_id": "c2", | |
| "email": "x@example.com", | |
| "error": "boom", | |
| }, | |
| ] | |
| log_path = tmp_path / "openai_adapter_run.jsonl" | |
| with log_path.open("w", encoding="utf-8") as f: | |
| for row in rows: | |
| f.write(json.dumps(row, ensure_ascii=False) + "\n") | |
| collected = _collect_usage_events(max_lines=1000) | |
| assert collected["source"] == "legacy_log" | |
| assert len(collected["events"]) == 2 | |
| snap = _build_usage_snapshot( | |
| collected["events"], | |
| source=collected["source"], | |
| log_path=collected["path"], | |
| days=2, | |
| hours=24, | |
| model_limit=10, | |
| events_limit=10, | |
| ) | |
| summary = snap["summary"] | |
| assert summary["total_requests"] == 2 | |
| assert summary["success_count"] == 1 | |
| assert summary["failure_count"] == 1 | |
| assert summary["total_tokens"] == 0 | |
| def test_build_cpa_usage_payload_shapes_apis_models_details(): | |
| now = datetime.now(timezone.utc) | |
| rows = [ | |
| { | |
| "ts": (now - timedelta(minutes=10)).isoformat().replace("+00:00", "Z"), | |
| "dt": now - timedelta(minutes=10), | |
| "model": "gpt-5.2", | |
| "success": True, | |
| "status": "success", | |
| "prompt_tokens": 120, | |
| "completion_tokens": 30, | |
| "total_tokens": 150, | |
| "cached_tokens": 20, | |
| "reasoning_tokens": 5, | |
| "account_email": "a@example.com", | |
| "conversation_id": "c-1", | |
| }, | |
| { | |
| "ts": (now - timedelta(minutes=5)).isoformat().replace("+00:00", "Z"), | |
| "dt": now - timedelta(minutes=5), | |
| "model": "gpt-5.2", | |
| "success": False, | |
| "status": "failed", | |
| "prompt_tokens": 90, | |
| "completion_tokens": 0, | |
| "total_tokens": 90, | |
| "cached_tokens": 0, | |
| "reasoning_tokens": 0, | |
| "account_email": "a@example.com", | |
| "conversation_id": "c-1", | |
| "error": "boom", | |
| }, | |
| ] | |
| payload = _build_cpa_usage_payload(rows, source="usage_event", log_path="x", event_limit=100) | |
| assert payload["total_requests"] == 2 | |
| assert payload["success_count"] == 1 | |
| assert payload["failure_count"] == 1 | |
| assert payload["total_tokens"] == 240 | |
| apis = payload.get("apis") or {} | |
| assert "POST /v1/chat/completions" in apis | |
| models = apis["POST /v1/chat/completions"]["models"] | |
| assert "gpt-5.2" in models | |
| details = models["gpt-5.2"]["details"] | |
| assert len(details) == 2 | |
| assert details[0]["tokens"]["input_tokens"] == 120 | |
| assert details[0]["failed"] is False | |
| assert details[1]["failed"] is True | |
| def test_extract_usage_rows_from_cpa_payload_parses_details(): | |
| payload = { | |
| "usage": { | |
| "apis": { | |
| "POST /v1/chat/completions": { | |
| "models": { | |
| "gpt-5.2": { | |
| "details": [ | |
| { | |
| "timestamp": "2026-02-01T00:00:00Z", | |
| "source": "a@example.com", | |
| "failed": False, | |
| "tokens": { | |
| "input_tokens": 40, | |
| "output_tokens": 10, | |
| "total_tokens": 50, | |
| "cached_tokens": 3, | |
| "reasoning_tokens": 2, | |
| }, | |
| } | |
| ] | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| rows = _extract_usage_rows_from_cpa_payload(payload) | |
| assert len(rows) == 1 | |
| row = rows[0] | |
| assert row["model"] == "gpt-5.2" | |
| assert row["success"] is True | |
| assert row["prompt_tokens"] == 40 | |
| assert row["completion_tokens"] == 10 | |
| assert row["total_tokens"] == 50 | |
| assert row["cached_tokens"] == 3 | |
| assert row["reasoning_tokens"] == 2 | |
| assert row["account_email"] == "a@example.com" | |
| def test_collect_usage_events_reads_latest_lines_when_capped(tmp_path, monkeypatch): | |
| monkeypatch.setenv("JSONL_LOG_DIR", str(tmp_path)) | |
| log_path = tmp_path / "openai_adapter_run.jsonl" | |
| rows = [ | |
| { | |
| "ts": "2026-02-01T00:00:00Z", | |
| "event": "usage_event", | |
| "model": "gpt-5.2", | |
| "success": True, | |
| "status": "success", | |
| "prompt_tokens": 1, | |
| "completion_tokens": 1, | |
| "total_tokens": 2, | |
| }, | |
| { | |
| "ts": "2026-02-01T00:01:00Z", | |
| "event": "usage_event", | |
| "model": "claude-opus-4-6", | |
| "success": True, | |
| "status": "success", | |
| "prompt_tokens": 2, | |
| "completion_tokens": 3, | |
| "total_tokens": 5, | |
| }, | |
| ] | |
| with log_path.open("w", encoding="utf-8") as f: | |
| for row in rows: | |
| f.write(json.dumps(row, ensure_ascii=False) + "\n") | |
| collected = _collect_usage_events(max_lines=1) | |
| assert collected["source"] == "usage_event" | |
| assert len(collected["events"]) == 1 | |
| assert collected["events"][0]["model"] == "claude-opus-4-6" | |
| def test_merge_runtime_workspace_rows_prefers_runtime_slug_and_tier(): | |
| runtime_rows = [ | |
| {"id": 61432, "slug": "123-99", "name": "123", "tier": "pro"}, | |
| {"id": 61520, "slug": "free-space", "name": "Free", "tier": "free"}, | |
| ] | |
| summary_rows = [ | |
| { | |
| "workspace_id": "61432", | |
| "account_count": 3, | |
| "ready_account_count": 2, | |
| "preferred_email": "a@example.com", | |
| "latest_updated_at": "2026-03-08T11:53:03Z", | |
| "accounts": [{"email": "a@example.com", "status": "ready"}], | |
| } | |
| ] | |
| out = _merge_runtime_workspace_rows(runtime_rows, summary_rows, account_email="owner@example.com") | |
| assert [row["workspace_id"] for row in out] == ["61432", "61520"] | |
| assert out[0]["workspace_slug"] == "123-99" | |
| assert out[0]["workspace_tier"] == "pro" | |
| assert out[0]["account_count"] == 3 | |
| assert out[0]["preferred_email"] == "a@example.com" | |
| assert out[1]["workspace_slug"] == "free-space" | |
| assert out[1]["workspace_tier"] == "free" | |
| assert out[1]["account_email"] == "owner@example.com" | |
| def test_build_bootstrap_config_payload_keeps_workspace_selection(): | |
| class DummySettings: | |
| admin_bearer_token = "x" | |
| class DummyRuntime: | |
| def workspace_selection_snapshot(self): | |
| return { | |
| "workspace_id": "61432", | |
| "workspace_slug": "123-99", | |
| "workspace_tier": "pro", | |
| "account_email": "a@example.com", | |
| } | |
| payload = _build_bootstrap_config_payload(DummySettings(), runtime=DummyRuntime()) | |
| assert payload["workspace_selection"]["workspace_id"] == "61432" | |
| assert payload["workspace_selection"]["workspace_slug"] == "123-99" | |
| assert payload["workspace_selection"]["workspace_tier"] == "pro" | |