Unhardcode

This commit is contained in:
2026-05-25 10:23:42 +02:00
parent e6fffdf041
commit 7714c068fe
9 changed files with 286 additions and 23 deletions
+86 -3
View File
@@ -7,6 +7,8 @@ import (
"strings"
"testing"
"time"
"gitea.westgate.pw/ShadowsOverWestgate/sow-tools/internal/project"
)
func TestBuildNativeGeneratesAndSkipsWikiPages(t *testing.T) {
@@ -98,7 +100,7 @@ func TestBuildNativeGeneratesAndSkipsWikiPages(t *testing.T) {
if err != nil {
t.Fatalf("read state before second build: %v", err)
}
digestBefore, err := computeWikiSourceDigest(filepath.Join(root, "topdata"))
digestBefore, err := computeWikiSourceDigest(proj)
if err != nil {
t.Fatalf("compute digest before second build: %v", err)
}
@@ -1998,13 +2000,20 @@ func TestWikiSourceDigestIncludesTemplates(t *testing.T) {
writeFile(t, filepath.Join(root, "data", "skills", "base.json"), `{"rows":[]}`+"\n")
templatePath := filepath.Join(root, "wiki", "templates", "pages", "skills.html")
writeFile(t, templatePath, `<h1>{{title}}</h1>`+"\n")
proj := &project.Project{
Root: root,
Config: project.Config{
Module: project.ModuleConfig{Name: "Test", ResRef: "test"},
TopData: project.TopDataConfig{Source: ".", Wiki: project.TopDataWikiConfig{Source: "wiki"}},
},
}
before, err := computeWikiSourceDigest(root)
before, err := computeWikiSourceDigest(proj)
if err != nil {
t.Fatalf("compute initial digest: %v", err)
}
writeFile(t, templatePath, `<h1>{{title}}</h1><p>Changed</p>`+"\n")
after, err := computeWikiSourceDigest(root)
after, err := computeWikiSourceDigest(proj)
if err != nil {
t.Fatalf("compute changed digest: %v", err)
}
@@ -2013,6 +2022,80 @@ func TestWikiSourceDigestIncludesTemplates(t *testing.T) {
}
}
func TestBuildWikiHonorsConfiguredWikiPaths(t *testing.T) {
root := t.TempDir()
mkdirAll(t, filepath.Join(root, "topdata", "data", "skills"))
mkdirAll(t, filepath.Join(root, "docs", "wiki", "page-fragments"))
mkdirAll(t, filepath.Join(root, "docs", "wiki", "config"))
mkdirAll(t, filepath.Join(root, "docs", "wiki", "sections"))
writeFile(t, filepath.Join(root, "topdata", "base_dialog.json"), "{}\n")
writeFile(t, filepath.Join(root, "topdata", "data", "skills", "base.json"), `{
"columns": ["Label", "Name"],
"rows": [
{
"id": 0,
"key": "skills:athletics",
"Label": "Athletics",
"Name": {"tlk": {"key": "skills:athletics.name", "text": "Athletics"}}
}
]
}`+"\n")
writeFile(t, filepath.Join(root, "topdata", "data", "skills", "lock.json"), `{"skills:athletics":0}`+"\n")
writeFile(t, filepath.Join(root, "docs", "wiki", "page-fragments", "skills.html"), `<h1>{{title}}</h1>`+"\n")
writeFile(t, filepath.Join(root, "docs", "wiki", "config", "providers.yaml"), "providers: {}\n")
writeFile(t, filepath.Join(root, "docs", "wiki", "config", "paths.yaml"), "page_paths: {}\n")
writeFile(t, filepath.Join(root, "docs", "wiki", "config", "tables.yaml"), "tables: {}\n")
writeFile(t, filepath.Join(root, "docs", "wiki", "config", "visibility.yaml"), "datasets: {}\n")
writeFile(t, filepath.Join(root, "docs", "wiki", "sections", "defaults.yaml"), `
manual_sections:
- id: user_top
heading: Custom Top
`)
statusPages := false
proj := &project.Project{
Root: root,
Config: project.Config{
Module: project.ModuleConfig{Name: "Test", ResRef: "test"},
Paths: project.PathConfig{Build: "build", Cache: ".cache"},
TopData: project.TopDataConfig{
Source: "topdata",
Build: ".cache",
Wiki: project.TopDataWikiConfig{
Source: "docs/wiki",
DataFile: "config/providers.yaml",
PagePathsFile: "config/paths.yaml",
TablesFile: "config/tables.yaml",
VisibilityFile: "config/visibility.yaml",
PageTemplatesDir: "page-fragments",
ManualSectionsFile: "sections/defaults.yaml",
PageIndexFile: "manifest/pages.json",
StatusPages: &statusPages,
},
},
},
}
result, err := buildWiki(proj, BuildResult{}, true, nil)
if err != nil {
t.Fatalf("build wiki with configured paths: %v", err)
}
if result.PageCount != 1 {
t.Fatalf("expected generated skill page, got %d", result.PageCount)
}
pagePath := filepath.Join(root, ".cache", "wiki", "pages", "skills", "Athletics.html")
pageRaw, err := os.ReadFile(pagePath)
if err != nil {
t.Fatalf("read configured-path wiki page: %v", err)
}
if !strings.Contains(string(pageRaw), "Custom Top") {
t.Fatalf("expected configured manual section file to be used, got:\n%s", pageRaw)
}
if _, err := os.Stat(filepath.Join(root, ".cache", "wiki", "manifest", "pages.json")); err != nil {
t.Fatalf("expected configured page-index file: %v", err)
}
}
func TestWikiPageIDForKeyNormalizesSlashSeparatorsOnly(t *testing.T) {
got := wikiPageIDForKey("feat:special/attacks_bull_rush")
if want := "feat:special_attacks_bull_rush"; got != want {