vitrine

render_test.go

package main

import (
	"net/http"
	"strings"
	"testing"
)

func TestHeadingAnchorsInRenderedPage(t *testing.T) {
	srv := newTestServer(t)
	rec := get(t, srv, "/demo/README.md")
	body := rec.Body.String()
	if !strings.Contains(body, `<h1 id="hello">`) {
		t.Errorf("heading missing slug id; body:\n%s", body)
	}
	if !strings.Contains(body, `<a href="#hello" class="anchor">`) {
		t.Errorf("heading missing chainlink anchor; body:\n%s", body)
	}
}

func TestDuplicateHeadingsGetDistinctIDs(t *testing.T) {
	srv := newTestServer(t)
	// dup.md is written by newTestServer with two identical headings.
	rec := get(t, srv, "/demo/dup.md")
	body := rec.Body.String()
	if !strings.Contains(body, `id="twice"`) || !strings.Contains(body, `id="twice-2"`) {
		t.Errorf("duplicate headings not disambiguated; body:\n%s", body)
	}
}

func TestStylesheetServed(t *testing.T) {
	rec := get(t, newTestServer(t), "/-/style.css")
	if rec.Code != 200 || !strings.HasPrefix(rec.Header().Get("Content-Type"), "text/css") {
		t.Errorf("status = %d, content-type = %q", rec.Code, rec.Header().Get("Content-Type"))
	}
}

// TestStylesheetCarriesThemeAccent checks that /-/style.css ends with the
// computed :root override (light and dark), after the embedded stylesheet:
// at equal specificity the later declaration wins,
// so appending (not prepending) is what makes the theme accent take effect.
func TestStylesheetCarriesThemeAccent(t *testing.T) {
	rec := get(t, newTestServer(t), "/-/style.css")
	if rec.Code != http.StatusOK {
		t.Fatalf("status = %d", rec.Code)
	}
	body := rec.Body.String()
	// "testhost" is the seed newTestServer uses; it hashes to hue 351, lightness 35%/70%.
	want := ":root { --accent: hsl(351 70% 35%); }\n" +
		"@media (prefers-color-scheme: dark) { :root { --accent: hsl(351 70% 70%); } }\n"
	if !strings.HasSuffix(body, want) {
		t.Errorf("stylesheet does not end with theme override %q; tail: %q", want, body[max(0, len(body)-200):])
	}
	if !strings.Contains(body, "--paper") {
		t.Errorf("embedded stylesheet missing before theme override")
	}
}

// TestNavShowsThemeName checks that pages name the host theme beside the site name.
func TestNavShowsThemeName(t *testing.T) {
	rec := get(t, newTestServer(t), "/")
	if !strings.Contains(rec.Body.String(), `<span class="theme">@testhost</span>`) {
		t.Errorf("nav missing theme name; body:\n%s", rec.Body.String())
	}
}

render_test.go · 2.3 KiB · a92ee56422