Files
proxy/cmd/proxyctl/main_test.go
T

186 lines
6.2 KiB
Go

package main
import (
"bytes"
"context"
"os"
"path/filepath"
"strings"
"testing"
"time"
"proxyctl/internal/profile"
"proxyctl/internal/target"
)
func TestVersionOutputIsHumanAndScriptFriendly(t *testing.T) {
previous := version
version = "v1.2.3"
t.Cleanup(func() { version = previous })
var output bytes.Buffer
if err := run([]string{"version"}, strings.NewReader(""), &output); err != nil {
t.Fatalf("run version: %v", err)
}
if got, want := output.String(), "proxyctl version v1.2.3\n"; got != want {
t.Fatalf("human version output = %q, want %q", got, want)
}
output.Reset()
if err := run([]string{"version", "--short"}, strings.NewReader(""), &output); err != nil {
t.Fatalf("run short version: %v", err)
}
if got, want := output.String(), "v1.2.3\n"; got != want {
t.Fatalf("script version output = %q, want %q", got, want)
}
}
func TestVersionDoesNotRequireUserOrTargetResolution(t *testing.T) {
var output bytes.Buffer
if err := runWithEnvironmentAndTargets([]string{"version", "--short"}, strings.NewReader(""), &output, failingIdentityEnvironment{}, nil, "", nil); err != nil {
t.Fatalf("version depended on runtime environment: %v", err)
}
}
func TestProductionCompositionRegistersAPTContainerGNOMEAndDaemonTargets(t *testing.T) {
home := t.TempDir()
environment := commandEnvironment{identity: profile.UserIdentity{Username: "tester", HomeDir: home, UID: os.Getuid(), GID: os.Getgid()}, env: map[string]string{"SHELL": "/bin/bash"}}
targets, err := productionTargets(environment)
if err != nil {
t.Fatal(err)
}
var ids []string
for _, adapter := range targets {
ids = append(ids, adapter.ID())
if _, ok := adapter.(target.Diagnosable); !ok {
t.Errorf("production Target %s does not contribute doctor checks", adapter.ID())
}
}
want := []string{"apt", "docker-containers", "gnome", "docker-daemon"}
if strings.Join(ids, ",") != strings.Join(want, ",") {
t.Fatalf("production Target IDs = %#v, want %#v", ids, want)
}
}
func TestDefaultInvocationStartsTUIWithNoProfileState(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
var output bytes.Buffer
if err := runWithSystemTargets(nil, strings.NewReader("q"), &output, []target.Target{}); 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 TestDefaultInvocationUsesAlternateScreenAndRestoresItOnExit(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
var output bytes.Buffer
if err := runWithSystemTargets(nil, strings.NewReader("q"), &output, []target.Target{}); err != nil {
t.Fatalf("run proxyctl: %v", err)
}
frame := output.String()
if !strings.Contains(frame, "\x1b[?1049h") {
t.Fatalf("default TUI did not enter the alternate screen: %q", frame)
}
if !strings.Contains(frame, "\x1b[?1049l") {
t.Fatalf("default TUI did not restore the previous screen: %q", frame)
}
}
type blockingDetectionTarget struct {
started chan struct{}
release chan struct{}
}
func (t *blockingDetectionTarget) ID() string { return "blocking-test" }
func (t *blockingDetectionTarget) Description() target.Description {
return target.Description{Name: "Blocking test Target"}
}
func (t *blockingDetectionTarget) Availability(context.Context) target.Availability {
return target.Availability{Available: true}
}
func (t *blockingDetectionTarget) Inspect(context.Context, *profile.Profile) target.Inspection {
return target.Inspection{State: target.Disabled}
}
func (t *blockingDetectionTarget) Confirmation(bool) string { return "" }
func (t *blockingDetectionTarget) SetDesired(context.Context, bool, *profile.Profile) target.OperationResult {
return target.OperationResult{}
}
func (t *blockingDetectionTarget) Detect(context.Context, *profile.Profile) target.Detection {
select {
case t.started <- struct{}{}:
default:
}
<-t.release
return target.Detection{
Availability: target.Availability{Available: true, Evidence: "hermetic test Target"},
Inspection: target.Inspection{State: target.Disabled, Evidence: "no configuration"},
}
}
func TestDeferredExitCompletesSafelyAndRestoresAlternateScreen(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
adapter := &blockingDetectionTarget{started: make(chan struct{}, 1), release: make(chan struct{})}
var output bytes.Buffer
done := make(chan error, 1)
go func() {
done <- runWithSystemTargets(nil, strings.NewReader("q"), &output, []target.Target{adapter})
}()
select {
case <-adapter.started:
case err := <-done:
t.Fatalf("TUI exited before progressive startup began: %v", err)
case <-time.After(5 * time.Second):
t.Fatal("progressive startup did not reach the hermetic Target")
}
select {
case err := <-done:
t.Fatalf("deferred exit returned before safe operation completion: %v", err)
case <-time.After(50 * time.Millisecond):
}
close(adapter.release)
select {
case err := <-done:
if err != nil {
t.Fatalf("run proxyctl: %v", err)
}
case <-time.After(5 * time.Second):
t.Fatal("TUI did not exit after safe operation completion")
}
frame := output.String()
enter := strings.Index(frame, "\x1b[?1049h")
leave := strings.LastIndex(frame, "\x1b[?1049l")
if enter < 0 || leave <= enter {
t.Fatalf("deferred exit did not enter and restore the alternate screen in order: %q", frame)
}
}
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 := runWithSystemTargets(nil, strings.NewReader("q"), &output, []target.Target{}); 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)
}
}