package web
import (
"fmt"
"net/http/httptest"
"net/url"
"os"
"os/exec"
"path/filepath"
"strings"
"sync/atomic"
"testing"
"craftdesign.group/quarry/internal/api"
"craftdesign.group/quarry/internal/artifact"
"craftdesign.group/quarry/internal/index"
"craftdesign.group/quarry/internal/ops"
"craftdesign.group/quarry/internal/zitcli"
)
// A file's own diff is one window, however the file changed: a
// rewritten 35,000-row data file (tcllib's map/perf/compare.txt) was
// 70,000 lines on one page, 41 MB of HTML, and a repository address
// lookup for every line number, 140,000 of them. Lines far longer than
// anyone reads are cut, and say so. The page stays a few MB, and it asks
// for the repository's address a fixed number of times.
func TestAFileDiffPageIsBounded(t *testing.T) {
if _, err := exec.LookPath("zit"); err != nil {
t.Skip("zit not on PATH")
}
dir := t.TempDir()
z := func(args ...string) string {
t.Helper()
cmd := exec.Command("zit", args...)
cmd.Dir = dir
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("zit %v: %v\n%s", args, err, out)
}
return string(out)
}
tip := func() string {
t.Helper()
for _, f := range strings.Fields(z("branch", "info", "trunk")) {
if len(f) == 64 && strings.Trim(f, "0123456789abcdef") == "" {
return f
}
}
t.Fatal("no tip")
return ""
}
write := func(name, tag string, rows, width int) {
t.Helper()
var b strings.Builder
for i := 0; i < rows; i++ {
fmt.Fprintf(&b, "%s,%d,%s\n", tag, i, strings.Repeat("9", width))
}
if err := os.WriteFile(filepath.Join(dir, name), []byte(b.String()), 0o644); err != nil {
t.Fatal(err)
}
}
z("init")
write("compare.txt", "a", 35000, 8)
write("wide.csv", "a", 1, 8)
z("add", "compare.txt", "wide.csv")
z("commit", "-m", "one", "--user", "u")
base := tip()
write("compare.txt", "b", 35000, 8)
write("wide.csv", "b", 300, 20000)
z("commit", "-m", "two", "--user", "u")
top := tip()
ix, err := index.Open(filepath.Join(t.TempDir(), "index.db"))
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { ix.Close() })
if err := ix.FoldNow([]index.Parsed{
{Hash: base, Art: &artifact.Artifact{Type: artifact.Manifest, Date: "2026-11-23T10:00:00", User: "u", Comment: "one"},
Changes: []string{"compare.txt", "wide.csv"}},
{Hash: top, Art: &artifact.Artifact{Type: artifact.Manifest, Date: "2026-11-23T11:00:00", User: "u", Comment: "two", Parents: []string{base}},
Changes: []string{"compare.txt", "wide.csv"}},
}); err != nil {
t.Fatal(err)
}
r := ops.NewRegistry()
if err := api.RegisterAll(r, api.Deps{Version: "v", Repos: api.NewRepos(map[string]*api.Repo{
"test": {Chat: "test", Index: ix, Zit: zitcli.New(filepath.Join(dir, ".zit"))}})}); err != nil {
t.Fatal(err)
}
var lookups atomic.Int64
h := &handlerHooks{addr: &Addresses{Of: func(key string) (string, string, bool) {
lookups.Add(1)
return "ns", key, true
}}}
h.drawPages()
const pageBound = 4 << 20
for _, file := range []string{"compare.txt", "wide.csv"} {
req := httptest.NewRequest("GET", "/r/test/checkin/"+top+"/diff?file="+url.QueryEscape(file), nil)
dd, err := diffWindow(r, req, "test", top)
if err != nil {
t.Fatal(err)
}
lookups.Store(0)
var buf strings.Builder
if err := h.template(diffPage).ExecuteTemplate(&buf, "content", pageData{Repo: "test", Diff: dd}); err != nil {
t.Fatal(err)
}
page := buf.String()
rows := strings.Count(page, `data-hook="diff-line-file"`)
if buf.Len() > pageBound || rows == 0 || rows > 2*3000 {
t.Errorf("FAIL: %s: the page is %d bytes with %d line links", file, buf.Len(), rows)
}
if n := lookups.Load(); n > 8 {
t.Errorf("FAIL: %s: the page looked up the repository's address %d times", file, n)
}
if dd.OmittedLines == 0 || dd.More(false) == "" {
t.Errorf("FAIL: %s: the window leaves out nothing (omitted %d)", file, dd.OmittedLines)
}
clipped := strings.Count(page, `data-hook="diff-line-clipped"`)
if (file == "wide.csv") != (clipped > 0) {
t.Errorf("FAIL: %s: %d lines marked cut", file, clipped)
}
t.Logf("%s: %d bytes, %d line links, %d lookups, %d lines cut", file, buf.Len(), rows, lookups.Load(), clipped)
}
if !t.Failed() {
t.Log("ok")
}
}