Files
karaoke-gen/backend/tests/api/test_client_errors.py
T
Andrew BeveridgeandClaude Opus 5.5 f006246bd6 fix(alerts): start-review 409 on finished jobs, per-cause crash patterns, one alert per job failure (v0.290.1)
From the 2026-11-03 #alerts triage.

- start-review returns 409 for a job not in review (and when losing the
  auto-approver race) instead of a 500; correction-data's same race too.
- The error monitor builds unhandled-exception patterns from the exception's
  cause, so a second crash on another endpoint is no longer swallowed by an
  already-open "Exception in ASGI application" pattern.
- YouTube video ids are normalized out of signatures; one job failure is one
  alert (fail_job and the known download-failure lines log WARNING).
- Suppressions: Zalo's injected script, python -c experiments on job images,
  decide-search health-probe aborts. Demotions: Spotify 403 on /lyrics, Dropbox
  token probe (ERROR moves to the refresh failing).
- Cold start: import the anyio backend and warm the middleware stack off-loop.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012RYLVTvqXyL4QyA2EjzQMG
2026-11-03 17:53:57 -05:00

174 lines
6.6 KiB
Python

"""API tests for POST /api/client-errors."""
from __future__ import annotations
from unittest.mock import MagicMock
import pytest
from fastapi import FastAPI
from fastapi.testclient import TestClient
from backend.api.routes.client_errors import router as client_errors_router
@pytest.fixture
def client(monkeypatch):
# reset the module-level limiter between tests
import backend.api.routes.client_errors as mod
mod._limiter = mod.RateLimiter(max_per_minute=60)
fake_adapter = MagicMock()
fake_adapter.upsert_pattern.return_value = MagicMock(
pattern_id="deadbeef", is_new=True, previous_status=None
)
monkeypatch.setattr(mod, "_get_adapter", lambda: fake_adapter)
app = FastAPI()
app.include_router(client_errors_router, prefix="/api")
client = TestClient(app)
client.fake_adapter = fake_adapter # type: ignore[attr-defined]
return client
def _payload(**overrides):
base = {
"message": "TypeError: Cannot read properties of null",
"stack": "TypeError: Cannot read properties of null\n at x (app.js:1:2)",
"url": "https://gen.nomadkaraoke.com/en/app/jobs/#/abc/review",
"user_agent": "Mozilla/5.0 (Android 14; Mobile; rv:138.0) Gecko/138.0 Firefox/138.0",
"release": "abc1234",
"user_email": None,
"viewport": {"w": 412, "h": 915},
"locale": "en",
"source": "window.onerror",
}
base.update(overrides)
return base
def test_accepts_valid_report_and_upserts(client):
resp = client.post("/api/client-errors", json=_payload())
assert resp.status_code == 202
assert resp.json()["pattern_id"] == "deadbeef"
client.fake_adapter.upsert_pattern.assert_called_once()
(pd,), _ = client.fake_adapter.upsert_pattern.call_args
assert pd.service == "frontend"
assert pd.resource_type == "browser"
def test_bot_report_is_accepted_but_not_persisted(client):
bot_ua = (
"Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; "
"bingbot/2.0; +http://www.bing.com/bingbot.htm) Chrome/136.0.0.0 Safari/537.36"
)
resp = client.post("/api/client-errors", json=_payload(user_agent=bot_ua))
# Accepted (so the bot won't retry) but never upserted / alerted.
assert resp.status_code == 202
assert resp.json()["is_new"] is False
client.fake_adapter.upsert_pattern.assert_not_called()
@pytest.mark.parametrize("message", ["Script error.", "Error: Script error.", "script error"])
def test_opaque_script_error_is_accepted_but_not_persisted(client, message):
# Opaque cross-origin error (older cached bundles still report it) — the
# "stack" is only our own handler's frame, so it's pure alert noise.
resp = client.post(
"/api/client-errors",
json=_payload(
message=message,
stack=f"Error: {message}\n@https://gen.nomadkaraoke.com/_next/static/chunks/x.js:6:874",
source="window.onerror",
extra={"filename": "", "lineno": 0, "colno": 0},
),
)
assert resp.status_code == 202
assert resp.json()["is_new"] is False
client.fake_adapter.upsert_pattern.assert_not_called()
def test_located_script_error_message_is_persisted(client):
# A genuine `throw new Error("Script error.")` from our own code has a real
# file/line on the ErrorEvent — actionable, so it must still be recorded.
resp = client.post(
"/api/client-errors",
json=_payload(
message="Error: Script error.",
stack="Error: Script error.\n at foo (https://gen.nomadkaraoke.com/_next/static/chunks/y.js:3:120)",
source="window.onerror",
extra={"filename": "https://gen.nomadkaraoke.com/_next/static/chunks/y.js", "lineno": 3, "colno": 120},
),
)
assert resp.status_code == 202
client.fake_adapter.upsert_pattern.assert_called_once()
def test_message_merely_mentioning_script_error_is_persisted(client):
resp = client.post("/api/client-errors", json=_payload(message="Script error in chunk loader"))
assert resp.status_code == 202
client.fake_adapter.upsert_pattern.assert_called_once()
def test_rejects_missing_message(client):
resp = client.post("/api/client-errors", json=_payload(message=""))
assert resp.status_code == 422
def test_caps_oversized_payload(client):
huge = "x" * 200_000
resp = client.post("/api/client-errors", json=_payload(stack=huge))
# server accepts but truncates — sample_message must be <= MAX
assert resp.status_code == 202
(pd,), _ = client.fake_adapter.upsert_pattern.call_args
from backend.services.error_monitor.frontend_ingestion import (
MAX_SAMPLE_MESSAGE_CHARS,
)
assert len(pd.sample_message) <= MAX_SAMPLE_MESSAGE_CHARS
def test_rate_limit_returns_429(client, monkeypatch):
import backend.api.routes.client_errors as mod
mod._limiter = mod.RateLimiter(max_per_minute=2)
assert client.post("/api/client-errors", json=_payload()).status_code == 202
assert client.post("/api/client-errors", json=_payload()).status_code == 202
r3 = client.post("/api/client-errors", json=_payload())
assert r3.status_code == 429
def test_strips_sensitive_query_string(client):
resp = client.post(
"/api/client-errors",
json=_payload(url="https://gen.nomadkaraoke.com/en/app/jobs/?token=SECRET#/abc/review"),
)
assert resp.status_code == 202
(pd,), _ = client.fake_adapter.upsert_pattern.call_args
assert "SECRET" not in pd.sample_message
assert "token=" not in pd.sample_message
ZALO_UA = (
"Mozilla/5.0 (Linux; Android 16; X Build/Y;) AppleWebKit/537.36 (KHTML, like Gecko) "
"Version/4.0 Chrome/156.0.8078.47 Mobile Safari/537.36 Zalo android/261002903 ZaloTheme/light"
)
ZALO_MESSAGE = "ReferenceError: zaloJSV2 is not defined"
def test_zalo_host_app_injection_is_accepted_but_not_persisted(client):
resp = client.post(
"/api/client-errors",
json=_payload(message=ZALO_MESSAGE, stack=f"{ZALO_MESSAGE}\n at <anonymous>:1:2", user_agent=ZALO_UA),
)
assert resp.status_code == 202
assert resp.json()["is_new"] is False
client.fake_adapter.upsert_pattern.assert_not_called()
def test_real_errors_inside_zalo_are_persisted(client):
resp = client.post("/api/client-errors", json=_payload(user_agent=ZALO_UA))
assert resp.status_code == 202
client.fake_adapter.upsert_pattern.assert_called_once()
def test_zalo_message_from_another_browser_is_persisted(client):
resp = client.post("/api/client-errors", json=_payload(message=ZALO_MESSAGE))
assert resp.status_code == 202
client.fake_adapter.upsert_pattern.assert_called_once()