|
| 1 | +import { describe, test, expect, vi } from 'vitest' |
| 2 | +import { shouldIncludeRule } from '../../scripts/lint-content.js' |
| 3 | + |
| 4 | +// Mock the get-rules module to provide test data for rule definitions |
| 5 | +vi.mock('../../lib/helpers/get-rules', () => ({ |
| 6 | + allRules: [ |
| 7 | + { |
| 8 | + names: ['MD001', 'heading-increment'], |
| 9 | + description: 'Heading levels should only increment by one level at a time', |
| 10 | + }, |
| 11 | + { |
| 12 | + names: ['MD002', 'first-heading-h1'], |
| 13 | + description: 'First heading should be a top level heading', |
| 14 | + }, |
| 15 | + ], |
| 16 | + customRules: [ |
| 17 | + { |
| 18 | + names: ['GHD053', 'header-content-requirement'], |
| 19 | + description: 'Headers must have content below them', |
| 20 | + }, |
| 21 | + { |
| 22 | + names: ['GHD030', 'code-fence-line-length'], |
| 23 | + description: 'Code fence content should not exceed line length limit', |
| 24 | + }, |
| 25 | + ], |
| 26 | + allConfig: {}, |
| 27 | +})) |
| 28 | + |
| 29 | +describe('shouldIncludeRule', () => { |
| 30 | + test('includes rule by long name', () => { |
| 31 | + expect(shouldIncludeRule('heading-increment', ['heading-increment'])).toBe(true) |
| 32 | + expect(shouldIncludeRule('header-content-requirement', ['header-content-requirement'])).toBe( |
| 33 | + true, |
| 34 | + ) |
| 35 | + }) |
| 36 | + |
| 37 | + test('includes built-in rule by short code', () => { |
| 38 | + expect(shouldIncludeRule('heading-increment', ['MD001'])).toBe(true) |
| 39 | + expect(shouldIncludeRule('first-heading-h1', ['MD002'])).toBe(true) |
| 40 | + }) |
| 41 | + |
| 42 | + test('includes custom rule by short code', () => { |
| 43 | + expect(shouldIncludeRule('header-content-requirement', ['GHD053'])).toBe(true) |
| 44 | + expect(shouldIncludeRule('code-fence-line-length', ['GHD030'])).toBe(true) |
| 45 | + }) |
| 46 | + |
| 47 | + test('excludes rule not in list', () => { |
| 48 | + expect(shouldIncludeRule('heading-increment', ['MD002'])).toBe(false) |
| 49 | + expect(shouldIncludeRule('header-content-requirement', ['GHD030'])).toBe(false) |
| 50 | + }) |
| 51 | + |
| 52 | + test('handles multiple rules', () => { |
| 53 | + const runRules = ['MD001', 'GHD053', 'some-other-rule'] |
| 54 | + expect(shouldIncludeRule('heading-increment', runRules)).toBe(true) |
| 55 | + expect(shouldIncludeRule('header-content-requirement', runRules)).toBe(true) |
| 56 | + expect(shouldIncludeRule('first-heading-h1', runRules)).toBe(false) |
| 57 | + }) |
| 58 | + |
| 59 | + test('handles unknown rules gracefully', () => { |
| 60 | + expect(shouldIncludeRule('non-existent-rule', ['MD001'])).toBe(false) |
| 61 | + }) |
| 62 | +}) |
0 commit comments