tests/Benchmark.Tests.ps1

$benchmarkRoot = Join-Path (Split-Path -Parent $PSScriptRoot) 'benchmark'
. (Join-Path $benchmarkRoot 'BenchmarkHelpers.ps1')

Describe 'Measure-OctaMetricSamples' {
    It 'accepts a metric when primary and cross-check agree within tolerance' {
        $result = Measure-OctaMetricSamples -Name 'Agreeing' -Samples 3 -TolerancePercent 10 `
            -Primary { 100 } -CrossCheck { 102 }
        $result.CrossCheckAgreedWithinTolerance | Should Be $true
    }

    It 'withholds a metric when primary and cross-check disagree beyond tolerance (FR-020)' {
        $result = Measure-OctaMetricSamples -Name 'Disagreeing' -Samples 3 -TolerancePercent 10 `
            -Primary { 100 } -CrossCheck { 500 }
        $result.CrossCheckAgreedWithinTolerance | Should Be $false
    }

    It 'withholds a metric when the cross-check source throws (e.g. disabled perf counters)' {
        $result = Measure-OctaMetricSamples -Name 'CrossCheckFails' -Samples 3 -TolerancePercent 10 `
            -Primary { 100 } -CrossCheck { throw 'simulated Get-Counter failure' }
        $result.CrossCheckAgreedWithinTolerance | Should Be $false
    }

    It 'reports the sample count and a non-negative standard deviation' {
        $result = Measure-OctaMetricSamples -Name 'Variance' -Samples 4 -TolerancePercent 50 `
            -Primary { Get-Random -Minimum 10 -Maximum 20 } -CrossCheck { 15 }
        $result.SampleCount | Should Be 4
        ($result.StdDev -ge 0) | Should Be $true
    }
}