"""Real Live E2E Integration Test executing the full consolidation runtime against live LLM APIs.""" from __future__ import annotations import asyncio import hashlib import json import os from pathlib import Path import pytest from src.runtime.cli.consolidate import run_consolidation from src.runtime.gateway.adapters import _load_env_file from src.runtime.storage.sqlite_store import SQLiteStore def test_live_e2e_real_api_consolidation(tmp_path: Path): """Executes a 100% REAL LIVE end-to-end consolidation against configured LLM endpoint. Asserts: 1. CLI run_consolidation returns exit code 0. 2. Result manifest JSON is persisted with SHA-256 verification. 3. Markdown file is rendered with YAML front-matter containing title, tags, and sentiment. 4. SQLite WAL tracks the claim and state transitions to 'completed_text'. 5. Tokens and real latency were recorded. """ _load_env_file() api_key = os.environ.get("OPENAI_API_KEY") or os.environ.get("GROQ_API_KEY") if not api_key: pytest.skip("No real LLM API key configured in .env or environment.") out_dir = tmp_path / "live_out" out_dir.mkdir(parents=True, exist_ok=True) db_file = tmp_path / "live_runtime.db" # Base configuration adapted to live endpoint base_cfg = json.loads(Path("runtime_config.local.json").read_text(encoding="utf-8")) base_cfg["paths"]["output_dir"] = str(out_dir) base_cfg["paths"]["sqlite_db"] = str(db_file) # Use the live provider from .env if OPENAI_API_KEY is present if os.environ.get("OPENAI_API_KEY"): base_cfg["roles"]["runtime_primary"]["provider"] = "openai" base_cfg["roles"]["runtime_primary"]["model"] = os.environ.get( "OPENAI_MODEL", "gpt-4o-mini" ) base_cfg["roles"]["runtime_primary"]["endpoint_url"] = os.environ.get( "OPENAI_BASE_URL", "https://api.openai.com/v1" ) cfg_file = tmp_path / "live_config.json" cfg_file.write_text(json.dumps(base_cfg, indent=2), encoding="utf-8") article_file = Path("examples/sample_article_valid.json") ecp_file = Path("examples/sample_ecp_snapshot.json") # Run full consolidation pipeline LIVE exit_code = asyncio.run(run_consolidation(article_file, ecp_file, cfg_file)) assert exit_code == 0, f"Expected exit code 0, got {exit_code}" # Verify artifacts on disk manifests = list(out_dir.glob("*.result.json")) assert len(manifests) == 1, f"Expected 1 manifest, found: {manifests}" manifest_data = json.loads(manifests[0].read_text(encoding="utf-8")) assert manifest_data["final_status"] == "completed_text" assert manifest_data["schema_version"] == "1.0.0" md_path = Path(manifest_data["markdown_path"]) assert md_path.exists(), f"Markdown file {md_path} does not exist" md_content = md_path.read_text(encoding="utf-8") assert md_content.startswith("---"), "Markdown must have YAML front-matter" assert "title:" in md_content assert "fingerprint:" in md_content assert "sentiment:" in md_content assert manifest_data["enrichment"]["sentiment"] is not None assert len(manifest_data["enrichment"]["tags"]) > 0 # SHA256 integrity verification calculated_hash = hashlib.sha256(md_content.encode("utf-8")).hexdigest() assert calculated_hash == manifest_data["markdown_hash"] # Verify SQLite tracking store = SQLiteStore(db_file) record = store.get_execution(manifest_data["fingerprint"]) assert record is not None assert record["current_status"] == "completed_text" assert record["final_status"] == "completed_text"