Skip to content

[Issue]: Add Pester tests for Validate-MarkdownFrontmatter.ps1 (detailed) #198

@WilliamBerryiii

Description

@WilliamBerryiii

Summary

Create dedicated unit tests for Validate-MarkdownFrontmatter.ps1 which has complex schema validation logic requiring thorough testing.

Parent Issue: #190

Requirements

  • Create scripts/tests/linting/Validate-MarkdownFrontmatter.Tests.ps1
  • Test YAML frontmatter parsing and extraction
  • Test schema validation against multiple schema types
  • Test schema-mapping.json pattern matching
  • Test error reporting and edge cases

Implementation Details

Why Separate Issue

Validate-MarkdownFrontmatter.ps1 deserves dedicated attention because:

  1. Complex YAML parsing logic
  2. Multiple schema types (instruction, prompt, docs, etc.)
  3. Pattern-matching for schema selection
  4. Critical for repository documentation standards

Test Scenarios

Describe 'Validate-MarkdownFrontmatter.ps1' {
    Context 'YAML Extraction' {
        It 'Should extract frontmatter from valid markdown' {
            $content = @"
---
title: Test
description: Test description
---
# Content
"@
            $result = Get-Frontmatter -Content $content
            $result.title | Should -Be 'Test'
        }

        It 'Should handle missing frontmatter' {
            $content = '# No frontmatter'
            $result = Get-Frontmatter -Content $content
            $result | Should -BeNullOrEmpty
        }

        It 'Should handle malformed YAML' {
            $content = @"
---
title: Test
  bad-indent: value
---
"@
            { Get-Frontmatter -Content $content } | Should -Throw
        }
    }

    Context 'Schema Selection' {
        BeforeAll {
            # Load schema mapping
            $schemaMapping = Get-Content 'scripts/linting/schemas/schema-mapping.json' | 
                ConvertFrom-Json
        }

        It 'Should select instruction schema for .instructions.md' {
            $schema = Get-SchemaForFile -Path 'test.instructions.md'
            $schema | Should -Match 'instruction-frontmatter'
        }

        It 'Should select prompt schema for .prompt.md' {
            $schema = Get-SchemaForFile -Path 'test.prompt.md'
            $schema | Should -Match 'prompt-frontmatter'
        }

        It 'Should fall back to base schema for unknown types' {
            $schema = Get-SchemaForFile -Path 'random.md'
            $schema | Should -Match 'base-frontmatter'
        }
    }

    Context 'Schema Validation' {
        It 'Should pass valid instruction frontmatter' {
            $frontmatter = @{
                description = 'Test description'
                applyTo = '**/*.ps1'
            }
            $result = Test-FrontmatterSchema -Data $frontmatter -SchemaType 'instruction'
            $result.IsValid | Should -BeTrue
        }

        It 'Should fail instruction frontmatter missing required fields' {
            $frontmatter = @{
                title = 'Missing description and applyTo'
            }
            $result = Test-FrontmatterSchema -Data $frontmatter -SchemaType 'instruction'
            $result.IsValid | Should -BeFalse
            $result.Errors | Should -Contain '*description*'
        }
    }
}

Mock Data

$TestCases = @(
    @{
        Name = 'Valid docs frontmatter'
        Content = @"
---
title: Getting Started
description: How to get started with the project
ms.date: 2025-01-15
---
"@
        SchemaType = 'docs'
        ExpectedValid = $true
    }
    @{
        Name = 'Invalid date format'
        Content = @"
---
title: Test
description: Test
ms.date: 01/15/2025
---
"@
        SchemaType = 'docs'
        ExpectedValid = $false
    }
)

Acceptance Criteria

  • Comprehensive frontmatter extraction tests
  • Schema selection tests for all file patterns
  • Validation tests for each schema type
  • Error message formatting tests
  • Edge case handling (empty files, binary content, huge files)
  • Integration test with real schema files

Dependencies

Estimated Effort

2-3 hours


Additional Context

Schema Files

Located in scripts/linting/schemas/:

  • base-frontmatter.schema.json
  • instruction-frontmatter.schema.json
  • prompt-frontmatter.schema.json
  • docs-frontmatter.schema.json
  • chatmode-frontmatter.schema.json
  • agent-frontmatter.schema.json
  • root-community-frontmatter.schema.json
  • schema-mapping.json

Related Issues

Metadata

Metadata

Labels

enhancementNew feature or requestlintingLinting rules and validationscriptsPowerShell, Bash, or Python scripts

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions