Files
main_backend/tests/unit/test_notification_service.py
T
Daniil 259d3da89f rev 4
2026-04-07 13:42:45 +03:00

103 lines
3.0 KiB
Python

from __future__ import annotations
import uuid
from types import SimpleNamespace
from unittest.mock import AsyncMock
import pytest
from cpv3.modules.notifications.service import NotificationService
from cpv3.modules.tasks.schemas import TaskWebhookEvent
@pytest.mark.asyncio
async def test_create_task_notification_persists_operation_title() -> None:
service = NotificationService(session=AsyncMock())
service._repo = SimpleNamespace(
create=AsyncMock(return_value=SimpleNamespace(id=uuid.uuid4()))
)
job = SimpleNamespace(
id=uuid.uuid4(),
project_id=uuid.uuid4(),
job_type="SILENCE_APPLY",
status="DONE",
project_pct=100,
current_message="Завершено",
)
event = TaskWebhookEvent(
status="DONE",
progress_pct=100,
current_message="Завершено",
)
publish_mock = AsyncMock()
from cpv3.modules import notifications as notifications_module
original_publish = notifications_module.service.publish_to_user
notifications_module.service.publish_to_user = publish_mock
try:
await service.create_task_notification(
user_id=uuid.uuid4(),
job=job,
event=event,
)
finally:
notifications_module.service.publish_to_user = original_publish
create_call = service._repo.create.await_args
notification = create_call.args[0]
assert notification.title == "Применение вырезок"
assert notification.message == "Завершено"
assert notification.payload == {
"job_type": "SILENCE_APPLY",
"progress_pct": 100,
"status": "DONE",
}
publish_mock.assert_awaited_once()
@pytest.mark.asyncio
async def test_create_task_notification_preserves_zero_progress_for_websocket() -> None:
service = NotificationService(session=AsyncMock())
service._repo = SimpleNamespace(
create=AsyncMock(return_value=SimpleNamespace(id=uuid.uuid4()))
)
job = SimpleNamespace(
id=uuid.uuid4(),
project_id=uuid.uuid4(),
job_type="MEDIA_CONVERT",
status="RUNNING",
project_pct=35.0,
current_message="Конвертация видео",
)
event = TaskWebhookEvent(
progress_pct=0.0,
current_message="Подготовка файла",
)
publish_mock = AsyncMock()
from cpv3.modules import notifications as notifications_module
original_publish = notifications_module.service.publish_to_user
notifications_module.service.publish_to_user = publish_mock
try:
await service.create_task_notification(
user_id=uuid.uuid4(),
job=job,
event=event,
)
finally:
notifications_module.service.publish_to_user = original_publish
published_message = publish_mock.await_args.args[1]
assert published_message.progress_pct == 0.0
assert published_message.message == "Подготовка файла"