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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
|
package main
import (
"bytes"
"errors"
"io"
"strings"
"testing"
"codeberg.org/snonux/hexai/internal"
"codeberg.org/snonux/hexai/internal/hexaimcp"
)
// The deprecation banner is unconditional: it must reach stderr on every
// invocation so users notice this binary is unmaintained.
func TestDeprecationWarning_Content(t *testing.T) {
for _, want := range []string{"DEPRECATION NOTICE", "EXPERIMENTAL", "NOT ACTIVELY MAINTAINED"} {
if !strings.Contains(deprecationWarning, want) {
t.Errorf("expected %q in deprecationWarning", want)
}
}
}
func TestDefaultLogPath(t *testing.T) {
path, err := defaultLogPath()
if err != nil {
t.Fatalf("defaultLogPath returned error: %v", err)
}
if path == "" {
t.Fatal("expected non-empty log path")
}
if !strings.HasSuffix(path, "hexai-mcp-server.log") {
t.Errorf("expected path to end with hexai-mcp-server.log, got %q", path)
}
}
func TestRun_ShowVersion(t *testing.T) {
var stdout bytes.Buffer
opts := mcpOptions{showVersion: true}
if err := run(opts, nil, &stdout, nil); err != nil {
t.Fatalf("run --version: %v", err)
}
got := strings.TrimSpace(stdout.String())
if got != internal.Version {
t.Fatalf("expected version %q, got %q", internal.Version, got)
}
}
func TestBuildOverrides(t *testing.T) {
opts := mcpOptions{
promptsDir: "/tmp/test-prompts",
slashCommandSync: true,
slashCommandDir: "/tmp/test-cmds",
}
overrides := buildOverrides(opts)
if overrides.PromptsDir != "/tmp/test-prompts" {
t.Fatalf("expected PromptsDir=/tmp/test-prompts, got %q", overrides.PromptsDir)
}
if !overrides.SlashCommandSync {
t.Fatal("expected SlashCommandSync=true")
}
if overrides.SlashCommandDir != "/tmp/test-cmds" {
t.Fatalf("expected SlashCommandDir=/tmp/test-cmds, got %q", overrides.SlashCommandDir)
}
}
func TestRun_SyncAll(t *testing.T) {
old := runBackfill
t.Cleanup(func() { runBackfill = old })
var gotLog, gotConfig string
var gotOverrides hexaimcp.MCPOverrides
runBackfill = func(logPath, configPath string, overrides hexaimcp.MCPOverrides) error {
gotLog = logPath
gotConfig = configPath
gotOverrides = overrides
return nil
}
opts := mcpOptions{
syncAll: true,
logPath: "/tmp/test.log",
configPath: "/tmp/cfg.toml",
promptsDir: "/tmp/prompts",
slashCommandSync: true,
slashCommandDir: "/tmp/cmds",
}
if err := run(opts, nil, nil, nil); err != nil {
t.Fatalf("run syncAll: %v", err)
}
if gotLog != "/tmp/test.log" {
t.Fatalf("expected logPath=/tmp/test.log, got %q", gotLog)
}
if gotConfig != "/tmp/cfg.toml" {
t.Fatalf("expected configPath=/tmp/cfg.toml, got %q", gotConfig)
}
if gotOverrides.PromptsDir != "/tmp/prompts" {
t.Fatalf("expected overrides.PromptsDir=/tmp/prompts, got %q", gotOverrides.PromptsDir)
}
if !gotOverrides.SlashCommandSync {
t.Fatal("expected overrides.SlashCommandSync=true")
}
if gotOverrides.SlashCommandDir != "/tmp/cmds" {
t.Fatalf("expected overrides.SlashCommandDir=/tmp/cmds, got %q", gotOverrides.SlashCommandDir)
}
}
func TestRun_SyncAllError(t *testing.T) {
old := runBackfill
t.Cleanup(func() { runBackfill = old })
wantErr := errors.New("backfill failed")
runBackfill = func(_, _ string, _ hexaimcp.MCPOverrides) error { return wantErr }
opts := mcpOptions{syncAll: true}
if err := run(opts, nil, nil, nil); !errors.Is(err, wantErr) {
t.Fatalf("expected backfill error, got: %v", err)
}
}
func TestRun_MCPServer(t *testing.T) {
old := runMCP
t.Cleanup(func() { runMCP = old })
called := false
runMCP = func(logPath, configPath string, overrides hexaimcp.MCPOverrides, stdin io.Reader, stdout, stderr io.Writer) error {
called = true
return nil
}
opts := mcpOptions{logPath: "/tmp/mcp.log"}
if err := run(opts, nil, nil, nil); err != nil {
t.Fatalf("run MCP: %v", err)
}
if !called {
t.Fatal("expected runMCP to be called")
}
}
func TestRun_MCPServerError(t *testing.T) {
old := runMCP
t.Cleanup(func() { runMCP = old })
wantErr := errors.New("server failed")
runMCP = func(_, _ string, _ hexaimcp.MCPOverrides, _ io.Reader, _, _ io.Writer) error { return wantErr }
if err := run(mcpOptions{}, nil, nil, nil); !errors.Is(err, wantErr) {
t.Fatalf("expected server error, got: %v", err)
}
}
// runMain version path: -version must short-circuit and write the version
// to stdout (not stderr, so it stays scriptable). Stderr still gets the
// deprecation banner — that's correct since the binary is leaving anyway.
func TestRunMain_VersionFlag(t *testing.T) {
var stdout, stderr bytes.Buffer
code := runMain([]string{"-version"}, nil, &stdout, &stderr)
if code != 0 {
t.Fatalf("runMain code = %d, want 0", code)
}
if got := strings.TrimSpace(stdout.String()); got != internal.Version {
t.Fatalf("stdout = %q, want version %q", got, internal.Version)
}
if !strings.Contains(stderr.String(), "DEPRECATION NOTICE") {
t.Fatalf("stderr missing deprecation banner: %q", stderr.String())
}
}
// runMain --sync-all path: forwards parsed options to runBackfill and
// returns 0 on success.
func TestRunMain_SyncAllSuccess(t *testing.T) {
old := runBackfill
t.Cleanup(func() { runBackfill = old })
var gotLog string
runBackfill = func(logPath string, _ string, _ hexaimcp.MCPOverrides) error {
gotLog = logPath
return nil
}
var stdout, stderr bytes.Buffer
code := runMain([]string{"-sync-all", "-log", "/tmp/sync.log"}, nil, &stdout, &stderr)
if code != 0 {
t.Fatalf("runMain code = %d, want 0; stderr=%q", code, stderr.String())
}
if gotLog != "/tmp/sync.log" {
t.Fatalf("logPath forwarded = %q, want /tmp/sync.log", gotLog)
}
}
// runMain run-error path: when the underlying server fails, runMain must
// return 1 (the production exit code) and write the error to stderr.
func TestRunMain_ServerErrorReturnsOne(t *testing.T) {
old := runMCP
t.Cleanup(func() { runMCP = old })
runMCP = func(string, string, hexaimcp.MCPOverrides, io.Reader, io.Writer, io.Writer) error {
return errors.New("mcp boom")
}
var stdout, stderr bytes.Buffer
code := runMain(nil, nil, &stdout, &stderr)
if code != 1 {
t.Fatalf("runMain code = %d, want 1", code)
}
if !strings.Contains(stderr.String(), "mcp boom") {
t.Fatalf("stderr missing error: %q", stderr.String())
}
}
// Bad flag must yield exit 2 without ever invoking the server stub.
func TestRunMain_BadFlagReturnsTwo(t *testing.T) {
old := runMCP
t.Cleanup(func() { runMCP = old })
called := false
runMCP = func(string, string, hexaimcp.MCPOverrides, io.Reader, io.Writer, io.Writer) error {
called = true
return nil
}
var stdout, stderr bytes.Buffer
code := runMain([]string{"--bogus"}, nil, &stdout, &stderr)
if code != 2 {
t.Fatalf("runMain code = %d, want 2", code)
}
if called {
t.Fatal("runMCP must not be called on flag-parse failure")
}
}
|