|
| 1 | +import fs from 'fs'; |
| 2 | +import os from 'os'; |
| 3 | +import path from 'path'; |
| 4 | +import { createRequire } from 'module'; |
| 5 | +import { |
| 6 | + afterEach, |
| 7 | + describe, |
| 8 | + expect, |
| 9 | + it, |
| 10 | +} from 'vitest'; |
| 11 | + |
| 12 | +const require = createRequire(import.meta.url); |
| 13 | +const { generateMd, generateLibraryDocs, getLibraries } = require('./libraryDocGenerator.cjs'); |
| 14 | + |
| 15 | +describe('libraryDocGenerator', () => { |
| 16 | + const tempDirs: string[] = []; |
| 17 | + |
| 18 | + afterEach(() => { |
| 19 | + tempDirs.forEach((dir) => { |
| 20 | + if (fs.existsSync(dir)) { |
| 21 | + fs.rmSync(dir, { recursive: true, force: true }); |
| 22 | + } |
| 23 | + }); |
| 24 | + tempDirs.length = 0; |
| 25 | + }); |
| 26 | + |
| 27 | + it('generateMd includes components, sequences, and reference sections', () => { |
| 28 | + const md = generateMd('demo-lib', { |
| 29 | + description: 'Demo description', |
| 30 | + reference: 'Some reference', |
| 31 | + doi: '10.1000/xyz', |
| 32 | + externalLink: 'https://example.com', |
| 33 | + components: { beta: {}, alpha: {} }, |
| 34 | + sequences: { second: {}, first: {} }, |
| 35 | + additionalDescription: 'Extra details', |
| 36 | + }, true); |
| 37 | + |
| 38 | + expect(md).toContain('# demo-lib'); |
| 39 | + expect(md).toContain('## Available Components'); |
| 40 | + expect(md).toContain('- alpha'); |
| 41 | + expect(md).toContain('- beta'); |
| 42 | + expect(md).toContain('## Available Sequences'); |
| 43 | + expect(md).toContain('- first'); |
| 44 | + expect(md).toContain('- second'); |
| 45 | + expect(md).toContain('## Reference'); |
| 46 | + expect(md).toContain('https://dx.doi.org/10.1000/xyz'); |
| 47 | + expect(md).toContain('## Additional Description'); |
| 48 | + }); |
| 49 | + |
| 50 | + it('generateMd handles example reference text and external-link-only docs links', () => { |
| 51 | + const exampleMd = generateMd('demo-lib', { |
| 52 | + description: 'Demo description', |
| 53 | + reference: 'Some reference', |
| 54 | + components: {}, |
| 55 | + sequences: {}, |
| 56 | + }, false); |
| 57 | + |
| 58 | + expect(exampleMd).toContain('This is an example study of the library `demo-lib`.'); |
| 59 | + expect(exampleMd).toContain('Some reference'); |
| 60 | + expect(exampleMd).not.toContain(':::note[Reference]'); |
| 61 | + |
| 62 | + const docsMd = generateMd('demo-lib', { |
| 63 | + description: 'Demo description', |
| 64 | + externalLink: 'https://example.com', |
| 65 | + components: {}, |
| 66 | + sequences: {}, |
| 67 | + }, true); |
| 68 | + |
| 69 | + expect(docsMd).toContain('referenceLinks={['); |
| 70 | + expect(docsMd).toContain('{name: "demo-lib", url: "https://example.com"}'); |
| 71 | + expect(docsMd).not.toContain('{name: "DOI"'); |
| 72 | + |
| 73 | + const docsWithDoiOnly = generateMd('demo-lib', { |
| 74 | + description: 'Demo description', |
| 75 | + doi: '10.1000/xyz', |
| 76 | + components: {}, |
| 77 | + sequences: {}, |
| 78 | + }, true); |
| 79 | + |
| 80 | + expect(docsWithDoiOnly).toContain('{name: "DOI", url: "https://dx.doi.org/10.1000/xyz"}'); |
| 81 | + expect(docsWithDoiOnly).not.toContain('{name: "demo-lib", url:'); |
| 82 | + }); |
| 83 | + |
| 84 | + it('getLibraries filters hidden entries and .DS_Store entries', () => { |
| 85 | + const base = fs.mkdtempSync(path.join(os.tmpdir(), 'lib-doc-list-')); |
| 86 | + tempDirs.push(base); |
| 87 | + const libsPath = path.join(base, 'public', 'libraries'); |
| 88 | + fs.mkdirSync(libsPath, { recursive: true }); |
| 89 | + fs.mkdirSync(path.join(libsPath, 'alpha')); |
| 90 | + fs.mkdirSync(path.join(libsPath, '.hidden')); |
| 91 | + fs.writeFileSync(path.join(libsPath, '.DS_Store'), ''); |
| 92 | + |
| 93 | + expect(getLibraries(libsPath)).toEqual(['alpha']); |
| 94 | + }); |
| 95 | + |
| 96 | + it('generateLibraryDocs writes docs and example markdown when assets folder exists', () => { |
| 97 | + const base = fs.mkdtempSync(path.join(os.tmpdir(), 'lib-doc-run-')); |
| 98 | + tempDirs.push(base); |
| 99 | + const libraryName = 'alpha'; |
| 100 | + const librariesPath = path.join(base, 'public', 'libraries', libraryName); |
| 101 | + const exampleAssetsPath = path.join(base, 'public', `library-${libraryName}`, 'assets'); |
| 102 | + |
| 103 | + fs.mkdirSync(librariesPath, { recursive: true }); |
| 104 | + fs.mkdirSync(exampleAssetsPath, { recursive: true }); |
| 105 | + fs.writeFileSync( |
| 106 | + path.join(librariesPath, 'config.json'), |
| 107 | + JSON.stringify({ |
| 108 | + description: 'Alpha description', |
| 109 | + components: { compA: {} }, |
| 110 | + sequences: {}, |
| 111 | + }), |
| 112 | + ); |
| 113 | + |
| 114 | + generateLibraryDocs(base); |
| 115 | + |
| 116 | + const docsOut = path.join(base, 'docsLibraries', `${libraryName}.md`); |
| 117 | + const exampleOut = path.join(exampleAssetsPath, `${libraryName}.md`); |
| 118 | + |
| 119 | + expect(fs.existsSync(docsOut)).toBe(true); |
| 120 | + expect(fs.existsSync(exampleOut)).toBe(true); |
| 121 | + expect(fs.readFileSync(docsOut, 'utf8')).toContain('# alpha'); |
| 122 | + expect(fs.readFileSync(exampleOut, 'utf8')).toContain('This is an example study'); |
| 123 | + }); |
| 124 | +}); |
0 commit comments