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

104 lines
3.0 KiB
Go

package fa
import (
"bytes"
"strings"
"testing"
"github.com/PuerkitoBio/goquery"
)
const syntheticJournalHTML = `<html><body>
<a class="c-usernameBlock__displayName js-displayName-block" href="/user/jjwriter/">
<span class="js-displayName">JJWriter</span>
</a>
<img class="user-nav-avatar" src="//d.example/avatars/jjwriter.png"/>
<div id="c-journalTitleTop">
<span id="c-journalTitleTop__subject"><h3>My Journal Entry</h3></span>
</div>
<div id="c-journalTitleBottom">
<span id="c-journalTitleTop__date"><span class="popup_date" data-time="1743851460" title="April 5, 2025 11:11:00 AM">Apr 5</span></span>
</div>
<div class="section-body journal-body-theme">
<div class="journal-content user-submitted-links"><p>Some <i>thoughts</i>.</p></div>
</div>
</body></html>`
func TestParseJournal_Synthetic(t *testing.T) {
doc, err := goquery.NewDocumentFromReader(strings.NewReader(syntheticJournalHTML))
if err != nil {
t.Fatalf("setup: %v", err)
}
j, err := parseJournal(42, doc)
if err != nil {
t.Fatalf("parseJournal: %v", err)
}
if j.ID != 42 {
t.Errorf("ID = %d; want 42", j.ID)
}
if j.Title != "My Journal Entry" {
t.Errorf("Title = %q", j.Title)
}
if j.Author.Name != "jjwriter" {
t.Errorf("Author.Name = %q", j.Author.Name)
}
if j.Author.DisplayName != "JJWriter" {
t.Errorf("Author.DisplayName = %q", j.Author.DisplayName)
}
if j.PostedAt.Year() != 2025 {
t.Errorf("PostedAt year = %d", j.PostedAt.Year())
}
if !strings.Contains(j.BodyText, "thoughts") {
t.Errorf("BodyText missing: %q", j.BodyText)
}
}
const syntheticUserJournalsHTML = `<html><body>
<section id="jid:111" class="journal">
<h2><a href="/journal/111/">First Entry</a></h2>
<span class="popup_date" title="Apr 1, 2026 10:00 AM">today</span>
<div class="journal-body">Hello.</div>
</section>
<section id="jid:222" class="journal">
<h2><a href="/journal/222/">Second Entry</a></h2>
<span class="popup_date" title="Mar 30, 2026 09:00 AM">yesterday</span>
<div class="journal-body">World.</div>
</section>
<a class="button standard" href="/journals/me/2/">Next</a>
</body></html>`
func TestParseUserJournalsPage_Synthetic(t *testing.T) {
doc, err := goquery.NewDocumentFromReader(strings.NewReader(syntheticUserJournalsHTML))
if err != nil {
t.Fatalf("setup: %v", err)
}
entries, hasNext := parseUserJournalsPage(doc)
if len(entries) != 2 {
t.Fatalf("entries = %d; want 2", len(entries))
}
if entries[0].ID != 111 || entries[1].ID != 222 {
t.Errorf("ids = [%d, %d]; want [111, 222]", entries[0].ID, entries[1].ID)
}
if entries[0].Title != "First Entry" {
t.Errorf("title[0] = %q", entries[0].Title)
}
if !hasNext {
t.Error("hasNext = false; want true")
}
}
func TestParseJournal_RealFixture(t *testing.T) {
raw := loadFixture(t, "journal.html")
doc, err := goquery.NewDocumentFromReader(bytes.NewReader(raw))
if err != nil {
t.Fatalf("read doc: %v", err)
}
j, err := parseJournal(0, doc)
if err != nil {
t.Fatalf("parseJournal(real): %v", err)
}
if j.Title == "" {
t.Error("real fixture: Title is empty")
}
}