// SPDX-FileCopyrightText: Amolith <amolith@secluded.site>
//
// SPDX-License-Identifier: LicenseRef-MutuaL-1.2

package mcp

import (
	"encoding/json"
	"testing"

	sdk "github.com/modelcontextprotocol/go-sdk/mcp"
)

func requireTextContent(t *testing.T, result *sdk.CallToolResult) string {
	t.Helper()

	if len(result.Content) != 1 {
		t.Fatalf("text content length = %d, want 1", len(result.Content))
	}
	textContent, ok := result.Content[0].(*sdk.TextContent)
	if !ok {
		t.Fatalf("content type = %T, want *sdk.TextContent", result.Content[0])
	}

	return textContent.Text
}

func requireStructuredContent(t *testing.T, result *sdk.CallToolResult, want any) {
	t.Helper()
	// tools.RESPONSES.5

	if result == nil {
		t.Fatal("result is nil")
	}
	if result.StructuredContent == nil {
		t.Fatal("structured content is nil")
	}

	gotJSON, err := json.Marshal(result.StructuredContent)
	if err != nil {
		t.Fatalf("marshal structured content: %v", err)
	}
	wantJSON, err := json.Marshal(want)
	if err != nil {
		t.Fatalf("marshal wanted structured content: %v", err)
	}
	if string(gotJSON) != string(wantJSON) {
		t.Fatalf("structured content = %s, want %s", gotJSON, wantJSON)
	}

	var object map[string]any
	if err := json.Unmarshal(gotJSON, &object); err != nil {
		t.Fatalf("structured content is not a JSON object: %v", err)
	}
}
