package atom10
import (
"bytes"
"encoding/xml"
"strings"
"testing"
"time"
"mercemay.top/src/tilstream/internal/feed"
)
func TestWriterMinimalFeed(t *testing.T) {
t.Parallel()
f := Feed{
Title: "TIL",
ID: "https://example.com/",
SelfLink: "https://example.com/atom.xml",
AltLink: "https://example.com/",
Updated: time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC),
Author: Person{Name: "me"},
}
entries := []feed.Item{
{
Title: "Hello",
Link: "https://example.com/hello",
GUID: "https://example.com/hello",
Summary: "intro",
Content: "<p>hi</p>",
Published: time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC),
Updated: time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC),
Categories: []string{"intro"},
},
}
var buf bytes.Buffer
if err := NewWriter().Write(&buf, f, entries); err != nil {
t.Fatalf("Write: %v", err)
}
out := buf.String()
for _, want := range []string{
`xmlns="http://www.w3.org/2005/Atom"`,
`<title>TIL</title>`,
`<id>https://example.com/</id>`,
`<category term="intro">`,
`rel="self"`,
`rel="alternate"`,
} {
if !strings.Contains(out, want) {
t.Errorf("missing %q in output:\n%s", want, out)
}
}
}
func TestFeedRoundTrip(t *testing.T) {
t.Parallel()
var buf bytes.Buffer
f := Feed{Title: "t", ID: "id"}
if err := NewWriter().Write(&buf, f, nil); err != nil {
t.Fatalf("Write: %v", err)
}
var doc feedXML
if err := xml.Unmarshal(buf.Bytes(), &doc); err != nil {
t.Fatalf("unmarshal: %v", err)
}
if doc.Title != "t" {
t.Errorf("title = %q", doc.Title)
}
}
func TestFormatRFC3339Zero(t *testing.T) {
t.Parallel()
if got := formatRFC3339(time.Time{}); got != "" {
t.Errorf("zero time should stringify empty, got %q", got)
}
}