summaryrefslogtreecommitdiff
path: root/internal/appconfig/app_sections_test.go
blob: e26d3f34167e84dad676472ece447b8f7f02971e (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
package appconfig

import (
	"reflect"
	"testing"
)

func TestSectionsRoundTrip(t *testing.T) {
	expected := App{}
	expected.ApplySections(testAppSections())

	got := App{}
	got.ApplySections(expected.Sections())

	if !reflect.DeepEqual(got, expected) {
		t.Fatalf("round-trip mismatch\n got: %#v\nwant: %#v", got, expected)
	}
}

func TestSectionsDefensiveCopies(t *testing.T) {
	sections := testAppSections()
	cfg := App{}
	cfg.ApplySections(sections)

	sections.Core.TriggerCharacters[0] = "mutated"
	sections.Core.ChatPrefixes[0] = "mutated"
	sections.Providers.CompletionConfigs[0].Provider = "mutated"
	sections.Providers.CLIConfigs[0].Model = "mutated"
	sections.Prompts.CustomActions[0].Title = "mutated"
	sections.Features.IgnoreExtraPatterns[0] = "mutated"
	sections.Features.TmuxEditAgents[0].Name = "mutated"

	assertNotEqual(t, cfg.TriggerCharacters[0], "mutated", "trigger characters")
	assertNotEqual(t, cfg.ChatPrefixes[0], "mutated", "chat prefixes")
	assertNotEqual(t, cfg.CompletionConfigs[0].Provider, "mutated", "completion configs")
	assertNotEqual(t, cfg.CLIConfigs[0].Model, "mutated", "cli configs")
	assertNotEqual(t, cfg.CustomActions[0].Title, "mutated", "custom actions")
	assertNotEqual(t, cfg.IgnoreExtraPatterns[0], "mutated", "ignore patterns")
	assertNotEqual(t, cfg.TmuxEditAgents[0].Name, "mutated", "tmux agents")

	out := cfg.Sections()
	out.Core.TriggerCharacters[0] = "mutated"
	out.Core.ChatPrefixes[0] = "mutated"
	out.Providers.CompletionConfigs[0].Provider = "mutated"
	out.Providers.CLIConfigs[0].Model = "mutated"
	out.Prompts.CustomActions[0].Title = "mutated"
	out.Features.IgnoreExtraPatterns[0] = "mutated"
	out.Features.TmuxEditAgents[0].Name = "mutated"

	assertNotEqual(t, cfg.TriggerCharacters[0], "mutated", "sections trigger characters")
	assertNotEqual(t, cfg.ChatPrefixes[0], "mutated", "sections chat prefixes")
	assertNotEqual(t, cfg.CompletionConfigs[0].Provider, "mutated", "sections completion configs")
	assertNotEqual(t, cfg.CLIConfigs[0].Model, "mutated", "sections cli configs")
	assertNotEqual(t, cfg.CustomActions[0].Title, "mutated", "sections custom actions")
	assertNotEqual(t, cfg.IgnoreExtraPatterns[0], "mutated", "sections ignore patterns")
	assertNotEqual(t, cfg.TmuxEditAgents[0].Name, "mutated", "sections tmux agents")
}

func assertNotEqual(t *testing.T, got, want, field string) {
	t.Helper()
	if got == want {
		t.Fatalf("%s unexpectedly changed to %q", field, got)
	}
}

func testAppSections() AppSections {
	return AppSections{
		Core:      testCoreConfig(),
		Providers: testProviderConfig(),
		Prompts:   testPromptConfig(),
		Features:  testFeatureConfig(),
	}
}

func testCoreConfig() CoreConfig {
	return CoreConfig{
		MaxTokens:             111,
		ContextMode:           "window",
		ContextWindowLines:    222,
		MaxContextTokens:      333,
		LogPreviewLimit:       444,
		RequestTimeout:        55,
		CodingTemperature:     sectionFloatPtr(0.4),
		ManualInvokeMinPrefix: 3,
		CompletionDebounceMs:  700,
		CompletionThrottleMs:  125,
		CompletionWaitAll:     sectionBoolPtr(false),
		TriggerCharacters:     []string{".", "("},
		Provider:              "openrouter",
		InlineOpen:            "<!",
		InlineClose:           "!>",
		ChatSuffix:            "?",
		ChatPrefixes:          []string{"@", "/"},
	}
}

func testProviderConfig() ProviderConfig {
	return ProviderConfig{
		OpenAIBaseURL:         "https://api.openai.test",
		OpenAIModel:           "gpt-test",
		OpenAITemperature:     sectionFloatPtr(0.6),
		OpenRouterBaseURL:     "https://openrouter.test",
		OpenRouterModel:       "or-test",
		OpenRouterTemperature: sectionFloatPtr(0.7),
		OllamaBaseURL:         "http://localhost:11434",
		OllamaModel:           "llama3.1",
		OllamaTemperature:     sectionFloatPtr(0.8),
		AnthropicBaseURL:      "https://api.anthropic.test",
		AnthropicModel:        "claude-test",
		AnthropicTemperature:  sectionFloatPtr(0.9),
		CompletionConfigs: []SurfaceConfig{{
			Provider:    "openai",
			Model:       "gpt-test",
			Temperature: sectionFloatPtr(0.2),
		}},
		CodeActionConfigs: []SurfaceConfig{{
			Provider:    "anthropic",
			Model:       "claude-test",
			Temperature: sectionFloatPtr(0.3),
		}},
		ChatConfigs: []SurfaceConfig{{
			Provider:    "openrouter",
			Model:       "or-test",
			Temperature: sectionFloatPtr(0.4),
		}},
		CLIConfigs: []SurfaceConfig{{
			Provider:    "ollama",
			Model:       "llama3.1",
			Temperature: sectionFloatPtr(0.5),
		}},
	}
}

func testPromptConfig() PromptConfig {
	return PromptConfig{
		PromptCompletionSystemGeneral:     "comp sys",
		PromptCompletionSystemParams:      "comp params",
		PromptCompletionSystemInline:      "comp inline",
		PromptCompletionUserGeneral:       "comp user",
		PromptCompletionUserParams:        "comp user params",
		PromptCompletionExtraHeader:       "comp header",
		PromptNativeCompletion:            "native",
		PromptChatSystem:                  "chat",
		PromptCodeActionRewriteSystem:     "rewrite sys",
		PromptCodeActionDiagnosticsSystem: "diag sys",
		PromptCodeActionDocumentSystem:    "doc sys",
		PromptCodeActionRewriteUser:       "rewrite user",
		PromptCodeActionDiagnosticsUser:   "diag user",
		PromptCodeActionDocumentUser:      "doc user",
		PromptCodeActionGoTestSystem:      "gotest sys",
		PromptCodeActionGoTestUser:        "gotest user",
		PromptCodeActionSimplifySystem:    "simplify sys",
		PromptCodeActionSimplifyUser:      "simplify user",
		PromptCLIDefaultSystem:            "cli default",
		PromptCLIExplainSystem:            "cli explain",
		CustomActions: []CustomAction{{
			ID:          "a1",
			Title:       "Action 1",
			Kind:        "refactor",
			Scope:       "selection",
			Hotkey:      "r",
			Instruction: "do it",
			System:      "sys",
			User:        "user",
		}},
		TmuxCustomMenuHotkey: "M-a",
	}
}

func testFeatureConfig() FeatureConfig {
	return FeatureConfig{
		StatsWindowMinutes:   15,
		IgnoreGitignore:      sectionBoolPtr(true),
		IgnoreExtraPatterns:  []string{"vendor/**", "tmp/**"},
		IgnoreLSPNotify:      sectionBoolPtr(false),
		TmuxEditPopupWidth:   "80%",
		TmuxEditPopupHeight:  "75%",
		TmuxEditDefaultAgent: "codex",
		TmuxEditAgents: []TmuxEditAgentCfg{{
			Name:           "codex",
			DisplayName:    "Codex",
			DetectPattern:  "(?i)codex",
			SectionPattern: "section",
			PromptPattern:  "prompt",
			StripPatterns:  []string{"x", "y"},
			ClearFirst:     sectionBoolPtr(true),
			ClearKeys:      "C-u",
			NewlineKeys:    "S-Enter",
			SubmitKeys:     "Enter",
		}},
		MCPPromptsDir:       ".hexai/prompts",
		MCPSlashCommandSync: true,
		MCPSlashCommandDir:  ".hexai/slash",
	}
}

func sectionFloatPtr(v float64) *float64 { return &v }

func sectionBoolPtr(v bool) *bool { return &v }