package httpapi

import (
	"net/http/httptest"
	"testing"
)

func TestOptionalTaskIdempotencyKeyRejectsMultipleValues(t *testing.T) {
	t.Parallel()
	request := httptest.NewRequest("POST", "/", nil)
	if _, present, err := optionalTaskIdempotencyKey(request); err != nil || present {
		t.Fatalf("missing key present=%v err=%v", present, err)
	}
	request.Header.Add("Idempotency-Key", "one")
	request.Header.Add("Idempotency-Key", "two")
	if _, _, err := optionalTaskIdempotencyKey(request); err == nil {
		t.Fatal("multiple keys must be rejected")
	}
}

func TestTaskIdempotencyRequestHashIsCanonical(t *testing.T) {
	t.Parallel()
	first := map[string]any{"model": "m", "n": float64(1), "nested": map[string]any{"b": true, "a": "x"}}
	second := map[string]any{"nested": map[string]any{"a": "x", "b": true}, "n": float64(1), "model": "m"}
	if taskIdempotencyRequestHash("images.generations", false, false, first) != taskIdempotencyRequestHash("images.generations", false, false, second) {
		t.Fatal("equivalent JSON objects must have the same request hash")
	}
	if taskIdempotencyRequestHash("images.generations", true, false, first) == taskIdempotencyRequestHash("images.generations", false, false, first) {
		t.Fatal("async response semantics must be part of the request hash")
	}
}
