Skip to content
quarry Artifact [7f6ddf040a]
Login

Artifact [7f6ddf040a]

Artifact 7f6ddf040a83677267dc15ab1d7d918615d976f142112cd987cefba56ff4f62c:


package web

import (
	"encoding/json"
	"fmt"
	"strings"
	"testing"
)

// A large check-in (checkin.diff's large mode) draws its files, each
// with its counts and a link to its own diff, and no hunks.
func TestALargeCheckinPageListsItsFiles(t *testing.T) {
	var dd diffDetail
	if err := json.Unmarshal([]byte(`{"from":"aa","to":"bbbb","large":true,"totalLines":40001,
		"files":[{"status":"M","name":"src/z.go"},{"status":"A","name":"gen/a b.json"}],
		"counts":{"src/z.go":{"Added":1,"Removed":2},"gen/a b.json":{"Added":39998,"Removed":0}}}`), &dd); err != nil {
		t.Fatal(err)
	}
	dd.Repo = "alpha"
	var buf strings.Builder
	if err := noHooks.template(diffPage).ExecuteTemplate(&buf, "diff-body", pageData{Repo: "alpha", Diff: dd}); err != nil {
		t.Fatal(err)
	}
	got := buf.String()
	if !strings.Contains(got, `data-hook="diff-large" data-files="2"`) || strings.Count(got, `data-hook="diff-large-file"`) != 2 {
		t.Fatalf("FAIL: the large diff does not list its two files: %s", got)
	}
	if !strings.Contains(got, `href="/r/alpha/checkin/bbbb/diff?file=gen%2Fa%20b.json"`) || !strings.Contains(got, "+39998") {
		t.Errorf("FAIL: a file lacks its own diff link or its counts: %s", got)
	}
	if i, j := strings.Index(got, "gen/a b.json"), strings.Index(got, "src/z.go"); i < 0 || j < 0 || i > j {
		t.Error("FAIL: the files are not in path order")
	}
}

// A change with thousands of files lists them a page at a time, each
// link at the repository's own address and keeping the branch mode,
// and the pager links are not for crawlers.
func TestAHugeFileListIsPaged(t *testing.T) {
	var dd diffDetail
	dd.Large, dd.To, dd.Repo, dd.Branch, dd.Base = true, "tip", "pkgsrc", true, "/r/dmestas/pkgsrc"
	for i := 0; i < 2500; i++ {
		dd.Files = append(dd.Files, struct {
			Status string `json:"status"`
			Name   string `json:"name"`
		}{"M", fmt.Sprintf("f%04d", i)})
	}
	page := func(n int) string {
		dd.FilePage = n
		var buf strings.Builder
		if err := noHooks.template(diffPage).ExecuteTemplate(&buf, "diff-body", pageData{Repo: "pkgsrc", Diff: dd}); err != nil {
			t.Fatal(err)
		}
		return buf.String()
	}
	first, last := page(0), page(3)
	if n := strings.Count(first, `data-hook="diff-large-file"`); n != largeFilesPage {
		t.Errorf("FAIL: the first page lists %d files", n)
	}
	if n := strings.Count(last, `data-hook="diff-large-file"`); n != 500 || !strings.Contains(last, "f2499") || strings.Contains(last, "f0000") {
		t.Errorf("FAIL: the last page lists %d files", n)
	}
	if !strings.Contains(first, `href="/r/dmestas/pkgsrc/checkin/tip/diff?branch=1&amp;file=f0000"`) {
		t.Error("FAIL: a file link is not at the canonical address with the branch mode")
	}
	if !strings.Contains(first, `data-hook="diff-large-pager"`) || !strings.Contains(first, `href="?branch=1&amp;files=2" rel="nofollow"`) {
		t.Errorf("FAIL: no crawler-free link to the next page")
	}
	if page(99) != last {
		t.Error("FAIL: a page past the end is not the last page")
	}
	t.Log("ok")
}