# [DEF:__tests__/test_tasks_logs:Module]
# @RELATION: VERIFIES -> ../tasks.py
# @PURPOSE: Contract testing for task logs API endpoints.
# [/DEF:__tests__/test_tasks_logs:Module]

import pytest
from fastapi import FastAPI
from fastapi.testclient import TestClient
from unittest.mock import MagicMock
from src.dependencies import get_task_manager, has_permission
from src.api.routes.tasks import router

# @TEST_FIXTURE: mock_app
@pytest.fixture
def client():
    app = FastAPI()
    app.include_router(router, prefix="/tasks")
    
    # Mock TaskManager
    mock_tm = MagicMock()
    app.dependency_overrides[get_task_manager] = lambda: mock_tm
    
    # Mock permissions (bypass for unit test)
    app.dependency_overrides[has_permission("tasks", "READ")] = lambda: True
    
    return TestClient(app), mock_tm

# @TEST_CONTRACT: get_task_logs_api -> Invariants
# @TEST_FIXTURE: valid_task_logs_request
def test_get_task_logs_success(client):
    tc, tm = client
    
    # Setup mock task
    mock_task = MagicMock()
    tm.get_task.return_value = mock_task
    tm.get_task_logs.return_value = [{"level": "INFO", "message": "msg1"}]
    
    response = tc.get("/tasks/task-1/logs?level=INFO")
    
    assert response.status_code == 200
    assert response.json() == [{"level": "INFO", "message": "msg1"}]
    tm.get_task.assert_called_with("task-1")
    # Verify filter construction inside route
    args = tm.get_task_logs.call_args
    assert args[0][0] == "task-1"
    assert args[0][1].level == "INFO"

# @TEST_EDGE: task_not_found
def test_get_task_logs_not_found(client):
    tc, tm = client
    tm.get_task.return_value = None
    
    response = tc.get("/tasks/missing/logs")
    assert response.status_code == 404
    assert response.json()["detail"] == "Task not found"

# @TEST_EDGE: invalid_limit
def test_get_task_logs_invalid_limit(client):
    tc, tm = client
    # limit=0 is ge=1 in Query
    response = tc.get("/tasks/task-1/logs?limit=0")
    assert response.status_code == 422

# @TEST_INVARIANT: response_purity
def test_get_task_log_stats_success(client):
    tc, tm = client
    tm.get_task.return_value = MagicMock()
    tm.get_task_log_stats.return_value = {"INFO": 5, "ERROR": 1}
    
    response = tc.get("/tasks/task-1/logs/stats")
    assert response.status_code == 200
    # response_model=LogStats might wrap this, but let's check basic structure
    # assuming tm.get_task_log_stats returns something compatible with LogStats
