package queryspec

import (
	"context"
	"testing"
)

type neutralCallerKey struct{}

func TestAggregateFetchLimitContextRoundTripIsNeutral(t *testing.T) {
	base := context.Background()
	if got := AggregateFetchLimitFromContext(base); got != 0 {
		t.Fatalf("empty context limit = %d, want 0", got)
	}
	if got := WithAggregateFetchLimit(base, 0); got != base {
		t.Fatal("non-positive limit should not allocate a new context")
	}
	if got := WithAggregateFetchLimit(base, -1); got != base {
		t.Fatal("negative limit should not allocate a new context")
	}

	ctx := WithAggregateFetchLimit(base, 37)
	if got := AggregateFetchLimitFromContext(ctx); got != 37 {
		t.Fatalf("round-trip limit = %d, want 37", got)
	}

	otherPackCtx := context.WithValue(ctx, neutralCallerKey{}, "inventory-pack")
	if got := AggregateFetchLimitFromContext(otherPackCtx); got != 37 {
		t.Fatalf("limit from other-pack caller context = %d, want 37", got)
	}
}
