47 lines
1.4 KiB
Go
47 lines
1.4 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestDefaultInvocationStartsTUIWithNoProfileState(t *testing.T) {
|
|
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
|
var output bytes.Buffer
|
|
if err := run(nil, strings.NewReader("q"), &output); err != nil {
|
|
t.Fatalf("run proxyctl: %v", err)
|
|
}
|
|
if !strings.Contains(output.String(), "No Active Profile") {
|
|
t.Fatalf("default TUI output missing no-Profile state: %q", output.String())
|
|
}
|
|
}
|
|
|
|
func TestDefaultInvocationLoadsPersistedActiveProfile(t *testing.T) {
|
|
xdg := t.TempDir()
|
|
t.Setenv("XDG_CONFIG_HOME", xdg)
|
|
writeConfig(t, filepath.Join(xdg, "proxyctl", "profiles.json"), `{
|
|
"profiles": [{"name":"office","mode":"single","singleUrl":"http://proxy.example:8080","httpUrl":"http://proxy.example:8080","httpsUrl":"http://proxy.example:8080","allUrl":"http://proxy.example:8080"}],
|
|
"activeProfile": "office"
|
|
}`)
|
|
var output bytes.Buffer
|
|
if err := run(nil, strings.NewReader("q"), &output); err != nil {
|
|
t.Fatalf("run proxyctl: %v", err)
|
|
}
|
|
if !strings.Contains(output.String(), "Active Profile: office") {
|
|
t.Fatalf("default TUI output missing Active Profile: %q", output.String())
|
|
}
|
|
}
|
|
|
|
func writeConfig(t *testing.T, path, contents string) {
|
|
t.Helper()
|
|
if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(path, []byte(contents), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|