Repository navigation
Expand file tree
/
Copy pathmain_test.go
More file actions
310 lines (284 loc) · 10.2 KB
/
Copy pathmain_test.go
File metadata and controls
310 lines (284 loc) · 10.2 KB
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
package main
import (
"context"
"errors"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"GoNavi-Wails/internal/ai/runharness"
aiservice "GoNavi-Wails/internal/ai/service"
"GoNavi-Wails/internal/app"
"github.com/wailsapp/wails/v2/pkg/menu"
"github.com/wailsapp/wails/v2/pkg/menu/keys"
"github.com/wailsapp/wails/v2/pkg/options"
"github.com/wailsapp/wails/v2/pkg/options/mac"
"github.com/wailsapp/wails/v2/pkg/options/windows"
)
func TestNewDesktopAgentToolCatalogIncludesWorkspaceInspection(t *testing.T) {
application := app.NewApp()
service := aiservice.NewService()
catalog, err := newDesktopAgentToolCatalog(application, service)
if err != nil {
t.Fatalf("newDesktopAgentToolCatalog: %v", err)
}
for _, name := range []string{"execute_sql", "inspect_active_tab"} {
descriptor, executor, err := catalog.Resolve(context.Background(), name)
if err != nil || executor == nil {
t.Fatalf("Resolve(%q) = %#v, %v", name, descriptor, err)
}
if name == "inspect_active_tab" && descriptor.Effect != runharness.ToolEffectReadOnly {
t.Fatalf("workspace tool effect = %q, want read_only", descriptor.Effect)
}
}
}
func TestShouldEnableWindowsMSISingleInstanceOnlyForInstalledMainGUI(t *testing.T) {
installDir := t.TempDir()
executablePath := filepath.Join(installDir, "GoNavi.exe")
if shouldEnableWindowsMSISingleInstance("windows", executablePath) {
t.Fatal("Portable executable unexpectedly enabled single-instance mode")
}
if err := os.WriteFile(filepath.Join(installDir, ".gonavi-msi-install"), []byte("MSI"), 0o644); err != nil {
t.Fatalf("WriteFile MSI marker: %v", err)
}
if !shouldEnableWindowsMSISingleInstance("windows", executablePath) {
t.Fatal("MSI executable did not enable single-instance lock")
}
if shouldEnableWindowsMSISingleInstance("darwin", executablePath) {
t.Fatal("non-Windows executable unexpectedly enabled single-instance mode")
}
}
func TestPrimaryWindowActivatorQueuesRequestsUntilRuntimeStartup(t *testing.T) {
type activationContextKey struct{}
ctx := context.WithValue(context.Background(), activationContextKey{}, "ready")
activatedWith := make([]context.Context, 0, 2)
activator := primaryWindowActivator{
show: func(runtimeCtx context.Context) {
activatedWith = append(activatedWith, runtimeCtx)
},
}
activator.requestActivation()
if len(activatedWith) != 0 {
t.Fatalf("activation ran before startup context was available: %d", len(activatedWith))
}
activator.bindRuntimeContext(ctx)
if len(activatedWith) != 1 || activatedWith[0] != ctx {
t.Fatalf("queued activation contexts = %#v, want startup context", activatedWith)
}
activator.requestActivation()
if len(activatedWith) != 2 || activatedWith[1] != ctx {
t.Fatalf("live activation contexts = %#v, want startup context twice", activatedWith)
}
}
func TestMainReturnsNonZeroForSpecialModeFailure(t *testing.T) {
const helperEnv = "GONAVI_SPECIAL_MODE_FAILURE_HELPER"
if os.Getenv(helperEnv) == "1" {
os.Args = []string{"GoNavi", "mcp-server", "invalid-mode"}
main()
return
}
cmd := exec.Command(os.Args[0], "-test.run=^TestMainReturnsNonZeroForSpecialModeFailure$")
cmd.Env = append(os.Environ(), helperEnv+"=1")
err := cmd.Run()
var exitErr *exec.ExitError
if !errors.As(err, &exitErr) {
t.Fatalf("main returned success for a special-mode failure: %v", err)
}
if exitErr.ExitCode() != 1 {
t.Fatalf("special-mode failure exit code = %d, want 1", exitErr.ExitCode())
}
}
func TestMainReturnsZeroForSuccessfulSpecialMode(t *testing.T) {
const helperEnv = "GONAVI_SPECIAL_MODE_SUCCESS_HELPER"
if os.Getenv(helperEnv) == "1" {
os.Args = []string{
"GoNavi",
"mcp-server",
"remote-config",
"--client",
"openclaw",
"--url",
"https://example.com/mcp",
"--token",
"test-token",
}
main()
return
}
cmd := exec.Command(os.Args[0], "-test.run=^TestMainReturnsZeroForSuccessfulSpecialMode$")
cmd.Env = append(os.Environ(), helperEnv+"=1")
if err := cmd.Run(); err != nil {
t.Fatalf("successful special mode returned error: %v", err)
}
}
func TestIsLowMemoryMode(t *testing.T) {
tests := []struct {
name string
env string
want bool
}{
{name: "disabled by default", env: "", want: false},
{name: "enabled with one", env: "1", want: true},
{name: "enabled with true", env: "true", want: true},
{name: "enabled with yes and whitespace", env: " yes ", want: true},
{name: "disabled with false", env: "false", want: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Setenv("GONAVI_LOW_MEMORY_MODE", tt.env)
if got := isLowMemoryMode(); got != tt.want {
t.Fatalf("isLowMemoryMode() = %v, want %v", got, tt.want)
}
})
}
}
func TestResolveInitialWindowStartStateDoesNotOverrideFrontendPreference(t *testing.T) {
for _, goos := range []string{"windows", "darwin", "linux"} {
t.Run(goos, func(t *testing.T) {
if got := resolveInitialWindowStartState(goos); got != options.Normal {
t.Fatalf("resolveInitialWindowStartState(%q) = %v, want Normal", goos, got)
}
})
}
}
func TestResolveMainWindowChromeUsesNativeMacControlsFromCreation(t *testing.T) {
macChrome := resolveMainWindowChrome(" Darwin ")
if macChrome.Frameless {
t.Fatal("macOS main window unexpectedly uses frameless Windows-style chrome")
}
if macChrome.TitleBar == nil {
t.Fatal("macOS main window title bar is nil")
}
if got, want := *macChrome.TitleBar, *mac.TitleBarHidden(); got != want {
t.Fatalf("macOS title bar = %+v, want %+v", got, want)
}
for _, goos := range []string{"windows", "linux"} {
t.Run(goos, func(t *testing.T) {
chrome := resolveMainWindowChrome(goos)
if !chrome.Frameless {
t.Fatalf("%s main window unexpectedly enables native macOS chrome", goos)
}
if chrome.TitleBar != nil {
t.Fatalf("%s main window title bar = %+v, want nil", goos, chrome.TitleBar)
}
})
}
}
func TestResolveWindowVisualOptions(t *testing.T) {
tests := []struct {
name string
goos string
lowMemoryMode bool
wantBackground options.RGBA
wantWebviewOpaque bool
wantWindowOpaque bool
wantWindowsBackdrop windows.BackdropType
}{
{
name: "windows defaults to opaque without acrylic",
goos: "windows",
wantBackground: options.RGBA{R: 255, G: 255, B: 255, A: 255},
wantWebviewOpaque: true,
wantWindowOpaque: true,
wantWindowsBackdrop: windows.None,
},
{
name: "windows low memory remains opaque",
goos: " Windows ",
lowMemoryMode: true,
wantBackground: options.RGBA{R: 255, G: 255, B: 255, A: 255},
wantWebviewOpaque: true,
wantWindowOpaque: true,
wantWindowsBackdrop: windows.None,
},
{
name: "mac default remains transparent",
goos: "darwin",
wantBackground: options.RGBA{R: 0, G: 0, B: 0, A: 0},
wantWebviewOpaque: false,
wantWindowOpaque: false,
wantWindowsBackdrop: windows.Acrylic,
},
{
name: "low memory remains opaque on other platforms",
goos: "darwin",
lowMemoryMode: true,
wantBackground: options.RGBA{R: 255, G: 255, B: 255, A: 255},
wantWebviewOpaque: true,
wantWindowOpaque: true,
wantWindowsBackdrop: windows.None,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
background, windowsOptions := resolveWindowVisualOptions(tt.goos, tt.lowMemoryMode)
if background == nil {
t.Fatal("resolveWindowVisualOptions() background is nil")
}
if got := *background; got != tt.wantBackground {
t.Fatalf("background = %+v, want %+v", got, tt.wantBackground)
}
if windowsOptions == nil {
t.Fatal("resolveWindowVisualOptions() windows options are nil")
}
if got := !windowsOptions.WebviewIsTransparent; got != tt.wantWebviewOpaque {
t.Fatalf("webview opaque = %v, want %v", got, tt.wantWebviewOpaque)
}
if got := !windowsOptions.WindowIsTranslucent; got != tt.wantWindowOpaque {
t.Fatalf("window opaque = %v, want %v", got, tt.wantWindowOpaque)
}
if got := windowsOptions.BackdropType; got != tt.wantWindowsBackdrop {
t.Fatalf("Windows backdrop = %v, want %v", got, tt.wantWindowsBackdrop)
}
if windowsOptions.Messages == nil {
t.Fatal("Windows WebView2 runtime messages are nil")
}
if !strings.Contains(windowsOptions.Messages.Webview2NotInstalled, "WebView2") {
t.Fatalf("WebView2 missing-runtime message = %q", windowsOptions.Messages.Webview2NotInstalled)
}
})
}
}
func TestBuildMacApplicationMenu(t *testing.T) {
called := 0
appMenu := buildMacApplicationMenu(func() {
called++
}, true)
if appMenu == nil {
t.Fatal("buildMacApplicationMenu() returned nil")
}
if len(appMenu.Items) != 3 {
t.Fatalf("expected 3 top-level menu items, got %d", len(appMenu.Items))
}
if appMenu.Items[0].Role != menu.AppMenuRole {
t.Fatalf("first top-level menu role = %v, want %v", appMenu.Items[0].Role, menu.AppMenuRole)
}
if appMenu.Items[1].Role != menu.EditMenuRole {
t.Fatalf("second top-level menu role = %v, want %v", appMenu.Items[1].Role, menu.EditMenuRole)
}
queryEditorMenu := appMenu.Items[2]
if queryEditorMenu.Label != "SQL" {
t.Fatalf("query editor menu label = %q, want %q", queryEditorMenu.Label, "SQL")
}
if queryEditorMenu.SubMenu == nil || len(queryEditorMenu.SubMenu.Items) != 1 {
t.Fatalf("query editor submenu items = %d, want 1", len(queryEditorMenu.SubMenu.Items))
}
copyCurrentLineItem := queryEditorMenu.SubMenu.Items[0]
if copyCurrentLineItem.Label != "Copy Current Line" {
t.Fatalf("menu item label = %q, want %q", copyCurrentLineItem.Label, "Copy Current Line")
}
if copyCurrentLineItem.Accelerator == nil {
t.Fatal("menu item accelerator is nil")
}
if copyCurrentLineItem.Accelerator.Key != "e" {
t.Fatalf("menu item accelerator key = %q, want %q", copyCurrentLineItem.Accelerator.Key, "e")
}
if len(copyCurrentLineItem.Accelerator.Modifiers) != 1 || copyCurrentLineItem.Accelerator.Modifiers[0] != keys.CmdOrCtrlKey {
t.Fatalf("menu item modifiers = %v, want [%v]", copyCurrentLineItem.Accelerator.Modifiers, keys.CmdOrCtrlKey)
}
copyCurrentLineItem.Click(&menu.CallbackData{MenuItem: copyCurrentLineItem})
if called != 1 {
t.Fatalf("native select-current-line callback called %d times, want 1", called)
}
}