package web
import (
"strings"
"testing"
"craftdesign.group/quarry/internal/index"
)
func renderDiffMore(t *testing.T, data pageData) string {
t.Helper()
var buf strings.Builder
if err := noHooks.template(diffPage).ExecuteTemplate(&buf, "diff-more", data); err != nil {
t.Fatal(err)
}
return buf.String()
}
// EACH FILE OF A DIFF FOLDS, OPEN TO START, WITH NO SCRIPT.
//
// The header is the <summary> of a <details open>, so a click folds the
// file with scripting off, and the id stays on the section around it so
// #f-path links still land on the file.
func TestEachFileDiffIsAnOpenFold(t *testing.T) {
for _, split := range []bool{false, true} {
page := renderDiffMore(t, pageData{Repo: "r", Diff: *rangeDiff(), DiffSplit: split})
sec := strings.Index(page, `<section class="filediff`)
fold := strings.Index(page, `<details class="filediff-fold" data-hook="filediff-fold" open>`)
sum := strings.Index(page, `<summary class="filediff-sum"><h3 class="filediff-head">`)
end := strings.Index(page, `</details>`)
if sec < 0 || !(sec < fold && fold < sum && sum < end) {
t.Errorf("FAIL: split=%v: the file is not a section > details[open] > summary: section %d, details %d, summary %d, end %d", split, sec, fold, sum, end)
}
if !strings.Contains(page, `id="f-a.go"`) || strings.Index(page, `id="f-a.go"`) > fold {
t.Errorf("FAIL: split=%v: the file's anchor is not on the section around the fold", split)
}
if strings.Contains(page, `data-hook="file-comment-count"`) {
t.Errorf("FAIL: split=%v: a file with no notes shows a comment count", split)
}
}
t.Log("ok: each file is an open fold")
}
// A FILE'S HEADER COUNTS THE LINE NOTES DRAWN UNDER ITS LINES.
//
// A range note is filed once, under its last line, so it counts once;
// a note filed under a line this window does not show is not drawn and
// not counted. Split and unified draw the same notes, so they agree.
func TestFileHeaderCountsItsLineNotes(t *testing.T) {
det := &landingDetail{CanSpeak: true, Diff: rangeDiff()}
det.State, det.ID = "open", "abcdef123456"
det.Comments = []forumPostRow{
{Hash: "r1aaaaaaaaaa", User: "ada", HTML: "x", Line: &index.LineNote{File: "a.go", Side: "new", Line: 2, To: 4, At: "cafe12"}},
{Hash: "r2bbbbbbbbbb", User: "bob", HTML: "y", Line: &index.LineNote{File: "a.go", Side: "new", Line: 5, At: "cafe12"}},
{Hash: "r3cccccccccc", User: "cy", HTML: "z", Line: &index.LineNote{File: "a.go", Side: "new", Line: 99, At: "cafe12"}},
{Hash: "r4dddddddddd", User: "di", HTML: "w", Line: &index.LineNote{File: "other.go", Side: "new", Line: 1, At: "cafe12"}},
}
for _, split := range []bool{false, true} {
data := pageData{Repo: "r", Diff: *det.Diff, Landing: det, DiffSplit: split}
lineNotesFor(&data, det, "cafe12deadbeef", "")
page := renderDiffMore(t, data)
if !strings.Contains(page, `data-hook="file-comment-count" data-count="2"`) {
t.Errorf("FAIL: split=%v: the header does not carry a count of 2", split)
}
if n := strings.Count(page, `data-hook="file-comment-count"`); n != 1 {
t.Errorf("FAIL: split=%v: %d count chips, want 1", split, n)
}
}
t.Log("ok: the count follows the drawn notes")
}
// "COLLAPSE ALL" AND "EXPAND ALL" SIT BESIDE THE VIEW TOGGLE, HIDDEN
// UNTIL SCRIPT UNHIDES THEM.
func TestDiffBodyOffersFoldAll(t *testing.T) {
dd := rangeDiff()
dd.To = "cafe12deadbeef"
var buf strings.Builder
if err := noHooks.template(diffPage).ExecuteTemplate(&buf, "diff-body", pageData{Repo: "r", Diff: *dd}); err != nil {
t.Fatal(err)
}
page := buf.String()
bar := strings.Index(page, `<span class="foldall" hidden data-attr:hidden="false">`)
if bar < 0 || bar < strings.Index(page, `<nav class="toggle"`) {
t.Fatal("FAIL: the fold-all pair is missing, or not after the view toggle")
}
for _, hook := range []string{"diff-collapse-all", "diff-expand-all"} {
if !strings.Contains(page, `<button type="button" data-hook="`+hook+`">`) {
t.Errorf("FAIL: no %s button", hook)
}
}
}