package websearch
import (
"strings"
"testing"
)
// long is a body long enough to clear minUseful, so the strict reduction is
// what is being tested rather than the short-page fallback.
var long = strings.TrimSpace(strings.Repeat("the quick brown fox jumps over the lazy dog. ", 8))
// A page off the open web is as likely to be malformed as not, so the reducer
// is judged on what it keeps and what it throws away rather than on whether it
// parsed anything correctly.
func TestHTMLToText(t *testing.T) {
cases := []struct {
name, in, want string
}{{
name: "furniture is dropped",
in: "top
" + long + "
",
want: long,
}, {
name: "script and style contents never appear",
in: "
",
want: "a & b 'd'",
}, {
name: "block elements end lines",
in: "
Title
One
a
b
",
want: "Title\nOne\na\nb",
}, {
name: "inline elements do not",
in: "
a bc d
",
want: "a b c d",
}, {
name: "indentation is collapsed",
in: "
\n\t\tspaced out\n
",
want: "spaced out",
}, {
name: "unclosed tags do not derail it",
in: "
one
two
three",
want: "one\ntwo\nthree",
}, {
name: "truncated mid-tag returns what there was",
in: "
kept
also kept
menu
" + long + "
"))
if err != nil {
t.Fatal(err)
}
if got != long {
t.Errorf("got %q, want %q", got, long)
}
}
// The bug this function was rewritten for. Real pages do not balance their
// tags — go.dev opens five