| 81 | } |
| 82 | |
| 83 | func TestNewDocCommand_GeneratesDocs(t *testing.T) { |
| 84 | t.Parallel() |
| 85 | |
| 86 | outputDir := filepath.Join(t.TempDir(), "cli-docs") |
| 87 | |
| 88 | root := newRootCommand(commandDeps{}) |
| 89 | root.SetArgs([]string{"doc", "--output-dir", outputDir}) |
| 90 | |
| 91 | if err := root.Execute(); err != nil { |
| 92 | t.Fatalf("doc command failed: %v", err) |
| 93 | } |
| 94 | |
| 95 | // Verify output directory was created and contains mdx files. |
| 96 | mdxFiles := findMDX(t, outputDir) |
| 97 | if len(mdxFiles) == 0 { |
| 98 | t.Fatal("doc command should generate .mdx files") |
| 99 | } |
| 100 | |
| 101 | // Verify agh.mdx exists at the root (from agh.md). Root index.mdx is |
| 102 | // hand-authored by the site and preserved by the post-processor. |
| 103 | rootMDX := filepath.Join(outputDir, "agh.mdx") |
| 104 | data, err := os.ReadFile(rootMDX) |
| 105 | if err != nil { |
| 106 | t.Fatalf("agh.mdx should exist at root: %v", err) |
| 107 | } |
| 108 | |
| 109 | content := string(data) |
| 110 | if !strings.Contains(content, "---") { |
| 111 | t.Error("agh.mdx should have YAML frontmatter") |
| 112 | } |
| 113 | if !strings.Contains(content, `title: "agh"`) { |
| 114 | t.Error("agh.mdx frontmatter should have title 'agh'") |
| 115 | } |
| 116 | |
| 117 | // Per-subdirectory meta.json is auto-generated; walk and ensure at least |
| 118 | // one such file exists (root meta.json is hand-maintained and NOT written). |
| 119 | var subdirMetas int |
| 120 | err = filepath.WalkDir(outputDir, func(p string, d fs.DirEntry, walkErr error) error { |
| 121 | if walkErr != nil { |
| 122 | return walkErr |
| 123 | } |
| 124 | if d.IsDir() { |
| 125 | return nil |
| 126 | } |
| 127 | if d.Name() != "meta.json" { |
| 128 | return nil |
| 129 | } |
| 130 | rel, relErr := filepath.Rel(outputDir, p) |
| 131 | if relErr != nil { |
| 132 | return relErr |
| 133 | } |
| 134 | if filepath.Dir(rel) == "." { |
| 135 | t.Errorf("doc command must not write root meta.json (hand-maintained)") |
| 136 | return nil |
| 137 | } |
| 138 | subdirMetas++ |
| 139 | return nil |
| 140 | }) |