import { readFile } from 'node:fs/promises' import path from 'node:path' import { describe, expect, it } from 'vitest' import { formatPackageCountLine, README_COUNT_PATTERN, } from '../packageCounts.js' import type { PackageCounts } from '../packageCounts.js' const ROOT = path.resolve(import.meta.dirname, '../..') /** The index of the package count line in `README.md` */ const README_LINE_INDEX = 8 const countCases: [PackageCounts, string][] = [ [ { plugins: 1, themes: 1, tools: 1 }, 'This repo contains 1 official plugin, 1 official theme and 1 tool package.', ], [ { plugins: 2, themes: 1, tools: 1 }, 'This repo contains 2 official plugins, 1 official theme and 1 tool package.', ], [ { plugins: 1, themes: 2, tools: 1 }, 'This repo contains 1 official plugin, 2 official themes and 1 tool package.', ], [ { plugins: 1, themes: 1, tools: 2 }, 'This repo contains 1 official plugin, 1 official theme and 2 tool packages.', ], [ { plugins: 0, themes: 0, tools: 0 }, 'This repo contains 0 official plugins, 0 official themes and 0 tool packages.', ], [ { plugins: 120, themes: 12, tools: 80 }, 'This repo contains 120 official plugins, 12 official themes and 80 tool packages.', ], ] const readReadmeLines = async (): Promise => (await readFile(path.join(ROOT, 'README.md'), 'utf8')).split(/\r?\n/u) describe('packageCounts', () => { describe('package count line pattern', () => { it('matches the package count line of README.md', async () => { const lines = await readReadmeLines() expect(lines[README_LINE_INDEX]).toMatch(README_COUNT_PATTERN) }) it('matches that line only', async () => { const lines = await readReadmeLines() const matched = lines.filter((line) => README_COUNT_PATTERN.test(line)) expect(matched).toStrictEqual([lines[README_LINE_INDEX]]) }) it('rejects a line without the tool packages', () => { expect( 'This repo contains 62 official plugins and 2 official themes.', ).not.toMatch(README_COUNT_PATTERN) }) }) describe('formatPackageCountLine()', () => { it('keeps the README line in sync with the current counts', async () => { const lines = await readReadmeLines() expect(formatPackageCountLine({ plugins: 62, themes: 2, tools: 8 })).toBe( lines[README_LINE_INDEX], ) }) it.each(countCases)('formats %o', (counts, expected) => { expect(formatPackageCountLine(counts)).toBe(expected) }) it('generates lines that README_COUNT_PATTERN matches', () => { for (const [counts] of countCases) expect(formatPackageCountLine(counts)).toMatch(README_COUNT_PATTERN) }) }) })