File size: 11,654 Bytes
7cc81cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
"""Mocked protocol tests for the audited WAN 2.2 worker integration."""

from __future__ import annotations

from collections.abc import Callable
from pathlib import Path

import httpx
import pytest

from app.core.config import Settings
from app.generation.domain.enums import (
    GenerationModality,
    WorkerCancellationStatus,
    WorkerErrorCategory,
    WorkerJobStatus,
)
from app.generation.domain.errors import GenerationWorkerError
from app.generation.domain.retry import GenerationRetryPolicy
from app.generation.model_registry import GenerationModelRegistration, GenerationModelRegistry
from app.generation.providers.wan import (
    WAN_MODEL_CAPABILITY,
    WAN_MODEL_ID,
    WAN_PROVIDER_ID,
    WanProviderAdapter,
)
from app.generation.providers.worker_client import RemoteWorkerClient
from app.generation.schemas.requests import GenerationRequestCreate


def _client(
    handler: Callable[[httpx.Request], httpx.Response], *, retries: int = 2
) -> RemoteWorkerClient:
    return RemoteWorkerClient(
        base_url="https://wan-worker.example",
        bearer_token="x" * 32,
        connect_timeout_seconds=1,
        request_timeout_seconds=1,
        read_timeout_seconds=1,
        retry_policy=GenerationRetryPolicy(max_retries=retries, backoff_seconds=0),
        http_client=httpx.AsyncClient(transport=httpx.MockTransport(handler)),
        sleep=lambda _: _no_sleep(),
    )


async def _no_sleep() -> None:
    return None


def _payload(**overrides: object) -> GenerationRequestCreate:
    value: dict[str, object] = {
        "provider": WAN_PROVIDER_ID,
        "model_id": WAN_MODEL_ID,
        "modality": "video",
        "input_asset_id": "11111111-1111-4111-8111-111111111111",
        "prompt": "Slow cinematic cloud movement",
        "wan": {
            "duration_seconds": 0.5,
            "steps": 4,
            "guidance_scale": 1.0,
            "guidance_scale_2": 1.0,
            "seed": 42,
            "randomize_seed": False,
        },
    }
    value.update(overrides)
    return GenerationRequestCreate.model_validate(value)


def _info() -> dict[str, object]:
    return {
        "id": "wan2.2",
        "name": "WAN 2.2 FP8 AOTI Faster",
        "type": "video",
        "task": "image-to-video",
        "status": "ready",
        "model_id": "Wan-AI/Wan2.2-I2V-A14B-Diffusers",
        "fps": 16,
    }


@pytest.mark.asyncio
async def test_wan_exact_model_discovery_and_readiness() -> None:
    def handler(request: httpx.Request) -> httpx.Response:
        if request.url.path == "/health":
            return httpx.Response(200, json={"status": "ok", "service": "mediarouter-wan-worker"})
        if request.url.path == "/ready":
            return httpx.Response(
                200,
                json={
                    "status": "ready",
                    "model_loaded": True,
                    "model": "wan2.2",
                    "accepting_jobs": True,
                },
            )
        return httpx.Response(200, json=_info())

    adapter = WanProviderAdapter(client=_client(handler))
    registry = GenerationModelRegistry(
        [
            GenerationModelRegistration(
                provider_id=WAN_PROVIDER_ID,
                model=WAN_MODEL_CAPABILITY,
                configuration_reference="wan-space",
            )
        ]
    )
    models = registry.verify_readiness(
        provider_id=WAN_PROVIDER_ID,
        worker_info=await adapter.info(),
        readiness=await adapter.ready(),
        provider_configured=adapter.available,
    )
    assert models[0].model.id == WAN_MODEL_ID
    assert models[0].model.modality is GenerationModality.VIDEO
    assert models[0].available


@pytest.mark.asyncio
async def test_wan_not_ready_and_model_mismatch_are_not_advertised() -> None:
    def handler(request: httpx.Request) -> httpx.Response:
        if request.url.path == "/ready":
            return httpx.Response(
                503,
                json={
                    "status": "not_ready",
                    "model_loaded": False,
                    "model": "other-model",
                    "accepting_jobs": False,
                },
            )
        return httpx.Response(
            200, json={"status": "ok"} if request.url.path == "/health" else _info()
        )

    adapter = WanProviderAdapter(client=_client(handler))
    with pytest.raises(GenerationWorkerError) as raised:
        await adapter.ready()
    assert raised.value.category is WorkerErrorCategory.WORKER_NOT_READY


@pytest.mark.asyncio
async def test_wan_model_identity_mismatch_remains_unavailable() -> None:
    wrong_info = {
        **_info(),
        "id": "different-wan-model",
        "name": "Different model",
    }

    def handler(request: httpx.Request) -> httpx.Response:
        if request.url.path == "/ready":
            return httpx.Response(
                200,
                json={
                    "status": "ready",
                    "model_loaded": True,
                    "model": WAN_MODEL_ID,
                    "accepting_jobs": True,
                },
            )
        return httpx.Response(200, json=wrong_info)

    adapter = WanProviderAdapter(client=_client(handler))
    with pytest.raises(GenerationWorkerError) as raised:
        await adapter.info()
    assert raised.value.category is WorkerErrorCategory.PROVIDER_ERROR


@pytest.mark.asyncio
async def test_wan_submission_is_multipart_and_has_no_automatic_retry(tmp_path: Path) -> None:
    seen: list[httpx.Request] = []

    def handler(request: httpx.Request) -> httpx.Response:
        seen.append(request)
        return httpx.Response(202, json={"job_id": "wan_" + "a" * 32, "status": "queued"})

    source = tmp_path / "input.png"
    source.write_bytes(b"not-decoded-in-adapter-test")
    adapter = WanProviderAdapter(client=_client(handler))
    job = await adapter.submit(
        payload={"prompt": "slow movement", "wan": {"duration_seconds": 0.5, "steps": 4}},
        idempotency_key="generation-request-id",
        input_path=source,
        input_mime_type="image/png",
    )
    assert job.status is WorkerJobStatus.QUEUED
    assert job.external_job_id.startswith("wan_")
    assert seen[0].headers["authorization"] == "Bearer " + "x" * 32
    body = seen[0].content.decode("latin-1")
    assert 'name="image"' in body
    assert 'name="duration_seconds"' in body
    assert 'name="width"' not in body


@pytest.mark.asyncio
async def test_wan_submission_connection_ambiguity_is_not_retried(tmp_path: Path) -> None:
    calls = 0
    request = httpx.Request("POST", "https://wan-worker.example/v1/generate")

    def handler(_: httpx.Request) -> httpx.Response:
        nonlocal calls
        calls += 1
        raise httpx.ConnectError("Bearer " + "x" * 32, request=request)

    source = tmp_path / "input.png"
    source.write_bytes(b"input")
    adapter = WanProviderAdapter(client=_client(handler, retries=3))
    with pytest.raises(GenerationWorkerError) as raised:
        await adapter.submit(
            payload={"prompt": "slow movement"},
            idempotency_key="generation-request-id",
            input_path=source,
            input_mime_type="image/png",
        )
    assert raised.value.category is WorkerErrorCategory.WORKER_UNAVAILABLE
    assert calls == 1
    assert "Bearer" not in str(raised.value)


@pytest.mark.asyncio
async def test_wan_completed_job_maps_a_safe_video_output() -> None:
    job_id = "wan_" + "b" * 32

    def handler(_: httpx.Request) -> httpx.Response:
        return httpx.Response(
            200,
            json={
                "job_id": job_id,
                "status": "completed",
                "output": {"type": "video", "filename": f"{job_id}.mp4"},
            },
        )

    adapter = WanProviderAdapter(client=_client(handler))
    job = await adapter.get_job(external_job_id=job_id)
    assert job.output is not None
    assert job.output.mime_type == "video/mp4"
    assert job.output.provider_output_id == job_id
    assert job.output.download_path == f"/v1/jobs/{job_id}/output"


@pytest.mark.asyncio
@pytest.mark.parametrize("status_code", [429, 502, 503, 504])
async def test_wan_polling_uses_shared_bounded_transient_retry(status_code: int) -> None:
    calls = 0
    job_id = "wan_" + "d" * 32

    def handler(_: httpx.Request) -> httpx.Response:
        nonlocal calls
        calls += 1
        if calls < 3:
            return httpx.Response(status_code, json={"detail": {"token": "never-store"}})
        return httpx.Response(200, json={"job_id": job_id, "status": "running"})

    job = await WanProviderAdapter(client=_client(handler, retries=2)).get_job(
        external_job_id=job_id
    )
    assert job.status is WorkerJobStatus.RUNNING
    assert calls == 3


@pytest.mark.asyncio
async def test_wan_polling_does_not_retry_permanent_client_errors() -> None:
    calls = 0
    job_id = "wan_" + "e" * 32

    def handler(_: httpx.Request) -> httpx.Response:
        nonlocal calls
        calls += 1
        return httpx.Response(400, json={"detail": {"code": "WAN_PARAMETERS_INVALID"}})

    with pytest.raises(GenerationWorkerError) as raised:
        await WanProviderAdapter(client=_client(handler, retries=3)).get_job(
            external_job_id=job_id
        )
    assert raised.value.category is WorkerErrorCategory.INVALID_REQUEST
    assert calls == 1


@pytest.mark.asyncio
async def test_wan_cancellation_only_confirms_queued_worker_cancellation() -> None:
    def queued_handler(_: httpx.Request) -> httpx.Response:
        return httpx.Response(200, json={"job_id": "wan_" + "c" * 32, "status": "cancelled"})

    def running_handler(_: httpx.Request) -> httpx.Response:
        return httpx.Response(
            409,
            json={
                "detail": {
                    "code": "WAN_JOB_NOT_CANCELLABLE",
                    "message": "A running job cannot be cancelled.",
                    "status": "running",
                }
            },
        )

    assert (
        await WanProviderAdapter(client=_client(queued_handler)).cancel(
            external_job_id="wan_" + "c" * 32
        )
    ).status is WorkerCancellationStatus.CANCELLED
    assert (
        await WanProviderAdapter(client=_client(running_handler)).cancel(
            external_job_id="wan_" + "c" * 32
        )
    ).status is WorkerCancellationStatus.FAILED


@pytest.mark.parametrize(
    "invalid",
    [
        {"prompt": " "},
        {"modality": "image"},
        {"input_asset_id": None},
    ],
)
@pytest.mark.asyncio
async def test_wan_request_validation_rejects_invalid_required_values(
    invalid: dict[str, object]
) -> None:
    adapter = WanProviderAdapter(client=None)
    with pytest.raises(Exception):
        payload = _payload(**invalid)
        await adapter.validate_request(payload)


@pytest.mark.parametrize("field", ["width", "height", "num_frames", "provider_payload"])
def test_wan_schema_rejects_unsupported_parameters(field: str) -> None:
    raw = _payload().model_dump()
    wan = dict(raw["wan"] or {})
    wan[field] = 1
    raw["wan"] = wan
    with pytest.raises(ValueError):
        GenerationRequestCreate.model_validate(raw)


def test_wan_configuration_is_optional_and_never_enables_flux() -> None:
    disabled = WanProviderAdapter.from_settings(Settings(_env_file=None))
    invalid = WanProviderAdapter.from_settings(
        Settings(_env_file=None, wan_space_url="https://wan-worker.example")
    )
    assert not disabled.available
    assert not invalid.available
    assert invalid.configuration_error is not None
    assert WAN_PROVIDER_ID == "wan"