101 lines
3.8 KiB
PHP
101 lines
3.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
use Tests\Support\TestLaneBudget;
|
|
use Tests\Support\TestLaneManifest;
|
|
|
|
it('evaluates lane budgets into within-budget, warning, and over-budget states', function (): void {
|
|
$warnBudget = TestLaneBudget::fromArray([
|
|
'thresholdSeconds' => 30,
|
|
'baselineSource' => 'measured-current-suite',
|
|
'enforcement' => 'warn',
|
|
'lifecycleState' => 'documented',
|
|
]);
|
|
|
|
$hardFailBudget = TestLaneBudget::fromArray([
|
|
'thresholdSeconds' => 30,
|
|
'baselineSource' => 'measured-current-suite',
|
|
'enforcement' => 'hard-fail',
|
|
'lifecycleState' => 'documented',
|
|
]);
|
|
|
|
expect($warnBudget->evaluate(24.5)['budgetStatus'])->toBe('within-budget')
|
|
->and($warnBudget->evaluate(31.2)['budgetStatus'])->toBe('warning')
|
|
->and($hardFailBudget->evaluate(31.2)['budgetStatus'])->toBe('over-budget');
|
|
});
|
|
|
|
it('evaluates family targets through the generic budget target path', function (): void {
|
|
$familyTargets = array_values(array_filter(
|
|
TestLaneManifest::budgetTargets(),
|
|
static fn (array $target): bool => $target['targetType'] === 'family',
|
|
));
|
|
|
|
$evaluations = TestLaneBudget::evaluateBudgetTargets(
|
|
$familyTargets,
|
|
0.0,
|
|
[],
|
|
[
|
|
'ops-ux-governance' => 18.4,
|
|
'action-surface-contract' => 7.8,
|
|
'browser-smoke' => 14.2,
|
|
],
|
|
);
|
|
|
|
expect($evaluations)->not->toBeEmpty()
|
|
->and($evaluations[0])->toHaveKeys([
|
|
'budgetId',
|
|
'targetType',
|
|
'targetId',
|
|
'thresholdSeconds',
|
|
'baselineSource',
|
|
'enforcement',
|
|
'lifecycleState',
|
|
'measuredSeconds',
|
|
'budgetStatus',
|
|
]);
|
|
});
|
|
|
|
it('fails closed when an active blocking profile is missing a required input', function (): void {
|
|
$profile = TestLaneBudget::enforcementProfile('fast-feedback', 'pull-request');
|
|
unset($profile['effectiveThresholdSeconds']);
|
|
|
|
expect(fn (): array => TestLaneBudget::evaluateTriggerAwareBudgetProfile($profile, 216.0))
|
|
->toThrow(InvalidArgumentException::class);
|
|
});
|
|
|
|
it('fails closed when an active blocking profile contains malformed inputs', function (): void {
|
|
$profile = TestLaneBudget::enforcementProfile('fast-feedback', 'pull-request');
|
|
$baseThresholdSeconds = (int) $profile['baseThresholdSeconds'];
|
|
$varianceAllowanceSeconds = (int) $profile['varianceAllowanceSeconds'];
|
|
$malformedProfiles = [
|
|
array_replace($profile, ['laneId' => '']),
|
|
array_replace($profile, ['baseThresholdSeconds' => 'not-numeric']),
|
|
array_replace($profile, ['varianceAllowanceSeconds' => -1]),
|
|
array_replace($profile, [
|
|
'effectiveThresholdSeconds' => $baseThresholdSeconds + $varianceAllowanceSeconds + 1,
|
|
]),
|
|
array_replace($profile, ['enforcementMode' => 'soft-warn']),
|
|
array_replace($profile, [
|
|
'baseThresholdSeconds' => 999,
|
|
'effectiveThresholdSeconds' => 999 + $varianceAllowanceSeconds,
|
|
]),
|
|
array_replace($profile, ['lifecycleState' => 'tampered']),
|
|
array_replace($profile, ['triggerClass' => 'manual']),
|
|
];
|
|
|
|
foreach ($malformedProfiles as $malformedProfile) {
|
|
expect(fn (): array => TestLaneBudget::evaluateTriggerAwareBudgetProfile($malformedProfile, 216.0))
|
|
->toThrow(InvalidArgumentException::class);
|
|
}
|
|
});
|
|
|
|
it('fails closed when the measured time is non-finite or negative', function (): void {
|
|
$profile = TestLaneBudget::enforcementProfile('fast-feedback', 'pull-request');
|
|
|
|
foreach ([NAN, INF, -0.001] as $measuredSeconds) {
|
|
expect(fn (): array => TestLaneBudget::evaluateTriggerAwareBudgetProfile($profile, $measuredSeconds))
|
|
->toThrow(InvalidArgumentException::class);
|
|
}
|
|
});
|