package app import ( "fmt" "testing" ) func TestHuangguoCardsKeepSearchSuggestionsInsideTheirLinks(t *testing.T) { body := `
搜索记录 清除 暂无搜索记录
1搜索建议甲2775.8w 2搜索建议乙123.4w
新上架1小时前更新至12集

新剧样本

旧剧样本

刚更新全8集
` items := parseHuangguoAIDramaCards(body, huangguoAIBaseURL+"/newest/", "最近上新") want := map[string]string{"12": "搜索建议甲", "117": "搜索建议乙", "501": "新剧样本", "502": "旧剧样本"} if len(items) != len(want) { t.Fatalf("got %d dramas, want %d", len(items), len(want)) } for _, item := range items { if item.Title != want[item.SourceID] || item.Name != want[item.SourceID] { t.Errorf("%s got title %q and name %q", item.ID, item.Title, item.Name) } switch item.SourceID { case "501": if fmt.Sprint(item.TotalEpisode) != "12" || fmt.Sprint(item.EpisodeCount) != "12" || item.Remark != "新上架 更新至12集" { t.Errorf("new card lost its count: count=%v remark=%q", item.TotalEpisode, item.Remark) } case "502": if fmt.Sprint(item.TotalEpisode) != "8" { t.Errorf("nested episode text lost its count: %v", item.TotalEpisode) } } } } func TestHuangguoCardsDoNotBorrowTitlesFromNeighborsOrFooter(t *testing.T) { body := `

隔壁剧名

` items := parseHuangguoAIDramaCards(body, huangguoAIBaseURL, "测试") want := map[string]string{"501": "结构化剧名", "502": "隔壁剧名", "503": "503"} if len(items) != len(want) { t.Fatalf("got %d dramas, want %d", len(items), len(want)) } for _, item := range items { if item.Title != want[item.SourceID] { t.Errorf("%s borrowed a title: %q", item.ID, item.Title) } } } func TestHuangguoNestedDetailMetaKeepsCountsAndViewsScoped(t *testing.T) { for _, entry := range []struct { name string meta string count string }{ {"new", `8.7分1小时前更新至 12 集 · 4.1w次播放`, "12"}, {"finished", `9.9分已完结 · 共 21 集 · 4.1w次播放`, "21"}, {"unknown", `8.7分新上架 · 1小时前 · 4.1w次播放`, ""}, } { t.Run(entry.name, func(t *testing.T) { body := `
` + entry.meta + `
全99集900w次播放
` item, err := parseHuangguoSortDetail(body, huangguoAIBaseURL+"/detail/453/", Drama{ID: "huangguoai:453", Source: sourceHuangguoAI, SourceID: "453"}) if err != nil || item.Title != "详情剧名" || item.Views != "4.1w次播放" || item.OnlineDate != "2026-09-14" { t.Fatalf("wrong detail metadata: title=%q views=%q date=%q error=%v", item.Title, item.Views, item.OnlineDate, err) } if entry.count == "" { if !valueEmpty(item.TotalEpisode) || !valueEmpty(item.EpisodeCount) { t.Fatal("invented a count from relative time or recommendations") } } else if fmt.Sprint(item.TotalEpisode) != entry.count || fmt.Sprint(item.EpisodeCount) != entry.count { t.Fatalf("got count %v, want %s", item.TotalEpisode, entry.count) } }) } }