105 lines
2.9 KiB
Go
105 lines
2.9 KiB
Go
package desktop
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/Apale7/opencode-provider-switch/internal/config"
|
|
)
|
|
|
|
func TestDesktopHTTPHandlerServesOverviewAndStaticApp(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
path := filepath.Join(t.TempDir(), "ocswitch.json")
|
|
cfg, err := config.Load(path)
|
|
if err != nil {
|
|
t.Fatalf("config.Load() error = %v", err)
|
|
}
|
|
cfg.UpsertProvider(config.Provider{
|
|
ID: "demo",
|
|
Name: "Demo",
|
|
BaseURL: "https://example.com/v1",
|
|
APIKey: "sk-demo-12345678",
|
|
Models: []string{"gpt-4.1-mini"},
|
|
})
|
|
cfg.UpsertAlias(config.Alias{
|
|
Alias: "chat",
|
|
DisplayName: "Chat",
|
|
Enabled: true,
|
|
Targets: []config.Target{{
|
|
Provider: "demo",
|
|
Model: "gpt-4.1-mini",
|
|
Enabled: true,
|
|
}},
|
|
})
|
|
if err := cfg.Save(); err != nil {
|
|
t.Fatalf("cfg.Save() error = %v", err)
|
|
}
|
|
|
|
h, err := newHandler(New(path), "test", "http://127.0.0.1:9982")
|
|
if err != nil {
|
|
t.Fatalf("newHandler() error = %v", err)
|
|
}
|
|
|
|
t.Run("overview api", func(t *testing.T) {
|
|
req := httptest.NewRequest(http.MethodGet, "/api/overview", nil)
|
|
resp := httptest.NewRecorder()
|
|
h.ServeHTTP(resp, req)
|
|
|
|
if resp.Code != http.StatusOK {
|
|
t.Fatalf("status = %d, want %d", resp.Code, http.StatusOK)
|
|
}
|
|
var payload struct {
|
|
Data struct {
|
|
ProviderCount int `json:"providerCount"`
|
|
AliasCount int `json:"aliasCount"`
|
|
Aliases []string `json:"availableAliases"`
|
|
} `json:"data"`
|
|
}
|
|
if err := json.Unmarshal(resp.Body.Bytes(), &payload); err != nil {
|
|
t.Fatalf("json.Unmarshal() error = %v", err)
|
|
}
|
|
if payload.Data.ProviderCount != 1 || payload.Data.AliasCount != 1 {
|
|
t.Fatalf("unexpected counts: %#v", payload.Data)
|
|
}
|
|
if len(payload.Data.Aliases) != 1 || payload.Data.Aliases[0] != "chat" {
|
|
t.Fatalf("unexpected aliases: %#v", payload.Data.Aliases)
|
|
}
|
|
})
|
|
|
|
t.Run("save desktop prefs", func(t *testing.T) {
|
|
req := httptest.NewRequest(http.MethodPost, "/api/desktop-prefs", strings.NewReader(`{"launchAtLogin":true,"minimizeToTray":true,"notifications":true}`))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
resp := httptest.NewRecorder()
|
|
h.ServeHTTP(resp, req)
|
|
|
|
if resp.Code != http.StatusOK {
|
|
t.Fatalf("status = %d, want %d", resp.Code, http.StatusOK)
|
|
}
|
|
loaded, err := config.Load(path)
|
|
if err != nil {
|
|
t.Fatalf("config.Load() error = %v", err)
|
|
}
|
|
if !loaded.Desktop.LaunchAtLogin || !loaded.Desktop.MinimizeToTray || !loaded.Desktop.Notifications {
|
|
t.Fatalf("persisted desktop prefs = %#v", loaded.Desktop)
|
|
}
|
|
})
|
|
|
|
t.Run("serves app shell", func(t *testing.T) {
|
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
resp := httptest.NewRecorder()
|
|
h.ServeHTTP(resp, req)
|
|
|
|
if resp.Code != http.StatusOK {
|
|
t.Fatalf("status = %d, want %d", resp.Code, http.StatusOK)
|
|
}
|
|
if !strings.Contains(resp.Body.String(), "ocswitch desktop") {
|
|
t.Fatalf("unexpected body = %q", resp.Body.String())
|
|
}
|
|
})
|
|
}
|