from src.core.canonical_command import STUB_REQUEST_TYPES, command_hash
from src.domain.cloud.service import CloudInternalService
from src.domain.integration_events.schemas import CommandAuthorizeRequest
from unittest.mock import AsyncMock
import pytest


@pytest.mark.asyncio
async def test_authorize_command_denies_stub_trio():
    svc = CloudInternalService(
        project_repo=AsyncMock(),
        workspace_repo=AsyncMock(),
        settings_repo=AsyncMock(),
        subscription_repo=AsyncMock(),
        usage_ledger=AsyncMock(),
        storage_usage_repo=AsyncMock(),
        connector_repo=AsyncMock(),
    )
    for cmd in STUB_REQUEST_TYPES:
        out = await svc.authorize_command(
            CommandAuthorizeRequest(
                workspace_id="00000000-0000-0000-0000-000000000001",
                project_id="00000000-0000-0000-0000-000000000002",
                command=cmd,
                command_id="cmd_1",
                idempotency_key="idem_1",
                connection_id="00000000-0000-0000-0000-000000000003",
                source_provider="make",
                request_id="req_1",
                correlation_id="cor_1",
            )
        )
        assert out.allowed is False
        assert out.reason_code == "stub_request_type"


def test_sensitive_hash_helper():
    payload = {"command": "codegen.deploy", "resource": {}, "input": {"x": 1}}
    assert len(command_hash(payload)) == 64
