package main

import (
	"bytes"
	"strings"
	"testing"

	proto "evening_detective_server/proto"

	"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
	"google.golang.org/genproto/googleapis/api/httpbody"
)

func newTestRawBodyMarshaler() *rawBodyMarshaler {
	return &rawBodyMarshaler{
		Marshaler: &runtime.HTTPBodyMarshaler{
			Marshaler: &runtime.JSONPb{},
		},
	}
}

func TestRawBodyDecoderDecodesHttpBody(t *testing.T) {
	m := newTestRawBodyMarshaler()
	raw := []byte("PK\x03\x04fake-zip-bytes")

	protoReq := &httpbody.HttpBody{}
	if err := m.NewDecoder(bytes.NewReader(raw)).Decode(protoReq); err != nil {
		t.Fatalf("Decode: %v", err)
	}
	if !bytes.Equal(protoReq.Data, raw) {
		t.Errorf("Data = %q, want %q", protoReq.Data, raw)
	}
}

func TestRawBodyMarshalerDelegatesToJSONForProtos(t *testing.T) {
	m := newTestRawBodyMarshaler()

	// Для protobuf-сообщения (не HttpBody) декодирование идёт через JSON.
	req := &proto.EchoReq{}
	if err := m.NewDecoder(strings.NewReader(`{"text":"hi"}`)).Decode(req); err != nil {
		t.Fatalf("Decode: %v", err)
	}
	if req.Text != "hi" {
		t.Errorf("Text = %q, want hi", req.Text)
	}
}
