// Code generated by cmd/genapi. DO NOT EDIT.

//go:build !ignore_autogenerated

package api

import (
	"bytes"
	"context"
	"errors"
	"io"
	"net/http"
	"strings"
	"testing"

	"github.com/lukaszraczylo/go-telegram/client"
	"github.com/stretchr/testify/mock"
	"github.com/stretchr/testify/require"
)

// genTestMockDoer is a testify-mock HTTPDoer used by generated tests only.
type genTestMockDoer struct{ mock.Mock }

func (m *genTestMockDoer) Do(r *http.Request) (*http.Response, error) {
	args := m.Called(r)
	if v := args.Get(0); v != nil {
		return v.(*http.Response), args.Error(1)
	}
	return nil, args.Error(1)
}

func genTestResp(status int, body string) *http.Response {
	return &http.Response{
		StatusCode: status,
		Body:       io.NopCloser(bytes.NewBufferString(body)),
		Header:     http.Header{"Content-Type": []string{"application/json"}},
	}
}

{{range .Methods}}{{$m := .}}{{$mName := title .Name}}{{$mWire := .Name}}
func Test_{{$mName}}_Success(t *testing.T) {
	m := &genTestMockDoer{}
	m.On("Do", mock.MatchedBy(func(r *http.Request) bool {
		return strings.HasSuffix(r.URL.Path, "/{{$mWire}}")
	})).Return(genTestResp(200, {{successResp $m}}), nil)

	bot := client.New("test:token", client.WithHTTPClient(m))
	{{- if .Params}}
	params := &{{$mName}}Params{
		{{- range .Params}}{{if .Required}}
		{{.Name}}: {{sentinelValue $m.Name .}},{{end}}
		{{- end}}
	}
	_, err := {{$mName}}(context.Background(), bot, params)
	{{- else}}
	_, err := {{$mName}}(context.Background(), bot, &{{$mName}}Params{})
	{{- end}}
	require.NoError(t, err)
}

func Test_{{$mName}}_APIError(t *testing.T) {
	m := &genTestMockDoer{}
	m.On("Do", mock.Anything).Return(
		genTestResp(200, `{"ok":false,"error_code":429,"description":"Too Many Requests","parameters":{"retry_after":1}}`), nil)

	bot := client.New("test:token", client.WithHTTPClient(m))
	{{- if .Params}}
	params := &{{$mName}}Params{
		{{- range .Params}}{{if .Required}}
		{{.Name}}: {{sentinelValue $m.Name .}},{{end}}
		{{- end}}
	}
	_, err := {{$mName}}(context.Background(), bot, params)
	{{- else}}
	_, err := {{$mName}}(context.Background(), bot, &{{$mName}}Params{})
	{{- end}}
	require.Error(t, err)
	var ae *client.APIError
	require.ErrorAs(t, err, &ae)
	require.Equal(t, 429, ae.Code)
	require.True(t, ae.IsRetryable())
}

func Test_{{$mName}}_NetworkError(t *testing.T) {
	m := &genTestMockDoer{}
	m.On("Do", mock.Anything).Return(nil, errors.New("dial tcp: timeout"))

	bot := client.New("test:token", client.WithHTTPClient(m))
	{{- if .Params}}
	params := &{{$mName}}Params{
		{{- range .Params}}{{if .Required}}
		{{.Name}}: {{sentinelValue $m.Name .}},{{end}}
		{{- end}}
	}
	_, err := {{$mName}}(context.Background(), bot, params)
	{{- else}}
	_, err := {{$mName}}(context.Background(), bot, &{{$mName}}Params{})
	{{- end}}
	require.Error(t, err)
	var ne *client.NetworkError
	require.ErrorAs(t, err, &ne)
}

func Test_{{$mName}}_ParseError(t *testing.T) {
	m := &genTestMockDoer{}
	m.On("Do", mock.Anything).Return(genTestResp(200, `not json`), nil)

	bot := client.New("test:token", client.WithHTTPClient(m))
	{{- if .Params}}
	params := &{{$mName}}Params{
		{{- range .Params}}{{if .Required}}
		{{.Name}}: {{sentinelValue $m.Name .}},{{end}}
		{{- end}}
	}
	_, err := {{$mName}}(context.Background(), bot, params)
	{{- else}}
	_, err := {{$mName}}(context.Background(), bot, &{{$mName}}Params{})
	{{- end}}
	require.Error(t, err)
	var pe *client.ParseError
	require.ErrorAs(t, err, &pe)
}

func Test_{{$mName}}_ContextCanceled(t *testing.T) {
	m := &genTestMockDoer{}
	m.On("Do", mock.Anything).Return(nil, context.Canceled).Maybe()

	ctx, cancel := context.WithCancel(context.Background())
	cancel()

	bot := client.New("test:token", client.WithHTTPClient(m))
	{{- if .Params}}
	params := &{{$mName}}Params{
		{{- range .Params}}{{if .Required}}
		{{.Name}}: {{sentinelValue $m.Name .}},{{end}}
		{{- end}}
	}
	_, err := {{$mName}}(ctx, bot, params)
	{{- else}}
	_, err := {{$mName}}(ctx, bot, &{{$mName}}Params{})
	{{- end}}
	require.Error(t, err)
	require.ErrorIs(t, err, context.Canceled)
}

// Test_{{$mName}}_MissingRequiredFields exercises Telegram's server-side
// validation: when a required field is omitted, Telegram returns 400 with
// a description like "Bad Request: <field> is empty". The library must
// surface this as *APIError with the ErrBadRequest sentinel.
func Test_{{$mName}}_MissingRequiredFields(t *testing.T) {
	m := &genTestMockDoer{}
	m.On("Do", mock.Anything).Return(
		genTestResp(200, `{"ok":false,"error_code":400,"description":"Bad Request: chat_id is empty"}`), nil)

	bot := client.New("test:token", client.WithHTTPClient(m))
	// Send a Params with all required fields zeroed — simulates a caller
	// that forgot to populate them. The bot library marshals as-is and
	// surfaces Telegram's 400 reply.
	_, err := {{$mName}}(context.Background(), bot, &{{$mName}}Params{})
	require.Error(t, err)
	var ae *client.APIError
	require.ErrorAs(t, err, &ae)
	require.Equal(t, 400, ae.Code)
	require.True(t, errors.Is(err, client.ErrBadRequest))
	require.False(t, ae.IsRetryable())
}

// Test_{{$mName}}_Forbidden exercises the 403 path (bot blocked by user,
// removed from chat, etc.). The library must surface the ErrForbidden
// sentinel.
func Test_{{$mName}}_Forbidden(t *testing.T) {
	m := &genTestMockDoer{}
	m.On("Do", mock.Anything).Return(
		genTestResp(200, `{"ok":false,"error_code":403,"description":"Forbidden: bot was blocked by the user"}`), nil)

	bot := client.New("test:token", client.WithHTTPClient(m))
	{{- if .Params}}
	params := &{{$mName}}Params{
		{{- range .Params}}{{if .Required}}
		{{.Name}}: {{sentinelValue $m.Name .}},{{end}}
		{{- end}}
	}
	_, err := {{$mName}}(context.Background(), bot, params)
	{{- else}}
	_, err := {{$mName}}(context.Background(), bot, &{{$mName}}Params{})
	{{- end}}
	require.Error(t, err)
	var ae *client.APIError
	require.ErrorAs(t, err, &ae)
	require.Equal(t, 403, ae.Code)
	require.True(t, errors.Is(err, client.ErrForbidden))
	require.False(t, ae.IsRetryable())
}

// Test_{{$mName}}_ServerError exercises the 5xx path. The library must
// classify these as retryable so RetryDoer / user retry logic kicks in.
func Test_{{$mName}}_ServerError(t *testing.T) {
	m := &genTestMockDoer{}
	m.On("Do", mock.Anything).Return(
		genTestResp(200, `{"ok":false,"error_code":500,"description":"Internal server error"}`), nil)

	bot := client.New("test:token", client.WithHTTPClient(m))
	{{- if .Params}}
	params := &{{$mName}}Params{
		{{- range .Params}}{{if .Required}}
		{{.Name}}: {{sentinelValue $m.Name .}},{{end}}
		{{- end}}
	}
	_, err := {{$mName}}(context.Background(), bot, params)
	{{- else}}
	_, err := {{$mName}}(context.Background(), bot, &{{$mName}}Params{})
	{{- end}}
	require.Error(t, err)
	var ae *client.APIError
	require.ErrorAs(t, err, &ae)
	require.Equal(t, 500, ae.Code)
	require.True(t, ae.IsRetryable(), "5xx must be retryable")
}

{{end}}
