95 lines
3.2 KiB
Go
95 lines
3.2 KiB
Go
package config
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"chub/internal/domain"
|
|
)
|
|
|
|
func TestStoreRoundTripAndCreatesPrivateFile(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "nested", "config.json")
|
|
store, err := New(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
want := File{Settings: Settings{ChromePath: "chrome.exe", RemoteDebugStartPort: domain.DefaultRemoteDebugPort, CloseOnExit: true}, Instances: []Instance{{ID: "a", Name: "运营", Launch: domain.LaunchSpec{Kind: domain.BrowserChrome, UserDataDir: `C:\profiles\a`}}}}
|
|
if err := store.Save(want); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, err := store.Load()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got.Version != currentVersion || got.Settings != want.Settings || len(got.Instances) != 1 || got.Instances[0].ID != "a" {
|
|
t.Fatalf("round trip mismatch: %#v", got)
|
|
}
|
|
}
|
|
|
|
func TestStoreAddsDefaultRemoteDebugPortForExistingConfig(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "config.json")
|
|
if err := os.WriteFile(path, []byte(`{"version":1,"settings":{},"instances":[]}`), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
store, err := New(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, err := store.Load()
|
|
if err != nil || got.Settings.RemoteDebugStartPort != domain.DefaultRemoteDebugPort {
|
|
t.Fatalf("settings = %#v, error = %v", got.Settings, err)
|
|
}
|
|
}
|
|
|
|
func TestStoreMissingFileReturnsDefaults(t *testing.T) {
|
|
store, err := New(filepath.Join(t.TempDir(), "config.json"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, err := store.Load()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got.Version != currentVersion || len(got.Instances) != 0 {
|
|
t.Fatalf("unexpected defaults: %#v", got)
|
|
}
|
|
}
|
|
|
|
func TestStoreRoundTripsReferencedProxyWithoutCredentials(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "config.json")
|
|
store, err := New(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
want := File{
|
|
Proxies: []ProxyProfile{{ID: "proxy-sg", Name: "新加坡出口", Server: "HTTPS://Proxy.Local:8443"}},
|
|
Instances: []Instance{{ID: "a", Name: "运营", ProxyID: "proxy-sg", Launch: domain.LaunchSpec{Kind: domain.BrowserChrome, UserDataDir: `C:\profiles\a`}}},
|
|
}
|
|
if err := store.Save(want); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, err := store.Load()
|
|
if err != nil || len(got.Proxies) != 1 || got.Proxies[0].Server != "https://proxy.local:8443" || got.Instances[0].ProxyID != "proxy-sg" || got.Instances[0].Launch.ProxyServer != "" {
|
|
t.Fatalf("proxy round trip = %#v, error = %v", got, err)
|
|
}
|
|
}
|
|
|
|
func TestStoreMigratesLegacyProxyAndRejectsUnsafeConfig(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "config.json")
|
|
if err := os.WriteFile(path, []byte(`{"version":1,"settings":{},"instances":[{"id":"a","name":"运营","launch":{"Kind":"chrome","UserDataDir":"C:\\profiles\\a","ProxyServer":"http://127.0.0.1:8080"}}]}`), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
store, err := New(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, err := store.Load()
|
|
if err != nil || len(got.Proxies) != 1 || got.Instances[0].ProxyID == "" || got.Instances[0].Launch.ProxyServer != "" {
|
|
t.Fatalf("legacy proxy migration = %#v, error = %v", got, err)
|
|
}
|
|
if err := store.Save(File{Proxies: []ProxyProfile{{ID: "bad", Name: "bad", Server: "http://user:secret@127.0.0.1:8080"}}}); err == nil {
|
|
t.Fatal("credential proxy was persisted")
|
|
}
|
|
}
|