package main
import (
"strings"
"testing"
)
func TestRenderRepoMarkdownImageGoesToRaw(t *testing.T) {
html, err := renderRepoMarkdown("alice/demo", "", "\n\n[docs](docs/USAGE.md)\n")
if err != nil {
t.Fatalf("renderRepoMarkdown: %v", err)
}
s := string(html)
if !strings.Contains(s, `<img src="/repo-raw/alice/demo?path=demo.gif"`) {
t.Errorf("image not rewritten to /repo-raw: %s", s)
}
if !strings.Contains(s, `<a href="/repo-blob/alice/demo?path=docs/USAGE.md"`) {
t.Errorf("link not rewritten to /repo-blob: %s", s)
}
}
func TestRenderRepoMarkdownAbsoluteImageUntouched(t *testing.T) {
html, err := renderRepoMarkdown("alice/demo", "", "\n")
if err != nil {
t.Fatalf("renderRepoMarkdown: %v", err)
}
if !strings.Contains(string(html), `src="https://img.example/badge.svg"`) {
t.Errorf("absolute image URL should be left untouched: %s", html)
}
}
func TestRenderFileContentMarkdownIsSource(t *testing.T) {
html, err := renderFileContent("alice/demo", "", "README.md", "# Title\n")
if err != nil {
t.Fatalf("renderFileContent: %v", err)
}
if strings.Contains(string(html), "<h1") {
t.Errorf("blob view of markdown should show source, not rendered HTML: %s", html)
}
if !strings.Contains(string(html), "<pre>") {
t.Errorf("expected a <pre> source block: %s", html)
}
}
func TestFileIcon(t *testing.T) {
cases := []struct {
name string
want string
}{
{"main.go", "file-code"},
{"index.tsx", "file-code"},
{"script.sh", "file-code"},
{"README.md", "file-doc"},
{"README", "file-doc"},
{"LICENSE", "file-doc"},
{"notes.txt", "file-doc"},
{"logo.png", "file-image"},
{"photo.JPEG", "file-image"},
{"config.yaml", "file-config"},
{"package.json", "file-config"},
{".gitignore", "file"},
{"Makefile", "file"},
{"data.bin", "file"},
}
for _, c := range cases {
if got := fileIcon(c.name); got != c.want {
t.Errorf("fileIcon(%q) = %q, want %q", c.name, got, c.want)
}
}
}
func TestHumanBytes(t *testing.T) {
cases := []struct {
n int64
want string
}{
{0, "0 B"},
{1, "1 B"},
{1023, "1023 B"},
{1024, "1.0 KiB"},
{1536, "1.5 KiB"},
{1024 * 1024, "1.0 MiB"},
{1024*1024*1024*3 + 1024*1024*512, "3.5 GiB"},
}
for _, c := range cases {
if got := humanBytes(c.n); got != c.want {
t.Errorf("humanBytes(%d) = %q, want %q", c.n, got, c.want)
}
}
}