"""Tests for prompt profile export endpoints (Phase PX.1). Covers: - GET /api/v1/prompts/profiles/{name}/export — single profile export - GET /api/v1/prompts/export/all — all profiles export - Validation of profile name, format, and structure """ import pytest from fastapi import FastAPI from fastapi.testclient import TestClient from app.core.sqlite_db import init_prompts_db, seed_default_profiles, _get_db from app.routers.prompts import router _VALID_STEPS = { "decompose", "filter", "generate", "generate_per_subq", "filter_intro", "filter_section", "filter_outro", } @pytest.fixture def client(tmp_path, monkeypatch): prompts_path = str(tmp_path / "prompts.db") monkeypatch.setenv("PROMPTS_DB_PATH", prompts_path) monkeypatch.setenv("HISTORY_DB_PATH", str(tmp_path / "history.db")) from app.core.config import get_settings get_settings.cache_clear() from app.core.dependencies import get_settings_cached get_settings_cached.cache_clear() conn = _get_db(prompts_path) init_prompts_db(conn) seed_default_profiles(conn) conn.close() test_app = FastAPI() test_app.include_router(router) yield TestClient(test_app) get_settings_cached.cache_clear() get_settings.cache_clear() def test_export_profile_valid(client): resp = client.get("/api/v1/prompts/profiles/A/export") assert resp.status_code == 200 data = resp.json() assert data["format"] == "legco-reranker-profile/v1" assert data["profile_name"] == "A" assert "exported_at" in data assert set(data["prompts"].keys()) == _VALID_STEPS def test_export_profile_has_content_disposition_header(client): resp = client.get("/api/v1/prompts/profiles/A/export") assert resp.status_code == 200 assert resp.headers["content-disposition"] == 'attachment; filename="legco-profile-A.json"' def test_export_profile_invalid_name(client): resp = client.get("/api/v1/prompts/profiles/X/export") assert resp.status_code == 400 def test_export_all(client): resp = client.get("/api/v1/prompts/export/all") assert resp.status_code == 200 data = resp.json() assert data["format"] == "legco-reranker-profile/v1" assert "exported_at" in data assert data["active_profile"] == "A" assert set(data["profiles"].keys()) == {"A", "B", "C"} for name in ("A", "B", "C"): assert set(data["profiles"][name]["prompts"].keys()) == _VALID_STEPS def test_export_profile_b_and_c(client): for name in ("B", "C"): resp = client.get(f"/api/v1/prompts/profiles/{name}/export") assert resp.status_code == 200 assert resp.json()["profile_name"] == name