Files
navi/internal/config/servers_file_test.go
T
M4andCursor 8b564110d5
ci / test (macos-latest) (push) Waiting to run
ci / test (ubuntu-latest) (push) Waiting to run
ci / test (windows-latest) (push) Waiting to run
Release 3.8.2.12: export/import full server list as JSON.
Save and replace profiles via system file dialog; buttons after Best; prefs unchanged on import.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-01 17:51:38 +03:00

97 lines
2.3 KiB
Go

package config
import (
"os"
"path/filepath"
"testing"
)
func TestServersFileRoundTrip(t *testing.T) {
cfg := &Config{
Active: "b",
Profiles: []Profile{
{Name: "a", Protocol: ProtocolNaive, Proxy: "https://u:p@a.example"},
{Name: "b", Protocol: ProtocolHysteria2, Proxy: "hy2://tok@b.example:443"},
},
SystemProxy: true,
ConnectOnLaunch: true,
SubscriptionURL: "https://sub.example/x",
}
dir := t.TempDir()
path := filepath.Join(dir, "servers.json")
n, err := WriteServersFile(path, cfg)
if err != nil {
t.Fatal(err)
}
if n != 2 {
t.Fatalf("exported %d, want 2", n)
}
other := &Config{
Active: "old",
SystemProxy: false,
ConnectOnLaunch: false,
SubscriptionURL: "https://keep.example",
SubscriptionNames: []string{"old"},
Profiles: []Profile{
{Name: "old", Protocol: ProtocolNaive, Proxy: "https://old"},
},
}
doc, err := ReadServersFile(path)
if err != nil {
t.Fatal(err)
}
if err := other.ApplyServersReplace(doc); err != nil {
t.Fatal(err)
}
if other.SubscriptionURL != "https://keep.example" {
t.Fatalf("prefs changed: %q", other.SubscriptionURL)
}
if other.SystemProxy {
t.Fatal("system_proxy should stay false")
}
if len(other.Profiles) != 2 || other.Active != "b" {
t.Fatalf("got active=%q profiles=%+v", other.Active, other.Profiles)
}
if other.SubscriptionNames != nil {
t.Fatalf("subscription_names should clear, got %v", other.SubscriptionNames)
}
}
func TestParseServersFileBareArray(t *testing.T) {
doc, err := ParseServersFile([]byte(`[{"name":"x","protocol":"naive","proxy":"https://x"}]`))
if err != nil {
t.Fatal(err)
}
if len(doc.Profiles) != 1 || doc.Active != "x" {
t.Fatalf("unexpected: %+v", doc)
}
}
func TestParseServersFileEmptyRejected(t *testing.T) {
if _, err := ParseServersFile([]byte(`{"profiles":[]}`)); err == nil {
t.Fatal("expected error")
}
if _, err := ParseServersFile([]byte(`[]`)); err == nil {
t.Fatal("expected error")
}
}
func TestWriteServersFilePermissions(t *testing.T) {
cfg := &Config{
Active: "a",
Profiles: []Profile{{Name: "a", Protocol: ProtocolNaive}},
}
path := filepath.Join(t.TempDir(), "out.json")
if _, err := WriteServersFile(path, cfg); err != nil {
t.Fatal(err)
}
st, err := os.Stat(path)
if err != nil {
t.Fatal(err)
}
if st.Size() == 0 {
t.Fatal("empty file")
}
}