package sitemap
import (
"testing"
)
const testURLSetXML = `
https://example.com/page1
2025-01-01
daily
0.8
https://example.com/page2
2025-06-15
0.5
https://example.com/gallery
https://example.com/img/photo1.jpg
A beautiful photo
https://example.com/img/photo2.jpg
`
const testSitemapIndexXML = `
https://example.com/sitemap1.xml
2025-01-01
https://example.com/sitemap2.xml
2025-06-15
`
func TestParseURLSet(t *testing.T) {
result, err := Parse(testURLSetXML)
if err != nil {
t.Fatalf("Parse urlset failed: %s", err.Error())
}
if result.Type != "urlset" {
t.Errorf("expected type 'urlset', got '%s'", result.Type)
}
if len(result.URLs) != 3 {
t.Fatalf("expected 3 URLs, got %d", len(result.URLs))
}
// Check first URL
u := result.URLs[0]
if u.Loc != "https://example.com/page1" {
t.Errorf("expected loc 'https://example.com/page1', got '%s'", u.Loc)
}
if u.LastMod != "2025-01-01" {
t.Errorf("expected lastmod '2025-01-01', got '%s'", u.LastMod)
}
if u.ChangeFreq != "daily" {
t.Errorf("expected changefreq 'daily', got '%s'", u.ChangeFreq)
}
if u.Priority != "0.8" {
t.Errorf("expected priority '0.8', got '%s'", u.Priority)
}
// Check third URL with images
u3 := result.URLs[2]
if len(u3.Images) != 2 {
t.Fatalf("expected 2 images, got %d", len(u3.Images))
}
if u3.Images[0].Loc != "https://example.com/img/photo1.jpg" {
t.Errorf("expected image loc, got '%s'", u3.Images[0].Loc)
}
if u3.Images[0].Caption != "A beautiful photo" {
t.Errorf("expected caption 'A beautiful photo', got '%s'", u3.Images[0].Caption)
}
// Sitemaps should be nil/empty
if len(result.Sitemaps) != 0 {
t.Errorf("expected empty sitemaps, got %d", len(result.Sitemaps))
}
}
func TestParseSitemapIndex(t *testing.T) {
result, err := Parse(testSitemapIndexXML)
if err != nil {
t.Fatalf("Parse sitemapindex failed: %s", err.Error())
}
if result.Type != "sitemapindex" {
t.Errorf("expected type 'sitemapindex', got '%s'", result.Type)
}
if len(result.Sitemaps) != 2 {
t.Fatalf("expected 2 sitemaps, got %d", len(result.Sitemaps))
}
if result.Sitemaps[0].Loc != "https://example.com/sitemap1.xml" {
t.Errorf("unexpected sitemap loc: %s", result.Sitemaps[0].Loc)
}
if result.Sitemaps[0].LastMod != "2025-01-01" {
t.Errorf("unexpected lastmod: %s", result.Sitemaps[0].LastMod)
}
// URLs should be nil/empty
if len(result.URLs) != 0 {
t.Errorf("expected empty URLs, got %d", len(result.URLs))
}
}
func TestParseEmpty(t *testing.T) {
_, err := Parse("")
if err == nil {
t.Error("expected error for empty input")
}
}
func TestParseInvalidXML(t *testing.T) {
_, err := Parse("")
if err == nil {
t.Error("expected error for non-sitemap XML")
}
}
func TestValidateURLSet(t *testing.T) {
err := Validate(testURLSetXML)
if err != nil {
t.Errorf("expected valid urlset, got: %s", err.Error())
}
}
func TestValidateSitemapIndex(t *testing.T) {
err := Validate(testSitemapIndexXML)
if err != nil {
t.Errorf("expected valid sitemapindex, got: %s", err.Error())
}
}
func TestValidateEmpty(t *testing.T) {
err := Validate("")
if err == nil {
t.Error("expected error for empty input")
}
}
func TestValidateMissingLoc(t *testing.T) {
xml := `
2025-01-01
`
err := Validate(xml)
if err == nil {
t.Error("expected error for missing ")
}
}
func TestValidateInvalidXML(t *testing.T) {
err := Validate("test")
if err == nil {
t.Error("expected error for malformed XML")
}
}