Skip to content
quarry Artifact [5e1605a711]
Login

Artifact [5e1605a711]

Artifact 5e1605a711614e699bb4c2ed871d300533ddcc7ee2a414c40977cdbfcbff12ec:


package api

import (
	"strings"
	"testing"

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

// A compiled wiki's citations name one file at one check-in in either
// of fossil's spellings; both reach quarry's file page, and a technote
// this repository does not hold goes to the list rather than a 404.
func TestFossilFileAndTechnoteLinksResolve(t *testing.T) {
	r := resolver{repo: "kb"}
	for in, want := range map[string]string{
		"/file?name=sources/specs/a.md&ci=0123456789abcdef&txt=1": "/r/kb/file/0123456789abcdef/sources/specs/a.md?txt=1",
		"/file/sources/specs/a.md?ci=0123456789abcdef":            "/r/kb/file/0123456789abcdef/sources/specs/a.md",
		"/file?name=README.md":                                    "/r/kb/file/trunk/README.md",
		"/file?name=dir/with%20space.md&ci=abc0123456":            "/r/kb/file/abc0123456/dir/with%20space.md",
		"/file":                      "",
		"/technote/0123456789abcdef": "/r/kb/technote",
	} {
		if got := r.ResolvePath(in); got != want {
			t.Errorf("FAIL: %s -> %q, want %q", in, got, want)
		}
	}
}

// Through the Markdown renderer, as a wiki page is drawn: the & in the
// rendered href is written &, and the check-in must survive it.
func TestAWikiCitationRendersAsALinkToTheFile(t *testing.T) {
	out, err := markup.Render([]byte("See [the spec](/file?name=sources/specs/a.md&ci=0123456789abcdef&txt=1)."), resolver{repo: "kb"})
	if err != nil {
		t.Fatal(err)
	}
	if want := `href="/r/kb/file/0123456789abcdef/sources/specs/a.md?txt=1"`; !strings.Contains(string(out), want) {
		t.Errorf("FAIL: rendered %s, want a link %s", out, want)
	}
}

// A technote this repository holds is linked itself, in either of
// fossil's spellings.
func TestATechnoteLinkGoesToTheTechnote(t *testing.T) {
	ix := wikiIndex(t)
	const ev = "0123456789abcdef0123456789abcdef01234567"
	if err := ix.FoldNow([]index.Parsed{{Hash: "tn1", Art: &artifact.Artifact{Type: artifact.Technote,
		EventID: ev, EventTime: "2026-08-15T09:00:00", Date: "2026-08-15T09:00:00", User: "dan",
		Mimetype: "text/x-markdown", Comment: "note", Body: []byte("body")}}}); err != nil {
		t.Fatal(err)
	}
	r := resolver{repo: "kb", ix: ix}
	for _, in := range []string{"/technote/" + ev[:12], "/technote?name=" + ev[:12], "/event/" + ev} {
		if got, want := r.ResolvePath(in), "/r/kb/technote/"+ev; got != want {
			t.Errorf("FAIL: %s -> %q, want %q", in, got, want)
		}
	}
	if !t.Failed() {
		t.Log("ok")
	}
}