|
| 1 | +package config |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | + "github.com/stretchr/testify/require" |
| 8 | +) |
| 9 | + |
| 10 | +// ---- ToInstallOptions ---- |
| 11 | + |
| 12 | +func TestToInstallOptions_AllFields(t *testing.T) { |
| 13 | + rc := &RemoteConfig{Username: "alice", Slug: "setup"} |
| 14 | + cfg := &Config{ |
| 15 | + Version: "1.2.3", |
| 16 | + Preset: "developer", |
| 17 | + User: "alice", |
| 18 | + DryRun: true, |
| 19 | + Silent: true, |
| 20 | + PackagesOnly: true, |
| 21 | + Update: true, |
| 22 | + Shell: "install", |
| 23 | + Macos: "configure", |
| 24 | + Dotfiles: "clone", |
| 25 | + GitName: "Alice", |
| 26 | + GitEmail: "alice@example.com", |
| 27 | + PostInstall: "mise install", |
| 28 | + AllowPostInstall: true, |
| 29 | + DotfilesURL: "https://github.com/alice/dotfiles", |
| 30 | + RemoteConfig: rc, |
| 31 | + } |
| 32 | + |
| 33 | + opts := cfg.ToInstallOptions() |
| 34 | + require.NotNil(t, opts) |
| 35 | + |
| 36 | + assert.Equal(t, "1.2.3", opts.Version) |
| 37 | + assert.Equal(t, "developer", opts.Preset) |
| 38 | + assert.Equal(t, "alice", opts.User) |
| 39 | + assert.True(t, opts.DryRun) |
| 40 | + assert.True(t, opts.Silent) |
| 41 | + assert.True(t, opts.PackagesOnly) |
| 42 | + assert.True(t, opts.Update) |
| 43 | + assert.Equal(t, "install", opts.Shell) |
| 44 | + assert.Equal(t, "configure", opts.Macos) |
| 45 | + assert.Equal(t, "clone", opts.Dotfiles) |
| 46 | + assert.Equal(t, "Alice", opts.GitName) |
| 47 | + assert.Equal(t, "alice@example.com", opts.GitEmail) |
| 48 | + assert.Equal(t, "mise install", opts.PostInstall) |
| 49 | + assert.True(t, opts.AllowPostInstall) |
| 50 | + assert.Equal(t, "https://github.com/alice/dotfiles", opts.DotfilesURL) |
| 51 | +} |
| 52 | + |
| 53 | +func TestToInstallOptions_ZeroConfig(t *testing.T) { |
| 54 | + cfg := &Config{} |
| 55 | + opts := cfg.ToInstallOptions() |
| 56 | + require.NotNil(t, opts) |
| 57 | + |
| 58 | + assert.Equal(t, "", opts.Version) |
| 59 | + assert.Equal(t, "", opts.Preset) |
| 60 | + assert.Equal(t, "", opts.User) |
| 61 | + assert.False(t, opts.DryRun) |
| 62 | + assert.False(t, opts.Silent) |
| 63 | + assert.False(t, opts.PackagesOnly) |
| 64 | + assert.False(t, opts.Update) |
| 65 | + assert.Equal(t, "", opts.Shell) |
| 66 | + assert.Equal(t, "", opts.Macos) |
| 67 | + assert.Equal(t, "", opts.Dotfiles) |
| 68 | + assert.Equal(t, "", opts.GitName) |
| 69 | + assert.Equal(t, "", opts.GitEmail) |
| 70 | + assert.Equal(t, "", opts.PostInstall) |
| 71 | + assert.False(t, opts.AllowPostInstall) |
| 72 | + assert.Equal(t, "", opts.DotfilesURL) |
| 73 | +} |
| 74 | + |
| 75 | +// ---- ToInstallState ---- |
| 76 | + |
| 77 | +func TestToInstallState_AllFields(t *testing.T) { |
| 78 | + rc := &RemoteConfig{Username: "bob", Slug: "dev"} |
| 79 | + git := &SnapshotGitConfig{UserName: "Bob", UserEmail: "bob@example.com"} |
| 80 | + macOSPrefs := []RemoteMacOSPref{ |
| 81 | + {Domain: "com.apple.dock", Key: "autohide", Type: "bool", Value: "true"}, |
| 82 | + } |
| 83 | + |
| 84 | + cfg := &Config{ |
| 85 | + SelectedPkgs: map[string]bool{"git": true, "curl": false}, |
| 86 | + OnlinePkgs: []Package{{Name: "git", Description: "VCS"}}, |
| 87 | + SnapshotTaps: []string{"homebrew/core"}, |
| 88 | + RemoteConfig: rc, |
| 89 | + SnapshotGit: git, |
| 90 | + SnapshotMacOS: macOSPrefs, |
| 91 | + SnapshotDotfiles: "https://github.com/bob/dotfiles", |
| 92 | + SnapshotShellOhMyZsh: true, |
| 93 | + SnapshotShellTheme: "agnoster", |
| 94 | + SnapshotShellPlugins: []string{"git", "z"}, |
| 95 | + } |
| 96 | + |
| 97 | + state := cfg.ToInstallState() |
| 98 | + require.NotNil(t, state) |
| 99 | + |
| 100 | + assert.Equal(t, map[string]bool{"git": true, "curl": false}, state.SelectedPkgs) |
| 101 | + assert.Len(t, state.OnlinePkgs, 1) |
| 102 | + assert.Equal(t, "git", state.OnlinePkgs[0].Name) |
| 103 | + assert.Equal(t, []string{"homebrew/core"}, state.SnapshotTaps) |
| 104 | + assert.Equal(t, rc, state.RemoteConfig) |
| 105 | + assert.Equal(t, git, state.SnapshotGit) |
| 106 | + assert.Equal(t, macOSPrefs, state.SnapshotMacOS) |
| 107 | + assert.Equal(t, "https://github.com/bob/dotfiles", state.SnapshotDotfiles) |
| 108 | + assert.True(t, state.SnapshotShellOhMyZsh) |
| 109 | + assert.Equal(t, "agnoster", state.SnapshotShellTheme) |
| 110 | + assert.Equal(t, []string{"git", "z"}, state.SnapshotShellPlugins) |
| 111 | +} |
| 112 | + |
| 113 | +func TestToInstallState_ZeroConfig(t *testing.T) { |
| 114 | + cfg := &Config{} |
| 115 | + state := cfg.ToInstallState() |
| 116 | + require.NotNil(t, state) |
| 117 | + |
| 118 | + assert.Nil(t, state.SelectedPkgs) |
| 119 | + assert.Nil(t, state.OnlinePkgs) |
| 120 | + assert.Nil(t, state.SnapshotTaps) |
| 121 | + assert.Nil(t, state.RemoteConfig) |
| 122 | + assert.Nil(t, state.SnapshotGit) |
| 123 | + assert.Nil(t, state.SnapshotMacOS) |
| 124 | + assert.Equal(t, "", state.SnapshotDotfiles) |
| 125 | + assert.False(t, state.SnapshotShellOhMyZsh) |
| 126 | + assert.Equal(t, "", state.SnapshotShellTheme) |
| 127 | + assert.Nil(t, state.SnapshotShellPlugins) |
| 128 | +} |
| 129 | + |
| 130 | +// ---- ApplyState ---- |
| 131 | + |
| 132 | +func TestApplyState_AllFields(t *testing.T) { |
| 133 | + rc := &RemoteConfig{Username: "carol", Slug: "home"} |
| 134 | + git := &SnapshotGitConfig{UserName: "Carol", UserEmail: "carol@example.com"} |
| 135 | + macOSPrefs := []RemoteMacOSPref{ |
| 136 | + {Domain: "NSGlobalDomain", Key: "AppleShowScrollBars", Type: "string", Value: "Always"}, |
| 137 | + } |
| 138 | + |
| 139 | + state := &InstallState{ |
| 140 | + SelectedPkgs: map[string]bool{"ripgrep": true}, |
| 141 | + OnlinePkgs: []Package{{Name: "ripgrep", Description: "Search tool"}}, |
| 142 | + SnapshotTaps: []string{"homebrew/cask"}, |
| 143 | + RemoteConfig: rc, |
| 144 | + SnapshotGit: git, |
| 145 | + SnapshotMacOS: macOSPrefs, |
| 146 | + SnapshotDotfiles: "https://github.com/carol/dotfiles", |
| 147 | + SnapshotShellOhMyZsh: true, |
| 148 | + SnapshotShellTheme: "powerlevel10k", |
| 149 | + SnapshotShellPlugins: []string{"git", "zsh-autosuggestions"}, |
| 150 | + } |
| 151 | + |
| 152 | + cfg := &Config{} |
| 153 | + cfg.ApplyState(state) |
| 154 | + |
| 155 | + assert.Equal(t, map[string]bool{"ripgrep": true}, cfg.SelectedPkgs) |
| 156 | + assert.Len(t, cfg.OnlinePkgs, 1) |
| 157 | + assert.Equal(t, "ripgrep", cfg.OnlinePkgs[0].Name) |
| 158 | + assert.Equal(t, []string{"homebrew/cask"}, cfg.SnapshotTaps) |
| 159 | + assert.Equal(t, rc, cfg.RemoteConfig) |
| 160 | + assert.Equal(t, git, cfg.SnapshotGit) |
| 161 | + assert.Equal(t, macOSPrefs, cfg.SnapshotMacOS) |
| 162 | + assert.Equal(t, "https://github.com/carol/dotfiles", cfg.SnapshotDotfiles) |
| 163 | + assert.True(t, cfg.SnapshotShellOhMyZsh) |
| 164 | + assert.Equal(t, "powerlevel10k", cfg.SnapshotShellTheme) |
| 165 | + assert.Equal(t, []string{"git", "zsh-autosuggestions"}, cfg.SnapshotShellPlugins) |
| 166 | +} |
| 167 | + |
| 168 | +func TestApplyState_ZeroState(t *testing.T) { |
| 169 | + cfg := &Config{ |
| 170 | + SelectedPkgs: map[string]bool{"old": true}, |
| 171 | + SnapshotDotfiles: "https://github.com/old/dotfiles", |
| 172 | + SnapshotShellOhMyZsh: true, |
| 173 | + } |
| 174 | + |
| 175 | + state := &InstallState{} |
| 176 | + cfg.ApplyState(state) |
| 177 | + |
| 178 | + // All fields should be overwritten with zero values from state. |
| 179 | + assert.Nil(t, cfg.SelectedPkgs) |
| 180 | + assert.Equal(t, "", cfg.SnapshotDotfiles) |
| 181 | + assert.False(t, cfg.SnapshotShellOhMyZsh) |
| 182 | +} |
| 183 | + |
| 184 | +// ---- Round-trip: ToInstallState → ApplyState ---- |
| 185 | + |
| 186 | +func TestInstallState_RoundTrip(t *testing.T) { |
| 187 | + rc := &RemoteConfig{Username: "eve", Slug: "workstation"} |
| 188 | + original := &Config{ |
| 189 | + SelectedPkgs: map[string]bool{"fd": true, "bat": true}, |
| 190 | + OnlinePkgs: []Package{{Name: "fd"}, {Name: "bat"}}, |
| 191 | + SnapshotTaps: []string{"homebrew/core", "homebrew/cask"}, |
| 192 | + RemoteConfig: rc, |
| 193 | + SnapshotDotfiles: "https://github.com/eve/dots", |
| 194 | + SnapshotShellOhMyZsh: true, |
| 195 | + SnapshotShellTheme: "robbyrussell", |
| 196 | + SnapshotShellPlugins: []string{"git"}, |
| 197 | + } |
| 198 | + |
| 199 | + state := original.ToInstallState() |
| 200 | + |
| 201 | + restored := &Config{} |
| 202 | + restored.ApplyState(state) |
| 203 | + |
| 204 | + assert.Equal(t, original.SelectedPkgs, restored.SelectedPkgs) |
| 205 | + assert.Equal(t, original.OnlinePkgs, restored.OnlinePkgs) |
| 206 | + assert.Equal(t, original.SnapshotTaps, restored.SnapshotTaps) |
| 207 | + assert.Equal(t, original.RemoteConfig, restored.RemoteConfig) |
| 208 | + assert.Equal(t, original.SnapshotDotfiles, restored.SnapshotDotfiles) |
| 209 | + assert.Equal(t, original.SnapshotShellOhMyZsh, restored.SnapshotShellOhMyZsh) |
| 210 | + assert.Equal(t, original.SnapshotShellTheme, restored.SnapshotShellTheme) |
| 211 | + assert.Equal(t, original.SnapshotShellPlugins, restored.SnapshotShellPlugins) |
| 212 | +} |
| 213 | + |
| 214 | +// ToInstallOptions does NOT include state fields (runtime-only fields). |
| 215 | +func TestToInstallOptions_DoesNotLeakStateFields(t *testing.T) { |
| 216 | + cfg := &Config{ |
| 217 | + SelectedPkgs: map[string]bool{"git": true}, |
| 218 | + OnlinePkgs: []Package{{Name: "git"}}, |
| 219 | + } |
| 220 | + opts := cfg.ToInstallOptions() |
| 221 | + // InstallOptions has no SelectedPkgs or OnlinePkgs — simply confirm it compiles |
| 222 | + // and returns a valid struct with the correct type. |
| 223 | + require.NotNil(t, opts) |
| 224 | + assert.IsType(t, &InstallOptions{}, opts) |
| 225 | +} |
0 commit comments