|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { buildSlugToDocsYmlFilePath } from "../types"; |
| 3 | + |
| 4 | +describe("buildSlugToDocsYmlFilePath", () => { |
| 5 | + it("returns empty map when docsYmlContent is null", () => { |
| 6 | + const result = buildSlugToDocsYmlFilePath(null); |
| 7 | + expect(result).toBeInstanceOf(Map); |
| 8 | + expect(result.size).toBe(0); |
| 9 | + }); |
| 10 | + |
| 11 | + it("returns empty map when docs.yml is not in the content", () => { |
| 12 | + const content = new Map([["other.yml", "some content"]]); |
| 13 | + const result = buildSlugToDocsYmlFilePath(content); |
| 14 | + expect(result).toBeInstanceOf(Map); |
| 15 | + expect(result.size).toBe(0); |
| 16 | + }); |
| 17 | + |
| 18 | + it("parses top-level products with slugs", () => { |
| 19 | + const docsYml = ` |
| 20 | +products: |
| 21 | + - display-name: API Reference |
| 22 | + slug: api |
| 23 | + path: ./api.yml |
| 24 | + - display-name: Guide |
| 25 | + slug: guide |
| 26 | + path: ./guide.yml |
| 27 | +`; |
| 28 | + const content = new Map([["docs.yml", docsYml]]); |
| 29 | + const result = buildSlugToDocsYmlFilePath(content); |
| 30 | + |
| 31 | + expect(result.size).toBe(2); |
| 32 | + expect(result.get("api")).toBe("api.yml"); |
| 33 | + expect(result.get("guide")).toBe("guide.yml"); |
| 34 | + }); |
| 35 | + |
| 36 | + it("parses top-level versions", () => { |
| 37 | + const docsYml = ` |
| 38 | +versions: |
| 39 | + - display-name: v2 |
| 40 | + slug: v2 |
| 41 | + path: ./versions/v2.yml |
| 42 | + - display-name: v1 |
| 43 | + slug: v1 |
| 44 | + path: ./versions/v1.yml |
| 45 | +`; |
| 46 | + const content = new Map([["docs.yml", docsYml]]); |
| 47 | + const result = buildSlugToDocsYmlFilePath(content); |
| 48 | + |
| 49 | + expect(result.size).toBe(2); |
| 50 | + expect(result.get("v2")).toBe("versions/v2.yml"); |
| 51 | + expect(result.get("v1")).toBe("versions/v1.yml"); |
| 52 | + }); |
| 53 | + |
| 54 | + it("parses products with nested versions", () => { |
| 55 | + const docsYml = ` |
| 56 | +products: |
| 57 | + - display-name: Platform |
| 58 | + slug: platform |
| 59 | + path: docs/products/platform/v2.yml |
| 60 | + versions: |
| 61 | + - display-name: v2 |
| 62 | + slug: platform-v2 |
| 63 | + path: docs/products/platform/v2.yml |
| 64 | + availability: stable |
| 65 | + - display-name: v1 |
| 66 | + slug: platform-v1 |
| 67 | + path: docs/products/platform/v1.yml |
| 68 | + - display-name: Wiki |
| 69 | + slug: wiki |
| 70 | + path: docs/products/wiki.yml |
| 71 | +`; |
| 72 | + const content = new Map([["docs.yml", docsYml]]); |
| 73 | + const result = buildSlugToDocsYmlFilePath(content); |
| 74 | + |
| 75 | + expect(result.size).toBe(4); |
| 76 | + // Product slugs |
| 77 | + expect(result.get("platform")).toBe("docs/products/platform/v2.yml"); |
| 78 | + expect(result.get("wiki")).toBe("docs/products/wiki.yml"); |
| 79 | + // Nested version slugs |
| 80 | + expect(result.get("platform-v2")).toBe("docs/products/platform/v2.yml"); |
| 81 | + expect(result.get("platform-v1")).toBe("docs/products/platform/v1.yml"); |
| 82 | + }); |
| 83 | + |
| 84 | + it("parses products with nested versions without explicit product slug", () => { |
| 85 | + const docsYml = ` |
| 86 | +products: |
| 87 | + - display-name: Platform |
| 88 | + path: docs/products/platform/v2.yml |
| 89 | + versions: |
| 90 | + - display-name: v2 |
| 91 | + slug: v2 |
| 92 | + path: docs/products/platform/v2.yml |
| 93 | + - display-name: v1 |
| 94 | + slug: v1 |
| 95 | + path: docs/products/platform/v1.yml |
| 96 | +`; |
| 97 | + const content = new Map([["docs.yml", docsYml]]); |
| 98 | + const result = buildSlugToDocsYmlFilePath(content); |
| 99 | + |
| 100 | + expect(result.size).toBe(2); |
| 101 | + // Product slug derived from path "v2" collides with explicit version slug "v2" |
| 102 | + // The version slug wins (last one to be processed) |
| 103 | + expect(result.get("v2")).toBe("docs/products/platform/v2.yml"); |
| 104 | + // Explicit version slugs |
| 105 | + expect(result.get("v1")).toBe("docs/products/platform/v1.yml"); |
| 106 | + }); |
| 107 | + |
| 108 | + it("derives slugs from paths when not explicitly provided", () => { |
| 109 | + const docsYml = ` |
| 110 | +products: |
| 111 | + - display-name: API |
| 112 | + path: docs/api.yml |
| 113 | + - display-name: Platform |
| 114 | + path: docs/products/platform.yml |
| 115 | +versions: |
| 116 | + - display-name: Version 2 |
| 117 | + path: ./versions/v2.yml |
| 118 | +tabs: |
| 119 | + - display-name: Home |
| 120 | + path: ./home.yml |
| 121 | +`; |
| 122 | + const content = new Map([["docs.yml", docsYml]]); |
| 123 | + const result = buildSlugToDocsYmlFilePath(content); |
| 124 | + |
| 125 | + expect(result.size).toBe(4); |
| 126 | + // All slugs derived from filenames |
| 127 | + expect(result.get("api")).toBe("docs/api.yml"); |
| 128 | + expect(result.get("platform")).toBe("docs/products/platform.yml"); |
| 129 | + expect(result.get("v2")).toBe("versions/v2.yml"); |
| 130 | + expect(result.get("home")).toBe("home.yml"); |
| 131 | + }); |
| 132 | + |
| 133 | + it("parses tabs with file references", () => { |
| 134 | + const docsYml = ` |
| 135 | +tabs: |
| 136 | + - display-name: Home |
| 137 | + slug: home |
| 138 | + path: ./tabs/home.yml |
| 139 | + - display-name: Guides |
| 140 | + slug: guides |
| 141 | + path: ./tabs/guides.yml |
| 142 | +`; |
| 143 | + const content = new Map([["docs.yml", docsYml]]); |
| 144 | + const result = buildSlugToDocsYmlFilePath(content); |
| 145 | + |
| 146 | + expect(result.size).toBe(2); |
| 147 | + expect(result.get("home")).toBe("tabs/home.yml"); |
| 148 | + expect(result.get("guides")).toBe("tabs/guides.yml"); |
| 149 | + }); |
| 150 | + |
| 151 | + it("normalizes paths by removing ./ prefix", () => { |
| 152 | + const docsYml = ` |
| 153 | +products: |
| 154 | + - slug: api |
| 155 | + path: ./api.yml |
| 156 | +versions: |
| 157 | + - slug: v2 |
| 158 | + path: ./versions/v2.yml |
| 159 | +tabs: |
| 160 | + - slug: home |
| 161 | + path: ./tabs/home.yml |
| 162 | +`; |
| 163 | + const content = new Map([["docs.yml", docsYml]]); |
| 164 | + const result = buildSlugToDocsYmlFilePath(content); |
| 165 | + |
| 166 | + expect(result.get("api")).toBe("api.yml"); |
| 167 | + expect(result.get("v2")).toBe("versions/v2.yml"); |
| 168 | + expect(result.get("home")).toBe("tabs/home.yml"); |
| 169 | + }); |
| 170 | + |
| 171 | + it("handles mixed configuration with products, versions, and tabs", () => { |
| 172 | + const docsYml = ` |
| 173 | +products: |
| 174 | + - display-name: Platform |
| 175 | + slug: platform |
| 176 | + path: docs/products/platform/v2.yml |
| 177 | + versions: |
| 178 | + - slug: platform-v2 |
| 179 | + path: docs/products/platform/v2.yml |
| 180 | + - slug: platform-v1 |
| 181 | + path: docs/products/platform/v1.yml |
| 182 | +versions: |
| 183 | + - slug: v3 |
| 184 | + path: versions/v3.yml |
| 185 | +tabs: |
| 186 | + - slug: home |
| 187 | + path: tabs/home.yml |
| 188 | +`; |
| 189 | + const content = new Map([["docs.yml", docsYml]]); |
| 190 | + const result = buildSlugToDocsYmlFilePath(content); |
| 191 | + |
| 192 | + expect(result.size).toBe(5); |
| 193 | + expect(result.get("platform")).toBe("docs/products/platform/v2.yml"); |
| 194 | + expect(result.get("platform-v2")).toBe("docs/products/platform/v2.yml"); |
| 195 | + expect(result.get("platform-v1")).toBe("docs/products/platform/v1.yml"); |
| 196 | + expect(result.get("v3")).toBe("versions/v3.yml"); |
| 197 | + expect(result.get("home")).toBe("tabs/home.yml"); |
| 198 | + }); |
| 199 | + |
| 200 | + it("ignores products without slug or path", () => { |
| 201 | + const docsYml = ` |
| 202 | +products: |
| 203 | + - display-name: Platform |
| 204 | + # Missing slug and path |
| 205 | + - slug: valid |
| 206 | + path: ./valid.yml |
| 207 | +`; |
| 208 | + const content = new Map([["docs.yml", docsYml]]); |
| 209 | + const result = buildSlugToDocsYmlFilePath(content); |
| 210 | + |
| 211 | + expect(result.size).toBe(1); |
| 212 | + expect(result.get("valid")).toBe("valid.yml"); |
| 213 | + }); |
| 214 | + |
| 215 | + it("ignores versions without slug or path", () => { |
| 216 | + const docsYml = ` |
| 217 | +versions: |
| 218 | + - display-name: v2 |
| 219 | + # Missing slug and path |
| 220 | + - slug: v1 |
| 221 | + path: ./v1.yml |
| 222 | +`; |
| 223 | + const content = new Map([["docs.yml", docsYml]]); |
| 224 | + const result = buildSlugToDocsYmlFilePath(content); |
| 225 | + |
| 226 | + expect(result.size).toBe(1); |
| 227 | + expect(result.get("v1")).toBe("v1.yml"); |
| 228 | + }); |
| 229 | + |
| 230 | + it("handles invalid yaml gracefully", () => { |
| 231 | + const docsYml = ` |
| 232 | +invalid yaml: [ |
| 233 | + this is not valid |
| 234 | +`; |
| 235 | + const content = new Map([["docs.yml", docsYml]]); |
| 236 | + const result = buildSlugToDocsYmlFilePath(content); |
| 237 | + |
| 238 | + // Should return empty map without throwing |
| 239 | + expect(result).toBeInstanceOf(Map); |
| 240 | + expect(result.size).toBe(0); |
| 241 | + }); |
| 242 | + |
| 243 | + it("parses real-world example with products containing versions (Plant Store)", () => { |
| 244 | + // This is the actual configuration from the user's bug report that was failing |
| 245 | + const docsYml = ` |
| 246 | +instances: |
| 247 | + - url: acmeco.docs.buildwithfern.com |
| 248 | +title: Plant Store |
| 249 | +layout: |
| 250 | + searchbar-placement: header |
| 251 | + page-width: full |
| 252 | + tabs-placement: header |
| 253 | +products: |
| 254 | + - display-name: Platform |
| 255 | + path: docs/products/platform/v2.yml |
| 256 | + versions: |
| 257 | + - display-name: v2 |
| 258 | + path: docs/products/platform/v2.yml |
| 259 | + availability: stable |
| 260 | + - display-name: v1 |
| 261 | + path: docs/products/platform/v1.yml |
| 262 | + - display-name: Wiki |
| 263 | + path: docs/products/wiki.yml |
| 264 | +colors: |
| 265 | + accentPrimary: |
| 266 | + dark: '#81C784' |
| 267 | + light: '#1B5E20' |
| 268 | +logo: |
| 269 | + dark: docs/assets/logo-dark.svg |
| 270 | + light: docs/assets/logo-light.svg |
| 271 | + height: 20 |
| 272 | + href: https://buildwithfern.com/?utm_campaign=demo&utm_medium=plantstore&utm_source=logo |
| 273 | +favicon: docs/assets/favicon.svg |
| 274 | +`; |
| 275 | + const content = new Map([["docs.yml", docsYml]]); |
| 276 | + const result = buildSlugToDocsYmlFilePath(content); |
| 277 | + |
| 278 | + // Should not throw and return a valid Map with derived slugs |
| 279 | + expect(result).toBeInstanceOf(Map); |
| 280 | + |
| 281 | + // Products and versions don't have explicit slugs, so slugs are derived from paths |
| 282 | + // Derived slugs: "v2" (from platform/v2.yml), "v1" (from platform/v1.yml), "wiki" (from wiki.yml) |
| 283 | + expect(result.size).toBe(3); |
| 284 | + expect(result.get("v2")).toBe("docs/products/platform/v2.yml"); |
| 285 | + expect(result.get("v1")).toBe("docs/products/platform/v1.yml"); |
| 286 | + expect(result.get("wiki")).toBe("docs/products/wiki.yml"); |
| 287 | + }); |
| 288 | +}); |
0 commit comments