logs: migrate to molecular CoT JSON protocol, fix regressions
- Replace BeliefFormatter with CotJsonFormatter (single-line JSON with ts, level, trace_id, src, marker, intent, payload, error, span_id) - Migrate belief_scope to use cot_log() for structured JSON output - Update monkey-patched reason/reflect/explore to pass extra= dict - Strip 200+ inline [REASON]/[REFLECT]/[EXPLORE] markers from 50+ files - Remove belief_scope from hot-path utilities (_parse_datetime, _json_load_if_needed) to eliminate startup log noise - Register TraceContextMiddleware in app.py (was implemented but unused) - Seed trace_id in startup_event/shutdown_event for background tasks - Fix broken relative imports in translate routes (3 dots → 4 dots) - Migrate 43 translate_routes log calls to logger.reason/explore - Fix SyntaxError (positional arg after extra=) in _repo_operations_routes - Fix IndentationError in rbac_permission_catalog except-block - Add smoke test tests/test_smoke_app.py (catches import errors before run)
This commit is contained in:
83
backend/tests/test_smoke_app.py
Normal file
83
backend/tests/test_smoke_app.py
Normal file
@@ -0,0 +1,83 @@
|
||||
# [DEF:TestSmokeApp:Module]
|
||||
# @COMPLEXITY: 2
|
||||
# @SEMANTICS: tests, smoke, app, imports, fastapi
|
||||
# @PURPOSE: Minimal smoke tests that verify the full application import chain
|
||||
# succeeds without SyntaxError, IndentationError, ImportError, or
|
||||
# NameError in any module.
|
||||
# @LAYER: Tests (Smoke)
|
||||
# @RELATION: VERIFIES -> src/app.py
|
||||
# @RELATION: VERIFIES -> src/core/cot_logger.py
|
||||
# @INVARIANT: All tests must pass without a running PostgreSQL instance.
|
||||
#
|
||||
# @RATIONALE: Uses SQLite in-memory database URLs to avoid requiring a live
|
||||
# PostgreSQL server during smoke testing. The database module's
|
||||
# _build_engine() already supports SQLite with
|
||||
# check_same_thread=False.
|
||||
|
||||
"""Smoke test: verify the full application imports without errors.
|
||||
|
||||
Catches: SyntaxError, IndentationError, ImportError, NameError in any module
|
||||
that is part of the application import chain.
|
||||
|
||||
Database dependency:
|
||||
The application's database module (src.core.database) creates SQLAlchemy
|
||||
engines at import time. To avoid needing a running PostgreSQL instance,
|
||||
environment variables are set to SQLite in-memory databases before the
|
||||
app is imported.
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Database URL override: use SQLite in-memory so no PostgreSQL is needed.
|
||||
# Must happen before ANY application module is imported.
|
||||
# ---------------------------------------------------------------------------
|
||||
os.environ.setdefault("DATABASE_URL", "sqlite:///:memory:")
|
||||
os.environ.setdefault("TASKS_DATABASE_URL", "sqlite:///:memory:")
|
||||
os.environ.setdefault("AUTH_DATABASE_URL", "sqlite:///:memory:")
|
||||
|
||||
# Ensure the backend root is on sys.path so `src` package is importable
|
||||
_src_path = str(Path(__file__).resolve().parent.parent)
|
||||
if _src_path not in sys.path:
|
||||
sys.path.insert(0, _src_path)
|
||||
|
||||
|
||||
def test_app_imports_cleanly():
|
||||
"""The application module tree imports without errors."""
|
||||
from src.app import app
|
||||
|
||||
assert app is not None
|
||||
assert app.title == "Superset Tools API"
|
||||
|
||||
|
||||
def test_app_has_expected_middleware():
|
||||
"""TraceContextMiddleware is registered (trace_id propagation)."""
|
||||
from src.app import app
|
||||
|
||||
middleware_classes = [m.cls.__name__ for m in app.user_middleware]
|
||||
assert "TraceContextMiddleware" in middleware_classes, (
|
||||
f"TraceContextMiddleware not found in app middleware: {middleware_classes}"
|
||||
)
|
||||
|
||||
|
||||
def test_cot_logger_imports():
|
||||
"""Core logging infrastructure is importable."""
|
||||
from src.core.cot_logger import (
|
||||
log,
|
||||
seed_trace_id,
|
||||
get_trace_id,
|
||||
push_span,
|
||||
pop_span,
|
||||
)
|
||||
|
||||
tid = seed_trace_id()
|
||||
assert tid is not None
|
||||
assert get_trace_id() == tid
|
||||
log("test", "REASON", "Smoke test log entry", {"ok": True})
|
||||
|
||||
# Verify push_span / pop_span round-trip
|
||||
prev = push_span("test_span")
|
||||
assert prev == "" # default empty span
|
||||
pop_span(prev)
|
||||
Reference in New Issue
Block a user