"""Tests for the /compact command.

/compact must send a typed ``{"type": "compact"}`` control message — not a chat
message — so the backend runs the real context compressor instead of a full
agent turn that merely produces summary text.
"""

from __future__ import annotations

import asyncio

from clients.terminal.tui.commands.builtin import CompactCommand
from clients.terminal.tui.context import TuiContext


class _FakeWsClient:
    def __init__(self) -> None:
        self.enqueued: list = []

    def enqueue(self, content) -> None:
        self.enqueued.append(content)


def test_compact_enqueues_typed_control_message() -> None:
    ws = _FakeWsClient()
    ctx = TuiContext(session_id="s", ws_client=ws)  # type: ignore[arg-type]

    asyncio.run(CompactCommand().execute(ctx, ""))

    assert ws.enqueued == [{"type": "compact"}]


def test_compact_no_ws_client_is_noop() -> None:
    ctx = TuiContext(session_id="s", ws_client=None)
    # Must not raise when there is no ws_client (e.g. session not attached yet).
    asyncio.run(CompactCommand().execute(ctx, ""))