Files
elmprodvpn/selective-vpn-api/app/traffic_appkey_test.go

136 lines
3.3 KiB
Go

package app
import "testing"
func TestCanonicalizeAppKey(t *testing.T) {
tests := []struct {
name string
appKey string
command string
want string
}{
{
name: "path vs bare command normalized to lowercase basename",
command: "/usr/bin/Google-Chrome-Stable --new-window",
want: "google-chrome-stable",
},
{
name: "quoted path with spaces",
command: "'/opt/My Apps/Opera' --private",
want: "opera",
},
{
name: "env wrapper skips assignments",
command: "env GTK_THEME=Adwaita /usr/bin/Brave-Browser --incognito",
want: "brave-browser",
},
{
name: "flatpak run app id",
command: "flatpak run org.mozilla.Firefox",
want: "flatpak:org.mozilla.firefox",
},
{
name: "snap run app id",
command: "snap run --experimental foo.Bar",
want: "snap:foo.bar",
},
{
name: "gtk-launch desktop id",
command: "gtk-launch Org.Gnome.Nautilus.desktop",
want: "desktop:org.gnome.nautilus.desktop",
},
{
name: "explicit app key fallback",
appKey: "Opera",
want: "opera",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
got := canonicalizeAppKey(tc.appKey, tc.command)
if got != tc.want {
t.Fatalf("canonicalizeAppKey(%q,%q) = %q, want %q", tc.appKey, tc.command, got, tc.want)
}
})
}
}
func TestSplitCommandTokens(t *testing.T) {
in := `env A=1 "/opt/My App/bin/App" --flag="x y"`
got := splitCommandTokens(in)
want := []string{"env", "A=1", "/opt/My App/bin/App", "--flag=x y"}
if len(got) != len(want) {
t.Fatalf("tokens len=%d want=%d tokens=%v", len(got), len(want), got)
}
for i := range want {
if got[i] != want[i] {
t.Fatalf("token[%d]=%q want=%q all=%v", i, got[i], want[i], got)
}
}
}
func TestDedupeTrafficAppProfilesByCanonicalAppKey(t *testing.T) {
in := []TrafficAppProfile{
{
ID: "chrome-old",
Target: "VPN",
AppKey: "Google-Chrome-Stable",
Command: "/usr/bin/Google-Chrome-Stable --new-window",
UpdatedAt: "2026-02-20T10:00:00Z",
},
{
ID: "chrome-new",
Target: "vpn",
AppKey: "google-chrome-stable",
Command: "google-chrome-stable --incognito",
UpdatedAt: "2026-02-20T11:00:00Z",
},
}
out, changed := dedupeTrafficAppProfiles(in)
if !changed {
t.Fatalf("expected changed=true")
}
if len(out) != 1 {
t.Fatalf("expected 1 profile, got %d", len(out))
}
if out[0].ID != "chrome-new" {
t.Fatalf("expected newest profile to win, got id=%q", out[0].ID)
}
if out[0].AppKey != "google-chrome-stable" {
t.Fatalf("expected canonical app key, got %q", out[0].AppKey)
}
}
func TestDedupeAppMarkItemsByCanonicalAppKey(t *testing.T) {
in := []appMarkItem{
{
ID: 101,
Target: "VPN",
AppKey: "Opera",
Command: "/usr/bin/Opera --private",
AddedAt: "2026-02-20T10:00:00Z",
},
{
ID: 202,
Target: "vpn",
AppKey: "opera",
Command: "opera --new-window",
AddedAt: "2026-02-20T11:00:00Z",
},
}
out, changed := dedupeAppMarkItems(in)
if !changed {
t.Fatalf("expected changed=true")
}
if len(out) != 1 {
t.Fatalf("expected 1 app mark item, got %d", len(out))
}
if out[0].ID != 202 {
t.Fatalf("expected newest item to win, got id=%d", out[0].ID)
}
if out[0].AppKey != "opera" {
t.Fatalf("expected canonical app key, got %q", out[0].AppKey)
}
}