internal/provider_migrate_test.go
260 lines · 8137 bytes
9 symbols in this file
TestProviderSelectionOptionsUsesDefaultAndSingleProvidersTestMigrateProviderConfigTestCanonicalProviderConfigValuePrefersStackedTokenTestMigrateOnVersionUpgradeWritesVersionAndUpdatesProviderTestLoadConfigDoesNotDumpAllDefaultsIntoSparseFileTestMigrateOnVersionUpgradeInjectsOnlyOptionsForCrossedVersionsTestInjectConfigOptionsSinceOnlyCrossedVersionsTestCompareVersionsOrderingTestConfiguredProviderNamesUsesStackedByDefault
1package internal
2
3import (
4 "os"
5 "path/filepath"
6 "strings"
7 "testing"
8
9 _ "github.com/wraient/curd/internal/loadproviders"
10)
11
12func TestProviderSelectionOptionsUsesDefaultAndSingleProviders(t *testing.T) {
13 withAllProvidersEnabledForTest(t)
14 options := providerSelectionOptions()
15 if len(options) == 0 {
16 t.Fatal("expected provider options")
17 }
18 if options[0].Key != "stacked" {
19 t.Fatalf("first option key = %q, want stacked", options[0].Key)
20 }
21 if !strings.Contains(options[0].Label, "Default with fallback") {
22 t.Fatalf("first option label = %q", options[0].Label)
23 }
24 for _, option := range options {
25 if strings.Contains(option.Label, ", then ") {
26 t.Fatalf("unexpected combo label %q", option.Label)
27 }
28 }
29}
30
31func TestMigrateProviderConfig(t *testing.T) {
32 cases := []struct {
33 name string
34 raw string
35 want string
36 changed bool
37 }{
38 {name: "legacy senshi default", raw: `["senshi"]`, want: "stacked", changed: true},
39 {name: "legacy allanime default", raw: `["allanime"]`, want: "stacked", changed: true},
40 {name: "stack alias", raw: "stack", want: "stacked", changed: true},
41 {name: "already stacked", raw: "stacked", want: "stacked", changed: false},
42 {name: "single anineko", raw: `["anineko"]`, want: `["anineko"]`, changed: false},
43 {name: "legacy pair", raw: `["senshi","anineko"]`, want: "stacked", changed: true},
44 }
45
46 for _, tc := range cases {
47 got, changed := migrateProviderConfig(tc.raw)
48 if changed != tc.changed || got != tc.want {
49 t.Fatalf("%s: got (%q, %v), want (%q, %v)", tc.name, got, changed, tc.want, tc.changed)
50 }
51 }
52}
53
54func TestCanonicalProviderConfigValuePrefersStackedToken(t *testing.T) {
55 withAllProvidersEnabledForTest(t)
56 if got := canonicalProviderConfigValue("stacked"); got != "stacked" {
57 t.Fatalf("got %q, want stacked", got)
58 }
59 if got := canonicalProviderConfigValue(""); got != "stacked" {
60 t.Fatalf("empty got %q, want stacked", got)
61 }
62}
63
64func TestMigrateOnVersionUpgradeWritesVersionAndUpdatesProvider(t *testing.T) {
65 withAllProvidersEnabledForTest(t)
66 tempDir := t.TempDir()
67 configPath := filepath.Join(tempDir, "curd.conf")
68 storagePath := filepath.Join(tempDir, "share")
69 if err := os.WriteFile(configPath, []byte("Provider=[\"senshi\"]\nStoragePath="+storagePath+"\n"), 0644); err != nil {
70 t.Fatalf("write config: %v", err)
71 }
72
73 config, err := LoadConfig(configPath)
74 if err != nil {
75 t.Fatalf("LoadConfig: %v", err)
76 }
77 if config.Provider != `["senshi"]` {
78 t.Fatalf("pre-migration provider = %q", config.Provider)
79 }
80
81 updated, err := MigrateOnVersionUpgrade(configPath, &config, "2.1.0")
82 if err != nil {
83 t.Fatalf("MigrateOnVersionUpgrade: %v", err)
84 }
85 if !updated {
86 t.Fatal("expected provider config update")
87 }
88 if config.Provider != "stacked" {
89 t.Fatalf("provider = %q, want stacked", config.Provider)
90 }
91
92 versionBytes, err := os.ReadFile(filepath.Join(storagePath, "curd_version"))
93 if err != nil {
94 t.Fatalf("read version file: %v", err)
95 }
96 if strings.TrimSpace(string(versionBytes)) != "2.1.0" {
97 t.Fatalf("version file = %q", string(versionBytes))
98 }
99
100 contents, err := os.ReadFile(configPath)
101 if err != nil {
102 t.Fatalf("read config: %v", err)
103 }
104 if !strings.Contains(string(contents), "Provider=stacked") {
105 t.Fatalf("config not persisted: %s", string(contents))
106 }
107
108 updated, err = MigrateOnVersionUpgrade(configPath, &config, "2.1.0")
109 if err != nil {
110 t.Fatalf("second migration: %v", err)
111 }
112 if updated {
113 t.Fatal("expected no update on same version")
114 }
115}
116
117func TestLoadConfigDoesNotDumpAllDefaultsIntoSparseFile(t *testing.T) {
118 tempDir := t.TempDir()
119 configPath := filepath.Join(tempDir, "curd.conf")
120 storagePath := filepath.Join(tempDir, "share")
121 initial := "StoragePath=" + storagePath + "\n" +
122 "AddMissingOptions=true\n" +
123 "Provider=stacked\n" +
124 "Player=mpv\n"
125 if err := os.WriteFile(configPath, []byte(initial), 0644); err != nil {
126 t.Fatalf("write config: %v", err)
127 }
128
129 config, err := LoadConfig(configPath)
130 if err != nil {
131 t.Fatalf("LoadConfig: %v", err)
132 }
133 // In-memory defaults still apply.
134 if config.VimKeys {
135 t.Fatal("VimKeys default should be false in memory")
136 }
137
138 after, err := os.ReadFile(configPath)
139 if err != nil {
140 t.Fatalf("read config: %v", err)
141 }
142 text := string(after)
143 // Sparse file must not gain every historical default on a normal load.
144 if strings.Contains(text, "VimKeys=") {
145 t.Fatalf("LoadConfig must not append versioned options without an upgrade:\n%s", text)
146 }
147 if strings.Contains(text, "SkipOp=") {
148 t.Fatalf("LoadConfig must not dump baseline defaults into sparse configs:\n%s", text)
149 }
150}
151
152func TestMigrateOnVersionUpgradeInjectsOnlyOptionsForCrossedVersions(t *testing.T) {
153 tempDir := t.TempDir()
154 configPath := filepath.Join(tempDir, "curd.conf")
155 storagePath := filepath.Join(tempDir, "share")
156 initial := "StoragePath=" + storagePath + "\n" +
157 "AddMissingOptions=true\n" +
158 "Provider=stacked\n" +
159 "Player=mpv\n"
160 if err := os.WriteFile(configPath, []byte(initial), 0644); err != nil {
161 t.Fatalf("write config: %v", err)
162 }
163 if err := os.MkdirAll(storagePath, 0755); err != nil {
164 t.Fatalf("mkdir storage: %v", err)
165 }
166 if err := os.WriteFile(filepath.Join(storagePath, "curd_version"), []byte("2.0.2\n"), 0644); err != nil {
167 t.Fatalf("write version: %v", err)
168 }
169
170 config := PopulateConfig(map[string]string{
171 "StoragePath": storagePath,
172 "AddMissingOptions": "true",
173 "Provider": "stacked",
174 "Player": "mpv",
175 })
176
177 updated, err := MigrateOnVersionUpgrade(configPath, &config, "2.0.3")
178 if err != nil {
179 t.Fatalf("MigrateOnVersionUpgrade: %v", err)
180 }
181 if !updated {
182 t.Fatal("expected config file to gain 2.0.3 options on upgrade")
183 }
184
185 after, err := os.ReadFile(configPath)
186 if err != nil {
187 t.Fatalf("read config after migrate: %v", err)
188 }
189 text := string(after)
190 if !strings.Contains(text, "VimKeys=false") {
191 t.Fatalf("expected VimKeys=false for 2.0.3 upgrade:\n%s", text)
192 }
193 if !strings.Contains(text, "MpvPlaybackStartTimeout=20") {
194 t.Fatalf("expected MpvPlaybackStartTimeout for 2.0.3 upgrade:\n%s", text)
195 }
196 // Must NOT dump unrelated baseline keys.
197 if strings.Contains(text, "SkipOp=") || strings.Contains(text, "DiscordPresence=") {
198 t.Fatalf("upgrade must not inject unregistered baseline options:\n%s", text)
199 }
200
201 updated, err = MigrateOnVersionUpgrade(configPath, &config, "2.0.3")
202 if err != nil {
203 t.Fatalf("second migrate: %v", err)
204 }
205 if updated {
206 t.Fatal("same version must not re-inject options")
207 }
208}
209
210func TestInjectConfigOptionsSinceOnlyCrossedVersions(t *testing.T) {
211 m := map[string]string{"Player": "mpv"}
212 // Already on 2.0.3 — nothing new.
213 if added := injectConfigOptionsSince(m, "2.0.3", "2.0.3"); len(added) != 0 {
214 t.Fatalf("same version should inject nothing, got %v", added)
215 }
216 // 2.0.2 → 2.0.3 gets VimKeys + timeout only.
217 added := injectConfigOptionsSince(m, "2.0.2", "2.0.3")
218 if len(added) != 2 {
219 t.Fatalf("expected 2 options for 2.0.2→2.0.3, got %v", added)
220 }
221 want := map[string]bool{"VimKeys": true, "MpvPlaybackStartTimeout": true}
222 for _, key := range added {
223 if !want[key] {
224 t.Fatalf("unexpected injected key %q in %v", key, added)
225 }
226 }
227 // Already present keys are skipped.
228 if second := injectConfigOptionsSince(m, "2.0.2", "2.0.3"); len(second) != 0 {
229 t.Fatalf("second inject should be empty, got %v", second)
230 }
231}
232
233func TestCompareVersionsOrdering(t *testing.T) {
234 if !versionLess("2.0.2", "2.0.3") {
235 t.Fatal("2.0.2 should be < 2.0.3")
236 }
237 if !versionLess("", "2.0.3") {
238 t.Fatal("empty stored version should be older than a release")
239 }
240 if versionLess("2.0.3", "2.0.3") {
241 t.Fatal("equal versions should not be less")
242 }
243 if !versionLessOrEqual("2.0.3", "2.0.3") {
244 t.Fatal("equal versions should be <= ")
245 }
246}
247
248func TestConfiguredProviderNamesUsesStackedByDefault(t *testing.T) {
249 withAllProvidersEnabledForTest(t)
250 got := ConfiguredProviderNames(&CurdConfig{})
251 want := []string{"senshi", "anipub", "anineko", "allanime", "animepahe"}
252 if len(got) != len(want) {
253 t.Fatalf("got %v, want %v", got, want)
254 }
255 for i := range want {
256 if got[i] != want[i] {
257 t.Fatalf("got %v, want %v", got, want)
258 }
259 }
260}