package app_test import ( "path/filepath" "strings" "testing" "proxyctl/internal/app" "proxyctl/internal/profile" ) func TestUserCanOpenSingleProfileInAdvancedModeWithoutLosingItsValue(t *testing.T) { application, err := app.New(profile.NewFileStore(filepath.Join(t.TempDir(), "profiles.json"))) if err != nil { t.Fatal(err) } created := application.Dispatch(app.Intent{Kind: app.CreateProfile, Profile: profile.Profile{ Name: "home", Mode: profile.SingleMode, SingleURL: "socks5h://127.0.0.1:1080", }}) if created.Err != nil { t.Fatal(created.Err) } edited := application.Dispatch(app.Intent{Kind: app.UpdateProfile, Name: "home", Profile: profile.Profile{ Name: "home", Mode: profile.AdvancedMode, SingleURL: "socks5h://127.0.0.1:1080", }}) if edited.Err != nil { t.Fatalf("open advanced editing: %v", edited.Err) } got := edited.Snapshot.Profiles[0] if got.HTTPURL != got.SingleURL || got.HTTPSURL != got.SingleURL || got.AllURL != got.SingleURL { t.Fatalf("advanced values = %q, %q, %q; want preserved %q", got.HTTPURL, got.HTTPSURL, got.AllURL, got.SingleURL) } } func TestUserCanViewEditActivateAndDeleteProfiles(t *testing.T) { application, err := app.New(profile.NewFileStore(filepath.Join(t.TempDir(), "profiles.json"))) if err != nil { t.Fatal(err) } for _, p := range []profile.Profile{ {Name: "home", Mode: profile.SingleMode, SingleURL: "http://localhost:8080"}, {Name: "office", Mode: profile.AdvancedMode, HTTPURL: "http://proxy.example:3128", HTTPSURL: "https://proxy.example:4443", AllURL: "socks5://proxy.example:1080"}, } { if result := application.Dispatch(app.Intent{Kind: app.CreateProfile, Profile: p}); result.Err != nil { t.Fatalf("create %s: %v", p.Name, result.Err) } } if result := application.Dispatch(app.Intent{Kind: app.CreateProfile, Profile: profile.Profile{Name: "home", Mode: profile.SingleMode, SingleURL: "http://localhost:9000"}}); result.Err == nil || !strings.Contains(result.Err.Error(), "already exists") { t.Fatalf("duplicate name error = %v", result.Err) } if result := application.Dispatch(app.Intent{Kind: app.ActivateProfile, Name: "office"}); result.Err != nil { t.Fatal(result.Err) } updated := application.Dispatch(app.Intent{Kind: app.UpdateProfile, Name: "office", Profile: profile.Profile{ Name: "work", Mode: profile.AdvancedMode, HTTPURL: "http://proxy.example:3128", AllURL: "socks5h://proxy.example:1080", }}) if updated.Err != nil { t.Fatal(updated.Err) } if updated.Snapshot.ActiveProfile != "work" { t.Fatalf("renamed Active Profile = %q", updated.Snapshot.ActiveProfile) } deleted := application.Dispatch(app.Intent{Kind: app.DeleteProfile, Name: "home", Confirm: true}) if deleted.Err != nil || len(deleted.Snapshot.Profiles) != 1 { t.Fatalf("delete Profile result = %#v", deleted) } if unconfirmed := application.Dispatch(app.Intent{Kind: app.DeleteProfile, Name: "work"}); unconfirmed.Err == nil { t.Fatal("deleting the final Profile did not require confirmation") } confirmed := application.Dispatch(app.Intent{Kind: app.DeleteProfile, Name: "work", Confirm: true}) if confirmed.Err != nil || confirmed.Snapshot.ActiveProfile != "" || len(confirmed.Snapshot.Profiles) != 0 { t.Fatalf("delete final Profile result = %#v", confirmed) } }