summaryrefslogtreecommitdiff
path: root/internal/lsp/llm_client_registry_test.go
blob: 5700a53f61f2960f35ac7590eee1ae7fe6fc16b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package lsp

import (
	"errors"
	"testing"

	"codeberg.org/snonux/hexai/internal/appconfig"
	"codeberg.org/snonux/hexai/internal/llm"
)

func TestLLMClientRegistryClientFor_CachesAlternateProviders(t *testing.T) {
	registry := newLLMClientRegistry()
	registry.applyOptions(fakeClient{name: "openai", model: "gpt-5.0"}, "openai")

	cfg := appconfig.App{}
	spec := requestSpec{
		provider:      "anthropic",
		entry:         appconfig.SurfaceConfig{Model: "claude-3-7-sonnet"},
		fallbackModel: "claude-3-7-sonnet",
	}

	buildCalls := 0
	builder := func(_ appconfig.App, provider, modelOverride string) (llm.Client, error) {
		buildCalls++
		return fakeClient{name: provider, model: modelOverride}, nil
	}

	first := registry.clientFor(spec, cfg, builder)
	second := registry.clientFor(spec, cfg, builder)
	if first == nil || second == nil {
		t.Fatal("expected alternate provider client")
	}
	if buildCalls != 1 {
		t.Fatalf("expected one build for cached alternate client, got %d", buildCalls)
	}
	if first.Name() != "anthropic" || second.Name() != "anthropic" {
		t.Fatalf("expected anthropic client, got %q and %q", first.Name(), second.Name())
	}
}

func TestLLMClientRegistryClientFor_FallsBackToBaseClientOnBuildError(t *testing.T) {
	base := fakeClient{name: "openai", model: "gpt-5.0"}
	registry := newLLMClientRegistry()
	registry.applyOptions(base, "openai")

	spec := requestSpec{provider: "anthropic"}
	builder := func(appconfig.App, string, string) (llm.Client, error) {
		return nil, errors.New("boom")
	}

	got := registry.clientFor(spec, appconfig.App{}, builder)
	if got != base {
		t.Fatalf("expected base client fallback, got %#v", got)
	}
}

func TestLLMClientRegistryCurrent_ReturnsConfiguredClient(t *testing.T) {
	registry := newLLMClientRegistry()
	client := fakeClient{name: "openrouter", model: "gpt-4.1-mini"}
	registry.applyOptions(client, "openrouter")

	if got := registry.current(); got != client {
		t.Fatalf("expected configured client, got %#v", got)
	}
}