"""
文件管理测试用例
"""

import pytest
import os
import time
from api.file_api import SysFileAPI


@pytest.mark.file
@pytest.mark.regression
class TestSysFile:
    """文件管理测试类"""

    @pytest.mark.asyncio
    async def test_upload_file(self, authenticated_client):
        """测试文件上传"""
        api = SysFileAPI(authenticated_client)
        test_file_path = "/tmp/test_file.txt"

        with open(test_file_path, "w") as f:
            f.write("This is a test file content")

        response = await api.upload(test_file_path, "test_user")

        os.remove(test_file_path)

        assert response.status_code == 201
        result = response.json()
        assert "id" in result

    @pytest.mark.asyncio
    async def test_get_all_files(self, authenticated_client):
        """测试获取所有文件"""
        api = SysFileAPI(authenticated_client)

        response = await api.get_all()

        assert response.status_code == 200
        assert isinstance(response.json(), list)

    @pytest.mark.asyncio
    async def test_get_file_by_id(self, authenticated_client):
        """测试根据ID获取文件"""
        api = SysFileAPI(authenticated_client)
        test_file_path = "/tmp/test_file.txt"

        with open(test_file_path, "w") as f:
            f.write("Test content")

        upload_response = await api.upload(test_file_path, "test_user")
        file_id = upload_response.json()["id"]

        os.remove(test_file_path)

        response = await api.get_by_id(file_id)

        assert response.status_code == 200
        assert response.json()["id"] == file_id

    @pytest.mark.asyncio
    async def test_download_file(self, authenticated_client):
        """测试文件下载"""
        api = SysFileAPI(authenticated_client)
        test_file_path = "/tmp/test_file.txt"

        with open(test_file_path, "w") as f:
            f.write("Download test content")

        upload_response = await api.upload(test_file_path, "test_user")
        file_name = upload_response.json()["fileName"]

        os.remove(test_file_path)

        response = await api.download(file_name)

        assert response.status_code == 200

    @pytest.mark.asyncio
    async def test_preview_file(self, authenticated_client):
        """测试文件预览"""
        api = SysFileAPI(authenticated_client)
        test_file_path = "/tmp/test_file.txt"

        with open(test_file_path, "w") as f:
            f.write("Preview test content")

        upload_response = await api.upload(test_file_path, "test_user")
        file_name = upload_response.json()["fileName"]

        os.remove(test_file_path)

        response = await api.preview(file_name)

        assert response.status_code == 200

    @pytest.mark.asyncio
    async def test_delete_file(self, authenticated_client):
        """测试删除文件"""
        api = SysFileAPI(authenticated_client)
        test_file_path = "/tmp/test_file.txt"

        with open(test_file_path, "w") as f:
            f.write("Delete test content")

        upload_response = await api.upload(test_file_path, "test_user")
        file_id = upload_response.json()["id"]

        os.remove(test_file_path)

        response = await api.delete(file_id)

        assert response.status_code == 204
