internal/tracking_test.go

251 lines · 7254 bytes

10 symbols in this file
  1package internal
  2
  3import (
  4	"testing"
  5	"time"
  6)
  7
  8func TestNormalizeRemoteTrackerAliases(t *testing.T) {
  9	t.Parallel()
 10
 11	cases := map[string]string{
 12		"":                    "none",
 13		"none":                "none",
 14		"off":                 "none",
 15		"local":               "none",
 16		"anilist":             "anilist",
 17		"ani-list":            "anilist",
 18		"mal":                 "myanimelist",
 19		"myanimelist":         "myanimelist",
 20		"my-anime-list":       "myanimelist",
 21		"my_anime_list":       "myanimelist",
 22		"both":                "anilist+myanimelist",
 23		"anilist,myanimelist": "anilist+myanimelist",
 24		"not-a-provider":      "",
 25	}
 26
 27	for input, want := range cases {
 28		if got := normalizeRemoteTracker(input); got != want {
 29			t.Fatalf("normalizeRemoteTracker(%q) = %q, want %q", input, got, want)
 30		}
 31	}
 32}
 33
 34func TestNormalizeTrackingConfigAlwaysKeepsLocalHistoryEnabled(t *testing.T) {
 35	t.Parallel()
 36
 37	config := &CurdConfig{
 38		TrackingLocal:      false,
 39		TrackingRemote:     "none",
 40		TrackingConfigured: true,
 41	}
 42
 43	normalizeTrackingConfig(config)
 44
 45	if !config.TrackingLocal {
 46		t.Fatalf("expected local tracking to be re-enabled when both trackers are disabled")
 47	}
 48	if config.TrackingRemote != TrackingRemoteNone {
 49		t.Fatalf("expected remote tracking to stay disabled, got %q", config.TrackingRemote)
 50	}
 51}
 52
 53func TestNormalizeTrackingConfigMarksInvalidRemoteForPrompt(t *testing.T) {
 54	t.Parallel()
 55
 56	config := &CurdConfig{
 57		TrackingLocal:      true,
 58		TrackingRemote:     "totally-invalid",
 59		TrackingConfigured: true,
 60	}
 61
 62	normalizeTrackingConfig(config)
 63
 64	if config.TrackingConfigured {
 65		t.Fatalf("expected invalid tracking config to require setup prompt")
 66	}
 67	if config.TrackingRemote != TrackingRemoteAniList {
 68		t.Fatalf("expected invalid remote tracker to normalize to the legacy AniList default, got %q", config.TrackingRemote)
 69	}
 70	if !config.TrackingLocal {
 71		t.Fatalf("expected local history to stay enabled after normalization")
 72	}
 73}
 74
 75func TestGetOrderedCategoriesHidesRemoteOnlyEntriesForLocalTracking(t *testing.T) {
 76	t.Parallel()
 77
 78	config := &CurdConfig{
 79		MenuOrder:      "CURRENT,UPDATE,PLANNING,ALL,PROVIDER",
 80		TrackingLocal:  true,
 81		TrackingRemote: TrackingRemoteNone,
 82	}
 83
 84	categories := getOrderedCategories(config)
 85	if len(categories) != 4 {
 86		t.Fatalf("expected 4 visible categories, got %d", len(categories))
 87	}
 88	if categories[0].Key != "CURRENT" || categories[1].Key != "ALL" || categories[2].Key != "PROVIDER" || categories[3].Key != "TRACKER" {
 89		t.Fatalf("unexpected categories for local-only tracking: %+v", categories)
 90	}
 91}
 92
 93func TestLocalAnimeListFromHistoryUsesNextEpisodeAsProgressBase(t *testing.T) {
 94	t.Parallel()
 95
 96	list := localAnimeListFromHistory([]Anime{
 97		{
 98			AnilistId: 123,
 99			MalId:     456,
100			Title: AnimeTitle{
101				English: "Frieren",
102				Romaji:  "Sousou no Frieren",
103			},
104			Ep: Episode{
105				Number: 7,
106			},
107			TotalEpisodes: 28,
108		},
109	})
110
111	if len(list.Watching) != 1 {
112		t.Fatalf("expected one local entry, got %d", len(list.Watching))
113	}
114
115	entry := list.Watching[0]
116	if entry.Media.ID != 123 || entry.Media.MalID != 456 {
117		t.Fatalf("unexpected IDs in local entry: %+v", entry.Media)
118	}
119	if entry.Progress != 6 {
120		t.Fatalf("expected progress 6 from next-episode value 7, got %d", entry.Progress)
121	}
122	if entry.Status != "CURRENT" {
123		t.Fatalf("expected CURRENT status, got %q", entry.Status)
124	}
125}
126
127func TestMyAnimeListStatusConversion(t *testing.T) {
128	t.Parallel()
129
130	if got := myAnimeListStatusToAniListStatus("watching", true); got != "REPEATING" {
131		t.Fatalf("expected rewatching MAL entry to map to REPEATING, got %q", got)
132	}
133	if got := myAnimeListStatusToAniListStatus("on_hold", false); got != "PAUSED" {
134		t.Fatalf("expected on_hold to map to PAUSED, got %q", got)
135	}
136	if got := aniListStatusToMyAnimeListStatus("REPEATING"); got != "watching" {
137		t.Fatalf("expected REPEATING to map to watching, got %q", got)
138	}
139	if got := aniListStatusToMyAnimeListStatus("PLANNING"); got != "plan_to_watch" {
140		t.Fatalf("expected PLANNING to map to plan_to_watch, got %q", got)
141	}
142}
143
144func TestDualSyncModeEnablesBothRemoteTrackers(t *testing.T) {
145	t.Parallel()
146
147	config := &CurdConfig{TrackingRemote: TrackingRemoteBoth}
148	if !UsesAniListTracking(config) {
149		t.Fatalf("expected dual-sync mode to include AniList")
150	}
151	if !UsesMyAnimeListTracking(config) {
152		t.Fatalf("expected dual-sync mode to include MyAnimeList")
153	}
154	if !UsesDualRemoteTracking(config) {
155		t.Fatalf("expected dual-sync mode to report itself as dual remote")
156	}
157}
158
159func TestExtractOAuthCodeFromInput(t *testing.T) {
160	t.Parallel()
161
162	code, err := extractOAuthCodeFromInput("http://localhost:8123/oauth/callback?code=test-code&state=expected", "expected")
163	if err != nil {
164		t.Fatalf("expected callback URL to parse, got error: %v", err)
165	}
166	if code != "test-code" {
167		t.Fatalf("expected parsed code to be test-code, got %q", code)
168	}
169
170	code, err = extractOAuthCodeFromInput("raw-code", "expected")
171	if err != nil {
172		t.Fatalf("expected raw code to parse, got error: %v", err)
173	}
174	if code != "raw-code" {
175		t.Fatalf("expected raw code passthrough, got %q", code)
176	}
177
178	if _, err := extractOAuthCodeFromInput("http://localhost:8123/oauth/callback?code=test-code&state=wrong", "expected"); err == nil {
179		t.Fatalf("expected state mismatch to fail")
180	}
181}
182
183func TestMergeAnimeEntriesPrefersNewestRemoteUpdate(t *testing.T) {
184	t.Parallel()
185
186	older := Entry{
187		Media:     Media{ID: 1, MalID: 101},
188		Status:    "CURRENT",
189		Progress:  3,
190		UpdatedAt: time.Date(2026, 5, 17, 10, 0, 0, 0, time.UTC),
191	}
192	newer := Entry{
193		Media:     Media{ID: 1, MalID: 101},
194		Status:    "COMPLETED",
195		Progress:  12,
196		UpdatedAt: time.Date(2026, 5, 17, 12, 0, 0, 0, time.UTC),
197	}
198
199	merged := mergeAnimeEntries(older, newer)
200	if merged.Status != "COMPLETED" || merged.Progress != 12 {
201		t.Fatalf("expected latest entry to win, got %+v", merged)
202	}
203}
204
205func TestBuildDualRemoteSyncPlanUsesNewestEntryAndPropagatesMissingAnime(t *testing.T) {
206	t.Parallel()
207
208	aniList := AnimeList{
209		Watching: []Entry{{
210			Media:     Media{ID: 1, MalID: 101},
211			Status:    "CURRENT",
212			Progress:  2,
213			UpdatedAt: time.Date(2026, 5, 17, 9, 0, 0, 0, time.UTC),
214		}},
215	}
216	myAnimeList := AnimeList{
217		Completed: []Entry{{
218			Media:     Media{ID: 1, MalID: 101},
219			Status:    "COMPLETED",
220			Progress:  12,
221			UpdatedAt: time.Date(2026, 5, 17, 11, 0, 0, 0, time.UTC),
222		}},
223		Planning: []Entry{{
224			Media:     Media{ID: 2, MalID: 202},
225			Status:    "PLANNING",
226			UpdatedAt: time.Date(2026, 5, 17, 8, 0, 0, 0, time.UTC),
227		}},
228	}
229
230	plan := buildDualRemoteSyncPlan(aniList, myAnimeList)
231	if len(plan.AniListUpdates) != 2 {
232		t.Fatalf("expected AniList to receive 2 updates, got %d", len(plan.AniListUpdates))
233	}
234	if len(plan.MyAnimeListUpdates) != 0 {
235		t.Fatalf("expected MyAnimeList to already match the winning entries, got %d updates", len(plan.MyAnimeListUpdates))
236	}
237
238	var mergedFirst *Entry
239	for _, entry := range getEntriesByCategory(plan.Merged, "ALL") {
240		if entry.Media.ID == 1 {
241			mergedFirst = &entry
242			break
243		}
244	}
245	if mergedFirst == nil {
246		t.Fatalf("expected merged list to contain media ID 1")
247	}
248	if mergedFirst.Status != "COMPLETED" || mergedFirst.Progress != 12 {
249		t.Fatalf("expected merged entry to keep latest MAL state, got %+v", *mergedFirst)
250	}
251}