| @@ -114,11 +114,11 @@ func TestMigrateOnVersionUpgradeWritesVersionAndUpdatesProvider(t *testing.T) { |
| 114 | } |
114 | } |
| 115 | } |
115 | } |
| 116 | |
116 | |
| 117 | func TestMigrateOnVersionUpgradeInjectsMissingOptionsOnce(t *testing.T) { |
117 | func TestLoadConfigAppendsMissingOptionsIncludingVimKeys(t *testing.T) { |
| 118 | tempDir := t.TempDir() |
118 | tempDir := t.TempDir() |
| 119 | configPath := filepath.Join(tempDir, "curd.conf") |
119 | configPath := filepath.Join(tempDir, "curd.conf") |
| 120 | storagePath := filepath.Join(tempDir, "share") |
120 | storagePath := filepath.Join(tempDir, "share") |
| 121 | // Minimal pre-upgrade config: no VimKeys, no MpvPlaybackStartTimeout. |
121 | // Minimal config: no VimKeys — must be appended on load (not a full rewrite). |
| 122 | initial := "StoragePath=" + storagePath + "\n" + |
122 | initial := "StoragePath=" + storagePath + "\n" + |
| 123 | "AddMissingOptions=true\n" + |
123 | "AddMissingOptions=true\n" + |
| 124 | "Provider=stacked\n" + |
124 | "Provider=stacked\n" + |
| @@ -131,17 +131,66 @@ func TestMigrateOnVersionUpgradeInjectsMissingOptionsOnce(t *testing.T) { |
| 131 | if err != nil { |
131 | if err != nil { |
| 132 | t.Fatalf("LoadConfig: %v", err) |
132 | t.Fatalf("LoadConfig: %v", err) |
| 133 | } |
133 | } |
| 134 | // LoadConfig must not rewrite the file just for missing defaults. |
134 | if config.VimKeys { |
| 135 | before, err := os.ReadFile(configPath) |
135 | t.Fatal("VimKeys default should be false") |
| |
136 | } |
| |
137 | |
| |
138 | after, err := os.ReadFile(configPath) |
| 136 | if err != nil { |
139 | if err != nil { |
| 137 | t.Fatalf("read config: %v", err) |
140 | t.Fatalf("read config: %v", err) |
| 138 | } |
141 | } |
| 139 | if strings.Contains(string(before), "VimKeys=") { |
142 | text := string(after) |
| 140 | t.Fatalf("LoadConfig should not inject VimKeys into the file every start:\n%s", before) |
143 | if !strings.Contains(text, "VimKeys=false") { |
| |
144 | t.Fatalf("expected VimKeys=false in config file:\n%s", text) |
| 141 | } |
145 | } |
| 142 | // In-memory default still applies. |
146 | // Original lines preserved (append-only). |
| 143 | if config.VimKeys { |
147 | if !strings.Contains(text, "Player=mpv") || !strings.Contains(text, "StoragePath="+storagePath) { |
| 144 | t.Fatal("VimKeys default should be false in memory") |
148 | t.Fatalf("original keys should remain:\n%s", text) |
| |
149 | } |
| |
150 | |
| |
151 | // Second load must not duplicate VimKeys. |
| |
152 | if _, err := LoadConfig(configPath); err != nil { |
| |
153 | t.Fatalf("second LoadConfig: %v", err) |
| |
154 | } |
| |
155 | after2, err := os.ReadFile(configPath) |
| |
156 | if err != nil { |
| |
157 | t.Fatalf("read config again: %v", err) |
| |
158 | } |
| |
159 | if strings.Count(string(after2), "VimKeys=") != 1 { |
| |
160 | t.Fatalf("VimKeys should appear once, got:\n%s", after2) |
| |
161 | } |
| |
162 | } |
| |
163 | |
| |
164 | func TestMigrateOnVersionUpgradeInjectsMissingOptionsOnce(t *testing.T) { |
| |
165 | tempDir := t.TempDir() |
| |
166 | configPath := filepath.Join(tempDir, "curd.conf") |
| |
167 | storagePath := filepath.Join(tempDir, "share") |
| |
168 | // Full-enough config written first so LoadConfig isn't under test here. |
| |
169 | initial := "StoragePath=" + storagePath + "\n" + |
| |
170 | "AddMissingOptions=true\n" + |
| |
171 | "Provider=stacked\n" + |
| |
172 | "Player=mpv\n" |
| |
173 | if err := os.WriteFile(configPath, []byte(initial), 0644); err != nil { |
| |
174 | t.Fatalf("write config: %v", err) |
| |
175 | } |
| |
176 | // Pretend we're already on an older stored version with a sparse file. |
| |
177 | if err := os.MkdirAll(storagePath, 0755); err != nil { |
| |
178 | t.Fatalf("mkdir storage: %v", err) |
| |
179 | } |
| |
180 | if err := os.WriteFile(filepath.Join(storagePath, "curd_version"), []byte("2.0.0\n"), 0644); err != nil { |
| |
181 | t.Fatalf("write version: %v", err) |
| |
182 | } |
| |
183 | |
| |
184 | config := PopulateConfig(map[string]string{ |
| |
185 | "StoragePath": storagePath, |
| |
186 | "AddMissingOptions": "true", |
| |
187 | "Provider": "stacked", |
| |
188 | "Player": "mpv", |
| |
189 | }) |
| |
190 | |
| |
191 | // Wipe keys that LoadConfig would have added so migration is the injector. |
| |
192 | if err := os.WriteFile(configPath, []byte(initial), 0644); err != nil { |
| |
193 | t.Fatalf("rewrite sparse config: %v", err) |
| 145 | } |
194 | } |
| 146 | |
195 | |
| 147 | updated, err := MigrateOnVersionUpgrade(configPath, &config, "2.0.3") |
196 | updated, err := MigrateOnVersionUpgrade(configPath, &config, "2.0.3") |
| @@ -160,13 +209,6 @@ func TestMigrateOnVersionUpgradeInjectsMissingOptionsOnce(t *testing.T) { |
| 160 | if !strings.Contains(text, "VimKeys=false") { |
209 | if !strings.Contains(text, "VimKeys=false") { |
| 161 | t.Fatalf("expected VimKeys=false appended on upgrade:\n%s", text) |
210 | t.Fatalf("expected VimKeys=false appended on upgrade:\n%s", text) |
| 162 | } |
211 | } |
| 163 | if !strings.Contains(text, "MpvPlaybackStartTimeout=20") { |
| |
| 164 | t.Fatalf("expected MpvPlaybackStartTimeout appended on upgrade:\n%s", text) |
| |
| 165 | } |
| |
| 166 | // Original keys preserved at the top (append-only for new keys). |
| |
| 167 | if !strings.HasPrefix(strings.TrimSpace(text), "StoragePath=") && !strings.Contains(text, "StoragePath="+storagePath) { |
| |
| 168 | t.Fatalf("original StoragePath should remain:\n%s", text) |
| |
| 169 | } |
| |
| 170 | |
212 | |
| 171 | // Same version: no further rewrite. |
213 | // Same version: no further rewrite. |
| 172 | updated, err = MigrateOnVersionUpgrade(configPath, &config, "2.0.3") |
214 | updated, err = MigrateOnVersionUpgrade(configPath, &config, "2.0.3") |