# backend/tests/test_export.py — per-user 내보내기 번들 + 민감 필드 redact
import io
import json
import zipfile


def test_export_zip_has_bundles_and_redacts(client):
    # 데모(지우) — 인증 없이 current_user=지우
    raw = client.get("/api/me/export").content
    z = zipfile.ZipFile(io.BytesIO(raw))
    names = set(z.namelist())
    assert {"profile.json", "tasks.json", "emails.json", "connectors.json"} <= names

    profile = json.loads(z.read("profile.json"))
    assert profile["id"] == "jiwoo"

    # 커넥터 토큰 등 민감 필드는 redact (평문/암호문 노출 금지)
    conns = json.loads(z.read("connectors.json"))
    for c in conns:
        assert c.get("token_enc") in (None, "", "[redacted]")

    tasks = json.loads(z.read("tasks.json"))
    assert len(tasks) > 0 and all(t["user_id"] == "jiwoo" for t in tasks)
