import fs from 'node:fs/promises' import path from 'node:path' import { afterAll, beforeAll, describe, expect, it } from 'vitest' import { expandString } from '../src/lib/api' import { resetMetadataCaches } from '../src/lib/context' import { describeVersionRange } from '../src/lib/readme/rules/utilities/version-range' // Expectations for the mdat project itself follow its package.json, so version // bumps don't break these tests const packageJson = JSON.parse( await fs.readFile(path.resolve(__dirname, '../package.json'), 'utf8'), ) as { engines: { node: string } } function useFixture(name: string) { const fixtureDirectory = path.resolve(__dirname, 'fixtures', name) let originalCwd: string beforeAll(() => { originalCwd = process.cwd() process.chdir(fixtureDirectory) resetMetadataCaches() }) afterAll(() => { process.chdir(originalCwd) resetMetadataCaches() }) } describe('install rule', () => { it('should offer CLI and library options for a package with both, and detect the Homebrew tap from the release script', async () => { const result = await expandString('') expect(result.toString()).toMatchInlineSnapshot(` " ### Installation There are several ways to install mdat depending on how you're planning to use it: #### CLI Run it once without installing: \`\`\`sh npx mdat \`\`\` Or install it globally with Homebrew: \`\`\`sh brew install kitschpatrol/tap/mdat \`\`\` Or install it globally with npm: \`\`\`sh npm install --global mdat \`\`\` #### Library Add it to your project to import the TypeScript API. This also puts the \`mdat\` CLI on your project's path: \`\`\`sh npm install mdat \`\`\` " `) }) it('should nest headings at the requested level', async () => { const result = await expandString('') const text = result.toString() expect(text).toContain('\n## Installation\n') expect(text).toContain('\n### CLI\n') expect(text).toContain('\n### Library\n') }) it('should install as a development dependency when asked', async () => { const result = await expandString('') const text = result.toString() expect(text).toContain('Add it to your project as a development dependency to import') expect(text).toContain('npm install --save-dev mdat') expect(text).not.toContain('npm install mdat') }) it('should allow overriding or suppressing the Homebrew formula', async () => { const overridden = await expandString("") expect(overridden.toString()).toContain('brew install example/tap/mdat') const suppressed = await expandString('') expect(suppressed.toString()).not.toContain('Homebrew') }) }) describe('install rule alias', () => { it('should produce the same content via the installation alias', async () => { const install = await expandString('') const installation = await expandString('') expect(installation.toString()).toContain('\n### Installation\n') expect(installation.toString()).toBe( install.toString().replaceAll('install -->', 'installation -->'), ) }) }) describe('install rule for a CLI-only package', () => { useFixture('cli-only') it('should skip the library section and normalize the homebrew- tap prefix', async () => { const result = await expandString('') expect(result.toString()).toMatchInlineSnapshot(` " ### Installation Run it once without installing: \`\`\`sh npx @example/cli-only-fixture \`\`\` Or install it globally with Homebrew: \`\`\`sh brew install example/tools/cli-only-fixture \`\`\` Or install it globally with npm: \`\`\`sh npm install --global @example/cli-only-fixture \`\`\` " `) }) }) describe('install rule for a library-only package', () => { useFixture('library-only') it('should emit a single install command', async () => { const result = await expandString('') expect(result.toString()).toMatchInlineSnapshot(` " ### Installation Add it to your project: \`\`\`sh npm install library-only-fixture \`\`\` " `) }) it('should emit a development dependency install command when asked', async () => { const result = await expandString('') expect(result.toString()).toMatchInlineSnapshot(` " ### Installation Add it to your project as a development dependency: \`\`\`sh npm install --save-dev library-only-fixture \`\`\` " `) }) }) describe('install rule for a private package', () => { useFixture('private-package') it('should refuse to generate install instructions', async () => { const result = await expandString('') expect(result.messages.map((message) => message.reason).join('\n')).toContain( 'package.json sets "private": true', ) expect(result.toString()).not.toContain('npm install') }) }) describe('dependencies rule', () => { it('should list the Node.js requirement and leave out optional peer dependencies', async () => { const result = await expandString('') const text = result.toString() expect(text).toContain('\n### Dependencies\n') expect(text).toContain( `- [Node.js](https://nodejs.org/) ${describeVersionRange(packageJson.engines.node)}`, ) expect(text).not.toContain('prettier') expect(text).toContain('') }) it('should nest the heading at the requested level', async () => { const result = await expandString('') expect(result.toString()).toContain('\n## Dependencies\n') }) }) describe('dependencies rule for a library-only package', () => { useFixture('library-only') it('should spell out compound engine ranges, operating systems, and required peer dependencies', async () => { const result = await expandString('') expect(result.toString()).toMatchInlineSnapshot(` " ### Dependencies - [Node.js](https://nodejs.org/) 24.16.0 or newer (specifically \`^24.16.0 || >=26.3.0\`) - Supported operating systems: macOS, Linux - [vite](https://www.npmjs.com/package/vite) \`^7.0.0\` _(peer dependency)_ " `) }) }) describe('describeVersionRange', () => { it.each([ ['>=24.16.0', '24.16.0 or newer'], ['>= 20', '20 or newer'], ['24.16.0', '24.16.0'], ['^24.16.0 || >=26.3.0', '24.16.0 or newer (specifically `^24.16.0 || >=26.3.0`)'], ['>=20.19.0 <21 || >=22.12.0', '20.19.0 or newer (specifically `>=20.19.0 <21 || >=22.12.0`)'], ['^3.0.0', '3.0.0 or newer (specifically `^3.0.0`)'], ['*', '`*`'], ])('should describe %s as %s', (range, expected) => { expect(describeVersionRange(range)).toBe(expected) }) })