package web
import (
"strings"
"testing"
)
// A file page draws a window and offers the next, because chroma emits
// a span per token: a whole-file render of a 198KB source built 40,083
// DOM nodes to show a reader forty lines.
func TestFileWindowOffersTheNextAndStopsAtTheEnd(t *testing.T) {
more := fileDetail{Hash: "abc", Path: "src/btree.c", NextFrom: 400}
var b strings.Builder
if err := noHooks.template(filePage).ExecuteTemplate(&b, "file-window",
pageData{Repo: "sqlite", File: more, FileHTML: "<pre>x</pre>"}); err != nil {
t.Fatal(err)
}
got := b.String()
if !strings.Contains(got, `id="file-more"`) || !strings.Contains(got, "data-on-intersect__once") {
t.Errorf("mid-file window has no sentinel for the next one:\n%s", got)
}
// The no-script reader needs a real link, not a sentinel that only
// script can act on.
if !strings.Contains(got, "<noscript>") || !strings.Contains(got, `href="?from=400"`) {
t.Errorf("no-script readers cannot reach the rest of the file:\n%s", got)
}
// At the end of the file there is nothing further to ask for, and
// a sentinel there would fetch forever.
var e strings.Builder
end := fileDetail{Hash: "abc", Path: "src/btree.c", NextFrom: 0, TotalLines: 812}
if err := noHooks.template(filePage).ExecuteTemplate(&e, "file-window",
pageData{Repo: "sqlite", File: end, FileHTML: "<pre>x</pre>"}); err != nil {
t.Fatal(err)
}
if strings.Contains(e.String(), `id="file-more"`) {
t.Errorf("the last window still asks for another:\n%s", e.String())
}
}
// The fragment URL must put .frag BEFORE the path: a {path...} wildcard
// has to be the last thing in a ServeMux pattern, and a suffix after it
// panics the router at registration.
func TestFileFragURLKeepsTheWildcardLast(t *testing.T) {
f := fileDetail{Hash: "abc", Path: "src/btree.c", NextFrom: 400}
got := f.MoreURL("sqlite")
if !strings.HasPrefix(got, "/r/sqlite/file.frag/abc/src/btree.c") {
t.Errorf("MoreURL = %q, want .frag ahead of the path", got)
}
if strings.Contains(got, ".c.frag") {
t.Errorf("MoreURL puts a suffix after the wildcard: %q", got)
}
}