|
| 1 | +/** |
| 2 | + * Unit tests for mergeGuidesIntoSpec — sections option — and prefixFromPath. |
| 3 | + * |
| 4 | + * Covers flat (backward-compat) mode and sectioned mode where guides |
| 5 | + * are grouped under H1 dividers (each guide rendered as H2) using |
| 6 | + * filename numeric prefix ranges. Also covers prefixFromPath edge cases |
| 7 | + * (exported helper, must handle all separator / no-prefix variants). |
| 8 | + */ |
| 9 | +import GuidesHelper from '../guides.js'; |
| 10 | + |
| 11 | +const { mergeGuidesIntoSpec, prefixFromPath } = GuidesHelper; |
| 12 | + |
| 13 | +describe('mergeGuidesIntoSpec — sections option:', () => { |
| 14 | + const makeGuide = (filePath, body = 'guide body') => ({ |
| 15 | + title: filePath.replace(/.*\//, '').replace(/\.md$/, '').replace(/^\d+[-_]/, '').replace(/[-_]/g, ' '), |
| 16 | + body, |
| 17 | + path: filePath, |
| 18 | + }); |
| 19 | + |
| 20 | + const loginGuide = makeGuide('/docs/01-login.md', 'Login content'); |
| 21 | + const signupGuide = makeGuide('/docs/02-signup.md', 'Signup content'); |
| 22 | + const subscribeGuide = makeGuide('/docs/10-subscribe.md', 'Subscribe content'); |
| 23 | + |
| 24 | + const baseSpec = () => ({ info: { description: 'overview' } }); |
| 25 | + |
| 26 | + test('without sections option: guides flatten into info.description as H1 entries', () => { |
| 27 | + const out = mergeGuidesIntoSpec(baseSpec(), [loginGuide, signupGuide]); |
| 28 | + expect(out.info.description).toContain('login'); |
| 29 | + expect(out.info.description).toContain('signup'); |
| 30 | + // Flat mode → H1 headings, no section grouping |
| 31 | + expect(out.info.description).not.toMatch(/^# auth$/m); |
| 32 | + expect(out.info.description).not.toMatch(/^# billing$/m); |
| 33 | + // Flat mode → each guide is a top-level H1 |
| 34 | + expect(out.info.description).toMatch(/^# /m); |
| 35 | + }); |
| 36 | + |
| 37 | + test('without sections option: existing description is preserved', () => { |
| 38 | + const out = mergeGuidesIntoSpec(baseSpec(), [loginGuide]); |
| 39 | + expect(out.info.description).toContain('overview'); |
| 40 | + }); |
| 41 | + |
| 42 | + test('with sections array: guides nest under H1 section dividers as H2', () => { |
| 43 | + const sections = [ |
| 44 | + { title: 'auth', prefixMin: 1, prefixMax: 9 }, |
| 45 | + { title: 'billing', prefixMin: 10, prefixMax: 19 }, |
| 46 | + ]; |
| 47 | + const out = mergeGuidesIntoSpec(baseSpec(), [loginGuide, signupGuide, subscribeGuide], { sections }); |
| 48 | + // H1 section headers present |
| 49 | + expect(out.info.description).toMatch(/^# auth$/m); |
| 50 | + expect(out.info.description).toMatch(/^# billing$/m); |
| 51 | + // Guides appear as H2 under their section |
| 52 | + expect(out.info.description).toMatch(/^## /m); |
| 53 | + // login and signup under auth (prefixes 1,2 → prefixMin:1 prefixMax:9) |
| 54 | + expect(out.info.description).toContain('Login content'); |
| 55 | + expect(out.info.description).toContain('Signup content'); |
| 56 | + // subscribe under billing (prefix 10 → prefixMin:10 prefixMax:19) |
| 57 | + expect(out.info.description).toContain('Subscribe content'); |
| 58 | + }); |
| 59 | + |
| 60 | + test('with sections: auth H1 appears before billing H1', () => { |
| 61 | + const sections = [ |
| 62 | + { title: 'auth', prefixMin: 1, prefixMax: 9 }, |
| 63 | + { title: 'billing', prefixMin: 10, prefixMax: 19 }, |
| 64 | + ]; |
| 65 | + const out = mergeGuidesIntoSpec(baseSpec(), [loginGuide, signupGuide, subscribeGuide], { sections }); |
| 66 | + const authIdx = out.info.description.indexOf('# auth'); |
| 67 | + const billingIdx = out.info.description.indexOf('# billing'); |
| 68 | + expect(authIdx).toBeLessThan(billingIdx); |
| 69 | + }); |
| 70 | + |
| 71 | + test('with sections: guides without matching prefix range become orphan H2 entries (never silently dropped)', () => { |
| 72 | + const sections = [{ title: 'auth', prefixMin: 1, prefixMax: 9 }]; |
| 73 | + // subscribeGuide has prefix 10, outside the only section range |
| 74 | + const out = mergeGuidesIntoSpec(baseSpec(), [loginGuide, subscribeGuide], { sections }); |
| 75 | + // The orphan is still present (not dropped) |
| 76 | + expect(out.info.description).toContain('Subscribe content'); |
| 77 | + }); |
| 78 | + |
| 79 | + test('with sections: sections with no matched guides are omitted from output', () => { |
| 80 | + const sections = [ |
| 81 | + { title: 'auth', prefixMin: 1, prefixMax: 9 }, |
| 82 | + { title: 'billing', prefixMin: 10, prefixMax: 19 }, |
| 83 | + ]; |
| 84 | + // Only auth-range guides |
| 85 | + const out = mergeGuidesIntoSpec(baseSpec(), [loginGuide, signupGuide], { sections }); |
| 86 | + expect(out.info.description).toMatch(/^# auth$/m); |
| 87 | + // billing section has no guides → should NOT appear |
| 88 | + expect(out.info.description).not.toMatch(/^# billing$/m); |
| 89 | + }); |
| 90 | + |
| 91 | + test('returns spec unchanged when guides array is empty', () => { |
| 92 | + const spec = baseSpec(); |
| 93 | + const out = mergeGuidesIntoSpec(spec, []); |
| 94 | + expect(out.info.description).toBe('overview'); |
| 95 | + }); |
| 96 | + |
| 97 | + test('returns spec unchanged when spec is falsy', () => { |
| 98 | + expect(mergeGuidesIntoSpec(null, [loginGuide])).toBeNull(); |
| 99 | + expect(mergeGuidesIntoSpec(undefined, [loginGuide])).toBeUndefined(); |
| 100 | + }); |
| 101 | + |
| 102 | + test('null options does not throw — falls back to flat mode', () => { |
| 103 | + // Passing null explicitly as third arg must not throw (null-safe options guard) |
| 104 | + const out = mergeGuidesIntoSpec(baseSpec(), [loginGuide], null); |
| 105 | + expect(out.info.description).toContain('Login content'); |
| 106 | + expect(out.info.description).toMatch(/^# /m); |
| 107 | + }); |
| 108 | + |
| 109 | + test('with sections: orphan appears after section markdown (appended at end, not before)', () => { |
| 110 | + const sections = [{ title: 'auth', prefixMin: 1, prefixMax: 9 }]; |
| 111 | + // subscribeGuide (prefix 10) is an orphan — should appear after the auth section |
| 112 | + const out = mergeGuidesIntoSpec(baseSpec(), [loginGuide, subscribeGuide], { sections }); |
| 113 | + const authIdx = out.info.description.indexOf('# auth'); |
| 114 | + const orphanIdx = out.info.description.indexOf('Subscribe content'); |
| 115 | + expect(authIdx).toBeGreaterThan(-1); |
| 116 | + expect(orphanIdx).toBeGreaterThan(authIdx); |
| 117 | + }); |
| 118 | +}); |
| 119 | + |
| 120 | +describe('prefixFromPath:', () => { |
| 121 | + test('extracts numeric prefix with dash separator', () => { |
| 122 | + expect(prefixFromPath('/foo/07-scheduling.md')).toBe(7); |
| 123 | + expect(prefixFromPath('/foo/14-cli.md')).toBe(14); |
| 124 | + expect(prefixFromPath('/foo/01-getting-started.md')).toBe(1); |
| 125 | + }); |
| 126 | + |
| 127 | + test('extracts numeric prefix with underscore separator', () => { |
| 128 | + expect(prefixFromPath('/foo/03_intro.md')).toBe(3); |
| 129 | + expect(prefixFromPath('/foo/20_advanced.md')).toBe(20); |
| 130 | + }); |
| 131 | + |
| 132 | + test('returns null when no numeric prefix present', () => { |
| 133 | + expect(prefixFromPath('/foo/welcome.md')).toBeNull(); |
| 134 | + expect(prefixFromPath('/foo/getting-started.md')).toBeNull(); |
| 135 | + }); |
| 136 | + |
| 137 | + test('returns null for pure-digit basename (no separator after digits)', () => { |
| 138 | + // "42.md" has no dash or underscore after digits — not a prefix pattern |
| 139 | + expect(prefixFromPath('/foo/42.md')).toBeNull(); |
| 140 | + }); |
| 141 | + |
| 142 | + test('handles octal-looking prefix correctly (parseInt radix 10)', () => { |
| 143 | + // "08-" would be invalid octal — must parse as decimal 8 |
| 144 | + expect(prefixFromPath('/foo/08-webhooks.md')).toBe(8); |
| 145 | + expect(prefixFromPath('/foo/09-billing.md')).toBe(9); |
| 146 | + }); |
| 147 | + |
| 148 | + test('handles empty-ish inputs without throwing', () => { |
| 149 | + expect(prefixFromPath('')).toBeNull(); |
| 150 | + expect(prefixFromPath('/foo/.md')).toBeNull(); |
| 151 | + }); |
| 152 | +}); |
0 commit comments