package storage import ( "testing" ) // ── chapterNumberFromKey ────────────────────────────────────────────────────── func TestChapterNumberFromKey(t *testing.T) { cases := []struct { key string want int }{ // Standard four-segment key. {"my-novel/vol-0/1-50/chapter-1.md", 1}, {"my-novel/vol-0/1-50/chapter-42.md", 42}, {"my-novel/vol-0/51-100/chapter-99.md", 99}, // Large chapter numbers. {"some-novel/vol-1/1001-1050/chapter-1024.md", 1024}, // Nested deeper paths should still work (last segment used). {"a/b/c/d/chapter-7.md", 7}, // Malformed / unexpected inputs — should return 0 without panicking. {"chapter-notanumber.md", 0}, {"", 0}, // No .md extension — TrimSuffix is a no-op; TrimPrefix still strips // "chapter-", so the number is parsed successfully. {"no-md-extension/chapter-5", 5}, {"my-novel/vol-0/1-50/chapter-0.md", 0}, // 0 is invalid (chapters are 1-based) {"my-novel/vol-0/1-50/chapter--1.md", 0}, } for _, tc := range cases { got := chapterNumberFromKey(tc.key) if got != tc.want { t.Errorf("chapterNumberFromKey(%q) = %d, want %d", tc.key, got, tc.want) } } } // ── splitChapterTitle ───────────────────────────────────────────────────────── func TestSplitChapterTitle(t *testing.T) { cases := []struct { raw string wantTitle string wantDate string }{ // No date — title is returned as-is. {"The Great Battle", "The Great Battle", ""}, // Leading numeric index is stripped. {"42 The Great Battle", "The Great Battle", ""}, // Relative date with plural unit. {"The Storm Arrives 3 days ago", "The Storm Arrives", "3 days ago"}, // Singular unit. {"A New Hope 1 week ago", "A New Hope", "1 week ago"}, // Minutes and seconds. {"Flash Fight 5 minutes ago", "Flash Fight", "5 minutes ago"}, {"Quick Strike 30 seconds ago", "Quick Strike", "30 seconds ago"}, // Months and years. {"Old Chapter 2 months ago", "Old Chapter", "2 months ago"}, {"Ancient Story 1 year ago", "Ancient Story", "1 year ago"}, // Leading index AND trailing date. {"5 The Final Chapter 2 hours ago", "The Final Chapter", "2 hours ago"}, // Extra whitespace. {" The Calm ", "The Calm", ""}, // Empty string. {"", "", ""}, } for _, tc := range cases { title, date := splitChapterTitle(tc.raw) if title != tc.wantTitle || date != tc.wantDate { t.Errorf("splitChapterTitle(%q) = (%q, %q), want (%q, %q)", tc.raw, title, date, tc.wantTitle, tc.wantDate) } } }