# -*- coding: utf-8 -*-
"""
conftest.py — service_monitor 私有测试配置

把 skill/code/web/ 加入 sys.path，让测试可 import service_monitor 包。
测试用独立的 tmp 数据目录，不碰真实 data/。
"""

import sys
from pathlib import Path

# tests/ 位于 web/service_monitor/tests/
TESTS_DIR = Path(__file__).resolve().parent
WEB_DIR = TESTS_DIR.parents[1]   # .../skill/code/web

if str(WEB_DIR) not in sys.path:
    sys.path.insert(0, str(WEB_DIR))

import pytest  # noqa: E402


@pytest.fixture
def tmp_data(tmp_path, monkeypatch):
    """把 service_monitor 的数据目录重定向到 tmp_path，隔离测试。"""
    from service_monitor.utils import paths as sm_paths
    from service_monitor.services import target_service, report_service

    data_dir = tmp_path / "data"
    reports_dir = data_dir / "reports"
    reports_dir.mkdir(parents=True, exist_ok=True)
    targets_file = data_dir / "targets.json"
    report_index_file = data_dir / "report_index.json"
    cooldown_file = data_dir / "alert_cooldown.json"

    monkeypatch.setattr(sm_paths, "DATA_DIR", data_dir)
    monkeypatch.setattr(sm_paths, "REPORTS_DIR", reports_dir)
    monkeypatch.setattr(sm_paths, "TARGETS_FILE", targets_file)
    monkeypatch.setattr(sm_paths, "REPORT_INDEX_FILE", report_index_file)
    monkeypatch.setattr(sm_paths, "COOLDOWN_FILE", cooldown_file)
    # service 模块在导入时已绑定常量引用，需同步 patch
    monkeypatch.setattr(target_service, "TARGETS_FILE", targets_file)
    monkeypatch.setattr(report_service, "REPORTS_DIR", reports_dir)
    monkeypatch.setattr(report_service, "REPORT_INDEX_FILE", report_index_file)
    monkeypatch.setattr(report_service, "COOLDOWN_FILE", cooldown_file)
    return {"data": data_dir, "reports": reports_dir, "targets": targets_file}
