package openai_compat

import (
	"context"
	"encoding/json"
	"fmt"
	"net/http"
	"net/http/httptest"
	"strings"
	"testing"

	"go.uber.org/zap"

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

func TestOpenAICompatExecuteStreamsDeltasAndFinishReason(t *testing.T) {
	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.URL.Path != "/v1/chat/completions" {
			t.Fatalf("unexpected path %s", r.URL.Path)
		}
		w.Header().Set("Content-Type", "text/event-stream")
		_, _ = fmt.Fprintf(w, "data: %s\n\n", `{"choices":[{"delta":{"reasoning_content":"think "}}]}`)
		_, _ = fmt.Fprintf(w, "data: %s\n\n", `{"choices":[{"delta":{"content":"hello "}}]}`)
		_, _ = fmt.Fprintf(w, "data: %s\n\n", `{"choices":[{"delta":{"content":"world"},"finish_reason":"stop"}],"usage":{"prompt_tokens":5,"completion_tokens":2}}`)
		_, _ = fmt.Fprintf(w, "data: [DONE]\n\n")
	}))
	defer server.Close()

	adapter := New(config.OpenAICompatConf{Endpoint: server.URL}, zap.NewNop())
	sink := &fakeSink{}
	err := adapter.Execute(context.Background(), runtime.ExecutionSpec{
		RunID:  "run-1",
		Target: "lemonade-model",
		Input:  map[string]any{"prompt": "say hello"},
	}, sink)
	if err != nil {
		t.Fatalf("Execute failed: %v", err)
	}

	events := sink.all()
	// start + reasoning_delta + delta + delta + complete
	if len(events) != 5 {
		t.Fatalf("expected 5 events, got %d: %+v", len(events), events)
	}
	if events[0].Type != runtime.EventTypeStart {
		t.Fatalf("expected start, got %+v", events[0])
	}
	if events[1].Type != runtime.EventTypeReasoningDelta || events[1].Delta != "think " {
		t.Fatalf("expected reasoning delta, got %+v", events[1])
	}
	if events[2].Delta+events[3].Delta != "hello world" {
		t.Fatalf("unexpected deltas: %q + %q", events[2].Delta, events[3].Delta)
	}
	complete := events[4]
	if complete.Type != runtime.EventTypeComplete {
		t.Fatalf("expected complete, got %+v", complete)
	}
	if complete.Metadata["finish_reason"] != "stop" {
		t.Fatalf("expected finish_reason stop, got %+v", complete.Metadata)
	}
	if complete.Usage == nil || complete.Usage.OutputTokens != 2 || complete.Usage.InputTokens != 5 {
		t.Fatalf("expected usage from chunk, got %+v", complete.Usage)
	}
}

func TestProtocolProfileOpenAICompatExecuteMappedRequest(t *testing.T) {
	requests := 0
	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		requests++
		if r.RequestURI != "/v1/chat/completions" {
			t.Errorf("RequestURI = %q", r.RequestURI)
		}
		if got := r.Header.Get("Authorization"); got != "Bearer request-secret" {
			t.Errorf("Authorization = %q", got)
		}
		var body map[string]any
		if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
			t.Fatalf("decode body: %v", err)
		}
		if got := body["model"]; got != "upstream-model" {
			t.Errorf("model = %v, want upstream-model", got)
		}
		w.Header().Set("Content-Type", "text/event-stream")
		_, _ = fmt.Fprint(w, "data: {\"choices\":[{\"delta\":{\"content\":\"ok\"}}]}\n\ndata: [DONE]\n\n")
	}))
	defer server.Close()
	profile, err := config.ResolveProtocolProfile("openai", "", config.BuiltInProtocolProfileCatalog())
	if err != nil {
		t.Fatalf("ResolveProtocolProfile: %v", err)
	}
	profile.BaseURL = server.URL + "/v1"
	profile.ModelMapping = map[string]string{"canonical-model": "upstream-model"}
	adapter := New(config.OpenAICompatConf{
		Headers: map[string]string{"Authorization": "request-secret"}, RuntimeProfile: &profile,
	}, zap.NewNop())
	if err := adapter.Execute(context.Background(), runtime.ExecutionSpec{
		RunID: "profile-execute", Target: "canonical-model", Input: map[string]any{"prompt": "hello"},
	}, &fakeSink{}); err != nil {
		t.Fatalf("Execute: %v", err)
	}
	if requests != 1 {
		t.Fatalf("requests = %d, want 1", requests)
	}
}

func TestOpenAICompatExecuteParsesReasoningAndCachedInputTokens(t *testing.T) {
	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
		w.Header().Set("Content-Type", "text/event-stream")
		_, _ = fmt.Fprintf(w, "data: %s\n\n", `{"choices":[{"delta":{"content":"hi"},"finish_reason":"stop"}],"usage":{"prompt_tokens":12,"completion_tokens":7,"prompt_tokens_details":{"cached_tokens":4},"completion_tokens_details":{"reasoning_tokens":3}}}`)
		_, _ = fmt.Fprintf(w, "data: [DONE]\n\n")
	}))
	defer server.Close()

	adapter := New(config.OpenAICompatConf{Endpoint: server.URL}, zap.NewNop())
	sink := &fakeSink{}
	if err := adapter.Execute(context.Background(), runtime.ExecutionSpec{
		RunID:  "run-1",
		Target: "qwen-model",
		Input:  map[string]any{"prompt": "hi"},
	}, sink); err != nil {
		t.Fatalf("Execute failed: %v", err)
	}

	events := sink.all()
	complete := events[len(events)-1]
	if complete.Type != runtime.EventTypeComplete || complete.Usage == nil {
		t.Fatalf("expected complete event with usage, got %+v", complete)
	}
	u := complete.Usage
	if u.InputTokens != 12 || u.OutputTokens != 7 {
		t.Fatalf("input/output tokens: got %d/%d, want 12/7", u.InputTokens, u.OutputTokens)
	}
	if u.CachedInputTokens != 4 {
		t.Fatalf("cached_input_tokens: got %d, want 4", u.CachedInputTokens)
	}
	if u.ReasoningTokens != 3 {
		t.Fatalf("reasoning_tokens: got %d, want 3", u.ReasoningTokens)
	}
}

func TestOpenAICompatExecuteStreamsReasoningAlias(t *testing.T) {
	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.URL.Path != "/v1/chat/completions" {
			t.Fatalf("unexpected path %s", r.URL.Path)
		}
		w.Header().Set("Content-Type", "text/event-stream")
		_, _ = fmt.Fprintf(w, "data: %s\n\n", `{"choices":[{"delta":{"reasoning":"think alias "}}]}`)
		_, _ = fmt.Fprintf(w, "data: %s\n\n", `{"choices":[{"delta":{"content":"answer"}}]}`)
		_, _ = fmt.Fprintf(w, "data: [DONE]\n\n")
	}))
	defer server.Close()

	adapter := New(config.OpenAICompatConf{Endpoint: server.URL}, zap.NewNop())
	sink := &fakeSink{}
	err := adapter.Execute(context.Background(), runtime.ExecutionSpec{
		RunID:  "run-1",
		Target: "qwen-model",
		Input:  map[string]any{"prompt": "say hello"},
	}, sink)
	if err != nil {
		t.Fatalf("Execute failed: %v", err)
	}

	events := sink.all()
	if len(events) != 4 {
		t.Fatalf("expected 4 events, got %d: %+v", len(events), events)
	}
	if events[1].Type != runtime.EventTypeReasoningDelta || events[1].Delta != "think alias " {
		t.Fatalf("expected reasoning alias delta, got %+v", events[1])
	}
	if events[2].Type != runtime.EventTypeDelta || events[2].Delta != "answer" {
		t.Fatalf("expected answer delta, got %+v", events[2])
	}
}

func TestOpenAICompatExecuteSendsHeaders(t *testing.T) {
	var gotAuth, gotContentType string
	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		gotAuth = r.Header.Get("Authorization")
		gotContentType = r.Header.Get("Content-Type")
		w.Header().Set("Content-Type", "text/event-stream")
		_, _ = fmt.Fprintf(w, "data: %s\n\n", `{"choices":[{"delta":{"content":"ok"}}]}`)
		_, _ = fmt.Fprintf(w, "data: [DONE]\n\n")
	}))
	defer server.Close()

	adapter := New(config.OpenAICompatConf{
		Endpoint: server.URL,
		Headers: map[string]string{
			"Authorization": "Bearer secret-token",
			// A user-provided Content-Type must not override the adapter-owned one.
			"Content-Type": "text/plain",
		},
	}, zap.NewNop())
	sink := &fakeSink{}
	if err := adapter.Execute(context.Background(), runtime.ExecutionSpec{
		RunID:  "run-h",
		Target: "lemonade-model",
		Input:  map[string]any{"prompt": "hi"},
	}, sink); err != nil {
		t.Fatalf("Execute failed: %v", err)
	}
	if gotAuth != "Bearer secret-token" {
		t.Fatalf("expected Authorization header, got %q", gotAuth)
	}
	if gotContentType != "application/json" {
		t.Fatalf("expected adapter-owned content type, got %q", gotContentType)
	}
}

func TestOpenAICompatExecutePassesOptionsAsTopLevelFields(t *testing.T) {
	var body map[string]any
	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
			t.Fatalf("decode: %v", err)
		}
		w.Header().Set("Content-Type", "text/event-stream")
		_, _ = fmt.Fprintf(w, "data: %s\n\n", `{"choices":[{"delta":{"content":"ok"}}]}`)
		_, _ = fmt.Fprintf(w, "data: [DONE]\n\n")
	}))
	defer server.Close()

	adapter := New(config.OpenAICompatConf{Endpoint: server.URL}, zap.NewNop())
	sink := &fakeSink{}
	if err := adapter.Execute(context.Background(), runtime.ExecutionSpec{
		RunID:  "run-o",
		Target: "lemonade-model",
		Input: map[string]any{
			"prompt": "hi",
			"options": map[string]any{
				"temperature": 0.2,
				"max_tokens":  float64(8),
				// adapter-owned fields must not be overridden by options
				"model":  "evil",
				"stream": false,
			},
		},
	}, sink); err != nil {
		t.Fatalf("Execute failed: %v", err)
	}
	if body["temperature"] != 0.2 {
		t.Fatalf("expected temperature top-level, got %v", body["temperature"])
	}
	if body["max_tokens"] != float64(8) {
		t.Fatalf("expected max_tokens top-level, got %v", body["max_tokens"])
	}
	if body["model"] != "lemonade-model" {
		t.Fatalf("options must not override model, got %v", body["model"])
	}
	if body["stream"] != true {
		t.Fatalf("options must not override stream, got %v", body["stream"])
	}
}

func TestOpenAICompatExecutePassesToolsAndToolChoice(t *testing.T) {
	var body map[string]any
	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
			t.Fatalf("decode: %v", err)
		}
		w.Header().Set("Content-Type", "text/event-stream")
		_, _ = fmt.Fprintf(w, "data: %s\n\n", `{"choices":[{"delta":{"content":"ok"}}]}`)
		_, _ = fmt.Fprintf(w, "data: [DONE]\n\n")
	}))
	defer server.Close()

	adapter := New(config.OpenAICompatConf{Endpoint: server.URL}, zap.NewNop())
	sink := &fakeSink{}
	if err := adapter.Execute(context.Background(), runtime.ExecutionSpec{
		RunID:  "run-t",
		Target: "qwen3.6:35b",
		Input: map[string]any{
			"prompt":      "hi",
			"tools":       []any{map[string]any{"type": "function"}},
			"tool_choice": "none",
		},
	}, sink); err != nil {
		t.Fatalf("Execute failed: %v", err)
	}
	if tools, ok := body["tools"].([]any); !ok || len(tools) != 1 {
		t.Fatalf("tools not passed: %+v", body["tools"])
	}
	if body["tool_choice"] != "none" {
		t.Fatalf("tool_choice not passed: %+v", body["tool_choice"])
	}
}

func TestOpenAICompatExecuteRetriesSingleToolWhenAutoUnsupported(t *testing.T) {
	attempts := 0
	var retryBody map[string]any
	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		attempts++
		if attempts == 1 {
			w.WriteHeader(http.StatusBadRequest)
			_, _ = w.Write([]byte(`{"error":{"message":"\"auto\" tool choice requires --enable-auto-tool-choice and --tool-call-parser to be set"}}`))
			return
		}
		if err := json.NewDecoder(r.Body).Decode(&retryBody); err != nil {
			t.Fatalf("decode retry: %v", err)
		}
		w.Header().Set("Content-Type", "text/event-stream")
		_, _ = fmt.Fprintf(w, "data: %s\n\n", `{"choices":[{"delta":{"content":"ok"}}]}`)
		_, _ = fmt.Fprintf(w, "data: [DONE]\n\n")
	}))
	defer server.Close()

	adapter := New(config.OpenAICompatConf{Endpoint: server.URL}, zap.NewNop())
	sink := &fakeSink{}
	if err := adapter.Execute(context.Background(), runtime.ExecutionSpec{
		RunID:  "run-retry",
		Target: "qwen3.6:35b",
		Input: map[string]any{
			"prompt": "status",
			"tools": []any{map[string]any{
				"type": "function",
				"function": map[string]any{
					"name": "run_commands",
				},
			}},
		},
	}, sink); err != nil {
		t.Fatalf("Execute failed: %v", err)
	}
	if attempts != 2 {
		t.Fatalf("attempts: got %d, want 2", attempts)
	}
	choice, ok := retryBody["tool_choice"].(map[string]any)
	if !ok {
		t.Fatalf("retry tool_choice missing: %+v", retryBody)
	}
	fn := choice["function"].(map[string]any)
	if choice["type"] != "function" || fn["name"] != "run_commands" {
		t.Fatalf("retry tool_choice: %+v", choice)
	}
}

func TestOpenAICompatExecuteFallsBackToTextToolsWhenNativeToolsUnsupported(t *testing.T) {
	attempts := 0
	var fallbackBody map[string]any
	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		attempts++
		switch attempts {
		case 1:
			w.WriteHeader(http.StatusBadRequest)
			_, _ = w.Write([]byte(`{"error":{"message":"\"auto\" tool choice requires --enable-auto-tool-choice and --tool-call-parser to be set"}}`))
			return
		case 2:
			w.WriteHeader(http.StatusBadRequest)
			_, _ = w.Write([]byte(`{"error":{"message":"tool_choice=\"function=ChatCompletionNamedFunction(name='run_commands') type='function'\" requires --tool-call-parser to be set"}}`))
			return
		}
		if err := json.NewDecoder(r.Body).Decode(&fallbackBody); err != nil {
			t.Fatalf("decode fallback: %v", err)
		}
		w.Header().Set("Content-Type", "text/event-stream")
		_, _ = fmt.Fprintf(w, "data: %s\n\n", `{"choices":[{"delta":{"content":"<tool_call>\n<function=run_commands>\n<parameter=commands>[\"git status\"]</parameter>\n</function>\n</tool_call>"}}]}`)
		_, _ = fmt.Fprintf(w, "data: [DONE]\n\n")
	}))
	defer server.Close()

	adapter := New(config.OpenAICompatConf{Endpoint: server.URL}, zap.NewNop())
	sink := &fakeSink{}
	if err := adapter.Execute(context.Background(), runtime.ExecutionSpec{
		RunID:  "run-text-fallback",
		Target: "qwen3.6:35b",
		Input: map[string]any{
			"messages": []any{
				map[string]any{"role": "system", "content": "Existing Cline system prompt."},
				map[string]any{"role": "user", "content": "status"},
			},
			"tools": []any{map[string]any{
				"type": "function",
				"function": map[string]any{
					"name": "run_commands",
					"parameters": map[string]any{
						"type": "object",
						"properties": map[string]any{
							"commands": map[string]any{"type": "array", "items": map[string]any{"type": "string"}},
						},
					},
				},
			}},
		},
	}, sink); err != nil {
		t.Fatalf("Execute failed: %v", err)
	}
	if attempts != 3 {
		t.Fatalf("attempts: got %d, want 3", attempts)
	}
	if _, ok := fallbackBody["tools"]; ok {
		t.Fatalf("fallback must omit tools: %+v", fallbackBody["tools"])
	}
	if _, ok := fallbackBody["tool_choice"]; ok {
		t.Fatalf("fallback must omit tool_choice: %+v", fallbackBody["tool_choice"])
	}
	messages := fallbackBody["messages"].([]any)
	first := messages[0].(map[string]any)
	if first["role"] != "system" || !strings.Contains(first["content"].(string), "<tool_call>") || !strings.Contains(first["content"].(string), "run_commands") {
		t.Fatalf("fallback system instruction missing tool format/name: %+v", first)
	}
	if !strings.Contains(first["content"].(string), "Existing Cline system prompt.") {
		t.Fatalf("fallback system instruction did not preserve existing system content: %+v", first)
	}
	if len(messages) != 2 || messages[1].(map[string]any)["role"] != "user" {
		t.Fatalf("fallback should keep a single leading system message: %+v", messages)
	}
	events := sink.all()
	complete := events[len(events)-1]
	if complete.Metadata[runtimeMetadataOpenAITextToolFallback] != "true" {
		t.Fatalf("fallback metadata missing: %+v", complete.Metadata)
	}
}

func TestOpenAICompatExecutePreservesToolCallMessages(t *testing.T) {
	var body map[string]any
	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
			t.Fatalf("decode: %v", err)
		}
		w.Header().Set("Content-Type", "text/event-stream")
		_, _ = fmt.Fprintf(w, "data: %s\n\n", `{"choices":[{"delta":{"content":"ok"}}]}`)
		_, _ = fmt.Fprintf(w, "data: [DONE]\n\n")
	}))
	defer server.Close()

	adapter := New(config.OpenAICompatConf{Endpoint: server.URL}, zap.NewNop())
	sink := &fakeSink{}
	if err := adapter.Execute(context.Background(), runtime.ExecutionSpec{
		RunID:  "run-tool-turn",
		Target: "qwen3.6:35b",
		Input: map[string]any{
			"messages": []any{
				map[string]any{
					"role":    "assistant",
					"content": "",
					"tool_calls": []any{map[string]any{
						"id":   "call_iop_1",
						"type": "function",
						"function": map[string]any{
							"name":      "run_commands",
							"arguments": `{"commands":["git status"]}`,
						},
					}},
				},
				map[string]any{
					"role":         "tool",
					"content":      "clean",
					"tool_call_id": "call_iop_1",
				},
			},
		},
	}, sink); err != nil {
		t.Fatalf("Execute failed: %v", err)
	}
	messages, ok := body["messages"].([]any)
	if !ok || len(messages) != 2 {
		t.Fatalf("messages not preserved: %+v", body["messages"])
	}
	assistantMsg := messages[0].(map[string]any)
	if calls, ok := assistantMsg["tool_calls"].([]any); !ok || len(calls) != 1 {
		t.Fatalf("assistant tool_calls not preserved: %+v", assistantMsg)
	}
	toolMsg := messages[1].(map[string]any)
	if toolMsg["tool_call_id"] != "call_iop_1" {
		t.Fatalf("tool_call_id not preserved: %+v", toolMsg)
	}
}

func TestOpenAICompatExecutePreservesNativeToolCalls(t *testing.T) {
	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
		w.Header().Set("Content-Type", "text/event-stream")
		_, _ = fmt.Fprintf(w, "data: %s\n\n", `{"choices":[{"delta":{"tool_calls":[{"index":0,"id":"call_1","type":"function","function":{"name":"run_commands","arguments":"{\"commands\":"}}]}}]}`)
		_, _ = fmt.Fprintf(w, "data: %s\n\n", `{"choices":[{"delta":{"tool_calls":[{"index":0,"function":{"arguments":"[\"git status\"]}"}}]},"finish_reason":"tool_calls"}]}`)
		_, _ = fmt.Fprintf(w, "data: [DONE]\n\n")
	}))
	defer server.Close()

	adapter := New(config.OpenAICompatConf{Endpoint: server.URL}, zap.NewNop())
	sink := &fakeSink{}
	if err := adapter.Execute(context.Background(), runtime.ExecutionSpec{
		RunID:  "run-native-tools",
		Target: "qwen3.6:35b",
		Input:  map[string]any{"prompt": "status"},
	}, sink); err != nil {
		t.Fatalf("Execute failed: %v", err)
	}
	events := sink.all()
	complete := events[len(events)-1]
	if complete.Metadata["finish_reason"] != "tool_calls" {
		t.Fatalf("finish_reason: %+v", complete.Metadata)
	}
	var toolCalls []map[string]any
	if err := json.Unmarshal([]byte(complete.Metadata[runtimeMetadataOpenAIToolCalls]), &toolCalls); err != nil {
		t.Fatalf("tool_calls metadata JSON: %v", err)
	}
	if len(toolCalls) != 1 || toolCalls[0]["id"] != "call_1" {
		t.Fatalf("tool_calls: %+v", toolCalls)
	}
	fn := toolCalls[0]["function"].(map[string]any)
	if fn["name"] != "run_commands" || fn["arguments"] != `{"commands":["git status"]}` {
		t.Fatalf("function: %+v", fn)
	}
}

func TestOpenAICompatExecuteUsesInputModelWhenTargetEmpty(t *testing.T) {
	var body map[string]any
	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
			t.Fatalf("decode: %v", err)
		}
		w.Header().Set("Content-Type", "text/event-stream")
		_, _ = fmt.Fprintf(w, "data: %s\n\n", `{"choices":[{"delta":{"content":"ok"}}]}`)
		_, _ = fmt.Fprintf(w, "data: [DONE]\n\n")
	}))
	defer server.Close()

	adapter := New(config.OpenAICompatConf{Endpoint: server.URL}, zap.NewNop())
	sink := &fakeSink{}
	if err := adapter.Execute(context.Background(), runtime.ExecutionSpec{
		RunID:  "run-fallback",
		Target: "",
		Input: map[string]any{
			"prompt": "hi",
			"model":  "fallback-model",
		},
	}, sink); err != nil {
		t.Fatalf("Execute failed: %v", err)
	}
	if body["model"] != "fallback-model" {
		t.Fatalf("expected fallback model, got %v", body["model"])
	}
}

func TestOpenAICompatExecuteRejectsEmptyEndpointOrModel(t *testing.T) {
	t.Run("empty_endpoint", func(t *testing.T) {
		adapter := New(config.OpenAICompatConf{}, zap.NewNop())
		err := adapter.Execute(context.Background(), runtime.ExecutionSpec{
			RunID: "r", Target: "m", Input: map[string]any{"prompt": "hi"},
		}, &fakeSink{})
		if err == nil || !strings.Contains(err.Error(), "endpoint is required") {
			t.Fatalf("expected endpoint error, got %v", err)
		}
	})

	t.Run("empty_model", func(t *testing.T) {
		adapter := New(config.OpenAICompatConf{Endpoint: "http://localhost:8000"}, zap.NewNop())
		err := adapter.Execute(context.Background(), runtime.ExecutionSpec{
			RunID: "r", Input: map[string]any{"prompt": "hi"},
		}, &fakeSink{})
		if err == nil || !strings.Contains(err.Error(), "target/model is required") {
			t.Fatalf("expected model error, got %v", err)
		}
	})
}

func TestOpenAICompatOperationURLFallback(t *testing.T) {
	cases := []struct {
		endpoint string
		path     string
		wantTail string
	}{
		{"http://host:8000", "/v1/models", "/v1/models"},
		{"http://host:8000/", "/v1/chat/completions", "/v1/chat/completions"},
		{"http://host:8000/v1", "/v1/models", "/v1/models"},
		{"http://host:8000/v1/", "/v1/chat/completions", "/v1/chat/completions"},
	}
	for _, c := range cases {
		got := resolveLegacyURL(strings.TrimRight(c.endpoint, "/"), c.path)
		if !strings.HasSuffix(got, c.wantTail) || strings.Contains(got, "/v1/v1") {
			t.Errorf("resolveLegacyURL(%q,%q)=%q want suffix %q without /v1/v1", c.endpoint, c.path, got, c.wantTail)
		}
	}
}
