Skip to content
quarry Artifact [ef084629ae]
Login

Artifact [ef084629ae]

Artifact ef084629ae8f358b48d061d87f6b0992aeb361d838ce57a5000c9722cf3e028c:


package markup

import (
	"strings"
	"testing"
)

// attachRes answers for one file and nothing else, so the test pins both
// halves of the contract: a known name resolves, an unknown one does not.
type attachRes struct{ fakeRes }

func (attachRes) ResolveAttachment(name string, image bool) string {
	if name != "crash.png" {
		return ""
	}
	if image {
		return "/r/x/attachment/blob1/view"
	}
	return "/r/x/attachment/blob1"
}

func TestAttachLinkAndImageResolve(t *testing.T) {
	out, err := Render([]byte("[the log](attach:crash.png) and ![shot](attach:crash.png)\n"), attachRes{})
	if err != nil {
		t.Fatal(err)
	}
	got := string(out)
	if !strings.Contains(got, `href="/r/x/attachment/blob1"`) {
		t.Fatalf("FAIL: link target not resolved:\n%s", got)
	}
	if !strings.Contains(got, `src="/r/x/attachment/blob1/view"`) {
		t.Fatalf("FAIL: image target did not take the inline route:\n%s", got)
	}
	if strings.Contains(got, "attach:") {
		t.Fatalf("FAIL: an attach scheme reached the page:\n%s", got)
	}
	t.Log("ok: attach:NAME resolves to the download route for a link and the inline route for an image")
}

func TestUnknownAttachmentRendersAsText(t *testing.T) {
	out, err := Render([]byte("[the log](attach:gone.txt) and ![missing shot](attach:gone.png)\n"), attachRes{})
	if err != nil {
		t.Fatal(err)
	}
	got := string(out)
	if strings.Contains(got, "attach:") || strings.Contains(got, "<img") || strings.Contains(got, "<a href") {
		t.Fatalf("FAIL: a dead attachment stayed a link:\n%s", got)
	}
	if !strings.Contains(got, "the log") || !strings.Contains(got, "missing shot") {
		t.Fatalf("FAIL: the reader lost the text:\n%s", got)
	}
	t.Log("ok: an unresolved attachment is plain text, never a dead link")
}

// A resolver that is not there must not leak the scheme either.
func TestAttachWithNoResolverIsText(t *testing.T) {
	out, err := Render([]byte("[the log](attach:crash.png)\n"), nil)
	if err != nil {
		t.Fatal(err)
	}
	if strings.Contains(string(out), "attach:") {
		t.Fatalf("FAIL: attach scheme survived a nil resolver:\n%s", out)
	}
	t.Log("ok: no resolver, no scheme on the page")
}

func TestFossilWikiAttachLink(t *testing.T) {
	got := string(mustFW(t, "see [attach:crash.png|the shot] and [attach:gone.txt|nothing]", attachRes{}))
	if !strings.Contains(got, `href="/r/x/attachment/blob1"`) {
		t.Fatalf("FAIL: fossil-wiki attach link not resolved:\n%s", got)
	}
	if strings.Contains(got, "attach:") {
		t.Fatalf("FAIL: a dead attach target stayed visible as markup:\n%s", got)
	}
	if !strings.Contains(got, "nothing") {
		t.Fatalf("FAIL: the dead link lost its label:\n%s", got)
	}
	t.Log("ok: fossil-wiki [attach:NAME|label] resolves, and a dead one is its label")
}