diff --git a/go/pkg/help/catalog_test.go b/go/pkg/help/catalog_test.go index 2fa51d2..667c81c 100644 --- a/go/pkg/help/catalog_test.go +++ b/go/pkg/help/catalog_test.go @@ -2,9 +2,6 @@ package help import ( . "dappco.re/go" - "fmt" - "os" - "path/filepath" ) func TestDefaultCatalog_Good(t *T) { @@ -177,9 +174,9 @@ func TestCatalog_Search_Good_ScoreTiebreaking(t *T) { func TestCatalog_LoadContentDir_Good(t *T) { dir := t.TempDir() - os.MkdirAll(filepath.Join(dir, "cli"), 0o755) - os.WriteFile(filepath.Join(dir, "cli", "dev-work.md"), []byte("---\ntitle: Dev Work\ntags: [cli, dev]\n---\n\n## Usage\n\ncore dev work syncs your workspace.\n"), 0o644) - os.WriteFile(filepath.Join(dir, "cli", "setup.md"), []byte("---\ntitle: Setup\ntags: [cli]\n---\n\n## Installation\n\nRun core setup to get started.\n"), 0o644) + MkdirAll(PathJoin(dir, "cli"), 0o755) + WriteFile(PathJoin(dir, "cli", "dev-work.md"), []byte("---\ntitle: Dev Work\ntags: [cli, dev]\n---\n\n## Usage\n\ncore dev work syncs your workspace.\n"), 0o644) + WriteFile(PathJoin(dir, "cli", "setup.md"), []byte("---\ntitle: Setup\ntags: [cli]\n---\n\n## Installation\n\nRun core setup to get started.\n"), 0o644) catalog, err := LoadContentDir(dir) RequireNoError(t, err) @@ -203,8 +200,8 @@ func TestCatalog_LoadContentDir_Good_Empty(t *T) { func TestCatalog_LoadContentDir_Good_SkipsNonMd(t *T) { dir := t.TempDir() - os.WriteFile(filepath.Join(dir, "readme.txt"), []byte("not markdown"), 0o644) - os.WriteFile(filepath.Join(dir, "topic.md"), []byte("---\ntitle: Topic\n---\n\nContent here.\n"), 0o644) + WriteFile(PathJoin(dir, "readme.txt"), []byte("not markdown"), 0o644) + WriteFile(PathJoin(dir, "topic.md"), []byte("---\ntitle: Topic\n---\n\nContent here.\n"), 0o644) catalog, err := LoadContentDir(dir) RequireNoError(t, err) @@ -220,18 +217,18 @@ func BenchmarkSearch(b *B) { for i := range 150 { c.Add(&Topic{ - ID: fmt.Sprintf("topic-%d", i), - Title: fmt.Sprintf("Topic Number %d About Various Subjects", i), - Content: fmt.Sprintf("This is the content of topic %d. It covers installation, configuration, deployment, and testing of the system.", i), - Tags: []string{"generated", fmt.Sprintf("tag%d", i%10)}, + ID: Sprintf("topic-%d", i), + Title: Sprintf("Topic Number %d About Various Subjects", i), + Content: Sprintf("This is the content of topic %d. It covers installation, configuration, deployment, and testing of the system.", i), + Tags: []string{"generated", Sprintf("tag%d", i%10)}, Sections: []Section{ { - ID: fmt.Sprintf("section-%d-a", i), + ID: Sprintf("section-%d-a", i), Title: "Overview", Content: "An overview of the topic and its purpose.", }, { - ID: fmt.Sprintf("section-%d-b", i), + ID: Sprintf("section-%d-b", i), Title: "Details", Content: "Detailed information about the topic including examples and usage.", }, diff --git a/go/pkg/help/generate_test.go b/go/pkg/help/generate_test.go index b6ce1bc..30eaaa5 100644 --- a/go/pkg/help/generate_test.go +++ b/go/pkg/help/generate_test.go @@ -3,11 +3,27 @@ package help import ( . "dappco.re/go" - "encoding/json" - "os" - "path/filepath" ) +// statExists is a small test helper that reports OK iff the path exists. +func statExists(path string) error { + r := Stat(path) + if !r.OK { + return r.Value.(error) + } + return nil +} + +// readFileBytes returns the bytes at path or fails the test. +func readFileBytes(t *T, path string) []byte { + t.Helper() + r := ReadFile(path) + if !r.OK { + t.Fatal(r.Error()) + } + return r.Value.([]byte) +} + // testCatalog builds a small catalog for generator tests. func testCatalog() *Catalog { c := &Catalog{ @@ -51,9 +67,8 @@ func TestGenerate_Good_FileStructure(t *T) { } for _, f := range expectedFiles { - path := filepath.Join(dir, f) - _, err := os.Stat(path) - AssertNoError(t, err, "expected generated file to exist: "+f) + path := PathJoin(dir, f) + AssertNoError(t, statExists(path), "expected generated file to exist: "+f) } } @@ -64,10 +79,7 @@ func TestGenerate_Good_IndexContainsTopics(t *T) { err := Generate(catalog, dir) RequireNoError(t, err) - content, err := os.ReadFile(filepath.Join(dir, "index.html")) - RequireNoError(t, err) - - html := string(content) + html := string(readFileBytes(t, PathJoin(dir, "index.html"))) AssertContains(t, html, "Getting Started") AssertContains(t, html, "Configuration") } @@ -79,10 +91,7 @@ func TestGenerate_Good_TopicContainsRenderedMarkdown(t *T) { err := Generate(catalog, dir) RequireNoError(t, err) - content, err := os.ReadFile(filepath.Join(dir, "topics", "getting-started.html")) - RequireNoError(t, err) - - html := string(content) + html := string(readFileBytes(t, PathJoin(dir, "topics", "getting-started.html"))) AssertContains(t, html, "Getting Started") AssertContains(t, html, "guide") } @@ -94,11 +103,12 @@ func TestGenerate_Good_SearchIndexJSON(t *T) { err := Generate(catalog, dir) RequireNoError(t, err) - content, err := os.ReadFile(filepath.Join(dir, "search-index.json")) - RequireNoError(t, err) + content := readFileBytes(t, PathJoin(dir, "search-index.json")) var entries []searchIndexEntry - RequireNoError(t, json.Unmarshal(content, &entries)) + if r := JSONUnmarshal(content, &entries); !r.OK { + t.Fatal(r.Error()) + } AssertLen(t, entries, 2, "search index should contain all topics") // Verify fields are populated @@ -119,10 +129,7 @@ func TestGenerate_Good_404Exists(t *T) { err := Generate(catalog, dir) RequireNoError(t, err) - content, err := os.ReadFile(filepath.Join(dir, "404.html")) - RequireNoError(t, err) - - html := string(content) + html := string(readFileBytes(t, PathJoin(dir, "404.html"))) AssertContains(t, html, "404") AssertContains(t, html, "Not Found") } @@ -149,9 +156,7 @@ func TestGenerate_Good_OverwriteExisting(t *T) { AssertNoError(t, err) // Verify files still exist and are valid - content, err := os.ReadFile(filepath.Join(dir, "index.html")) - RequireNoError(t, err) - AssertContains(t, string(content), "Getting Started") + AssertContains(t, string(readFileBytes(t, PathJoin(dir, "index.html"))), "Getting Started") } func TestGenerate_Good_SearchPageHasScript(t *T) { @@ -161,10 +166,7 @@ func TestGenerate_Good_SearchPageHasScript(t *T) { err := Generate(catalog, dir) RequireNoError(t, err) - content, err := os.ReadFile(filepath.Join(dir, "search.html")) - RequireNoError(t, err) - - html := string(content) + html := string(readFileBytes(t, PathJoin(dir, "search.html"))) AssertContains(t, html, "