Skip to content
quarry Artifact [6f0ede772a]
Login

Artifact [6f0ede772a]

Artifact 6f0ede772ac7cc4ea9d312b901b39abae6016076e7433cd1752d470fcc2c7114:


package web

import (
	"regexp"
	"strings"
	"testing"

	"craftdesign.group/quarry/internal/artifact"
	"craftdesign.group/quarry/internal/index"
)

func renderCheckin(t *testing.T, d pageData) string {
	t.Helper()
	var sb strings.Builder
	if d.Repo == "" {
		d.Repo = "alpha"
	}
	// The handler sets OwnH1: the content draws the page's one heading.
	d.OwnH1 = true
	if d.Checkin.Hash == "" {
		d.Checkin.Hash = strings.Repeat("a1", 32)
	}
	if err := noHooks.template(checkinPage).Execute(&sb, d); err != nil {
		t.Fatal(err)
	}
	return sb.String()
}

// The subject names the page: it is the one h1, long ones are marked so
// the stylesheet can set them smaller, and the 64-character line is
// gone in favour of one chip that carries the full hash for the copy
// button without showing it.
func TestCheckinPageIsNamedBySubject(t *testing.T) {
	hash := strings.Repeat("a1", 32)
	short := renderCheckin(t, pageData{CheckinLead: "fix the shift key"})
	if n := strings.Count(short, "<h1"); n != 1 {
		t.Errorf("FAIL: %d h1 elements, want 1", n)
	}
	if !strings.Contains(short, `<h1 class="ck-title" data-hook="ci-title">fix the shift key</h1>`) {
		t.Errorf("FAIL: the subject is not the heading:\n%s", short)
	}
	long := renderCheckin(t, pageData{CheckinLead: "x", CheckinLongLead: true})
	if !strings.Contains(long, `<h1 class="ck-title long" data-hook="ci-title">`) {
		t.Error("FAIL: a long subject is not marked long")
	}
	for _, gone := range []string{`class="ci-fullhash"`, `class="ci-sub`, `class="ci-h"`} {
		if strings.Contains(short, gone) {
			t.Errorf("FAIL: page still carries %s", gone)
		}
	}
	chip := regexp.MustCompile(`(?s)<span class="ci-hash" data-hook="ci-hash-chip">.*?</span>`).FindString(short)
	if chip == "" {
		t.Fatal("FAIL: no hash chip")
	}
	for _, want := range []string{
		`<code aria-hidden="true">` + hash[:10] + `</code>`,
		`<code class="ci-hash-full sr-only" data-hook="ci-hash-full">` + hash + `</code>`,
		`<button type="button" class="ci-copy"`,
	} {
		if !strings.Contains(chip, want) {
			t.Errorf("FAIL: chip lacks %s:\n%s", want, chip)
		}
	}
	if strings.Count(short, hash) != 1+strings.Count(short, "/"+hash) {
		t.Error("FAIL: the full hash is shown somewhere other than the chip's hidden copy and links")
	}
}

// Who, branch, parent and the merge parents live in the meta line, not
// the row of links, which holds only places to go.
func TestCheckinMetaLineAndLinksRow(t *testing.T) {
	var d pageData
	d.Checkin.User, d.Checkin.Branch = "dan", "feature"
	d.Checkin.Parents = []string{strings.Repeat("b", 64), strings.Repeat("c", 64)}
	d.Checkin.Hidden = true
	html := renderCheckin(t, d)
	meta := regexp.MustCompile(`(?s)<p class="ck-meta".*?</p>`).FindString(html)
	links := regexp.MustCompile(`(?s)<p class="ci-links".*?</p>`).FindString(html)
	if meta == "" || links == "" {
		t.Fatalf("FAIL: meta %q links %q", meta, links)
	}
	for _, want := range []string{
		`href="/r/alpha/timeline?branch=feature"`,
		`href="/r/alpha/checkin/` + strings.Repeat("b", 64) + `"`,
		`href="/r/alpha/checkin/` + strings.Repeat("c", 64) + `"`,
		`<code>` + strings.Repeat("b", 10) + `</code>`,
	} {
		if !strings.Contains(meta, want) {
			t.Errorf("FAIL: meta line lacks %s", want)
		}
	}
	if strings.Contains(links, "timeline?branch=") || strings.Contains(links, strings.Repeat("b", 64)) {
		t.Error("FAIL: the links row still carries the branch or a parent")
	}
	for _, want := range []string{`/tree/`, `/tags"`, `/manifest"`, `class="ct-flag"`} {
		if !strings.Contains(links, want) {
			t.Errorf("FAIL: links row lacks %s", want)
		}
	}
}

// A corrected comment is folded, with the original and the history
// link inside; an uncorrected one has no fold.
func TestCheckinCorrectedCommentIsFolded(t *testing.T) {
	var d pageData
	if strings.Contains(renderCheckin(t, d), `data-hook="ct-was"`) {
		t.Error("FAIL: an uncorrected comment has a fold")
	}
	d.Checkin.CommentOrig = "teh original"
	html := renderCheckin(t, d)
	fold := regexp.MustCompile(`(?s)<details class="ct-was" data-hook="ct-was">.*?</details>`).FindString(html)
	if fold == "" || !strings.Contains(fold, "<summary>") || !strings.Contains(fold, "teh original") ||
		!strings.Contains(fold, `/tags">`) {
		t.Errorf("FAIL: fold %q", fold)
	}
}

// A numbered landing is named by its number alone; the id is the
// fallback for one without.
func TestCheckinOpenLandingDropsTheIDWhenNumbered(t *testing.T) {
	id := strings.Repeat("b", 64)
	line := func(n int) string {
		html := renderCheckin(t, pageData{OpenLanding: &landingRow{ID: id, Number: n, Branch: "f", Target: "trunk", Title: "t"}})
		return regexp.MustCompile(`(?s)<span class="set-why" data-hook="ci-open-landing">.*?</span>`).FindString(html)
	}
	if got := line(7); !strings.Contains(got, `data-hook="landing-number"`) || strings.Contains(got, id[:12]+"<") ||
		strings.Contains(got, ">landing "+id[:12]) {
		t.Errorf("FAIL: numbered line %q", got)
	}
	if got := line(0); !strings.Contains(got, id[:12]) || strings.Contains(got, `data-hook="landing-number"`) {
		t.Errorf("FAIL: unnumbered line %q", got)
	}
}

// Context rows: the message is the link, the hash is not.
func TestContextRowLinksTheMessage(t *testing.T) {
	h := strings.Repeat("d", 64)
	html := renderCheckin(t, pageData{Context: []ciContext{{Hash: h, User: "dan", Branch: "trunk", Text: "the message", Here: true}}})
	row := regexp.MustCompile(`(?s)<li.*?</li>`).FindString(html)
	if !strings.Contains(row, `<a class="ci-ctx-c" href="/r/alpha/checkin/`+h+`"`) ||
		!strings.Contains(row, `<code class="ci-ctx-h">`+h[:10]+`</code>`) {
		t.Errorf("FAIL: row %s", row)
	}
	if !regexp.MustCompile(`<a class="ci-ctx-c"[^>]*>[^<]*</a>`).MatchString(row) {
		t.Errorf("FAIL: the message link holds markup, which cannot nest a link: %s", row)
	}
}

// The note is plain text under the message: no fold, no label, and a
// footer that links the wiki page, its editor and its history.
func TestArtifactNoteIsPlainWithAFooter(t *testing.T) {
	d := pageData{Note: "<p>why</p>", NoteName: "checkin/abc", NoteUser: "dan", NoteDate: "2026-08-20T12:00:00", CanWikiNote: true}
	html := renderCheckin(t, d)
	if strings.Contains(html, "<details class=\"a-note") || strings.Contains(html, "<summary>About") {
		t.Error("FAIL: the note is still folded or labelled")
	}
	foot := regexp.MustCompile(`(?s)<p class="a-note-f" data-hook="note-footer">.*?</p>`).FindString(html)
	for _, want := range []string{`href="/r/alpha/wiki/checkin%2Fabc"`, `data-hook="note-edit" href="/r/alpha/wiki/checkin%2Fabc/edit"`,
		`data-hook="note-history" href="/r/alpha/wiki/checkin%2Fabc/history"`} {
		if !strings.Contains(foot, want) {
			t.Errorf("FAIL: footer lacks %s:\n%s", want, foot)
		}
	}
	d.CanWikiNote, d.NoteUser, d.NoteDate = false, "", ""
	foot = regexp.MustCompile(`(?s)<p class="a-note-f" data-hook="note-footer">.*?</p>`).FindString(renderCheckin(t, d))
	if foot == "" || strings.Contains(foot, "note-edit") || !strings.Contains(foot, `href="/r/alpha/wiki/checkin%2Fabc"`) {
		t.Errorf("FAIL: a note with no metadata and no write letter: %s", foot)
	}
}

// Through the real handler: the tab title keeps the short hash, the
// heading is the subject, and a note that opens by repeating the subject
// loses that heading but keeps its footer.
func TestCheckinPageThroughTheHandler(t *testing.T) {
	ix := landingIndex(t, false)
	tip := strings.Repeat("11", 20)
	if err := ix.FoldNow([]index.Parsed{{Hash: strings.Repeat("d4", 20), Art: &artifact.Artifact{Type: artifact.Wiki,
		WikiTitle: "checkin/" + tip, Date: "2026-08-20T12:00:00", User: "eve",
		Mimetype: "text/x-markdown", Body: []byte("# The caps work\n\nWhy it is shaped so.\n")}}}); err != nil {
		t.Fatal(err)
	}
	html := landingGet(t, landingServer(t, ix, "ok"), "/r/alpha/checkin/"+tip)
	if title := regexp.MustCompile(`<title[^>]*>(.*?)</title>`).FindStringSubmatch(html); title == nil || !strings.Contains(title[1], tip[:10]) {
		t.Errorf("FAIL: title %v lacks the short hash", title)
	}
	if !strings.Contains(html, `data-hook="ci-title"`) || strings.Count(html, "<h1") != 1 {
		t.Error("FAIL: not exactly one h1, the subject")
	}
	note := html[strings.Index(html, `data-hook="artifact-note"`):]
	note = note[:strings.Index(note, "</section>")]
	if !strings.Contains(note, "Why it is shaped so") || strings.Contains(note, "<h1") {
		t.Errorf("FAIL: repeated heading kept or body lost:\n%s", note)
	}
	if !strings.Contains(note, `data-hook="note-footer"`) || !strings.Contains(note, `data-hook="note-edit"`) ||
		!strings.Contains(note, `data-hook="note-history"`) {
		t.Errorf("FAIL: footer incomplete:\n%s", note)
	}
}