package domain import ( "errors" "path/filepath" "testing" ) func TestLaunchSpecNormalizeCanonicalizesSafeFields(t *testing.T) { profile := filepath.Join(t.TempDir(), "profile") normalized, err := (LaunchSpec{ Kind: BrowserChrome, Executable: " chrome.exe ", UserDataDir: profile + string(filepath.Separator), ProfileDirectory: " Default ", TargetURL: " https://example.com/path ", ExtraArgs: []string{"--no-first-run"}, }).Normalize() if err != nil { t.Fatalf("Normalize() error = %v", err) } if normalized.Executable != "chrome.exe" || normalized.UserDataDir != filepath.Clean(profile) || normalized.ProfileDirectory != "Default" || normalized.TargetURL != "https://example.com/path" { t.Fatalf("normalized spec = %#v", normalized) } normalized.ExtraArgs[0] = "changed" if normalized.ExtraArgs[0] == "" { t.Fatal("extra args unexpectedly empty") } } func TestLaunchSpecNormalizeRejectsUnsafeValues(t *testing.T) { base := LaunchSpec{Kind: BrowserEdge, UserDataDir: filepath.Join(t.TempDir(), "profile"), TargetURL: "https://example.com"} cases := []LaunchSpec{ {UserDataDir: base.UserDataDir, TargetURL: base.TargetURL}, {Kind: base.Kind, UserDataDir: "relative", TargetURL: base.TargetURL}, {Kind: base.Kind, UserDataDir: base.UserDataDir, TargetURL: "file:///tmp/test"}, {Kind: base.Kind, UserDataDir: base.UserDataDir, TargetURL: "https://user@example.com"}, } for i, spec := range cases { if _, err := spec.Normalize(); !errors.Is(err, ErrInvalidLaunchSpec) { t.Errorf("case %d error = %v, want ErrInvalidLaunchSpec", i, err) } } }