Files
go-fa-api/gallery_parser_test.go
2026-05-25 22:27:18 +02:00

84 lines
2.2 KiB
Go

package fa
import (
"bytes"
"strings"
"testing"
"github.com/PuerkitoBio/goquery"
)
const syntheticGalleryHTML = `<html><body>
<figure id="sid-1001" class="t-image r-general">
<a href="/view/1001/" title="Submission One">
<img src="//d.example/thumb/1001.png" data-src="//d.example/thumb/1001.png"/>
</a>
<figcaption>
<p>Submission One</p>
<a href="/user/artistone/">ArtistOne</a>
</figcaption>
</figure>
<figure id="sid-1002" class="t-image r-adult">
<a href="/view/1002/" title="Submission Two">
<img src="//d.example/thumb/1002.png"/>
</a>
<figcaption>
<p>Submission Two</p>
<a href="/user/artisttwo/">ArtistTwo</a>
</figcaption>
</figure>
<a class="button standard" href="/gallery/me/2/">Next</a>
</body></html>`
func TestParseGalleryPage_Synthetic(t *testing.T) {
doc, err := goquery.NewDocumentFromReader(strings.NewReader(syntheticGalleryHTML))
if err != nil {
t.Fatalf("setup: %v", err)
}
items, hasNext := parseGalleryPage(doc, false)
if len(items) != 2 {
t.Fatalf("items = %d; want 2", len(items))
}
if items[0].ID != 1001 || items[1].ID != 1002 {
t.Errorf("ids = [%d, %d]", items[0].ID, items[1].ID)
}
if items[0].Title != "Submission One" {
t.Errorf("items[0].Title = %q", items[0].Title)
}
if items[0].Rating != RatingGeneral {
t.Errorf("items[0].Rating = %q; want General", items[0].Rating)
}
if items[1].Rating != RatingAdult {
t.Errorf("items[1].Rating = %q; want Adult", items[1].Rating)
}
if items[0].Author.Name != "artistone" {
t.Errorf("items[0].Author.Name = %q", items[0].Author.Name)
}
if !strings.HasPrefix(items[0].ThumbURL, "https://") {
t.Errorf("items[0].ThumbURL = %q; want absolute URL", items[0].ThumbURL)
}
if !hasNext {
t.Error("hasNext = false; want true")
}
}
func TestParseGalleryPage_RealFixture(t *testing.T) {
raw := loadFixture(t, "gallery_page1.html")
doc, err := goquery.NewDocumentFromReader(bytes.NewReader(raw))
if err != nil {
t.Fatalf("read doc: %v", err)
}
items, _ := parseGalleryPage(doc, false)
if len(items) == 0 {
t.Fatal("real fixture: no items parsed")
}
for i, it := range items {
if it.ID == 0 {
t.Errorf("item %d: ID == 0", i)
}
if it.Title == "" {
t.Errorf("item %d: empty Title", i)
}
}
}