Artifact 16d614b4b113eb53de7d4033d620ad40446288d7ab82af0f582e6fce8e101d53:
- File
internal/web/filewindow_test.go
— part of check-in
[c517ec9464]
at
2026-08-31 12:46:16
on branch trunk
— file pages draw a window, not the whole file
Chroma emits a span per token, so whole-file renders built DOMs nobody asked for: 198KB of build.c became 40,083 nodes and 137ms of parse. Lexing still starts at byte zero — a window opening inside a block comment cannot colour itself — but the iterator stops at the window's last line, so the tail is never tokenised. 103ms to 2.7ms for a first screen. Scroll fetches the next window; no-script gets a link. (user: quarry, size: 2074) [annotate] [blame] [check-ins using]
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 := 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 := 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) } }