from anyfast.models import GenerationResponse, TaskStatus


class TestGenerationResponse:
    def test_extract_task_id(self):
        resp = GenerationResponse({"task_id": "abc-123"})
        assert resp.task_id == "abc-123"

    def test_extract_camel_case_task_id(self):
        resp = GenerationResponse({"taskId": "def-456"})
        assert resp.task_id == "def-456"

    def test_extract_id_fallback(self):
        resp = GenerationResponse({"id": "ghi-789"})
        assert resp.task_id == "ghi-789"

    def test_no_task_id(self):
        resp = GenerationResponse({"status": "ok"})
        assert resp.task_id is None

    def test_none_data(self):
        resp = GenerationResponse(None)
        assert resp.task_id is None


class TestTaskStatus:
    def test_parse_fields(self):
        status = TaskStatus({
            "id": "task-1",
            "action": "image-gen",
            "status": "SUCCESS",
            "progress": "100%",
            "result": {"url": "https://example.com/image.png"},
        })
        assert status.id == "task-1"
        assert status.action == "image-gen"
        assert status.result == {"url": "https://example.com/image.png"}

    def test_defaults(self):
        status = TaskStatus({})
        assert status.id == ""
        assert status.status == ""
        assert status.result is None

    def test_is_completed(self):
        assert TaskStatus({"status": "SUCCESS"}).is_completed()
        assert not TaskStatus({"status": "PENDING"}).is_completed()

    def test_is_failed(self):
        assert TaskStatus({"status": "FAILURE"}).is_failed()
        assert not TaskStatus({"status": "SUCCESS"}).is_failed()

    def test_is_processing(self):
        for s in ("PENDING", "PROCESSING", "RUNNING", "QUEUED"):
            assert TaskStatus({"status": s}).is_processing()
        assert not TaskStatus({"status": "SUCCESS"}).is_processing()

    def test_progress_percent(self):
        assert TaskStatus({"progress": "50%"}).progress_percent == 50
        assert TaskStatus({"progress": "Step 75 of 100"}).progress_percent == 75
        assert TaskStatus({}).progress_percent == 0
        assert TaskStatus({"progress": ""}).progress_percent == 0
