Single-binary Go server that fetches markdown from Gitea (mc org), renders to HTML with goldmark (GFM, chroma syntax highlighting, heading anchors), and serves a navigable read-only documentation site. Features: - Boot fetch with retry, webhook refresh, 15-minute poll fallback - In-memory cache with atomic per-repo swap - chi router with htmx partial responses for SPA-like navigation - HMAC-SHA256 webhook validation - Responsive CSS, TOC generation, priority doc ordering - $PORT env var support for MCP agent port assignment 33 tests across config, cache, render, and server packages. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
326 lines
7.2 KiB
Go
326 lines
7.2 KiB
Go
package server
|
|
|
|
import (
|
|
"crypto/hmac"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.wntrmute.dev/mc/mcdoc/internal/cache"
|
|
"git.wntrmute.dev/mc/mcdoc/internal/render"
|
|
)
|
|
|
|
func newTestServer(t *testing.T, c *cache.Cache, secret string) *Server {
|
|
t.Helper()
|
|
srv, err := New(Config{
|
|
Cache: c,
|
|
WebhookSecret: secret,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("new server: %v", err)
|
|
}
|
|
return srv
|
|
}
|
|
|
|
func TestHealthNotReady(t *testing.T) {
|
|
c := cache.New()
|
|
srv := newTestServer(t, c, "")
|
|
handler := srv.Handler()
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/health", nil)
|
|
w := httptest.NewRecorder()
|
|
handler.ServeHTTP(w, req)
|
|
|
|
if w.Code != http.StatusServiceUnavailable {
|
|
t.Fatalf("expected 503, got %d", w.Code)
|
|
}
|
|
}
|
|
|
|
func TestHealthReady(t *testing.T) {
|
|
c := cache.New()
|
|
c.SetReady()
|
|
srv := newTestServer(t, c, "")
|
|
handler := srv.Handler()
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/health", nil)
|
|
w := httptest.NewRecorder()
|
|
handler.ServeHTTP(w, req)
|
|
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("expected 200, got %d", w.Code)
|
|
}
|
|
}
|
|
|
|
func TestIndexNotReady(t *testing.T) {
|
|
c := cache.New()
|
|
srv := newTestServer(t, c, "")
|
|
handler := srv.Handler()
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
w := httptest.NewRecorder()
|
|
handler.ServeHTTP(w, req)
|
|
|
|
if w.Code != http.StatusServiceUnavailable {
|
|
t.Fatalf("expected 503, got %d", w.Code)
|
|
}
|
|
}
|
|
|
|
func TestIndexReady(t *testing.T) {
|
|
c := cache.New()
|
|
c.SetRepo(&cache.RepoInfo{Name: "testrepo", Description: "A test"})
|
|
c.SetReady()
|
|
|
|
srv := newTestServer(t, c, "")
|
|
handler := srv.Handler()
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
w := httptest.NewRecorder()
|
|
handler.ServeHTTP(w, req)
|
|
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("expected 200, got %d", w.Code)
|
|
}
|
|
if !strings.Contains(w.Body.String(), "testrepo") {
|
|
t.Fatal("expected testrepo in index page")
|
|
}
|
|
}
|
|
|
|
func TestRepoPage(t *testing.T) {
|
|
c := cache.New()
|
|
c.SetRepo(&cache.RepoInfo{
|
|
Name: "mcr",
|
|
Docs: []*cache.Document{
|
|
{Repo: "mcr", URLPath: "README", Title: "README"},
|
|
},
|
|
})
|
|
c.SetReady()
|
|
|
|
srv := newTestServer(t, c, "")
|
|
handler := srv.Handler()
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/mcr/", nil)
|
|
w := httptest.NewRecorder()
|
|
handler.ServeHTTP(w, req)
|
|
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("expected 200, got %d", w.Code)
|
|
}
|
|
if !strings.Contains(w.Body.String(), "README") {
|
|
t.Fatal("expected README in repo page")
|
|
}
|
|
}
|
|
|
|
func TestRepoPageNotFound(t *testing.T) {
|
|
c := cache.New()
|
|
c.SetReady()
|
|
srv := newTestServer(t, c, "")
|
|
handler := srv.Handler()
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/nonexistent/", nil)
|
|
w := httptest.NewRecorder()
|
|
handler.ServeHTTP(w, req)
|
|
|
|
if w.Code != http.StatusNotFound {
|
|
t.Fatalf("expected 404, got %d", w.Code)
|
|
}
|
|
}
|
|
|
|
func TestDocPage(t *testing.T) {
|
|
c := cache.New()
|
|
c.SetRepo(&cache.RepoInfo{
|
|
Name: "mcr",
|
|
Docs: []*cache.Document{
|
|
{
|
|
Repo: "mcr",
|
|
URLPath: "ARCHITECTURE",
|
|
Title: "Architecture",
|
|
HTML: "<h1>Architecture</h1><p>content</p>",
|
|
Headings: []render.Heading{
|
|
{Level: 1, ID: "architecture", Text: "Architecture"},
|
|
},
|
|
},
|
|
},
|
|
})
|
|
c.SetReady()
|
|
|
|
srv := newTestServer(t, c, "")
|
|
handler := srv.Handler()
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/mcr/ARCHITECTURE", nil)
|
|
w := httptest.NewRecorder()
|
|
handler.ServeHTTP(w, req)
|
|
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("expected 200, got %d", w.Code)
|
|
}
|
|
body := w.Body.String()
|
|
if !strings.Contains(body, "Architecture") {
|
|
t.Fatal("expected Architecture in doc page")
|
|
}
|
|
if !strings.Contains(body, "content") {
|
|
t.Fatal("expected rendered content in doc page")
|
|
}
|
|
}
|
|
|
|
func TestDocPageNotFound(t *testing.T) {
|
|
c := cache.New()
|
|
c.SetRepo(&cache.RepoInfo{Name: "mcr"})
|
|
c.SetReady()
|
|
|
|
srv := newTestServer(t, c, "")
|
|
handler := srv.Handler()
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/mcr/nonexistent", nil)
|
|
w := httptest.NewRecorder()
|
|
handler.ServeHTTP(w, req)
|
|
|
|
if w.Code != http.StatusNotFound {
|
|
t.Fatalf("expected 404, got %d", w.Code)
|
|
}
|
|
}
|
|
|
|
func TestHTMXPartialResponse(t *testing.T) {
|
|
c := cache.New()
|
|
c.SetRepo(&cache.RepoInfo{Name: "r", Description: "test"})
|
|
c.SetReady()
|
|
|
|
srv := newTestServer(t, c, "")
|
|
handler := srv.Handler()
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
req.Header.Set("HX-Request", "true")
|
|
w := httptest.NewRecorder()
|
|
handler.ServeHTTP(w, req)
|
|
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("expected 200, got %d", w.Code)
|
|
}
|
|
body := w.Body.String()
|
|
// Partial should not include the full layout (no <html> tag)
|
|
if strings.Contains(body, "<html") {
|
|
t.Fatal("htmx response should not include full layout")
|
|
}
|
|
}
|
|
|
|
func TestWebhookValidSignature(t *testing.T) {
|
|
secret := "test-secret"
|
|
payload := `{"repository":{"name":"mcr","full_name":"mc/mcr"}}`
|
|
|
|
mac := hmac.New(sha256.New, []byte(secret))
|
|
mac.Write([]byte(payload))
|
|
sig := hex.EncodeToString(mac.Sum(nil))
|
|
|
|
c := cache.New()
|
|
c.SetReady()
|
|
|
|
webhookCh := make(chan string, 1)
|
|
srv := newTestServer(t, c, secret)
|
|
srv.onWebhook = func(repo string) {
|
|
webhookCh <- repo
|
|
}
|
|
handler := srv.Handler()
|
|
|
|
req := httptest.NewRequest(http.MethodPost, "/webhook", strings.NewReader(payload))
|
|
req.Header.Set("X-Gitea-Signature", sig)
|
|
w := httptest.NewRecorder()
|
|
handler.ServeHTTP(w, req)
|
|
|
|
if w.Code != http.StatusNoContent {
|
|
t.Fatalf("expected 204, got %d: %s", w.Code, w.Body.String())
|
|
}
|
|
|
|
select {
|
|
case repo := <-webhookCh:
|
|
if repo != "mcr" {
|
|
t.Fatalf("webhook repo = %q, want mcr", repo)
|
|
}
|
|
case <-time.After(2 * time.Second):
|
|
t.Fatal("webhook callback not called within timeout")
|
|
}
|
|
}
|
|
|
|
func TestWebhookInvalidSignature(t *testing.T) {
|
|
c := cache.New()
|
|
srv := newTestServer(t, c, "real-secret")
|
|
handler := srv.Handler()
|
|
|
|
req := httptest.NewRequest(http.MethodPost, "/webhook", strings.NewReader(`{"repository":{"name":"mcr"}}`))
|
|
req.Header.Set("X-Gitea-Signature", "bad-signature")
|
|
w := httptest.NewRecorder()
|
|
handler.ServeHTTP(w, req)
|
|
|
|
if w.Code != http.StatusForbidden {
|
|
t.Fatalf("expected 403, got %d", w.Code)
|
|
}
|
|
}
|
|
|
|
func TestWebhookNoSecret(t *testing.T) {
|
|
c := cache.New()
|
|
srv := newTestServer(t, c, "")
|
|
handler := srv.Handler()
|
|
|
|
req := httptest.NewRequest(http.MethodPost, "/webhook", strings.NewReader(`{}`))
|
|
w := httptest.NewRecorder()
|
|
handler.ServeHTTP(w, req)
|
|
|
|
if w.Code != http.StatusInternalServerError {
|
|
t.Fatalf("expected 500, got %d", w.Code)
|
|
}
|
|
}
|
|
|
|
func TestExtractRepoName(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
payload string
|
|
want string
|
|
}{
|
|
{
|
|
name: "valid payload",
|
|
payload: `{"repository":{"id":1,"name":"mcr","full_name":"mc/mcr"}}`,
|
|
want: "mcr",
|
|
},
|
|
{
|
|
name: "no repository",
|
|
payload: `{"action":"push"}`,
|
|
want: "",
|
|
},
|
|
{
|
|
name: "empty",
|
|
payload: `{}`,
|
|
want: "",
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got := extractRepoName([]byte(tt.payload))
|
|
if got != tt.want {
|
|
t.Fatalf("extractRepoName = %q, want %q", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestSanitizePath(t *testing.T) {
|
|
tests := []struct {
|
|
input string
|
|
want string
|
|
}{
|
|
{"/foo/bar", "foo/bar"},
|
|
{"../etc/passwd", "etc/passwd"},
|
|
{"normal/path", "normal/path"},
|
|
{"../../bad", "bad"},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
got := sanitizePath(tt.input)
|
|
if got != tt.want {
|
|
t.Fatalf("sanitizePath(%q) = %q, want %q", tt.input, got, tt.want)
|
|
}
|
|
}
|
|
}
|