import json
from pathlib import Path

from qianniu_exporter.cli import _extract_template_items, _manifest_from_template_items, scan_har_file


def test_scan_har_detects_download_and_list(tmp_path: Path):
    har = {
        "log": {
            "entries": [
                {
                    "request": {
                        "method": "POST",
                        "url": "https://sycm.taobao.com/api/qushu/template/list.json",
                        "headers": [{"name": "Cookie", "value": "secret"}, {"name": "Accept", "value": "application/json"}],
                        "postData": {"text": "page=1"},
                    },
                    "response": {"status": 200, "headers": [], "content": {"mimeType": "application/json"}},
                },
                {
                    "request": {
                        "method": "GET",
                        "url": "https://sycm.taobao.com/api/qushu/export?id=1",
                        "headers": [{"name": "Cookie", "value": "secret"}],
                    },
                    "response": {
                        "status": 200,
                        "headers": [{"name": "content-disposition", "value": "attachment; filename*=UTF-8''test.xlsx"}],
                        "content": {"mimeType": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"},
                    },
                },
            ]
        }
    }
    path = tmp_path / "sample.har"
    path.write_text(json.dumps(har), encoding="utf-8")
    result = scan_har_file(path)
    assert result["candidate_count"] == 2
    assert result["download_count"] == 1
    assert result["list_or_template_count"] == 1
    assert "Cookie" not in result["candidates"][0]["request_headers"]
    assert result["candidates"][1]["suggested_filename"] == "test.xlsx"


def test_build_manifest_from_template_list_with_example_url():
    payload = {
        "data": {
            "records": [
                {"templateId": "t001", "templateName": "店铺经营核心日报"},
                {"templateId": "t002", "templateName": "商品流量日报"},
            ]
        }
    }
    items = _extract_template_items(payload)
    manifest = _manifest_from_template_items(
        items,
        download_example={"method": "GET", "url": "https://sycm.taobao.com/api/qushu/export?id=demo", "request_headers": {}},
    )
    assert manifest["download_count"] == 2
    assert manifest["candidates"][0]["url"] == "https://sycm.taobao.com/api/qushu/export?id=t001"
    assert manifest["candidates"][0]["suggested_filename"] == "店铺经营核心日报.xlsx"
    assert manifest["candidates"][1]["url"] == "https://sycm.taobao.com/api/qushu/export?id=t002"


def test_build_manifest_from_direct_download_url_field():
    payload = {
        "result": {
            "list": [
                {"id": "1", "name": "A", "downloadUrl": "https://example.test/a.xlsx"},
                {"id": "2", "name": "B", "downloadUrl": "https://example.test/b.xlsx"},
            ]
        }
    }
    items = _extract_template_items(payload)
    manifest = _manifest_from_template_items(items)
    assert manifest["download_count"] == 2
    assert manifest["candidates"][0]["url"] == "https://example.test/a.xlsx"
    assert manifest["candidates"][1]["suggested_filename"] == "B.xlsx"
