Skip to content
quarry Artifact [80586097f1]
Login

Artifact [80586097f1]

Artifact 80586097f13e0c5d44186e8b66da9dd555e83736c1cd8da7866c8504197a0215:


package api

import (
	"net/http"
	"net/http/httptest"
	"os/exec"
	"path/filepath"
	"strings"
	"testing"

	"craftdesign.group/quarry/internal/xfer"
)

// What a delete and a rename write must read back through stock fossil:
// the clone takes, the deleted page is hidden from `wiki list`, the
// moved one is listed under its new title, and `wiki export` of the new
// title carries the text.
func TestStockFossilReadsADeleteAndARename(t *testing.T) {
	if _, err := exec.LookPath("fossil"); err != nil {
		t.Skip("SKIPPED, NOT PASSED: fossil is not on PATH, so wiki delete, rename and the whitespace-deletion rule were NOT checked against stock fossil; install fossil where this runs")
	}
	f := newWikiFixture(t)
	f.save(t, "Plain", "stays\n")
	f.save(t, "Doomed", "to go\n")
	f.save(t, "Old title", "the moved text\n")
	// Fossil calls a page with nothing after its leading whitespace
	// deleted; the forge's list must agree with `fossil wiki list`.
	f.save(t, "Blank", "  \n\t\n")
	if _, err := f.r.Invoke(as("pat", "k"), "wiki.delete", wikiDeleteIn{Repo: "test", Name: "Doomed"}); err != nil {
		t.Fatal(err)
	}
	renamed, err := f.r.Invoke(as("pat", "kf"), "wiki.rename", wikiRenameIn{Repo: "test", Name: "Old title", To: "New title"})
	if err != nil {
		t.Fatal(err)
	}

	front := xfer.New()
	defer front.Close()
	front.Register("alpha", f.dir)
	mux := http.NewServeMux()
	mux.Handle("POST /r/{repo}", front)
	mux.Handle("POST /r/{repo}/xfer", front)
	srv := httptest.NewServer(mux)
	defer srv.Close()

	work := t.TempDir()
	fossil := func(args ...string) string {
		t.Helper()
		cmd := exec.Command("fossil", args...)
		cmd.Dir = work
		cmd.Stdin = nil
		out, err := cmd.CombinedOutput()
		if err != nil {
			t.Fatalf("FAIL: fossil %v: %v\n%s", args, err, out)
		}
		return string(out)
	}
	fossil("clone", srv.URL+"/r/alpha", "c.fossil")
	repo := filepath.Join(work, "c.fossil")

	live := strings.Fields(strings.ReplaceAll(fossil("wiki", "list", "-R", repo), " ", "_"))
	want := map[string]bool{"Plain": true, "New_title": true}
	forge := f.live(t)
	if len(forge) != 2 {
		t.Errorf("FAIL: the forge lists %v, want Plain and New title only", forge)
	}
	for _, title := range live {
		if !want[title] {
			t.Errorf("FAIL: fossil wiki list shows %q, which is deleted or moved", title)
		}
		delete(want, title)
	}
	for title := range want {
		t.Errorf("FAIL: fossil wiki list does not show %q", title)
	}
	if out := fossil("wiki", "export", "New title", "-R", repo); !strings.Contains(out, "the moved text") {
		t.Errorf("FAIL: fossil wiki export of the new title: %q", out)
	}
	all := fossil("wiki", "list", "--all", "-R", repo)
	if !strings.Contains(all, "Doomed") || !strings.Contains(all, "Old title") {
		t.Errorf("FAIL: fossil wiki list --all lost the ended pages: %q", all)
	}
	// The ending edit's C card reads back from stock fossil as written,
	// and the clone above took the repository holding it.
	if out := fossil("artifact", renamed.(wikiRenameOut).Ended, "-R", repo); !strings.Contains(out, "C moved\\sto\\sNew\\stitle\n") {
		t.Errorf("FAIL: fossil artifact of the old page's ending edit lacks the C card: %q", out)
	}
	if out := fossil("wiki", "export", "Old title", "-R", repo); strings.TrimSpace(out) != "" {
		t.Errorf("FAIL: fossil wiki export of the moved title is not empty: %q", out)
	}
	if !t.Failed() {
		t.Logf("ok: stock fossil lists %v; --all also holds the ended pages", live)
	}
}