package agent

import (
	"fmt"
	"net/http"
	"testing"

	"github.com/stretchr/testify/require"

	"github.com/tidbcloud/AgentTeam/internal/store/taskboard"
	"github.com/tidbcloud/AgentTeam/internal/workspace/owner"
)

func TestHTTPStatusMapsOwnerMismatch(t *testing.T) {
	t.Parallel()

	require.Equal(t, http.StatusMisdirectedRequest, HTTPStatus(owner.ErrOwnerMismatch))
}

func TestHTTPStatusMapsTaskForbidden(t *testing.T) {
	t.Parallel()

	require.Equal(t, http.StatusForbidden, HTTPStatus(taskboard.ErrForbidden))
}

func TestInvalidChannelTargetMapsToStableCode(t *testing.T) {
	t.Parallel()

	err := fmt.Errorf("parse channel: %w", taskboard.ErrInvalidChannelTarget)

	// 400 status, and a distinct envelope code the exec client can key a hint on
	// without guessing from HTTP status or parsing the raw message.
	require.Equal(t, http.StatusBadRequest, HTTPStatus(err))
	require.Equal(t, taskboard.InvalidChannelTargetCode, agentErrorFor(err).Code)

	// A generic bad request must not carry the channel-target code.
	generic := fmt.Errorf("other: %w", taskboard.ErrBadRequest)
	require.NotEqual(t, taskboard.InvalidChannelTargetCode, agentErrorFor(generic).Code)
}
