I have this code for test classes that test my commands for my FastAPI websocket cqrs app. I will share with you a couple of those test classes and the code for my new command handler. Write test class for it to cover it 100% with integration tests.
Here is code for new command(it is a gambling game where people bet some number of points and then one of better takes it all after the session is resolved):
import asyncio
from profile.commands.base import BaseCommandHandler
from profile.commands.dispatcher import CommandDispatcher
from profile.constants import CommandType, ErrorCode, WheelContestantSessionStatus
from profile.models import CommandExecutionContext
from profile.schemas import Response
from typing import List, Optional, Tuple, Union

from diamonds_domain.pulsar_manager import (
    PulsarBroadcastEvent,
    PulsarBroadcastWheelEvent,
    PulsarCommandEvent,
)
from exceptions import CommandExecutionError, GameExecutionError

__all__ = ["JoinWheelSessionCommandHandler"]


@CommandDispatcher.register(CommandType.JOIN_WHEEL_SESSION)
class JoinWheelSessionCommandHandler(BaseCommandHandler):
    async def handle(
        self,
        context: CommandExecutionContext,
    ) -> Tuple[
        List[PulsarCommandEvent], Optional[Union[Response, PulsarBroadcastEvent]]
    ]:
        if context.game is None:
            raise CommandExecutionError(code=ErrorCode.COMMAND_EXECUTION_ERROR)

        if context.profile.direct_points < self.payload.bet:
            raise CommandExecutionError(code=ErrorCode.COMMAND_EXECUTION_ERROR)

        action_type, action_value = await self._get_action_value(context)

        try:
            session, status = await context.game.join_session(
                context.profile.auth_user_uuid, context.profile.name, self.payload.bet
            )
            if status == WheelContestantSessionStatus.CREATED:
                asyncio.create_task(context.game.resolve_session())
        except GameExecutionError:
            raise CommandExecutionError(code=ErrorCode.COMMAND_EXECUTION_ERROR)

        events = [
            self._create_pulsar_event(
                action_type=action_type,
                value=self.payload.bet,
                auth_user_uuid=context.profile.auth_user_uuid,
                idempotency_key=session.session_uuid,
            )
        ]

        notification = PulsarBroadcastEvent(
            payload=PulsarBroadcastWheelEvent(
                created_at=session.created_at,
                pool_value=session.pool_value,
                contestants=[
                    contestant.model_dump() for contestant in session.contestants
                ],
                status=session.status,
                winner=session.winner.model_dump() if session.winner else None,
            ),
        )

        return events, notification


And here are the examples of tests for other commands:
import asyncio
import uuid
from decimal import Decimal
from profile.constants import ErrorCode, GameStatus, ResponseType
from profile.schemas import (
    AuthCommand,
    AuthCommandPayload,
    MakeDiamondleAttemptCommand,
    MakeDiamondleAttemptCommandPayload,
    Response,
)
from typing import Tuple

import pytest
from async_asgi_testclient import TestClient
from diamonds_domain.profile.repositories import ProfileRepository
from diamonds_domain.redis_manager import RedisManager
from faker import Faker

fake = Faker()


@pytest.mark.asyncio
class TestMakeDiamondleAttemptCommand:
    @pytest.mark.parametrize("number_of_attempts", [1, 2, 3, 4, 5])
    async def test__win__ok(
        self,
        client: TestClient,
        profile_repo: ProfileRepository,
        redis_manager: RedisManager,
        valid_token_and_auth_user_uuid: Tuple[str, uuid.UUID],
        valid_connection_state: str,
        number_of_attempts: int,
    ) -> None:
        valid_token, auth_user_uuid = valid_token_and_auth_user_uuid
        answer = await redis_manager.get_diamondle_word()

        async with client.websocket_connect(
            f"/ws/{valid_connection_state}"
        ) as ws_client:
            # Authenticate a new user
            await ws_client.send_text(
                AuthCommand(
                    payload=AuthCommandPayload(token=valid_token, ref_code=None)
                ).model_dump_json()
            )

            await asyncio.sleep(0.1)

            # Receive profile response
            resp = Response.model_validate_json(await ws_client.receive_text())
            assert resp.type == ResponseType.PROFILE
            assert resp.response.direct_points == 0

            # Send commands
            for _ in range(number_of_attempts - 1):
                await ws_client.send_text(
                    MakeDiamondleAttemptCommand(
                        payload=MakeDiamondleAttemptCommandPayload(word="wrong")
                    ).model_dump_json()
                )
                await asyncio.sleep(0.1)

                # Receive response with updated game state
                resp = Response.model_validate_json(await ws_client.receive_text())
                assert resp.type == ResponseType.DIAMONDLE_SESSION
                assert resp.response.status == GameStatus.ONGOING

            # Send command with correct word
            await ws_client.send_text(
                MakeDiamondleAttemptCommand(
                    payload=MakeDiamondleAttemptCommandPayload(word=answer.word)
                ).model_dump_json()
            )
            await asyncio.sleep(0.1)

            # Receive response with updated game state
            resp = Response.model_validate_json(await ws_client.receive_text())
            assert resp.type == ResponseType.DIAMONDLE_SESSION
            assert resp.response.status == GameStatus.VICTORY

            await asyncio.sleep(0.1)

            # Receive the actual worker updates
            resp = Response.model_validate_json(await ws_client.receive_text())
            assert resp.type == ResponseType.PROFILE
            assert resp.response.direct_points == Decimal(10 - (number_of_attempts - 1))

    async def test__lose__ok(
        self,
        client: TestClient,
        profile_repo: ProfileRepository,
        redis_manager: RedisManager,
        valid_token_and_auth_user_uuid: Tuple[str, uuid.UUID],
        valid_connection_state: str,
    ) -> None:
        valid_token, auth_user_uuid = valid_token_and_auth_user_uuid

        async with client.websocket_connect(
            f"/ws/{valid_connection_state}"
        ) as ws_client:
            # Authenticate a new user
            await ws_client.send_text(
                AuthCommand(
                    payload=AuthCommandPayload(token=valid_token, ref_code=None)
                ).model_dump_json()
            )

            await asyncio.sleep(0.1)

            # Receive profile response
            resp = Response.model_validate_json(await ws_client.receive_text())
            assert resp.type == ResponseType.PROFILE
            assert resp.response.direct_points == 0

            # Send commands
            for _ in range(4):
                await ws_client.send_text(
                    MakeDiamondleAttemptCommand(
                        payload=MakeDiamondleAttemptCommandPayload(word="wrong")
                    ).model_dump_json()
                )
                await asyncio.sleep(0.1)

                # Receive response with updated game state
                resp = Response.model_validate_json(await ws_client.receive_text())
                assert resp.type == ResponseType.DIAMONDLE_SESSION
                assert resp.response.status == GameStatus.ONGOING

            # Send command with incorrect word again
            await ws_client.send_text(
                MakeDiamondleAttemptCommand(
                    payload=MakeDiamondleAttemptCommandPayload(word="wrong")
                ).model_dump_json()
            )
            await asyncio.sleep(0.1)

            # Receive response with updated game state
            resp = Response.model_validate_json(await ws_client.receive_text())
            assert resp.type == ResponseType.DIAMONDLE_SESSION
            assert resp.response.status == GameStatus.DEFEAT

            await asyncio.sleep(0.1)

            # Receive the actual worker updates
            resp = Response.model_validate_json(await ws_client.receive_text())
            assert resp.type == ResponseType.PROFILE
            assert resp.response.direct_points == Decimal(1)

    @pytest.mark.parametrize("number_of_commands", [2, 3])
    async def test__idempotency__ok(
        self,
        client: TestClient,
        profile_repo: ProfileRepository,
        redis_manager: RedisManager,
        valid_token_and_auth_user_uuid: Tuple[str, uuid.UUID],
        valid_connection_state: str,
        number_of_commands: int,
    ) -> None:
        valid_token, auth_user_uuid = valid_token_and_auth_user_uuid
        answer = await redis_manager.get_diamondle_word()

        async with client.websocket_connect(
            f"/ws/{valid_connection_state}"
        ) as ws_client:
            # Authenticate a new user
            await ws_client.send_text(
                AuthCommand(
                    payload=AuthCommandPayload(token=valid_token, ref_code=None)
                ).model_dump_json()
            )

            await asyncio.sleep(0.1)

            # Receive profile response
            resp = Response.model_validate_json(await ws_client.receive_text())
            assert resp.type == ResponseType.PROFILE
            assert resp.response.direct_points == 0

            # Send commands
            for _ in range(4):
                await ws_client.send_text(
                    MakeDiamondleAttemptCommand(
                        payload=MakeDiamondleAttemptCommandPayload(word="wrong")
                    ).model_dump_json()
                )
                await asyncio.sleep(0.1)

                # Receive response with updated game state
                resp = Response.model_validate_json(await ws_client.receive_text())
                assert resp.type == ResponseType.DIAMONDLE_SESSION
                assert resp.response.status == GameStatus.ONGOING

            await asyncio.sleep(1)

            for _ in range(number_of_commands):
                # Send multiple win commands
                await ws_client.send_text(
                    MakeDiamondleAttemptCommand(
                        payload=MakeDiamondleAttemptCommandPayload(word=answer.word)
                    ).model_dump_json()
                )

            for _ in range(number_of_commands):
                # Receive multiple notifications for optimistic update
                resp = Response.model_validate_json(await ws_client.receive_text())
                assert resp.type == ResponseType.DIAMONDLE_SESSION
                assert resp.response.status == GameStatus.VICTORY
                assert resp.response.win_streak == 1
                assert resp.response.games == 1
                assert resp.response.wins == 1

            await asyncio.sleep(0.1)

            # Receive the actual worker updates
            resp = Response.model_validate_json(await ws_client.receive_text())
            assert resp.type == ResponseType.PROFILE
            assert resp.response.direct_points == Decimal(6)

            await asyncio.sleep(0.1)

        # Check if DB profile was actually updated
        db_profile = await profile_repo.get_by_auth_user_uuid(auth_user_uuid)
        assert db_profile.direct_points != 0
        assert db_profile.direct_points == Decimal(6)

        # Check if cached profile was actually updated
        cache_profile = await redis_manager.get_cached_profile(auth_user_uuid)
        assert cache_profile.direct_points != 0
        assert cache_profile.direct_points == Decimal(6)

        # Check if cached gaming profile was actually updated
        cache_profile = await redis_manager.get_cached_gaming_profile(auth_user_uuid)
        assert cache_profile.diamondle_wins == 1
        assert cache_profile.diamondle_games == 1
        assert cache_profile.diamondle_win_streak == 1

    async def test__with_active_cooldown__error(
        self,
        client: TestClient,
        profile_repo: ProfileRepository,
        redis_manager: RedisManager,
        valid_token_and_auth_user_uuid: Tuple[str, uuid.UUID],
        valid_connection_state: str,
    ) -> None:
        valid_token, auth_user_uuid = valid_token_and_auth_user_uuid

        async with client.websocket_connect(
            f"/ws/{valid_connection_state}"
        ) as ws_client:
            # Authenticate a new user
            await ws_client.send_text(
                AuthCommand(
                    payload=AuthCommandPayload(token=valid_token, ref_code=None)
                ).model_dump_json()
            )

            await asyncio.sleep(0.1)

            # Receive profile response
            resp = Response.model_validate_json(await ws_client.receive_text())
            assert resp.type == ResponseType.PROFILE
            assert resp.response.direct_points == 0

            # Send commands
            for _ in range(5):
                await ws_client.send_text(
                    MakeDiamondleAttemptCommand(
                        payload=MakeDiamondleAttemptCommandPayload(word="wrong")
                    ).model_dump_json()
                )
                await asyncio.sleep(0.1)

                # Receive response with updated game state
                resp = Response.model_validate_json(await ws_client.receive_text())
                assert resp.type == ResponseType.DIAMONDLE_SESSION

            await asyncio.sleep(0.1)

            # Receive the actual worker updates
            resp = Response.model_validate_json(await ws_client.receive_text())
            assert resp.type == ResponseType.PROFILE
            assert resp.response.direct_points == Decimal(1)

            await asyncio.sleep(1)

            # Send command again
            await ws_client.send_text(
                MakeDiamondleAttemptCommand(
                    payload=MakeDiamondleAttemptCommandPayload(word="wrong")
                ).model_dump_json()
            )
            await asyncio.sleep(0.1)

            # Receive an error
            resp = Response.model_validate_json(await ws_client.receive_text())
            assert resp.type == ResponseType.ERROR
            assert resp.response.message == ErrorCode.COOLDOWN_ERROR


import asyncio
import uuid
from decimal import Decimal
from profile.constants import EGG_PRICE, ErrorCode, ResponseType
from profile.schemas import (
    AuthCommand,
    AuthCommandPayload,
    ClaimBonusCodePointsCommand,
    ClaimBonusCodePointsCommandPayload,
    PurchaseEggCommand,
    Response,
)
from typing import Tuple

import pytest
from async_asgi_testclient import TestClient
from diamonds_domain.egg.repositories import EggRepository
from diamonds_domain.profile.repositories import ProfileRepository
from diamonds_domain.redis_manager import RedisManager
from faker import Faker

fake = Faker()


@pytest.mark.asyncio
class TestPurchaseEggCommand:
    async def test__ok(
        self,
        client: TestClient,
        profile_repo: ProfileRepository,
        egg_repo: EggRepository,
        redis_manager: RedisManager,
        valid_token_and_auth_user_uuid: Tuple[str, uuid.UUID],
        valid_connection_state: str,
    ) -> None:
        valid_token, auth_user_uuid = valid_token_and_auth_user_uuid

        # Create valid bonus code
        code = fake.user_name()
        code_value = await redis_manager.set_bonus_code(
            code=code, uuid=uuid.uuid4(), value=Decimal(2000), usage_limit=10, ttl=60
        )

        async with client.websocket_connect(
            f"/ws/{valid_connection_state}"
        ) as ws_client:
            # Authenticate a new user
            await ws_client.send_text(
                AuthCommand(
                    payload=AuthCommandPayload(token=valid_token, ref_code=None)
                ).model_dump_json()
            )

            await asyncio.sleep(0.1)

            # Receive profile response
            resp = Response.model_validate_json(await ws_client.receive_text())
            assert resp.type == ResponseType.PROFILE
            assert resp.response.direct_points == 0

            # Check if there are no eggs for this profile
            assert (
                await egg_repo.get_eggs_by_profile_uuid(resp.response.profile_uuid)
                == []
            )
            assert await redis_manager.get_eggs(auth_user_uuid) is None

            # Send bonus code command
            await ws_client.send_text(
                ClaimBonusCodePointsCommand(
                    payload=ClaimBonusCodePointsCommandPayload(code=code)
                ).model_dump_json()
            )
            await asyncio.sleep(0.1)

            # Receive notification for optimistic update
            notification = Response.model_validate_json(await ws_client.receive_text())
            assert notification.type == ResponseType.CLAIM_BONUS_CODE_POINTS

            await asyncio.sleep(0.1)

            # Receive the actual worker updates
            resp = Response.model_validate_json(await ws_client.receive_text())
            assert resp.type == ResponseType.PROFILE
            assert resp.response.direct_points == code_value.value

            # Send command
            await ws_client.send_text(PurchaseEggCommand().model_dump_json())
            await asyncio.sleep(0.1)

            # Receive notification for optimistic update
            notification = Response.model_validate_json(await ws_client.receive_text())
            assert notification.type == ResponseType.PURCHASE_EGG
            assert notification.response.direct_points == (
                resp.response.direct_points - EGG_PRICE
            )

            await asyncio.sleep(0.1)

            # Receive the actual worker updates
            resp = Response.model_validate_json(await ws_client.receive_text())
            assert resp.type == ResponseType.PROFILE
            assert resp.response.direct_points == notification.response.direct_points

        # Check if DB profile was actually updated
        db_profile = await profile_repo.get_by_auth_user_uuid(auth_user_uuid)
        assert db_profile.direct_points == notification.response.direct_points

        # Check if cached profile was actually updated
        cache_profile = await redis_manager.get_cached_profile(auth_user_uuid)
        assert cache_profile.direct_points == notification.response.direct_points

        # Check if DB egg was actually created
        db_eggs = await egg_repo.get_eggs_by_profile_uuid(resp.response.profile_uuid)
        assert len(db_eggs) == 1

        # Check if cached eggs were actually updated
        cache_eggs = await redis_manager.get_eggs(auth_user_uuid)
        assert len(cache_eggs.eggs) == 1

    @pytest.mark.parametrize("number_of_commands", [2, 3])
    async def test__idempotency__ok(
        self,
        client: TestClient,
        profile_repo: ProfileRepository,
        egg_repo: EggRepository,
        redis_manager: RedisManager,
        valid_token_and_auth_user_uuid: Tuple[str, uuid.UUID],
        valid_connection_state: str,
        number_of_commands: int,
    ) -> None:
        valid_token, auth_user_uuid = valid_token_and_auth_user_uuid

        # Create valid bonus code
        code = fake.user_name()
        code_value = await redis_manager.set_bonus_code(
            code=code, uuid=uuid.uuid4(), value=Decimal(2000), usage_limit=10, ttl=60
        )

        async with client.websocket_connect(
            f"/ws/{valid_connection_state}"
        ) as ws_client:
            # Authenticate a new user
            await ws_client.send_text(
                AuthCommand(
                    payload=AuthCommandPayload(token=valid_token, ref_code=None)
                ).model_dump_json()
            )

            await asyncio.sleep(0.1)

            # Receive profile response
            resp = Response.model_validate_json(await ws_client.receive_text())
            assert resp.type == ResponseType.PROFILE
            assert resp.response.direct_points == 0

            # Check if there are no eggs for this profile
            assert (
                await egg_repo.get_eggs_by_profile_uuid(resp.response.profile_uuid)
                == []
            )
            assert await redis_manager.get_eggs(auth_user_uuid) is None

            # Send bonus code command
            await ws_client.send_text(
                ClaimBonusCodePointsCommand(
                    payload=ClaimBonusCodePointsCommandPayload(code=code)
                ).model_dump_json()
            )
            await asyncio.sleep(0.1)

            # Receive notification for optimistic update
            notification = Response.model_validate_json(await ws_client.receive_text())
            assert notification.type == ResponseType.CLAIM_BONUS_CODE_POINTS

            await asyncio.sleep(0.1)

            # Receive the actual worker updates
            resp = Response.model_validate_json(await ws_client.receive_text())
            assert resp.type == ResponseType.PROFILE
            assert resp.response.direct_points == code_value.value

            # Send multiple commands
            for _ in range(number_of_commands):
                await ws_client.send_text(PurchaseEggCommand().model_dump_json())

            await asyncio.sleep(0.1)

            # Receive multiple notifications
            for _ in range(number_of_commands):
                notification = Response.model_validate_json(
                    await ws_client.receive_text()
                )
                assert notification.type == ResponseType.PURCHASE_EGG
                assert notification.response.direct_points == (
                    resp.response.direct_points - EGG_PRICE
                )

            await asyncio.sleep(0.1)

            # Receive the actual worker updates
            resp = Response.model_validate_json(await ws_client.receive_text())
            assert resp.type == ResponseType.PROFILE
            assert resp.response.direct_points == notification.response.direct_points

        # Check if DB profile was actually updated
        db_profile = await profile_repo.get_by_auth_user_uuid(auth_user_uuid)
        assert db_profile.direct_points == notification.response.direct_points

        # Check if cached profile was actually updated
        cache_profile = await redis_manager.get_cached_profile(auth_user_uuid)
        assert cache_profile.direct_points == notification.response.direct_points

        # Check if DB egg was actually created
        db_eggs = await egg_repo.get_eggs_by_profile_uuid(cache_profile.uuid)
        assert len(db_eggs) == 1

        # Check if cached eggs were actually updated
        cache_eggs = await redis_manager.get_eggs(auth_user_uuid)
        assert len(cache_eggs.eggs) == 1

    async def test__with_not_enough_diamonds__error(
        self,
        client: TestClient,
        profile_repo: ProfileRepository,
        redis_manager: RedisManager,
        valid_token_and_auth_user_uuid: Tuple[str, uuid.UUID],
        valid_connection_state: str,
    ) -> None:
        valid_token, auth_user_uuid = valid_token_and_auth_user_uuid

        async with client.websocket_connect(
            f"/ws/{valid_connection_state}"
        ) as ws_client:
            # Authenticate a new user
            await ws_client.send_text(
                AuthCommand(
                    payload=AuthCommandPayload(token=valid_token, ref_code=None)
                ).model_dump_json()
            )

            await asyncio.sleep(0.1)

            # Receive profile response
            resp = Response.model_validate_json(await ws_client.receive_text())
            assert resp.type == ResponseType.PROFILE
            assert resp.response.direct_points == 0

            # Send command
            await ws_client.send_text(PurchaseEggCommand().model_dump_json())
            await asyncio.sleep(0.1)

            # Receive an error
            resp = Response.model_validate_json(await ws_client.receive_text())
            assert resp.type == ResponseType.ERROR
            assert resp.response.message == ErrorCode.COMMAND_EXECUTION_ERROR


import asyncio
import uuid
from profile.constants import ErrorCode, ResponseType
from profile.schemas import (
    AuthCommand,
    AuthCommandPayload,
    ClaimPointsCommand,
    ClaimRefPointsCommand,
    Response,
)
from typing import Callable, Tuple

import pytest
from async_asgi_testclient import TestClient
from diamonds_domain.profile.repositories import ActionRepository, ProfileRepository
from diamonds_domain.redis_manager import RedisManager
from faker import Faker
from security import create_state

fake = Faker()


@pytest.mark.asyncio
class TestClaimRefPointsCommand:
    async def test__ok(
        self,
        client: TestClient,
        profile_repo: ProfileRepository,
        action_repo: ActionRepository,
        redis_manager: RedisManager,
        token_and_auth_user_uuid_factory: Callable[[], Tuple[str, uuid.UUID]],
    ) -> None:
        # Generate token for User 1
        valid_token_1, auth_user_uuid = token_and_auth_user_uuid_factory()
        valid_token_2, _ = token_and_auth_user_uuid_factory()

        async with client.websocket_connect(f"/ws/{create_state()}") as ws_client:
            # Authenticate User 1
            await ws_client.send_text(
                AuthCommand(
                    payload=AuthCommandPayload(token=valid_token_1, ref_code=None)
                ).model_dump_json()
            )

            await asyncio.sleep(0.1)

            # Receive profile response
            resp = Response.model_validate_json(await ws_client.receive_text())
            assert resp.type == ResponseType.PROFILE

            # Receive User 1 ref code
            ref_code = resp.response.ref_code

        async with client.websocket_connect(f"/ws/{create_state()}") as ws_client:
            # Authenticate User 2 using User 1 ref code
            await ws_client.send_text(
                AuthCommand(
                    payload=AuthCommandPayload(token=valid_token_2, ref_code=ref_code)
                ).model_dump_json()
            )

            await asyncio.sleep(0.1)

            # Receive profile response
            resp = Response.model_validate_json(await ws_client.receive_text())
            assert resp.type == ResponseType.PROFILE

            # Send command
            await ws_client.send_text(ClaimPointsCommand().model_dump_json())

            # Receive notification for optimistic update
            notification = Response.model_validate_json(await ws_client.receive_text())
            assert notification.type == ResponseType.CLAIM_POINTS

            await asyncio.sleep(0.1)

            # Receive the actual worker updates
            resp = Response.model_validate_json(await ws_client.receive_text())
            assert resp.type == ResponseType.PROFILE

        async with client.websocket_connect(f"/ws/{create_state()}") as ws_client:
            # Authenticate User 1 again
            await ws_client.send_text(
                AuthCommand(
                    payload=AuthCommandPayload(token=valid_token_1, ref_code=None)
                ).model_dump_json()
            )

            await asyncio.sleep(0.1)

            # Receive profile response
            resp = Response.model_validate_json(await ws_client.receive_text())
            assert resp.type == ResponseType.PROFILE

            # Check if User 1 acquired ref points while has no direct points
            assert resp.response.direct_points == 0
            assert resp.response.ref_points != 0

            # Send command
            await ws_client.send_text(ClaimRefPointsCommand().model_dump_json())

            # Receive notification for optimistic update
            notification = Response.model_validate_json(await ws_client.receive_text())
            assert notification.type == ResponseType.CLAIM_REF_POINTS
            assert notification.response.direct_points != 0
            assert notification.response.ref_points == 0

            await asyncio.sleep(0.1)

            # Receive the actual worker updates
            resp = Response.model_validate_json(await ws_client.receive_text())
            assert resp.type == ResponseType.PROFILE
            assert resp.response.direct_points != 0
            assert resp.response.ref_points == 0

        # Check if DB profile was actually updated
        db_profile = await profile_repo.get_by_auth_user_uuid(auth_user_uuid)
        assert db_profile.direct_points != 0
        assert db_profile.ref_points == 0

        # Check if cached profile was actually updated
        cache_profile = await redis_manager.get_cached_profile(auth_user_uuid)
        assert cache_profile.direct_points != 0
        assert cache_profile.ref_points == 0

    @pytest.mark.parametrize("number_of_commands", [2, 3])
    async def test__idempotency__ok(
        self,
        client: TestClient,
        profile_repo: ProfileRepository,
        action_repo: ActionRepository,
        redis_manager: RedisManager,
        token_and_auth_user_uuid_factory: Callable[[], Tuple[str, uuid.UUID]],
        number_of_commands: int,
    ) -> None:
        # Generate token for User 1
        valid_token_1, auth_user_uuid = token_and_auth_user_uuid_factory()
        valid_token_2, _ = token_and_auth_user_uuid_factory()

        async with client.websocket_connect(f"/ws/{create_state()}") as ws_client:
            # Authenticate User 1
            await ws_client.send_text(
                AuthCommand(
                    payload=AuthCommandPayload(token=valid_token_1, ref_code=None)
                ).model_dump_json()
            )

            await asyncio.sleep(0.1)

            # Receive profile response
            resp = Response.model_validate_json(await ws_client.receive_text())
            assert resp.type == ResponseType.PROFILE

            # Receive User 1 ref code
            ref_code = resp.response.ref_code

        async with client.websocket_connect(f"/ws/{create_state()}") as ws_client:
            # Authenticate User 2 using User 1 ref code
            await ws_client.send_text(
                AuthCommand(
                    payload=AuthCommandPayload(token=valid_token_2, ref_code=ref_code)
                ).model_dump_json()
            )

            await asyncio.sleep(0.1)

            # Receive profile response
            resp = Response.model_validate_json(await ws_client.receive_text())
            assert resp.type == ResponseType.PROFILE

            # Send command
            await ws_client.send_text(ClaimPointsCommand().model_dump_json())

            # Receive notification for optimistic update
            notification = Response.model_validate_json(await ws_client.receive_text())
            assert notification.type == ResponseType.CLAIM_POINTS

            await asyncio.sleep(0.1)

            # Receive the actual worker updates
            resp = Response.model_validate_json(await ws_client.receive_text())
            assert resp.type == ResponseType.PROFILE

        async with client.websocket_connect(f"/ws/{create_state()}") as ws_client:
            # Authenticate User 1 again
            await ws_client.send_text(
                AuthCommand(
                    payload=AuthCommandPayload(token=valid_token_1, ref_code=None)
                ).model_dump_json()
            )

            await asyncio.sleep(0.1)

            # Receive profile response
            resp = Response.model_validate_json(await ws_client.receive_text())
            assert resp.type == ResponseType.PROFILE

            # Check if User 1 acquired ref points while has no direct points
            assert resp.response.direct_points == 0
            assert resp.response.ref_points != 0
            claim_value = resp.response.ref_points

            for _ in range(number_of_commands):
                # Send multiple command
                await ws_client.send_text(ClaimRefPointsCommand().model_dump_json())

            for idx in range(number_of_commands):
                # Receive multiple notifications for optimistic update
                notification = Response.model_validate_json(
                    await ws_client.receive_text()
                )
                assert notification.type == ResponseType.CLAIM_REF_POINTS
                assert notification.response.direct_points != 0
                assert notification.response.ref_points == 0

            await asyncio.sleep(0.1)

            # Receive the actual worker updates
            resp = Response.model_validate_json(await ws_client.receive_text())
            assert resp.type == ResponseType.PROFILE
            assert resp.response.direct_points == claim_value
            assert resp.response.ref_points == 0

            await asyncio.sleep(0.1)

        # Check if DB profile was actually updated
        db_profile = await profile_repo.get_by_auth_user_uuid(auth_user_uuid)
        assert db_profile.direct_points != 0
        assert db_profile.direct_points == claim_value
        assert db_profile.ref_points == 0

        # Check if cached profile was actually updated
        cache_profile = await redis_manager.get_cached_profile(auth_user_uuid)
        assert cache_profile.direct_points != 0
        assert cache_profile.direct_points == claim_value
        assert cache_profile.ref_points == 0

    async def test__with_active_cooldown__error(
        self,
        client: TestClient,
        profile_repo: ProfileRepository,
        action_repo: ActionRepository,
        redis_manager: RedisManager,
        token_and_auth_user_uuid_factory: Callable[[], Tuple[str, uuid.UUID]],
    ) -> None:
        # Generate token for User 1
        valid_token_1, auth_user_uuid = token_and_auth_user_uuid_factory()
        valid_token_2, _ = token_and_auth_user_uuid_factory()

        async with client.websocket_connect(f"/ws/{create_state()}") as ws_client:
            # Authenticate User 1
            await ws_client.send_text(
                AuthCommand(
                    payload=AuthCommandPayload(token=valid_token_1, ref_code=None)
                ).model_dump_json()
            )

            await asyncio.sleep(0.1)

            # Receive profile response
            resp = Response.model_validate_json(await ws_client.receive_text())
            assert resp.type == ResponseType.PROFILE

            # Receive User 1 ref code
            ref_code = resp.response.ref_code

        async with client.websocket_connect(f"/ws/{create_state()}") as ws_client:
            # Authenticate User 2 using User 1 ref code
            await ws_client.send_text(
                AuthCommand(
                    payload=AuthCommandPayload(token=valid_token_2, ref_code=ref_code)
                ).model_dump_json()
            )

            await asyncio.sleep(0.1)

            # Receive profile response
            resp = Response.model_validate_json(await ws_client.receive_text())
            assert resp.type == ResponseType.PROFILE

            # Send command
            await ws_client.send_text(ClaimPointsCommand().model_dump_json())

            # Receive notification for optimistic update
            notification = Response.model_validate_json(await ws_client.receive_text())
            assert notification.type == ResponseType.CLAIM_POINTS

            await asyncio.sleep(0.1)

            # Receive the actual worker updates
            resp = Response.model_validate_json(await ws_client.receive_text())
            assert resp.type == ResponseType.PROFILE

        async with client.websocket_connect(f"/ws/{create_state()}") as ws_client:
            # Authenticate User 1 again
            await ws_client.send_text(
                AuthCommand(
                    payload=AuthCommandPayload(token=valid_token_1, ref_code=None)
                ).model_dump_json()
            )

            await asyncio.sleep(0.1)

            # Receive profile response
            resp = Response.model_validate_json(await ws_client.receive_text())
            assert resp.type == ResponseType.PROFILE

            # Check if User 1 acquired ref points while has no direct points
            assert resp.response.direct_points == 0
            assert resp.response.ref_points != 0

            # Send command
            await ws_client.send_text(ClaimRefPointsCommand().model_dump_json())

            # Receive notification for optimistic update
            notification = Response.model_validate_json(await ws_client.receive_text())
            assert notification.type == ResponseType.CLAIM_REF_POINTS

            await asyncio.sleep(0.1)

            # Receive the actual worker updates
            resp = Response.model_validate_json(await ws_client.receive_text())
            assert resp.type == ResponseType.PROFILE

            # Send command again
            await ws_client.send_text(ClaimRefPointsCommand().model_dump_json())

            # Receive an error
            resp = Response.model_validate_json(await ws_client.receive_text())
            assert resp.type == ResponseType.ERROR
            assert resp.response.message == ErrorCode.COOLDOWN_ERROR

    async def test__with_zero_ref_points__error(
        self,
        client: TestClient,
        profile_repo: ProfileRepository,
        action_repo: ActionRepository,
        redis_manager: RedisManager,
        valid_token_and_auth_user_uuid: Tuple[str, uuid.UUID],
        valid_connection_state: str,
    ) -> None:
        valid_token, auth_user_uuid = valid_token_and_auth_user_uuid

        async with client.websocket_connect(
            f"/ws/{valid_connection_state}"
        ) as ws_client:
            # Authenticate a new user
            await ws_client.send_text(
                AuthCommand(
                    payload=AuthCommandPayload(token=valid_token, ref_code=None)
                ).model_dump_json()
            )

            await asyncio.sleep(0.1)

            # Receive profile response
            resp = Response.model_validate_json(await ws_client.receive_text())
            assert resp.type == ResponseType.PROFILE

            # Check if user has no ref points
            assert resp.response.ref_points == 0

            # Send command
            await ws_client.send_text(ClaimRefPointsCommand().model_dump_json())

            # Receive an error
            resp = Response.model_validate_json(await ws_client.receive_text())
            assert resp.type == ResponseType.ERROR
            assert resp.response.message == ErrorCode.COMMAND_EXECUTION_ERROR