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>
155 lines
3.6 KiB
Go
155 lines
3.6 KiB
Go
package config
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestLoadValidConfig(t *testing.T) {
|
|
dir := t.TempDir()
|
|
path := filepath.Join(dir, "mcdoc.toml")
|
|
|
|
content := `
|
|
[server]
|
|
listen_addr = ":9090"
|
|
|
|
[gitea]
|
|
url = "https://git.example.com"
|
|
org = "myorg"
|
|
webhook_secret = "secret123"
|
|
poll_interval = "5m"
|
|
fetch_timeout = "10s"
|
|
max_concurrency = 2
|
|
|
|
[gitea.exclude_paths]
|
|
patterns = ["vendor/"]
|
|
|
|
[gitea.exclude_repos]
|
|
names = ["ignore-me"]
|
|
|
|
[log]
|
|
level = "debug"
|
|
`
|
|
if err := os.WriteFile(path, []byte(content), 0600); err != nil {
|
|
t.Fatalf("write config: %v", err)
|
|
}
|
|
|
|
cfg, err := Load(path)
|
|
if err != nil {
|
|
t.Fatalf("load: %v", err)
|
|
}
|
|
|
|
if cfg.Server.ListenAddr != ":9090" {
|
|
t.Fatalf("listen_addr = %q, want :9090", cfg.Server.ListenAddr)
|
|
}
|
|
if cfg.Gitea.URL != "https://git.example.com" {
|
|
t.Fatalf("gitea.url = %q", cfg.Gitea.URL)
|
|
}
|
|
if cfg.Gitea.Org != "myorg" {
|
|
t.Fatalf("gitea.org = %q", cfg.Gitea.Org)
|
|
}
|
|
if cfg.Gitea.PollInterval.Duration != 5*time.Minute {
|
|
t.Fatalf("poll_interval = %v", cfg.Gitea.PollInterval.Duration)
|
|
}
|
|
if cfg.Gitea.FetchTimeout.Duration != 10*time.Second {
|
|
t.Fatalf("fetch_timeout = %v", cfg.Gitea.FetchTimeout.Duration)
|
|
}
|
|
if cfg.Gitea.MaxConcurrency != 2 {
|
|
t.Fatalf("max_concurrency = %d", cfg.Gitea.MaxConcurrency)
|
|
}
|
|
if len(cfg.Gitea.ExcludePaths.Patterns) != 1 {
|
|
t.Fatalf("exclude_paths = %v", cfg.Gitea.ExcludePaths.Patterns)
|
|
}
|
|
if len(cfg.Gitea.ExcludeRepos.Names) != 1 {
|
|
t.Fatalf("exclude_repos = %v", cfg.Gitea.ExcludeRepos.Names)
|
|
}
|
|
if cfg.Log.Level != "debug" {
|
|
t.Fatalf("log.level = %q", cfg.Log.Level)
|
|
}
|
|
}
|
|
|
|
func TestLoadDefaults(t *testing.T) {
|
|
dir := t.TempDir()
|
|
path := filepath.Join(dir, "mcdoc.toml")
|
|
|
|
// Minimal config — everything should get defaults
|
|
if err := os.WriteFile(path, []byte(""), 0600); err != nil {
|
|
t.Fatalf("write config: %v", err)
|
|
}
|
|
|
|
cfg, err := Load(path)
|
|
if err != nil {
|
|
t.Fatalf("load: %v", err)
|
|
}
|
|
|
|
if cfg.Server.ListenAddr != ":8080" {
|
|
t.Fatalf("default listen_addr = %q, want :8080", cfg.Server.ListenAddr)
|
|
}
|
|
if cfg.Gitea.URL != "https://git.wntrmute.dev" {
|
|
t.Fatalf("default gitea.url = %q", cfg.Gitea.URL)
|
|
}
|
|
if cfg.Gitea.PollInterval.Duration != 15*time.Minute {
|
|
t.Fatalf("default poll_interval = %v", cfg.Gitea.PollInterval.Duration)
|
|
}
|
|
}
|
|
|
|
func TestLoadMissingFile(t *testing.T) {
|
|
_, err := Load("/nonexistent/mcdoc.toml")
|
|
if err == nil {
|
|
t.Fatal("expected error for missing file")
|
|
}
|
|
}
|
|
|
|
func TestPortEnvOverride(t *testing.T) {
|
|
dir := t.TempDir()
|
|
path := filepath.Join(dir, "mcdoc.toml")
|
|
if err := os.WriteFile(path, []byte(""), 0600); err != nil {
|
|
t.Fatalf("write config: %v", err)
|
|
}
|
|
|
|
t.Setenv("PORT", "12345")
|
|
cfg, err := Load(path)
|
|
if err != nil {
|
|
t.Fatalf("load: %v", err)
|
|
}
|
|
if cfg.Server.ListenAddr != ":12345" {
|
|
t.Fatalf("PORT override: got %q, want :12345", cfg.Server.ListenAddr)
|
|
}
|
|
}
|
|
|
|
func TestEnvOverride(t *testing.T) {
|
|
dir := t.TempDir()
|
|
path := filepath.Join(dir, "mcdoc.toml")
|
|
if err := os.WriteFile(path, []byte(""), 0600); err != nil {
|
|
t.Fatalf("write config: %v", err)
|
|
}
|
|
|
|
t.Setenv("MCDOC_GITEA_ORG", "custom-org")
|
|
cfg, err := Load(path)
|
|
if err != nil {
|
|
t.Fatalf("load: %v", err)
|
|
}
|
|
if cfg.Gitea.Org != "custom-org" {
|
|
t.Fatalf("env override: got %q, want custom-org", cfg.Gitea.Org)
|
|
}
|
|
}
|
|
|
|
func TestValidationFailsOnEmptyURL(t *testing.T) {
|
|
dir := t.TempDir()
|
|
path := filepath.Join(dir, "mcdoc.toml")
|
|
content := `
|
|
[gitea]
|
|
url = ""
|
|
`
|
|
if err := os.WriteFile(path, []byte(content), 0600); err != nil {
|
|
t.Fatalf("write config: %v", err)
|
|
}
|
|
|
|
_, err := Load(path)
|
|
if err == nil {
|
|
t.Fatal("expected validation error for empty gitea.url")
|
|
}
|
|
}
|