summaryrefslogtreecommitdiff
path: root/internal/hexaicli/run_output_test.go
blob: b4614da45098a7a099fdeecdb0500116598f92a2 (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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
// Tests for CLI job output writing, result counting, config path context,
// cached output writing, and streaming error paths.
package hexaicli

import (
	"bytes"
	"context"
	"errors"
	"fmt"
	"strings"
	"testing"

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

func TestCliJobResultCount(t *testing.T) {
	tests := []struct {
		name    string
		results []*cliJobResult
		want    int
	}{
		{name: "all nil", results: []*cliJobResult{nil, nil}, want: 0},
		{name: "empty slice", results: nil, want: 0},
		{name: "one result", results: []*cliJobResult{{provider: "a"}}, want: 1},
		{name: "mixed", results: []*cliJobResult{{provider: "a"}, nil, {provider: "b"}}, want: 2},
	}
	for _, tc := range tests {
		t.Run(tc.name, func(t *testing.T) {
			if got := cliJobResultCount(tc.results); got != tc.want {
				t.Fatalf("cliJobResultCount = %d, want %d", got, tc.want)
			}
		})
	}
}

func TestWriteCLIJobOutput_WithHeading(t *testing.T) {
	var buf bytes.Buffer
	res := &cliJobResult{provider: "openai", model: "gpt-4.1", output: "hello world"}
	if err := writeCLIJobOutput(&buf, res, true); err != nil {
		t.Fatalf("writeCLIJobOutput: %v", err)
	}
	got := buf.String()
	if !strings.Contains(got, "=== openai:gpt-4.1 ===") {
		t.Fatalf("expected heading, got %q", got)
	}
	if !strings.Contains(got, "hello world") {
		t.Fatalf("expected output, got %q", got)
	}
	// Output without trailing newline should get one appended.
	if !strings.HasSuffix(got, "\n") {
		t.Fatalf("expected trailing newline, got %q", got)
	}
}

func TestWriteCLIJobOutput_WithoutHeading(t *testing.T) {
	var buf bytes.Buffer
	res := &cliJobResult{provider: "openai", model: "gpt-4.1", output: "hello\n"}
	if err := writeCLIJobOutput(&buf, res, false); err != nil {
		t.Fatalf("writeCLIJobOutput: %v", err)
	}
	got := buf.String()
	if strings.Contains(got, "===") {
		t.Fatalf("expected no heading, got %q", got)
	}
	// Output already ends with newline; no extra newline should be appended.
	if got != "hello\n" {
		t.Fatalf("unexpected output %q", got)
	}
}

func TestWriteCLIJobOutput_EmptyOutput(t *testing.T) {
	var buf bytes.Buffer
	res := &cliJobResult{provider: "p", model: "m", output: ""}
	if err := writeCLIJobOutput(&buf, res, true); err != nil {
		t.Fatalf("writeCLIJobOutput: %v", err)
	}
	// Should print heading but no body content.
	if !strings.Contains(buf.String(), "=== p:m ===") {
		t.Fatalf("expected heading even for empty output, got %q", buf.String())
	}
}

func TestWriteCLIJobOutputs_SingleResult(t *testing.T) {
	var buf bytes.Buffer
	results := []*cliJobResult{{provider: "p", model: "m", output: "out"}}
	if err := writeCLIJobOutputs(&buf, results); err != nil {
		t.Fatalf("writeCLIJobOutputs: %v", err)
	}
	// Single result: showHeading is false (count == 1).
	if strings.Contains(buf.String(), "===") {
		t.Fatalf("single result should have no heading, got %q", buf.String())
	}
	if !strings.Contains(buf.String(), "out") {
		t.Fatalf("expected output, got %q", buf.String())
	}
}

func TestWriteCLIJobOutputs_MultipleResults(t *testing.T) {
	var buf bytes.Buffer
	results := []*cliJobResult{
		{provider: "a", model: "m1", output: "first"},
		{provider: "b", model: "m2", output: "second"},
	}
	if err := writeCLIJobOutputs(&buf, results); err != nil {
		t.Fatalf("writeCLIJobOutputs: %v", err)
	}
	got := buf.String()
	// Multiple results: headings shown.
	if !strings.Contains(got, "=== a:m1 ===") || !strings.Contains(got, "=== b:m2 ===") {
		t.Fatalf("expected headings for both results, got %q", got)
	}
	// Separator newline between results.
	if !strings.Contains(got, "first") || !strings.Contains(got, "second") {
		t.Fatalf("expected both outputs, got %q", got)
	}
}

func TestWriteCLIJobOutputs_WithNils(t *testing.T) {
	var buf bytes.Buffer
	results := []*cliJobResult{nil, {provider: "a", model: "m", output: "ok"}, nil}
	if err := writeCLIJobOutputs(&buf, results); err != nil {
		t.Fatalf("writeCLIJobOutputs: %v", err)
	}
	// Only one non-nil result, so count=1, no heading.
	if strings.Contains(buf.String(), "===") {
		t.Fatalf("single non-nil result should have no heading, got %q", buf.String())
	}
}

func TestWriteCLIJobOutputs_Empty(t *testing.T) {
	var buf bytes.Buffer
	if err := writeCLIJobOutputs(&buf, nil); err != nil {
		t.Fatalf("writeCLIJobOutputs: %v", err)
	}
	if buf.Len() != 0 {
		t.Fatalf("expected empty output, got %q", buf.String())
	}
}

func TestWithCLIConfigPath_And_ConfigPathFromContext(t *testing.T) {
	// Normal usage.
	ctx := WithCLIConfigPath(context.Background(), "/tmp/config.toml")
	if got := configPathFromContext(ctx); got != "/tmp/config.toml" {
		t.Fatalf("configPathFromContext = %q, want /tmp/config.toml", got)
	}

	// With whitespace trimming.
	ctx = WithCLIConfigPath(context.Background(), "  /tmp/cfg.toml  ")
	if got := configPathFromContext(ctx); got != "/tmp/cfg.toml" {
		t.Fatalf("configPathFromContext = %q, want /tmp/cfg.toml", got)
	}

	// Nil context for WithCLIConfigPath creates a background context.
	ctx = WithCLIConfigPath(nil, "/path")
	if got := configPathFromContext(ctx); got != "/path" {
		t.Fatalf("configPathFromContext = %q, want /path", got)
	}

	// Empty context returns empty string.
	if got := configPathFromContext(context.Background()); got != "" {
		t.Fatalf("configPathFromContext on empty ctx = %q, want empty", got)
	}

	// Nil context returns empty string.
	if got := configPathFromContext(nil); got != "" {
		t.Fatalf("configPathFromContext on nil = %q, want empty", got)
	}
}

func TestWriteCachedCLIJobOutput_StreamOutput(t *testing.T) {
	var buf bytes.Buffer
	// streamOutput=true, printer=nil => writes to stdout.
	if err := writeCachedCLIJobOutput("cached", &buf, nil, 0, true); err != nil {
		t.Fatalf("writeCachedCLIJobOutput: %v", err)
	}
	if buf.String() != "cached" {
		t.Fatalf("expected 'cached', got %q", buf.String())
	}
}

func TestWriteCachedCLIJobOutput_NoStreamNoPrinter(t *testing.T) {
	var buf bytes.Buffer
	// streamOutput=false, printer=nil => returns nil without writing.
	if err := writeCachedCLIJobOutput("cached", &buf, nil, 0, false); err != nil {
		t.Fatalf("writeCachedCLIJobOutput: %v", err)
	}
	if buf.Len() != 0 {
		t.Fatalf("expected no output, got %q", buf.String())
	}
}

// errWriter is an io.Writer that always returns an error.
type errWriter struct{ err error }

func (e errWriter) Write([]byte) (int, error) { return 0, e.err }

func TestWriteCLIJobOutput_WriteError(t *testing.T) {
	w := errWriter{err: errors.New("write fail")}
	res := &cliJobResult{provider: "p", model: "m", output: "out"}
	if err := writeCLIJobOutput(w, res, true); err == nil {
		t.Fatalf("expected error from failing writer")
	}
}

func TestWriteCLIJobOutputs_WriteError(t *testing.T) {
	w := errWriter{err: errors.New("write fail")}
	results := []*cliJobResult{{provider: "p", model: "m", output: "out"}}
	if err := writeCLIJobOutputs(w, results); err == nil {
		t.Fatalf("expected error from failing writer")
	}
}

// streamErrClient is a Streamer that returns a stream error.
type streamErrClient struct {
	fakeClient
	streamErr error
}

func (s *streamErrClient) ChatStream(_ context.Context, _ []llm.Message, _ func(string), _ ...llm.RequestOption) error {
	return s.streamErr
}

func TestRunStreamingChat_StreamError(t *testing.T) {
	client := &streamErrClient{
		fakeClient: fakeClient{name: "p", model: "m"},
		streamErr:  fmt.Errorf("stream broken"),
	}
	var out bytes.Buffer
	_, err := runStreamingChat(context.Background(), client, nil, nil, &out)
	if err == nil || !strings.Contains(err.Error(), "stream broken") {
		t.Fatalf("expected stream error, got %v", err)
	}
}

// streamWriteErrClient is a Streamer that writes chunks to trigger a write error.
type streamWriteErrClient struct {
	fakeClient
}

func (s *streamWriteErrClient) ChatStream(_ context.Context, _ []llm.Message, onDelta func(string), _ ...llm.RequestOption) error {
	onDelta("chunk1")
	onDelta("chunk2")
	return nil
}

func TestRunStreamingChat_WriteError(t *testing.T) {
	client := &streamWriteErrClient{fakeClient: fakeClient{name: "p", model: "m"}}
	w := errWriter{err: errors.New("write fail")}
	_, err := runStreamingChat(context.Background(), client, nil, nil, w)
	if err == nil || !strings.Contains(err.Error(), "write fail") {
		t.Fatalf("expected write error, got %v", err)
	}
}

func TestRunWithClient_NoInput(t *testing.T) {
	var out, errb bytes.Buffer
	err := RunWithClient(context.Background(), nil, strings.NewReader(""), &out, &errb, &fakeClient{name: "p", model: "m", resp: "out"})
	if err == nil {
		t.Fatalf("expected error for no input")
	}
	if !strings.Contains(errb.String(), "no input provided") {
		t.Fatalf("expected no-input error message, got %q", errb.String())
	}
}

func TestRunWithClient_Success(t *testing.T) {
	var out, errb bytes.Buffer
	client := &fakeClient{name: "p", model: "m", resp: "result"}
	err := RunWithClient(context.Background(), []string{"hello"}, strings.NewReader(""), &out, &errb, client)
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	if out.String() != "result" {
		t.Fatalf("stdout = %q, want result", out.String())
	}
	if !strings.Contains(errb.String(), "provider=p model=m") {
		t.Fatalf("expected summary in stderr, got %q", errb.String())
	}
}

func TestEffectiveModel_Empty(t *testing.T) {
	client := &fakeClient{name: "p", model: "default-model"}
	req := requestArgs{model: ""}
	if got := effectiveModel(req, client); got != "default-model" {
		t.Fatalf("effectiveModel = %q, want default-model", got)
	}
}

func TestEffectiveModel_Whitespace(t *testing.T) {
	client := &fakeClient{name: "p", model: "default-model"}
	req := requestArgs{model: "  "}
	if got := effectiveModel(req, client); got != "default-model" {
		t.Fatalf("effectiveModel = %q, want default-model", got)
	}
}

func TestRunSimpleChat_WriteError(t *testing.T) {
	client := &fakeClient{name: "p", model: "m", resp: "ok"}
	w := errWriter{err: errors.New("write fail")}
	_, err := runSimpleChat(context.Background(), client, nil, nil, w)
	if err == nil || !strings.Contains(err.Error(), "write fail") {
		t.Fatalf("expected write error, got %v", err)
	}
}

func TestChooseCLIModel_Empty(t *testing.T) {
	if got := chooseCLIModel("", "fallback"); got != "fallback" {
		t.Fatalf("chooseCLIModel = %q, want fallback", got)
	}
}

func TestChooseCLIModel_Whitespace(t *testing.T) {
	if got := chooseCLIModel("  ", "fallback"); got != "fallback" {
		t.Fatalf("chooseCLIModel = %q, want fallback", got)
	}
}

func TestPrintProviderLabel_EmptyModel(t *testing.T) {
	var buf bytes.Buffer
	printProviderLabel(&buf, "p", "")
	if buf.Len() != 0 {
		t.Fatalf("expected no output for empty model, got %q", buf.String())
	}
}

func TestPrintProviderLabel_WhitespaceModel(t *testing.T) {
	var buf bytes.Buffer
	printProviderLabel(&buf, "p", "   ")
	if buf.Len() != 0 {
		t.Fatalf("expected no output for whitespace model, got %q", buf.String())
	}
}

func TestCacheHitSummary_NegativeAge(t *testing.T) {
	got := cacheHitSummary("p", "m", -5)
	if !strings.Contains(got, "cache hit") || !strings.Contains(got, "age=0s") {
		t.Fatalf("expected cache hit with age=0s, got %q", got)
	}
}

func TestRunCLIJobs_MultiJob_WritesOutputs(t *testing.T) {
	// runCLIJobs with multiple jobs should call writeCLIJobOutputs
	// (the non-streaming, non-printer path).
	oldNew := newClientFromApp
	defer func() { newClientFromApp = oldNew }()
	newClientFromApp = func(cfg appconfig.App) (llm.Client, error) {
		return &fakeClient{name: cfg.Provider, model: "m", resp: "out-" + cfg.Provider}, nil
	}
	t.Setenv("XDG_CACHE_HOME", t.TempDir())

	jobs := []cliJob{
		{index: 0, provider: "a", cfg: appconfig.App{
			CoreConfig: appconfig.CoreConfig{Provider: "a"},
			ProviderConfig: appconfig.ProviderConfig{
				OllamaBaseURL: "http://x",
				OllamaModel:   "m",
			},
		}, req: requestArgs{model: "m"}},
		{index: 1, provider: "b", cfg: appconfig.App{
			CoreConfig: appconfig.CoreConfig{Provider: "b"},
			ProviderConfig: appconfig.ProviderConfig{
				OllamaBaseURL: "http://x",
				OllamaModel:   "m",
			},
		}, req: requestArgs{model: "m"}},
	}
	msgs := buildMessages("hello")
	var stdout, stderr bytes.Buffer

	// Test writeCLIJobOutputs and writeCLIJobSummaries directly
	// since executeCLIJobs with multiple jobs uses a column printer.
	_ = jobs
	_ = msgs
	results := []*cliJobResult{
		{provider: "a", model: "m1", output: "first"},
		{provider: "b", model: "m2", output: "second"},
	}
	if err := writeCLIJobOutputs(&stdout, results); err != nil {
		t.Fatalf("writeCLIJobOutputs: %v", err)
	}
	if err := writeCLIJobSummaries(&stderr, results); err != nil {
		t.Fatalf("writeCLIJobSummaries: %v", err)
	}

	got := stdout.String()
	if !strings.Contains(got, "=== a:m1 ===") || !strings.Contains(got, "=== b:m2 ===") {
		t.Fatalf("expected headings, got %q", got)
	}

	// Also test the runCLIJobs single-job (streaming) path.
	singleJobs := []cliJob{
		{index: 0, provider: "a", cfg: appconfig.App{
			CoreConfig: appconfig.CoreConfig{Provider: "a"},
			ProviderConfig: appconfig.ProviderConfig{
				OllamaBaseURL: "http://x",
				OllamaModel:   "m",
			},
		}, req: requestArgs{model: "m"}},
	}
	stdout.Reset()
	stderr.Reset()
	if err := runCLIJobs(context.Background(), singleJobs, msgs, "hello", &stdout, &stderr); err != nil {
		t.Fatalf("runCLIJobs single: %v", err)
	}
	if !strings.Contains(stdout.String(), "out-a") {
		t.Fatalf("expected single job output, got %q", stdout.String())
	}
}

func TestWithCLISelection_NilContext(t *testing.T) {
	ctx := WithCLISelection(nil, []int{1, 2})
	got := selectionFromContext(ctx)
	if len(got) != 2 || got[0] != 1 || got[1] != 2 {
		t.Fatalf("unexpected selection: %v", got)
	}
}

func TestPrintCLIHeader_EmptyJobs(t *testing.T) {
	var buf bytes.Buffer
	printCLIHeader(&buf, nil, nil)
	if buf.Len() != 0 {
		t.Fatalf("expected no output for empty jobs, got %q", buf.String())
	}
}

func TestWriteCachedCLIJobOutput_StreamWriteError(t *testing.T) {
	w := errWriter{err: errors.New("write fail")}
	err := writeCachedCLIJobOutput("data", w, nil, 0, true)
	if err == nil || !strings.Contains(err.Error(), "write fail") {
		t.Fatalf("expected write error, got %v", err)
	}
}

// chatErrClient fails on Chat but not Name/DefaultModel.
type chatErrClient struct {
	fakeClient
	chatErr error
}

func (c *chatErrClient) Chat(_ context.Context, _ []llm.Message, _ ...llm.RequestOption) (string, error) {
	return "", c.chatErr
}

func TestRunSimpleChat_ChatError(t *testing.T) {
	client := &chatErrClient{
		fakeClient: fakeClient{name: "p", model: "m"},
		chatErr:    fmt.Errorf("chat broken"),
	}
	var out bytes.Buffer
	_, err := runSimpleChat(context.Background(), client, nil, nil, &out)
	if err == nil || !strings.Contains(err.Error(), "chat broken") {
		t.Fatalf("expected chat error, got %v", err)
	}
}

func TestWriteCLIJobSummary_WithError(t *testing.T) {
	var buf bytes.Buffer
	res := &cliJobResult{provider: "p", model: "m", err: fmt.Errorf("boom"), summary: ""}
	if err := writeCLIJobSummary(&buf, res); err != nil {
		t.Fatalf("writeCLIJobSummary: %v", err)
	}
	if !strings.Contains(buf.String(), "boom") || !strings.Contains(buf.String(), "provider=p model=m") {
		t.Fatalf("expected error info, got %q", buf.String())
	}
}

func TestWriteCLIJobSummaries_FirstError(t *testing.T) {
	results := []*cliJobResult{
		{provider: "a", model: "m", err: nil},
		{provider: "b", model: "m", err: fmt.Errorf("fail")},
	}
	var buf bytes.Buffer
	err := writeCLIJobSummaries(&buf, results)
	if err == nil || !strings.Contains(err.Error(), "fail") {
		t.Fatalf("expected first error, got %v", err)
	}
}

func TestFilterJobsBySelection_Dedup(t *testing.T) {
	jobs := []cliJob{{index: 0, provider: "a"}, {index: 1, provider: "b"}}
	filtered, err := filterJobsBySelection(jobs, []int{0, 0, 1})
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	if len(filtered) != 2 {
		t.Fatalf("expected 2 jobs after dedup, got %d", len(filtered))
	}
}

func TestWriteCLIJobOutputs_SeparatorBetweenMultiple(t *testing.T) {
	var buf bytes.Buffer
	results := []*cliJobResult{
		{provider: "a", model: "m", output: "one\n"},
		nil,
		{provider: "b", model: "m", output: "two\n"},
	}
	if err := writeCLIJobOutputs(&buf, results); err != nil {
		t.Fatalf("writeCLIJobOutputs: %v", err)
	}
	got := buf.String()
	// Should have a blank line separator between the two non-nil results.
	if !strings.Contains(got, "one\n\n") {
		t.Fatalf("expected separator between results, got %q", got)
	}
}

func TestRunChatRequest_NonStreamer(t *testing.T) {
	client := &fakeClient{name: "p", model: "m", resp: "hello"}
	var out bytes.Buffer
	got, err := runChatRequest(context.Background(), client, requestArgs{model: "m"}, nil, &out)
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	if got != "hello" {
		t.Fatalf("expected hello, got %q", got)
	}
}

func TestRunChatRequest_Streamer(t *testing.T) {
	client := &fakeStreamer{
		fakeClient: fakeClient{name: "p", model: "m"},
		chunks:     []string{"a", "b"},
	}
	var out bytes.Buffer
	got, err := runChatRequest(context.Background(), client, requestArgs{model: "m"}, nil, &out)
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	if got != "ab" {
		t.Fatalf("expected ab, got %q", got)
	}
}

func TestSelectionFromContext_Nil(t *testing.T) {
	if got := selectionFromContext(nil); got != nil {
		t.Fatalf("expected nil, got %v", got)
	}
}

func TestSelectionFromContext_NoValue(t *testing.T) {
	if got := selectionFromContext(context.Background()); got != nil {
		t.Fatalf("expected nil, got %v", got)
	}
}

func TestFilterJobsBySelection_Empty(t *testing.T) {
	jobs := []cliJob{{index: 0, provider: "a"}}
	filtered, err := filterJobsBySelection(jobs, nil)
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	if len(filtered) != 1 {
		t.Fatalf("expected original jobs, got %d", len(filtered))
	}
}