"""Tests for correlation resolution in outbound dispatch."""

import asyncio
import pytest
from unittest.mock import AsyncMock, MagicMock
from nanobot.bus.queue import MessageBus
from nanobot.bus.events import OutboundMessage


@pytest.mark.asyncio
async def test_dispatch_resolves_correlation_before_channel_send():
    """Correlation Future should be resolved when outbound message is dispatched."""
    bus = MessageBus()
    future = bus.register_correlation("corr-1")

    msg = OutboundMessage(channel="telegram", chat_id="123", content="response", metadata={"correlation_id": "corr-1"})
    await bus.publish_outbound(msg)

    # Simulate what _dispatch_outbound does: consume + resolve
    consumed = await asyncio.wait_for(bus.consume_outbound(), timeout=1.0)
    bus.resolve_correlation(consumed)

    assert future.done()
    assert future.result() == "response"
