package config_test

import (
	"math"
	"strings"
	"testing"
	"time"

	"iop/packages/go/config"
	"iop/packages/go/execution"
)

func TestNodeProviderResponseStallTimeoutValidation(t *testing.T) {
	for _, tc := range []struct {
		name string
		raw  int64
		want int64
		bad  bool
	}{
		{name: "omitted defaults", want: execution.DefaultResponseStallTimeoutMS},
		{name: "positive preserved", raw: 60000, want: 60000},
		{name: "exact safe boundary preserved", raw: math.MaxInt64 / int64(time.Millisecond), want: math.MaxInt64 / int64(time.Millisecond)},
		{name: "first overflowing millisecond rejected", raw: math.MaxInt64/int64(time.Millisecond) + 1, bad: true},
		{name: "negative rejected", raw: -1, bad: true},
		{name: "overflow rejected", raw: 99999999999999, bad: true},
	} {
		t.Run(tc.name, func(t *testing.T) {
			provider := config.NodeProviderConf{ID: "p1", Type: "vllm", Category: config.CategoryAPI, Models: []string{"m"}, ResponseStallTimeoutMS: tc.raw}
			err := provider.Validate()
			if (err != nil) != tc.bad {
				t.Fatalf("Validate() error = %v, want bad=%t", err, tc.bad)
			}
			if tc.bad {
				if !strings.Contains(err.Error(), "response_stall_timeout_ms") {
					t.Fatalf("error = %q", err)
				}
				return
			}
			if got := provider.EffectiveResponseStallTimeoutMS(); got != tc.want {
				t.Errorf("effective timeout = %d, want %d", got, tc.want)
			}
		})
	}
}
