Files
2026-10-26 16:28:14 +08:00

109 lines
4.3 KiB
Go

package sitemap
import (
"testing"
"time"
"videoweb/pub/models/videocategorymod"
"videoweb/pub/models/videoinformationmod"
"go.mongodb.org/mongo-driver/bson"
)
func TestIsSitemapChannelPath(t *testing.T) {
if !isSitemapChannelPath("/drama", "/drama", "/drama-rank") {
t.Fatal("channel home should match")
}
if !isSitemapChannelPath("/drama/all", "/drama", "/drama-rank") {
t.Fatal("channel child path should match")
}
if !isSitemapChannelPath("/drama-rank", "/drama", "/drama-rank") {
t.Fatal("hyphen rank path should match")
}
if isSitemapChannelPath("/drama-rank", "/drama") {
t.Fatal("hyphen rank must not match prefix-only")
}
if isSitemapChannelPath("/video-rank", "/drama", "/drama-rank") {
t.Fatal("other channel rank should not match")
}
}
func TestBuildDetailURLs(t *testing.T) {
items := []idWithTime{
{Id: 53942, Slug: "sirozFs", LastMod: time.Date(2026, 7, 29, 0, 0, 0, 0, time.UTC)},
}
urls := buildDetailURLs("https://example.com/", "/video-detail", items, "weekly", 0.8)
if len(urls) != 1 {
t.Fatalf("buildDetailURLs() returned %d URLs, want 1", len(urls))
}
if want := "https://example.com/video-detail/sirozFs"; urls[0].Loc != want {
t.Fatalf("buildDetailURLs() loc = %q, want %q", urls[0].Loc, want)
}
fallback := buildDetailURLs("https://example.com/", "/video-detail", []idWithTime{{Id: 12}}, "weekly", 0.8)
if len(fallback) != 1 || fallback[0].Loc != "https://example.com/video-detail/12" {
t.Fatalf("empty slug should fall back to id, got %#v", fallback)
}
}
func TestBuildTagURLs(t *testing.T) {
slugURL := buildTagURLs("https://example.com/", "/video", []idWithTime{{Id: 9, Slug: "saduanfa"}}, "weekly", 0.6)
if len(slugURL) != 1 || slugURL[0].Loc != "https://example.com/video/t-saduanfa" {
t.Fatalf("slug tag loc = %#v", slugURL)
}
idURL := buildTagURLs("https://example.com/", "/drama", []idWithTime{{Id: 10142}}, "weekly", 0.6)
if len(idURL) != 1 || idURL[0].Loc != "https://example.com/drama/t-10142" {
t.Fatalf("id tag loc = %#v", idURL)
}
}
func TestBuildCategoryURLs(t *testing.T) {
slugURL := buildCategoryURLs("https://example.com/", "/video", []idWithTime{{Id: 37513, Slug: "hot-av"}}, "weekly", 0.6)
if len(slugURL) != 1 || slugURL[0].Loc != "https://example.com/video/c-hot-av" {
t.Fatalf("slug category loc = %#v", slugURL)
}
idURL := buildCategoryURLs("https://example.com/", "/drama", []idWithTime{{Id: 10001}}, "weekly", 0.6)
if len(idURL) != 1 || idURL[0].Loc != "https://example.com/drama/c-10001" {
t.Fatalf("id category loc = %#v", idURL)
}
}
func TestBuildPlayletSitemapURLs(t *testing.T) {
items := []idWithTime{{Id: 88, Slug: "chuanchengjiaofubei", LastMod: time.Date(2026, 9, 4, 0, 0, 0, 0, time.UTC)}}
detail := buildDetailURLs("https://example.com/", "/drama-detail", items, "weekly", 0.8)
if len(detail) != 1 || detail[0].Loc != "https://example.com/drama-detail/chuanchengjiaofubei" {
t.Fatalf("playlet detail loc = %#v", detail)
}
category := buildCategoryURLs("https://example.com/", "/drama", items, "weekly", 0.6)
if len(category) != 1 || category[0].Loc != "https://example.com/drama/c-chuanchengjiaofubei" {
t.Fatalf("playlet category loc = %#v", category)
}
idOnly := buildCategoryURLs("https://example.com/", "/video", []idWithTime{{Id: 37513}}, "weekly", 0.6)
if len(idOnly) != 1 || idOnly[0].Loc != "https://example.com/video/c-37513" {
t.Fatalf("category id fallback loc = %#v", idOnly)
}
tag := buildTagURLs("https://example.com/", "/drama", items, "weekly", 0.6)
if len(tag) != 1 || tag[0].Loc != "https://example.com/drama/t-chuanchengjiaofubei" {
t.Fatalf("playlet tag loc = %#v", tag)
}
}
func TestVideoCategoryContentFilter(t *testing.T) {
filter := videoCategoryContentFilter(&videocategorymod.VideoCategory{
Status: true,
Tags: []int64{11, 22},
})
tagsFilter, ok := filter[videoinformationmod.FN.VideoInformation.Tags].(bson.M)
if !ok {
t.Fatalf("tags filter = %#v, want bson.M", filter[videoinformationmod.FN.VideoInformation.Tags])
}
in, ok := tagsFilter["$in"].([]int64)
if !ok || len(in) != 2 || in[0] != 11 || in[1] != 22 {
t.Fatalf("tags $in = %#v, want [11 22]", tagsFilter["$in"])
}
emptyTags := videoCategoryContentFilter(&videocategorymod.VideoCategory{Status: true})
if _, exists := emptyTags[videoinformationmod.FN.VideoInformation.Tags]; exists {
t.Fatalf("empty tag category should not add tags filter: %#v", emptyTags)
}
}