package webread
import (
"fmt"
"strings"
"testing"
"amadan.net/oficina/equipo/internal/websource"
)
func mustURL(t testing.TB, raw string) websource.URL {
t.Helper()
u, err := websource.Parse(raw)
if err != nil {
t.Fatalf("Parse(%q): %v", raw, err)
}
return u
}
const shopPage = `
The Shop & Co
Welcome
Open Monday to
Saturday.
About Services
Elsewhere
bad
loop
`
func TestExtractHTML(t *testing.T) {
base := mustURL(t, "https://shop.example.com/")
p := extractHTML([]byte(shopPage), base)
if p.title != "The Shop & Co" {
t.Errorf("title %q", p.title)
}
metas := map[string]string{}
for _, m := range p.metas {
metas[m.name] = m.content
}
if metas["description"] != "Cuts and shaves since 1990" || metas["theme-color"] != "#1a2b3c" || metas["og:title"] != "The Shop" || metas["og:image"] != "/og.png" {
t.Errorf("metas %v", metas)
}
if !strings.Contains(p.text, "Welcome") || !strings.Contains(p.text, "Open Monday to Saturday.") {
t.Errorf("text %q", p.text)
}
if strings.Contains(p.text, "var x") || strings.Contains(p.text, "hidden") {
t.Errorf("script or comment in text %q", p.text)
}
if !strings.Contains(p.styles, "#112233") {
t.Errorf("styles %q", p.styles)
}
var got []string
for _, r := range p.refs {
s := kindName[r.kind] + " "
if r.err != nil {
s += "!" + websource.Reason(r.err)
} else {
s += r.url.String()
}
if r.logo {
s += " logo"
}
got = append(got, s)
}
want := []string{
"og https://shop.example.com/og.png",
"stylesheet https://shop.example.com/a.css",
"icon https://shop.example.com/favicon.png",
"icon https://cdn.example.net/touch.png",
"img https://shop.example.com/img/Logo.png logo",
"img https://shop.example.com/hero.jpg",
"link https://shop.example.com/about",
"link https://shop.example.com/services?x=1",
"link https://other.example.org/",
"link !ip_literal",
"img https://shop.example.com/brand.svg logo",
}
if strings.Join(got, "\n") != strings.Join(want, "\n") {
t.Errorf("refs:\n%s\nwant:\n%s", strings.Join(got, "\n"), strings.Join(want, "\n"))
}
}
func TestExtractBaseHref(t *testing.T) {
p := extractHTML([]byte(`x`), mustURL(t, "https://shop.example.com/"))
if len(p.refs) != 1 || p.refs[0].url.String() != "https://shop.example.com/sub/page" {
t.Fatalf("refs %+v", p.refs)
}
// A base that fails the contract is ignored.
p = extractHTML([]byte(`x`), mustURL(t, "https://shop.example.com/"))
if len(p.refs) != 1 || p.refs[0].url.String() != "https://shop.example.com/page" {
t.Fatalf("refs %+v", p.refs)
}
}
func TestExtractBounds(t *testing.T) {
var b strings.Builder
for i := range MaxLinks + 50 {
fmt.Fprintf(&b, `p`, i)
}
b.WriteString(strings.Repeat("word ", MaxPageText))
p := extractHTML([]byte(b.String()), mustURL(t, "https://shop.example.com/"))
if len(p.refs) != MaxLinks {
t.Errorf("%d refs, want %d", len(p.refs), MaxLinks)
}
if len(p.text) > MaxPageText {
t.Errorf("text %d bytes", len(p.text))
}
// Unterminated constructs end the scan without a panic.
for _, s := range []string{"<", "", "t", "", "`, i)
}
for i := range 5 {
fmt.Fprintf(&b, `
`, i)
}
b.WriteString(`
homex`)
for i := range 8 {
fmt.Fprintf(&b, `p`, i)
}
b.WriteString(``)
page := mustURL(t, "https://shop.example.com/")
got := expansions(extractHTML([]byte(b.String()), page), page)
var lines []string
for _, c := range got {
if c.err != nil {
lines = append(lines, "!"+websource.Reason(c.err))
} else {
lines = append(lines, c.url.EscapedPath)
}
}
want := "/s0.css /s1.css /s2.css /s3.css /s4.css /logo0.png /logo1.png /logo2.png /page0 /page1 /page2 /page3 /page4 !port_refused"
if strings.Join(lines, " ") != want {
t.Fatalf("expansion\n got %s\nwant %s", strings.Join(lines, " "), want)
}
}