package web
import (
"os"
"regexp"
"strings"
"testing"
)
// TestQuickStartRecipesStartRight: the first recipe on an empty
// repository's page pushes an existing repository to it (it adopts that
// project's code and history), and every other recipe starts with a
// clone of this repository, which is what gives a new local copy its
// project code. Every recipe ends in a push, and the first contact is
// never a pull or a sync.
func TestQuickStartRecipesStartRight(t *testing.T) {
pres := regexp.MustCompile(`(?s)<pre data-hook="(qs-[a-z]+)">(.*?)</pre>`).FindAllStringSubmatch(quickStartSource, -1)
if len(pres) < 2 {
t.Fatalf("FAIL: want at least two recipes, got %d", len(pres))
}
if pres[0][1] != "qs-push" {
t.Errorf("FAIL: the first recipe is %s, not the push of an existing repository", pres[0][1])
}
for _, m := range pres {
lines := strings.Split(strings.TrimSpace(m[2]), "\n")
clones := strings.HasPrefix(lines[0], "zit clone {{.CloneURL}}")
if m[1] == "qs-push" {
clones = false
if !strings.Contains(m[2], "zit remote {{.CloneURL}}") {
t.Errorf("FAIL: recipe %s never names this repository as the remote", m[1])
}
} else if !clones {
t.Errorf("FAIL: recipe %s starts with %q, not a clone of this repository", m[1], lines[0])
}
if !strings.HasPrefix(lines[len(lines)-1], "zit push") {
t.Errorf("FAIL: recipe %s does not end in a push", m[1])
}
for _, l := range lines {
if strings.HasPrefix(l, "zit pull") || strings.HasPrefix(l, "zit sync") {
t.Errorf("FAIL: recipe %s contacts the repository with %q before pushing", m[1], l)
}
}
}
if strings.Count(quickStartSource, "<pre") != len(pres) {
t.Errorf("FAIL: a recipe on the page has no qs- hook")
}
if !strings.Contains(quickStartSource, `data-hook="qs-first"`) {
t.Errorf("FAIL: the page does not say the first contact must be a push")
}
if !t.Failed() {
t.Log("ok")
}
}
// TestQuickStartLinksTheAgentsGuide: the empty repository's page points
// an agent session at the guide, after the recipes, and the link
// resolves to a guide that exists in this tree.
func TestQuickStartLinksTheAgentsGuide(t *testing.T) {
m := regexp.MustCompile(`<[a-z]+[^>]*data-hook="agents-guide"[^>]*>.*?<a href="([^"]+)"`).FindStringSubmatch(quickStartSource)
if m == nil {
t.Fatalf("FAIL: the page has no agents-guide link")
}
const path = "/r/quarry/doc/trunk/docs/agents.md"
if m[1] != path {
t.Errorf("FAIL: agents-guide href is %q, want %q", m[1], path)
}
if _, err := os.Stat("../../docs/agents.md"); err != nil {
t.Errorf("FAIL: the guide the link names is not in the tree: %v", err)
}
if strings.Index(quickStartSource, `data-hook="agents-guide"`) < strings.LastIndex(quickStartSource, `data-hook="qs-files"`) {
t.Errorf("FAIL: the link comes before the last recipe")
}
if !t.Failed() {
t.Log("ok")
}
}