TenantAtlas/apps/platform/tests/Feature/Guards/TestLaneCommandContractTest.php
Ahmed Darrazi 8144a3be78
Some checks failed
PR Fast Feedback / fast-feedback (pull_request) Failing after 7m2s
feat: implement spec 439 branch and evidence truth
2026-07-11 21:13:42 +02:00

225 lines
14 KiB
PHP

<?php
declare(strict_types=1);
use Tests\Support\TestLaneManifest;
it('keeps composer lane commands wired to the checked-in lane inventory', function (): void {
$composer = json_decode((string) file_get_contents(base_path('composer.json')), true, 512, JSON_THROW_ON_ERROR);
$scripts = $composer['scripts'] ?? [];
expect($scripts)->toHaveKeys([
'test',
'test:fast',
'test:confidence',
'test:browser',
'test:heavy',
'test:profile',
'test:junit',
'test:report',
'test:report:confidence',
'test:report:browser',
'test:report:heavy',
'test:report:profile',
'test:report:junit',
'sail:test',
])
->and(TestLaneManifest::commandRef('fast-feedback'))->toBe('test')
->and(TestLaneManifest::commandRef('confidence'))->toBe('test:confidence')
->and(TestLaneManifest::commandRef('browser'))->toBe('test:browser')
->and(TestLaneManifest::commandRef('heavy-governance'))->toBe('test:heavy')
->and(TestLaneManifest::commandRef('profiling'))->toBe('test:profile')
->and(TestLaneManifest::commandRef('junit'))->toBe('test:junit');
});
it('keeps the host-side lane runner and report scripts checked in at repo root', function (): void {
expect(file_exists(repo_path('scripts/platform-test-lane')))->toBeTrue()
->and(file_exists(repo_path('scripts/platform-test-report')))->toBeTrue()
->and(file_exists(repo_path('scripts/platform-test-artifacts')))->toBeTrue();
});
it('passes artifact staging inputs through php argv for sail execution', function (): void {
$artifactRunner = (string) file_get_contents(repo_path('scripts/platform-test-artifacts'));
expect($artifactRunner)
->toContain('./vendor/bin/sail php -- "${LANE}" "${STAGING_DIRECTORY}" "${ARTIFACT_DIRECTORY}"')
->and($artifactRunner)->toContain('$laneId = (string) ($argv[1] ?? \'\');')
->and($artifactRunner)->toContain('$stagingDirectory = (string) ($argv[2] ?? \'\');')
->and($artifactRunner)->toContain('$artifactDirectory = (string) ($argv[3] ?? \'\');')
->and($artifactRunner)->not->toContain("getenv('LANE_ID')")
->and($artifactRunner)->not->toContain("getenv('STAGING_DIRECTORY')")
->and($artifactRunner)->not->toContain("getenv('ARTIFACT_DIRECTORY')");
});
it('keeps heavy-governance baseline capture support inside the checked-in wrappers', function (): void {
$laneRunner = (string) file_get_contents(repo_path('scripts/platform-test-lane'));
$reportRunner = (string) file_get_contents(repo_path('scripts/platform-test-report'));
expect($laneRunner)->toContain('--capture-baseline', 'copy_heavy_baseline_artifacts', 'heavy-governance-baseline.${suffix}')
->and($reportRunner)->toContain('--capture-baseline', 'copy_heavy_baseline_artifacts', 'heavy-governance-baseline.${suffix}')
->and($reportRunner)->toContain('junit)', 'test:report:junit')
->and($reportRunner)->toContain('--history-file=')
->and($reportRunner)->toContain('--history-bundle=')
->and($reportRunner)->toContain('--fetch-latest-history')
->and($reportRunner)->toContain('TENANTATLAS_GITEA_TOKEN')
->and($reportRunner)->toContain('trend-history.json');
});
it('avoids expanding an empty forwarded-argument array in the lane runner', function (): void {
$laneRunner = (string) file_get_contents(repo_path('scripts/platform-test-lane'));
expect($laneRunner)
->toContain('if [[ ${#remaining_args[@]} -gt 0 ]]; then')
->and($laneRunner)->toContain('sail_composer=(')
->and($laneRunner)->toContain('"TENANTATLAS_CI_WORKFLOW_ID=${WORKFLOW_ID}"')
->and($laneRunner)->toContain('"TENANTATLAS_CI_TRIGGER_CLASS=${TRIGGER_CLASS}"')
->and($laneRunner)->toContain('"${sail_composer[@]}" -- "${remaining_args[@]}"')
->and($laneRunner)->toContain('"${sail_composer[@]}"')
->and($laneRunner)->not->toContain('./vendor/bin/sail composer run')
->and($laneRunner)->toContain('--workflow-id=')
->and($laneRunner)->toContain('--trigger-class=');
});
it('propagates manifest lane and report result codes without duplicating final-result policy', function (): void {
$laneRunner = (string) file_get_contents(repo_path('scripts/platform-test-lane'));
$reportRunner = (string) file_get_contents(repo_path('scripts/platform-test-report'));
$composerContents = (string) file_get_contents(base_path('composer.json'));
$composer = json_decode($composerContents, true, 512, JSON_THROW_ON_ERROR);
$scripts = $composer['scripts'] ?? [];
expect($laneRunner)->toContain('set -euo pipefail')
->and($laneRunner)->toContain('./vendor/bin/sail', 'run', 'env', 'composer', 'run', '--timeout=0')
->and($laneRunner)->toContain('"TENANTATLAS_CI_WORKFLOW_ID=${WORKFLOW_ID}"')
->and($laneRunner)->toContain('"TENANTATLAS_CI_TRIGGER_CLASS=${TRIGGER_CLASS}"')
->and($laneRunner)->not->toContain('"${sail_composer[@]}" || true')
->and($reportRunner)->toContain('set -euo pipefail')
->and($reportRunner)->toContain('./vendor/bin/sail', 'run', 'env', 'composer', 'run', '--timeout=0')
->and($reportRunner)->toContain('"TENANTATLAS_CI_WORKFLOW_ID=${WORKFLOW_ID}"')
->and($reportRunner)->toContain('"TENANTATLAS_CI_TRIGGER_CLASS=${TRIGGER_CLASS}"')
->and($reportRunner)->not->toContain('"${sail_composer[@]}" || true');
foreach ([
'test' => "exit(\\Tests\\Support\\TestLaneManifest::runLane('fast-feedback'));",
'test:fast' => "exit(\\Tests\\Support\\TestLaneManifest::runLane('fast-feedback'));",
'test:confidence' => "exit(\\Tests\\Support\\TestLaneManifest::runLane('confidence'));",
'test:browser' => "exit(\\Tests\\Support\\TestLaneManifest::runLane('browser'));",
'test:heavy' => "exit(\\Tests\\Support\\TestLaneManifest::runLane('heavy-governance'));",
'test:profile' => "exit(\\Tests\\Support\\TestLaneManifest::runLane('profiling'));",
'test:junit' => "exit(\\Tests\\Support\\TestLaneManifest::runLane('junit'));",
'test:report' => "exit(\\Tests\\Support\\TestLaneManifest::renderLatestReport('fast-feedback', 'shared-test-fixture-slimming'));",
'test:report:confidence' => "exit(\\Tests\\Support\\TestLaneManifest::renderLatestReport('confidence', 'shared-test-fixture-slimming'));",
'test:report:browser' => "exit(\\Tests\\Support\\TestLaneManifest::renderLatestReport('browser', ''));",
'test:report:heavy' => "exit(\\Tests\\Support\\TestLaneManifest::renderLatestReport('heavy-governance', ''));",
'test:report:profile' => "exit(\\Tests\\Support\\TestLaneManifest::renderLatestReport('profiling', ''));",
'test:report:junit' => "exit(\\Tests\\Support\\TestLaneManifest::renderLatestReport('junit', ''));",
] as $scriptName => $expectedExitCall) {
expect($scripts)->toHaveKey($scriptName)
->and(implode("\n", (array) $scripts[$scriptName]))->toContain($expectedExitCall);
}
foreach ([$laneRunner, $reportRunner, $composerContents] as $entryPointContents) {
expect($entryPointContents)
->not->toContain('TestLaneBudget::')
->not->toContain('enforcementMode')
->not->toContain('hard-fail')
->not->toContain('soft-warn')
->not->toContain('trend-only');
}
});
it('keeps CI workflow validation and wrong-lane drift detection in the checked-in manifest', function (): void {
$validExecution = TestLaneManifest::validateWorkflowExecution('pr-fast-feedback', 'fast-feedback');
$wrongLaneExecution = TestLaneManifest::validateWorkflowExecution('pr-fast-feedback', 'confidence');
expect($validExecution['valid'])->toBeTrue()
->and($validExecution['primaryFailureClassId'])->toBeNull()
->and($wrongLaneExecution['valid'])->toBeFalse()
->and($wrongLaneExecution['workflowLaneMatched'])->toBeFalse()
->and($wrongLaneExecution['primaryFailureClassId'])->toBe('wrapper-failure');
});
it('degrades invalid CI workflow context to wrapper-failure metadata instead of crashing the lane context lookup', function (): void {
$originalWorkflowId = getenv('TENANTATLAS_CI_WORKFLOW_ID');
$originalTriggerClass = getenv('TENANTATLAS_CI_TRIGGER_CLASS');
putenv('TENANTATLAS_CI_WORKFLOW_ID=missing-workflow-profile');
putenv('TENANTATLAS_CI_TRIGGER_CLASS=pull-request');
try {
$context = TestLaneManifest::currentCiContext('fast-feedback');
expect($context['entryPointResolved'])->toBeFalse()
->and($context['workflowLaneMatched'])->toBeFalse()
->and($context['primaryFailureClassId'])->toBe('wrapper-failure')
->and($context['unresolvedEntryPoints'])->toContain('workflow-profile');
} finally {
putenv($originalWorkflowId === false ? 'TENANTATLAS_CI_WORKFLOW_ID' : sprintf('TENANTATLAS_CI_WORKFLOW_ID=%s', $originalWorkflowId));
putenv($originalTriggerClass === false ? 'TENANTATLAS_CI_TRIGGER_CLASS' : sprintf('TENANTATLAS_CI_TRIGGER_CLASS=%s', $originalTriggerClass));
}
});
it('fails closed when an explicit trigger contradicts its workflow profile', function (): void {
$originalWorkflowId = getenv('TENANTATLAS_CI_WORKFLOW_ID');
$originalTriggerClass = getenv('TENANTATLAS_CI_TRIGGER_CLASS');
try {
foreach ([
['workflowId' => 'heavy-governance-manual', 'laneId' => 'heavy-governance', 'triggerClass' => 'manual', 'valid' => true],
['workflowId' => 'browser-scheduled', 'laneId' => 'browser', 'triggerClass' => 'scheduled', 'valid' => true],
['workflowId' => 'heavy-governance-manual', 'laneId' => 'heavy-governance', 'triggerClass' => 'scheduled', 'valid' => false],
['workflowId' => 'browser-scheduled', 'laneId' => 'browser', 'triggerClass' => 'manual', 'valid' => false],
] as $fixture) {
putenv('TENANTATLAS_CI_WORKFLOW_ID='.$fixture['workflowId']);
putenv('TENANTATLAS_CI_TRIGGER_CLASS='.$fixture['triggerClass']);
$context = TestLaneManifest::currentCiContext($fixture['laneId']);
expect($context['entryPointResolved'])->toBe($fixture['valid'])
->and($context['workflowLaneMatched'])->toBeTrue();
if (! $fixture['valid']) {
expect($context['primaryFailureClassId'] ?? null)->toBe('wrapper-failure')
->and($context['unresolvedEntryPoints'] ?? [])->toContain('trigger-class');
}
}
} finally {
putenv($originalWorkflowId === false ? 'TENANTATLAS_CI_WORKFLOW_ID' : sprintf('TENANTATLAS_CI_WORKFLOW_ID=%s', $originalWorkflowId));
putenv($originalTriggerClass === false ? 'TENANTATLAS_CI_TRIGGER_CLASS' : sprintf('TENANTATLAS_CI_TRIGGER_CLASS=%s', $originalTriggerClass));
}
});
it('routes the foundational lane commands through stable artisan arguments', function (): void {
$fastFeedbackConfig = TestLaneManifest::laneConfigurationPath('fast-feedback');
$fastFeedbackContents = (string) file_get_contents(TestLaneManifest::absolutePath($fastFeedbackConfig));
$confidenceConfig = TestLaneManifest::laneConfigurationPath('confidence');
$confidenceContents = (string) file_get_contents(TestLaneManifest::absolutePath($confidenceConfig));
$browserConfig = TestLaneManifest::laneConfigurationPath('browser');
$browserContents = (string) file_get_contents(TestLaneManifest::absolutePath($browserConfig));
$heavyConfig = TestLaneManifest::laneConfigurationPath('heavy-governance');
$heavyContents = (string) file_get_contents(TestLaneManifest::absolutePath($heavyConfig));
expect(TestLaneManifest::buildCommand('fast-feedback'))->toContain('--parallel')
->and(TestLaneManifest::buildCommand('fast-feedback'))->toContain('--configuration='.$fastFeedbackConfig)
->and($fastFeedbackContents)->not->toContain('tests/Feature/Drift/DriftBulkAcknowledgeAllMatchingConfirmationTest.php')
->and($fastFeedbackContents)->not->toContain('tests/Feature/Findings/FindingBulkActionsTest.php')
->and($fastFeedbackContents)->not->toContain('tests/Feature/Guards/ActionSurfaceContractTest.php')
->and(TestLaneManifest::buildCommand('confidence'))->toContain('--configuration='.$confidenceConfig)
->and($confidenceContents)->toContain('tests/Feature/Baselines/BaselineCompareMatrixCompareAllActionTest.php')
->and($confidenceContents)->not->toContain('tests/Feature/Drift/DriftBulkAcknowledgeAllMatchingConfirmationTest.php')
->and($confidenceContents)->not->toContain('tests/Feature/Filament/BaselineActionAuthorizationTest.php')
->and($confidenceContents)->not->toContain('tests/Feature/Filament/BaselineProfileCaptureStartSurfaceTest.php')
->and($confidenceContents)->not->toContain('tests/Feature/Filament/BaselineProfileCompareStartSurfaceTest.php')
->and($confidenceContents)->not->toContain('tests/Feature/Findings/FindingBulkActionsTest.php')
->and($confidenceContents)->not->toContain('tests/Feature/Guards/ActionSurfaceContractTest.php')
->and(TestLaneManifest::buildCommand('browser'))->toContain('--configuration='.$browserConfig)
->and($browserContents)->toContain('tests/Browser/Spec190BaselineCompareMatrixSmokeTest.php')
->and(TestLaneManifest::buildCommand('heavy-governance'))->toContain('--configuration='.$heavyConfig)
->and($heavyContents)->toContain('tests/Feature/Drift/DriftBulkAcknowledgeAllMatchingConfirmationTest.php')
->and($heavyContents)->toContain('tests/Feature/Filament/BaselineActionAuthorizationTest.php')
->and($heavyContents)->toContain('tests/Feature/Filament/BaselineProfileCaptureStartSurfaceTest.php')
->and($heavyContents)->toContain('tests/Feature/Filament/BaselineProfileCompareStartSurfaceTest.php')
->and($heavyContents)->toContain('tests/Feature/Findings/FindingBulkActionsTest.php')
->and($heavyContents)->toContain('tests/Feature/Guards/ActionSurfaceContractTest.php')
->and(TestLaneManifest::buildCommand('junit'))->toContain('--parallel');
});