"""Terminal quick commands API — custom cmd stored without trailing CR."""

import pytest


@pytest.mark.integration
@pytest.mark.asyncio
async def test_create_quick_command_stores_trimmed_cmd_without_cr(
    integration_env, api_client, auth_headers
):
    """Frontend save uses cmd.trim() — store body only; CR is send-time in execQuickCmd."""
    name = "verify-enter-fix"
    cmd_body = "echo __nexus_qc_enter__"

    create = await api_client.post(
        "/api/terminal/quick-commands",
        headers=auth_headers,
        json={"name": name, "cmd": cmd_body},
    )
    assert create.status_code == 201, create.text
    created = create.json()
    cmd_id = created["id"]
    assert created["cmd"] == cmd_body
    assert not created["cmd"].endswith("\r")

    listed = await api_client.get("/api/terminal/quick-commands", headers=auth_headers)
    assert listed.status_code == 200
    match = next((c for c in listed.json() if c["id"] == cmd_id), None)
    assert match is not None
    assert match["cmd"] == cmd_body

    delete = await api_client.delete(
        f"/api/terminal/quick-commands/{cmd_id}",
        headers=auth_headers,
    )
    assert delete.status_code == 204
