package common

import (
	"errors"
	"testing"
)

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

	testCases := map[string]struct {
		err  error
		want bool
	}{
		"classifies textconsumer error as transient": {
			err:  errors.New("TextConsumer read message failed"),
			want: true,
		},
		"classifies [503] signature as transient": {
			err:  errors.New("request failed with [503]"),
			want: true,
		},
		"classifies json code 401 as transient": {
			err:  errors.New(`request failed: {"code":401,"message":"unauthorized"}`),
			want: true,
		},
		"classifies ring-balancer failure as transient": {
			err:  errors.New("ring-balancer failed to pick a connection"),
			want: true,
		},
		"classifies invalid checkpoint tso as non-transient": {
			err:  errors.New("invalid checkpoint tso"),
			want: false,
		},
	}

	for name, tc := range testCases {
		tc := tc
		t.Run(name, func(t *testing.T) {
			t.Parallel()
			got := isTransientGetChangefeedError(tc.err)
			if got != tc.want {
				t.Fatalf("isTransientGetChangefeedError(%q) = %v, want %v", tc.err.Error(), got, tc.want)
			}
		})
	}
}
