Skip to content
quarry Artifact [5a9fbedf54]
Login

Artifact [5a9fbedf54]

Artifact 5a9fbedf5465f9158cd5e8cffe9cdc66f85ed71d5ef564a26b3037af001b01c1:


package chat

import "testing"

// Fossil's artifact clause arrives as HTML; the channel holds markdown
// links at the upstream instead, and nothing but http(s) or a path.
func TestFossilAnchorsBecomeMarkdownAtTheUpstream(t *testing.T) {
	up := "https://fossil.example/home"
	for in, want := range map[string]string{
		"x (check-in: <a href='/r/libfossil/info/6df13e'>6df13e</a>, user: <a href='/r/libfossil/timeline?u=d&amp;c=6df'>d</a>)": "x (check-in: [6df13e](https://fossil.example/r/libfossil/info/6df13e), user: [d](https://fossil.example/r/libfossil/timeline?u=d&c=6df))",
		`<a href="javascript:alert(1)">click</a>`: "click",
		`<a href='//evil.example/x'>x</a>`:        "x",
		"no anchors here":                         "no anchors here",
	} {
		if got := anchorsAsMarkdown(in, up); got != want {
			t.Errorf("FAIL: %q\n got %q\nwant %q", in, got, want)
		}
	}
	t.Log("ok")
}

// Fossil's robots write formatting markup too; the channel keeps the
// words. Prose that only looks like a tag is left as it was.
func TestFossilFormattingTagsAreDropped(t *testing.T) {
	for in, want := range map[string]string{
		"<h1 class='error'>Shutting down httpd due to system load: possible crawler swarm</h1>": "Shutting down httpd due to system load: possible crawler swarm",
		"a <b>bold</b> word<br>":         "a bold word",
		"use <dive> here, and 3 < 4 > 2": "use <dive> here, and 3 < 4 > 2",
	} {
		if got := StripFossilFormatting(in); got != want {
			t.Errorf("FAIL: %q -> %q, want %q", in, got, want)
		}
	}
	t.Log("ok")
}

// A stored line tidied with no upstream keeps an anchor's words only.
func TestTidyFossilWithNoUpstreamKeepsWords(t *testing.T) {
	in := "(check-in: <a href='/r/libfossil/info/8fc2ac33aa'>8fc2ac33aa</a>) <h1 class='error'>down</h1>"
	if got, want := TidyFossil(in, ""), "(check-in: 8fc2ac33aa) down"; got != want {
		t.Errorf("FAIL: %q, want %q", got, want)
	}
	t.Log("ok")
}