summaryrefslogtreecommitdiff
path: root/internal/hexailsp/run_test.go
blob: badb27c58881a0bc7707ffb4261c8e407cf8449c (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Tests for the Hexai LSP runner using a fake server factory and environment keys.
package hexailsp

import (
	"bytes"
	"io"
	"log"
	"os"
	"path/filepath"
	"testing"

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

// TestMain registers all built-in LLM providers before tests run, mirroring
// the explicit registration done in production binaries via RunWithConfig.
func TestMain(m *testing.M) {
	llm.RegisterAllProviders()
	os.Exit(m.Run())
}

// fake server capturing options and recording run calls
type fakeServer struct {
	ran  bool
	opts lsp.ServerOptions
}

func (f *fakeServer) Run() error { f.ran = true; return nil }

func TestRunWithFactory_UsesDefaultsAndCallsServer(t *testing.T) {
	old := os.Getenv("OPENAI_API_KEY")
	t.Cleanup(func() { _ = os.Setenv("OPENAI_API_KEY", old) })
	_ = os.Setenv("OPENAI_API_KEY", "")

	var stderr bytes.Buffer
	logger := log.New(&stderr, "hexai-lsp-server ", 0)
	cfg := appconfig.Load(nil) // defaults
	var gotOpts lsp.ServerOptions
	factory := func(r io.Reader, w io.Writer, logger *log.Logger, opts lsp.ServerOptions) ServerRunner {
		gotOpts = opts
		return &fakeServer{opts: opts}
	}
	if err := RunWithFactory("", "", bytes.NewBuffer(nil), bytes.NewBuffer(nil), logger, cfg, nil, factory); err != nil {
		t.Fatalf("RunWithFactory error: %v", err)
	}
	if gotOpts.Config == nil {
		t.Fatalf("expected Config to be set in ServerOptions")
	}
	if gotOpts.Config.MaxTokens != cfg.MaxTokens {
		t.Fatalf("MaxTokens want %d got %d", cfg.MaxTokens, gotOpts.Config.MaxTokens)
	}
	if gotOpts.Config.ContextMode != cfg.ContextMode {
		t.Fatalf("ContextMode want %q got %q", cfg.ContextMode, gotOpts.Config.ContextMode)
	}
	if gotOpts.Config.ContextWindowLines != cfg.ContextWindowLines {
		t.Fatalf("ContextWindowLines want %d got %d", cfg.ContextWindowLines, gotOpts.Config.ContextWindowLines)
	}
	if gotOpts.Config.MaxContextTokens != cfg.MaxContextTokens {
		t.Fatalf("MaxContextTokens want %d got %d", cfg.MaxContextTokens, gotOpts.Config.MaxContextTokens)
	}

	if gotOpts.Client != nil { // with no env, openai client fails to build
		t.Fatalf("expected nil client when API key missing")
	}
}

func TestRunWithFactory_BuildsClientWhenKeysPresent(t *testing.T) {
	// Set a dummy OpenAI key to allow client creation
	old := os.Getenv("OPENAI_API_KEY")
	t.Cleanup(func() { _ = os.Setenv("OPENAI_API_KEY", old) })
	_ = os.Setenv("OPENAI_API_KEY", "dummy")

	var stderr bytes.Buffer
	logger := log.New(&stderr, "hexai-lsp-server ", 0)
	cfg := appconfig.Load(nil) // defaults, provider=openai by default
	var got llm.Client
	factory := func(r io.Reader, w io.Writer, logger *log.Logger, opts lsp.ServerOptions) ServerRunner {
		got = opts.Client
		return &fakeServer{opts: opts}
	}
	if err := RunWithFactory("", "", bytes.NewBuffer(nil), bytes.NewBuffer(nil), logger, cfg, nil, factory); err != nil {
		t.Fatalf("RunWithFactory error: %v", err)
	}
	if got == nil {
		t.Fatalf("expected non-nil client when OPENAI_API_KEY is set")
	}
}

func TestRun_RespectsLogPathFlag(t *testing.T) {
	tmp := t.TempDir()
	logFile := filepath.Join(tmp, "hexai-lsp-server.log")
	// Run with real Run but nil env key so client disabled; ensure no panic and file created
	if err := Run(logFile, bytes.NewBuffer(nil), bytes.NewBuffer(nil), bytes.NewBuffer(nil)); err != nil {
		t.Fatalf("Run error: %v", err)
	}
	if _, err := os.Stat(logFile); err != nil {
		t.Fatalf("expected log file to be created: %v", err)
	}
}

func TestRunWithFactory_NormalizesContextMode_AndSetsPreviewLimit(t *testing.T) {
	t.Cleanup(func() { logging.SetLogPreviewLimit(0) })
	var stderr bytes.Buffer
	logger := log.New(&stderr, "hexai-lsp-server ", 0)
	cfg := appconfig.App{
		CoreConfig: appconfig.CoreConfig{
			ContextMode:     "  File-On-New-Func  ",
			LogPreviewLimit: 3,
		},
	}
	var gotOpts lsp.ServerOptions
	factory := func(r io.Reader, w io.Writer, logger *log.Logger, opts lsp.ServerOptions) ServerRunner {
		gotOpts = opts
		return &fakeServer{opts: opts}
	}
	if err := RunWithFactory("", "", bytes.NewBuffer(nil), bytes.NewBuffer(nil), logger, cfg, nil, factory); err != nil {
		t.Fatalf("RunWithFactory error: %v", err)
	}
	if gotOpts.Config == nil {
		t.Fatalf("expected Config to be set in ServerOptions")
	}
	if gotOpts.Config.ContextMode != "file-on-new-func" {
		t.Fatalf("ContextMode not normalized: %q", gotOpts.Config.ContextMode)
	}
	if logging.PreviewForLog("abcdef") != "abc…" {
		t.Fatalf("PreviewForLog not respecting limit: %q", logging.PreviewForLog("abcdef"))
	}
}

func TestRunWithFactory_LogContextFlag(t *testing.T) {
	var stderr bytes.Buffer
	logger := log.New(&stderr, "hexai-lsp-server ", 0)
	cfg := appconfig.App{}
	var got1, got2 lsp.ServerOptions
	first := true
	factory := func(r io.Reader, w io.Writer, logger *log.Logger, opts lsp.ServerOptions) ServerRunner {
		if first {
			got1 = opts
			first = false
		} else {
			got2 = opts
		}
		return &fakeServer{opts: opts}
	}
	if err := RunWithFactory("/tmp/some.log", "", bytes.NewBuffer(nil), bytes.NewBuffer(nil), logger, cfg, nil, factory); err != nil {
		t.Fatalf("RunWithFactory error: %v", err)
	}
	if !got1.LogContext {
		t.Fatalf("expected LogContext true when logPath is non-empty")
	}
	if err := RunWithFactory("", "", bytes.NewBuffer(nil), bytes.NewBuffer(nil), logger, cfg, nil, factory); err != nil {
		t.Fatalf("RunWithFactory error: %v", err)
	}
	if got2.LogContext {
		t.Fatalf("expected LogContext false when logPath is empty")
	}
}