TenantAtlas/tests/Feature/Baselines/BaselineSnapshotBackfillTest.php
ahmido 8426741068 feat: add baseline snapshot truth guards (#189)
## Summary
- add explicit BaselineSnapshot lifecycle truth with conservative backfill and a shared truth resolver
- block baseline compare from building, incomplete, or superseded snapshots and align workspace/tenant UI truth surfaces with effective snapshot state
- surface artifact truth separately from operation outcome across baseline profile, snapshot, compare, and operation run pages

## Testing
- integrated browser smoke test on the active feature surfaces
- `vendor/bin/sail artisan test --compact tests/Feature/Filament/BaselineSnapshotTruthSurfaceTest.php tests/Feature/Filament/BaselineProfileCompareStartSurfaceTest.php`
- targeted baseline lifecycle and compare guard coverage added in Pest
- `vendor/bin/sail bin pint --dirty --format agent`

## Notes
- Livewire v4 compliance preserved
- no panel provider registration changes were needed; Laravel 12 providers remain in `bootstrap/providers.php`
- global search remains disabled for the affected baseline resources by design
- destructive actions remain confirmation-gated; capture and compare actions keep their existing authorization and confirmation behavior
- no new panel assets were added; existing deploy flow for `filament:assets` is unchanged

Co-authored-by: Ahmed Darrazi <ahmed.darrazi@live.de>
Reviewed-on: #189
2026-03-23 11:32:00 +00:00

114 lines
4.6 KiB
PHP

<?php
declare(strict_types=1);
use App\Models\BaselineProfile;
use App\Models\BaselineSnapshot;
use App\Models\BaselineSnapshotItem;
use App\Models\OperationRun;
use App\Models\Workspace;
use App\Support\Baselines\BaselineReasonCodes;
use App\Support\Baselines\BaselineSnapshotLifecycleState;
use App\Support\OperationRunOutcome;
use App\Support\OperationRunType;
use Illuminate\Database\Migrations\Migration;
function classifyLegacySnapshotForTest(BaselineSnapshot $snapshot): array
{
/** @var Migration $migration */
$migration = require base_path('database/migrations/2026_03_23_000001_add_lifecycle_state_to_baseline_snapshots_table.php');
$reflection = new ReflectionMethod($migration, 'classifyLegacySnapshot');
$reflection->setAccessible(true);
return $reflection->invoke(
$migration,
(object) [
'id' => (int) $snapshot->getKey(),
'workspace_id' => (int) $snapshot->workspace_id,
'baseline_profile_id' => (int) $snapshot->baseline_profile_id,
'captured_at' => $snapshot->captured_at,
'created_at' => $snapshot->created_at,
'updated_at' => $snapshot->updated_at,
'summary_jsonb' => json_encode($snapshot->summary_jsonb),
],
is_array($snapshot->summary_jsonb) ? $snapshot->summary_jsonb : [],
BaselineSnapshotItem::query()->where('baseline_snapshot_id', (int) $snapshot->getKey())->count(),
);
}
it('classifies legacy snapshots as complete when summary counts prove completion', function (): void {
$workspace = Workspace::factory()->create();
$profile = BaselineProfile::factory()->active()->create([
'workspace_id' => (int) $workspace->getKey(),
]);
$snapshot = BaselineSnapshot::factory()->complete()->create([
'workspace_id' => (int) $workspace->getKey(),
'baseline_profile_id' => (int) $profile->getKey(),
'summary_jsonb' => ['total_items' => 2],
]);
BaselineSnapshotItem::factory()->count(2)->create([
'baseline_snapshot_id' => (int) $snapshot->getKey(),
]);
$classification = classifyLegacySnapshotForTest($snapshot);
expect($classification['lifecycle_state'])->toBe(BaselineSnapshotLifecycleState::Complete->value)
->and(data_get($classification, 'completion_meta.expected_items'))->toBe(2)
->and(data_get($classification, 'completion_meta.persisted_items'))->toBe(2);
});
it('classifies proven empty legacy captures as complete when the producer run confirms zero subjects', function (): void {
$workspace = Workspace::factory()->create();
$profile = BaselineProfile::factory()->active()->create([
'workspace_id' => (int) $workspace->getKey(),
]);
$snapshot = BaselineSnapshot::factory()->complete()->create([
'workspace_id' => (int) $workspace->getKey(),
'baseline_profile_id' => (int) $profile->getKey(),
'summary_jsonb' => ['total_items' => 0],
]);
OperationRun::factory()->create([
'workspace_id' => (int) $workspace->getKey(),
'type' => OperationRunType::BaselineCapture->value,
'outcome' => OperationRunOutcome::Succeeded->value,
'context' => [
'baseline_profile_id' => (int) $profile->getKey(),
'result' => ['snapshot_id' => (int) $snapshot->getKey()],
'baseline_capture' => ['subjects_total' => 0],
],
'completed_at' => now(),
]);
$classification = classifyLegacySnapshotForTest($snapshot);
expect($classification['lifecycle_state'])->toBe(BaselineSnapshotLifecycleState::Complete->value)
->and(data_get($classification, 'completion_meta.was_empty_capture'))->toBeTrue();
});
it('classifies ambiguous legacy snapshots as incomplete with a conservative reason code', function (): void {
$workspace = Workspace::factory()->create();
$profile = BaselineProfile::factory()->active()->create([
'workspace_id' => (int) $workspace->getKey(),
]);
$snapshot = BaselineSnapshot::factory()->complete()->create([
'workspace_id' => (int) $workspace->getKey(),
'baseline_profile_id' => (int) $profile->getKey(),
'summary_jsonb' => ['total_items' => 2],
]);
BaselineSnapshotItem::factory()->create([
'baseline_snapshot_id' => (int) $snapshot->getKey(),
]);
$classification = classifyLegacySnapshotForTest($snapshot);
expect($classification['lifecycle_state'])->toBe(BaselineSnapshotLifecycleState::Incomplete->value)
->and(data_get($classification, 'completion_meta.finalization_reason_code'))->toBe(BaselineReasonCodes::SNAPSHOT_LEGACY_CONTRADICTORY);
});