package runtimeattachments

import (
	"errors"
	"testing"

	"github.com/stretchr/testify/require"

	"github.com/tidbcloud/AgentTeam/desktop/internal/types"
)

func TestAppendManifestRendersLocalPathsWithoutParsing(t *testing.T) {
	got := AppendManifest("please review", []types.RuntimeAttachment{
		{
			ID:       "att_1",
			Filename: "registrations.xlsx",
			MIME:     "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
			Path:     "/tmp/registrations.xlsx",
			URI:      "file:///tmp/registrations.xlsx",
			Size:     123,
		},
	})

	require.Contains(t, got, "please review")
	require.Contains(t, got, "AgentTeam has not parsed their contents")
	require.Contains(t, got, "path: /tmp/registrations.xlsx")
	require.Contains(t, got, "content type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
}

func TestAppendPreparationFailureRendersVisibleError(t *testing.T) {
	got := AppendPreparationFailure("please review", []types.MessageAttachment{
		{ID: "att_1", Filename: "registrations.xlsx", ContentType: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", SizeBytes: 123},
	}, errors.New("attachment too large"))

	require.Contains(t, got, "please review")
	require.Contains(t, got, "Attachment preparation failed")
	require.Contains(t, got, "attachment too large")
	require.Contains(t, got, "registrations.xlsx")
	require.Contains(t, got, "content type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
	require.Contains(t, got, "size: 123 bytes")
}

func TestAttachmentMetadataUsesPortableExtensions(t *testing.T) {
	require.Equal(t, "ext:.csv", MetadataSummary("registrations.csv", "", -1))
	require.Equal(t, "ext:.bin, mime:application/octet-stream, size:3 bytes", MetadataSummary("", "application/octet-stream", 3))
	require.Equal(t, "text/csv; charset=utf-8", ContentTypeForPath("/tmp/registrations.csv"))
	require.Equal(t, ".xlsx", ExtensionFromContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"))
	require.Equal(t, ".svg", ExtensionFromContentType("image/svg+xml"))
}

func TestCacheFilenameSanitizesAttachmentID(t *testing.T) {
	require.Equal(t, "att-unsafe-id.xlsx", CacheFilename(" att/unsafe:id ", "registrations.xlsx", ""))
	require.Equal(t, "attachment.png", CacheFilename("", "", "image/png"))
}
