package schema import ( "strings" "testing" ) func TestEmbeddedSchema(t *testing.T) { s, err := Embedded() if err != nil { t.Fatal(err) } if len(s.Options) < 150 { t.Fatalf("only %d options in embedded schema", len(s.Options)) } for _, o := range s.Options { if o.Kind == "" { t.Errorf("%s has no kind", o.Key) } if o.Category == "" { t.Errorf("%s has no category", o.Key) } if (o.Kind == KindEnum || o.Kind == KindChoice) && len(o.Values) == 0 { t.Errorf("%s has no values", o.Key) } if o.Kind == KindEnum && o.Default() != "" && !contains(o.Values, o.Default()) { t.Errorf("%s: default %q is not a valid value", o.Key, o.Default()) } if !o.Repeatable && o.Default() != "" { if err := o.Validate(o.Default()); err != nil { t.Errorf("%s: default %q does not validate: %v", o.Key, o.Default(), err) } } } for _, key := range []string{"font-family", "keybind", "palette", "theme", "cursor-style", "shell-integration-features"} { if s.Lookup(key) == nil { t.Errorf("missing %s", key) } } } func TestParseDocs(t *testing.T) { out := `# Font families. # # Can be repeated. font-family = font-family-bold = # The palette. palette = 0=#000000 palette = 1=#ff0000 selection-word-chars = '" ` got := ParseDocs(out) if len(got) != 4 { t.Fatalf("got %d entries", len(got)) } if got[0].Doc != "Font families.\n\nCan be repeated." { t.Errorf("doc = %q", got[0].Doc) } if got[1].Doc != got[0].Doc { t.Errorf("font-family-bold should share the font-family docs") } if len(got[2].Defaults) != 2 { t.Errorf("palette defaults = %v", got[2].Defaults) } if got[3].Defaults[0] != " \t'\"" { t.Errorf("leading whitespace must be kept, got %q", got[3].Defaults[0]) } } func TestValidate(t *testing.T) { opt := func(k Kind) *Option { return &Option{Kind: k, Values: []string{"cell-foreground"}, Flags: []string{"cursor", "sudo"}} } cases := []struct { kind Kind value string ok bool }{ {KindBool, "true", true}, {KindBool, "yes", false}, {KindColor, "#1e1e2e", true}, {KindColor, "fff", true}, {KindColor, "dark slate gray", true}, {KindColor, "cell-foreground", true}, {KindColor, "#12345", false}, {KindMetric, "-10%", true}, {KindMetric, "2", true}, {KindMetric, "1.5", false}, {KindDuration, "750ms", true}, {KindDuration, "1h 30m", true}, {KindDuration, "0", true}, {KindDuration, "5", false}, {KindPadding, "2,4", true}, {KindPadding, "a", false}, {KindFlags, "no-cursor,sudo", true}, {KindFlags, "false", true}, {KindFlags, "bogus", false}, {KindKeybind, "ctrl+==reset_font_size", true}, {KindKeybind, "clear", true}, {KindKeybind, "ctrl+a", false}, {KindPalette, "4=#89b4fa", true}, {KindPalette, "256=#000000", false}, {KindFloat, "0.5", true}, {KindFloat, "x", false}, } for _, c := range cases { err := opt(c.kind).Validate(c.value) if (err == nil) != c.ok { t.Errorf("%s %q: err = %v, want ok=%v", c.kind, c.value, err, c.ok) } } } // Types and compatibility entries that arrived with Ghostty 1.4. func TestGhostty14Options(t *testing.T) { s, err := Embedded() if err != nil { t.Fatal(err) } cases := []struct { key, value string ok bool }{ {"scrollback-limit-lines", "unlimited", true}, {"scrollback-limit-bytes", "100000000", true}, {"scrollback-limit-bytes", "-1", false}, {"clipboard-write-limit-bytes", "lots", false}, {"window-padding-balance", "equal", true}, {"window-padding-balance", "maybe", false}, {"copy-on-select", "both", true}, {"copy-on-select", "true", true}, // old value, still read {"copy-on-select", "yes", false}, {"gtk-single-instance", "desktop", true}, } for _, c := range cases { o := s.Lookup(c.key) if o == nil { t.Fatalf("%s missing", c.key) } if err := o.Validate(c.value); (err == nil) != c.ok { t.Errorf("%s = %q: err = %v, want ok=%v", c.key, c.value, err, c.ok) } } if !s.Lookup("copy-on-select").IsOldValue("true") || s.Lookup("copy-on-select").IsOldValue("both") { t.Error("copy-on-select: old values wrong") } if c := s.OldKey("scrollback-limit"); c == nil || c.RenamedTo != "scrollback-limit-bytes" { t.Errorf("OldKey(scrollback-limit) = %+v", c) } if c := s.OldKey("bold-is-bright"); c == nil || c.RenamedTo != "" || len(c.Sets) != 1 || c.Sets[0] != "bold-color" { t.Errorf("OldKey(bold-is-bright) = %+v", c) } if s.OldKey("copy-on-select") != nil { t.Error("a current option is not an old key") } if got := s.OldNames("scrollback-limit-bytes"); len(got) != 1 || got[0] != "scrollback-limit" { t.Errorf("OldNames(scrollback-limit-bytes) = %v", got) } // Refreshing from the installed Ghostty keeps the old values. s.Refresh("# Copy\ncopy-on-select = none\n", "1.4.0") if !s.Lookup("copy-on-select").IsOldValue("false") { t.Error("old values lost on refresh") } } func TestFlags(t *testing.T) { o := &Option{Kind: KindFlags, Flags: []string{"cursor", "sudo", "title"}, Defaults: []string{"cursor,no-sudo,title"}} st := o.ParseFlags("no-cursor") if st["cursor"] || st["sudo"] || !st["title"] { t.Fatalf("state = %v", st) } if got := o.FormatFlags(st); got != "no-cursor,no-sudo,title" { t.Fatalf("format = %q", got) } } func TestSplitKeybind(t *testing.T) { cases := map[string][2]string{ "ctrl+a=new_tab": {"ctrl+a", "new_tab"}, "==text:=": {"=", "text:="}, "ctrl+==text:=hello": {"ctrl+=", "text:=hello"}, "super+shift+,=reload": {"super+shift+,", "reload"}, "ctrl+a>n=new_window": {"ctrl+a>n", "new_window"}, "§=goto_split:next": {"§", "goto_split:next"}, } for in, want := range cases { tr, ac := SplitKeybind(in) if tr != want[0] || ac != want[1] { t.Errorf("SplitKeybind(%q) = %q, %q", in, tr, ac) } } } func TestCategoriesCoverEverything(t *testing.T) { s, _ := Embedded() cats := s.Categories() total := 0 for _, c := range cats { total += len(s.InCategory(c)) } if total != len(s.Options) { t.Fatalf("categories hold %d of %d options", total, len(s.Options)) } if !strings.Contains(strings.Join(cats, ","), CatFonts) { t.Fatal("missing Fonts category") } } func TestPlatformDetection(t *testing.T) { s, _ := Embedded() want := map[string]string{ "keybind": "", "fullscreen": "", "window-theme": "", "window-step-resize": "macOS", "undo-timeout": "macOS", "window-subtitle": "Linux", "macos-icon": "macOS", "gtk-wide-tabs": "Linux", } for key, p := range want { if o := s.Lookup(key); o == nil || o.Platform != p { t.Errorf("%s: platform = %q, want %q", key, o.Platform, p) } } }