#Requires -Modules Pester
# Copyright (c) Microsoft Corporation.
# SPDX-License-Identifier: MIT
<#
.SYNOPSIS
    Asserts disclaimer Note + reviewer checkbox patterns are present in both backlog
    instruction files, and the `### Exit Point Reminder` heading with all four named
    exit points exists in both planner identity files.
#>

BeforeDiscovery {
    $script:repoRoot = (Resolve-Path (Join-Path $PSScriptRoot '../../..')).Path

    $script:noteLiteral = '> **Note:** This work item was generated by the Security Planner AI agent. Review for accuracy and adjust before assigning to a person.'
    $script:checkboxLiteral = '- [ ] Reviewed by a human security professional before execution'

    $script:backlogFiles = @(
        (Join-Path $script:repoRoot '.github/instructions/security/backlog-handoff.instructions.md'),
        (Join-Path $script:repoRoot '.github/instructions/security/sssc-backlog.instructions.md')
    )
    $script:identityFiles = @(
        (Join-Path $script:repoRoot '.github/instructions/security/identity.instructions.md'),
        (Join-Path $script:repoRoot '.github/instructions/security/sssc-identity.instructions.md')
    )

    $script:exitPoints = @(
        'Phase 6 completion',
        'Compact handoff',
        'Error exit',
        'User-initiated exit'
    )
}

Describe 'Backlog instruction files include disclaimer Note and reviewer checkbox in every WI template variant' {
    It 'Backlog file <_> contains the Note literal at least twice' -ForEach $script:backlogFiles {
        Test-Path $_ | Should -BeTrue
        $content = Get-Content -Path $_ -Raw
        $count = ([regex]::Matches($content, [regex]::Escape($script:noteLiteral))).Count
        $count | Should -BeGreaterOrEqual 2 -Because "Note literal must appear in both ADO and GitHub WI variants in $_"
    }

    It 'Backlog file <_> contains the reviewer checkbox literal at least twice' -ForEach $script:backlogFiles {
        $content = Get-Content -Path $_ -Raw
        $count = ([regex]::Matches($content, [regex]::Escape($script:checkboxLiteral))).Count
        $count | Should -BeGreaterOrEqual 2 -Because "reviewer checkbox must appear in both ADO and GitHub WI variants in $_"
    }
}

Describe 'Identity files include Exit Point Reminder section with four named exit points' {
    It 'Identity file <_> contains ### Exit Point Reminder heading' -ForEach $script:identityFiles {
        Test-Path $_ | Should -BeTrue
        $content = Get-Content -Path $_ -Raw
        $content | Should -Match '(?m)^###\s+Exit Point Reminder\s*$'
    }

    It 'Identity file <_> mentions all four exit-point labels' -ForEach $script:identityFiles {
        $content = Get-Content -Path $_ -Raw
        foreach ($label in $script:exitPoints) {
            $content | Should -BeLike "*$label*" -Because "exit point '$label' must be listed in $_"
        }
    }
}
