clone

fifteen puzzle games for the terminal, with daily puzzles, weekly ladders and xp, built on bubble tea

chore/regenerate-vhs-gifs
puzzletea / cmd / config_test.go
config_test.go 4.2K
1package cmd23import (4 "bytes"5 "fmt"6 "path/filepath"7 "testing"89 "github.com/FelineStateMachine/puzzletea/config"10 "github.com/FelineStateMachine/puzzletea/store"11 "github.com/FelineStateMachine/puzzletea/theme"12 "github.com/spf13/cobra"13)1415func TestNewContinueAndRootUseActiveConfig(t *testing.T) {16 configPath, dbPath := writeCommandConfig(t)17 restore := snapshotCommandGlobals(t)18 defer restore()1920 flagConfigPath = configPath2122 var seen []string23 launchNewGameFn = func(gameArg, modeArg, seed string, cfg *config.Config) error {24 seen = append(seen, cfg.DBPath)25 if gameArg == "" {26 t.Fatal("expected game argument to be forwarded")27 }28 return nil29 }30 continueGameFn = func(name string, cfg *config.Config) error {31 seen = append(seen, cfg.DBPath)32 if name == "" {33 t.Fatal("expected continue name to be forwarded")34 }35 return nil36 }3738 if err := newCmd.RunE(newCmd, []string{"nonogram", "mini"}); err != nil {39 t.Fatalf("newCmd.RunE returned error: %v", err)40 }41 if err := continueCmd.RunE(continueCmd, []string{"resume-me"}); err != nil {42 t.Fatalf("continueCmd.RunE returned error: %v", err)43 }4445 flagNew = "sudoku:easy"46 if err := RootCmd.RunE(RootCmd, nil); err != nil {47 t.Fatalf("RootCmd.RunE returned error: %v", err)48 }49 flagNew = ""5051 flagContinue = "resume-me"52 if err := RootCmd.RunE(RootCmd, nil); err != nil {53 t.Fatalf("RootCmd.RunE returned error: %v", err)54 }5556 if got, want := len(seen), 4; got != want {57 t.Fatalf("saw %d config loads, want %d", got, want)58 }59 for i, got := range seen {60 if got != dbPath {61 t.Fatalf("config load %d used DB path %q, want %q", i, got, dbPath)62 }63 }64}6566func TestListCmdUsesActiveConfig(t *testing.T) {67 configPath, dbPath := writeCommandConfig(t)68 restore := snapshotCommandGlobals(t)69 defer restore()7071 flagConfigPath = configPath7273 s, err := store.Open(dbPath)74 if err != nil {75 t.Fatal(err)76 }77 t.Cleanup(func() { _ = s.Close() })78 if err := s.CreateGame(&store.GameRecord{79 Name: "from-custom-config",80 GameType: "Lights Out",81 Mode: "Easy",82 InitialState: "{}",83 SaveState: "{}",84 Status: store.StatusNew,85 RunKind: store.RunKindNormal,86 }); err != nil {87 t.Fatal(err)88 }8990 var out bytes.Buffer91 cmd := &cobra.Command{}92 cmd.SetOut(&out)93 if err := listCmd.RunE(cmd, nil); err != nil {94 t.Fatalf("listCmd.RunE returned error: %v", err)95 }96 if !bytes.Contains(out.Bytes(), []byte("from-custom-config")) {97 t.Fatalf("list output %q did not include custom DB record", out.String())98 }99}100101func TestLoadConfigThemeFlagOverridesPersistedTheme(t *testing.T) {102 configPath, _ := writeCommandConfig(t)103 restore := snapshotCommandGlobals(t)104 defer restore()105 t.Cleanup(func() { _ = theme.Apply("") })106107 flagTheme = "Dracula"108 if themeDoc := theme.LookupTheme(flagTheme); themeDoc == nil {109 t.Fatalf("expected theme %q to exist", flagTheme)110 }111112 _ = loadConfig(configPath)113114 want := theme.LookupTheme(flagTheme).Palette()115 got := theme.Current()116 if fmt.Sprint(got.FG) != fmt.Sprint(want.FG) {117 t.Fatalf("theme override FG = %v, want %v", got.FG, want.FG)118 }119}120121func snapshotCommandGlobals(t *testing.T) func() {122 t.Helper()123124 prevNew := flagNew125 prevContinue := flagContinue126 prevTheme := flagTheme127 prevConfigPath := flagConfigPath128 prevSetSeed := flagSetSeed129 prevWithSeed := flagWithSeed130 prevExport := flagExport131 prevOutput := flagOutput132 prevExportSpecPath := exportSpecPath133 prevListAll := listAll134 prevLaunchNew := launchNewGameFn135 prevLaunchSeeded := launchSeededGameFn136 prevContinueFn := continueGameFn137 prevOpenStore := openStoreFn138139 return func() {140 flagNew = prevNew141 flagContinue = prevContinue142 flagTheme = prevTheme143 flagConfigPath = prevConfigPath144 flagSetSeed = prevSetSeed145 flagWithSeed = prevWithSeed146 flagExport = prevExport147 flagOutput = prevOutput148 exportSpecPath = prevExportSpecPath149 listAll = prevListAll150 launchNewGameFn = prevLaunchNew151 launchSeededGameFn = prevLaunchSeeded152 continueGameFn = prevContinueFn153 openStoreFn = prevOpenStore154 }155}156157func writeCommandConfig(t *testing.T) (string, string) {158 t.Helper()159160 configPath := filepath.Join(t.TempDir(), "config.json")161 dbPath := filepath.Join(t.TempDir(), "history.db")162 cfg := &config.Config{163 DBPath: dbPath,164 Theme: "Nord",165 }166 if err := cfg.Save(configPath); err != nil {167 t.Fatal(err)168 }169 return configPath, dbPath170}