67 lines
2.2 KiB
Go
67 lines
2.2 KiB
Go
package opml
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"git.pengzhan.dev/haopengzhan/rsspipe/internal/models"
|
|
)
|
|
|
|
func TestParseAndGenerateOPML(t *testing.T) {
|
|
sampleOPML := `<?xml version="1.0" encoding="UTF-8"?>
|
|
<opml version="2.0">
|
|
<head>
|
|
<title>Miniflux Subscriptions</title>
|
|
</head>
|
|
<body>
|
|
<outline text="Technology" title="Technology">
|
|
<outline type="rss" text="Hacker News" title="Hacker News" xmlUrl="https://news.ycombinator.com/rss" htmlUrl="https://news.ycombinator.com/" />
|
|
<outline type="rss" text="Go Blog" title="The Go Programming Language" xmlUrl="https://go.dev/blog/feed.atom" htmlUrl="https://go.dev/blog" />
|
|
</outline>
|
|
<outline type="rss" text="Ars Technica" title="Ars Technica" xmlUrl="https://feeds.arstechnica.com/arstechnica/index" htmlUrl="https://arstechnica.com" />
|
|
</body>
|
|
</opml>`
|
|
|
|
feeds, err := ParseOPML([]byte(sampleOPML))
|
|
if err != nil {
|
|
t.Fatalf("ParseOPML failed: %v", err)
|
|
}
|
|
|
|
if len(feeds) != 3 {
|
|
t.Fatalf("expected 3 feeds, got %d", len(feeds))
|
|
}
|
|
|
|
// Verify first feed with category
|
|
if feeds[0].Title != "Hacker News" || feeds[0].FeedURL != "https://news.ycombinator.com/rss" || feeds[0].CategoryTitle != "Technology" {
|
|
t.Errorf("unexpected feed 0: %+v", feeds[0])
|
|
}
|
|
|
|
// Verify uncategorized feed
|
|
if feeds[2].Title != "Ars Technica" || feeds[2].FeedURL != "https://feeds.arstechnica.com/arstechnica/index" || feeds[2].CategoryTitle != "" {
|
|
t.Errorf("unexpected feed 2: %+v", feeds[2])
|
|
}
|
|
|
|
// Test Export Generation
|
|
catID := "cat-1"
|
|
categories := []models.Category{
|
|
{ID: catID, Title: "Technology"},
|
|
}
|
|
modelFeeds := []models.Feed{
|
|
{ID: "f-1", Title: "Hacker News", FeedURL: "https://news.ycombinator.com/rss", SiteURL: "https://news.ycombinator.com/", CategoryID: &catID},
|
|
{ID: "f-2", Title: "Ars Technica", FeedURL: "https://feeds.arstechnica.com/arstechnica/index", SiteURL: "https://arstechnica.com"},
|
|
}
|
|
|
|
exportedXML, err := GenerateOPML(modelFeeds, categories)
|
|
if err != nil {
|
|
t.Fatalf("GenerateOPML failed: %v", err)
|
|
}
|
|
|
|
reParsed, err := ParseOPML(exportedXML)
|
|
if err != nil {
|
|
t.Fatalf("Failed to re-parse exported OPML: %v", err)
|
|
}
|
|
|
|
if len(reParsed) != 2 {
|
|
t.Fatalf("expected 2 re-parsed feeds, got %d", len(reParsed))
|
|
}
|
|
}
|