TenantAtlas/apps/platform/tests/Feature/Filament/TenantGovernanceAggregateMemoizationTest.php
ahmido e64bae9cfc feat: cut over tenant core to managed environments (#335)
## Summary
- replace the legacy Tenant and TenantMembership core models with ManagedEnvironment and ManagedEnvironmentMembership
- propagate the managed environment naming and key changes across Filament resources, pages, controllers, jobs, models, and supporting runtime paths
- add feature 279 spec artifacts and focused managed-environment test coverage for model behavior, route binding, panel context, authorization, and legacy guardrails

## Validation
- `cd apps/platform && ./vendor/bin/sail artisan test --compact tests/Feature/ManagedEnvironment/LegacyTenantCoreGuardTest.php tests/Feature/ManagedEnvironment/ManagedEnvironmentAuthorizationTest.php tests/Feature/ManagedEnvironment/ManagedEnvironmentPanelContextTest.php tests/Feature/ManagedEnvironment/ManagedEnvironmentRouteBindingTest.php tests/Unit/ManagedEnvironment/ManagedEnvironmentContextResolverTest.php tests/Unit/ManagedEnvironment/ManagedEnvironmentModelTest.php`
- `cd apps/platform && ./vendor/bin/sail bin pint --dirty --format agent`

## Notes
- branch pushed from commit `1123b122`
- browser smoke test file was added but not run in this pass

Co-authored-by: Ahmed Darrazi <ahmed.darrazi@live.de>
Reviewed-on: #335
2026-05-07 06:38:14 +00:00

123 lines
4.7 KiB
PHP

<?php
declare(strict_types=1);
use App\Filament\Widgets\Dashboard\BaselineCompareNow;
use App\Filament\Widgets\Dashboard\NeedsAttention;
use App\Models\BaselineProfile;
use App\Models\BaselineSnapshot;
use App\Models\BaselineTenantAssignment;
use App\Models\ManagedEnvironment;
use App\Support\Baselines\BaselineCompareReasonCode;
use App\Support\Baselines\TenantGovernanceAggregateResolver;
use App\Support\OperationRunOutcome;
use App\Support\OperationRunStatus;
use App\Support\OperationRunType;
use App\Support\Ui\DerivedState\DerivedStateFamily;
use App\Support\Ui\DerivedState\RequestScopedDerivedStateStore;
use Filament\Facades\Filament;
use Livewire\Livewire;
function createTenantGovernanceMemoizationTenant(): array
{
[$user, $tenant] = createUserWithTenant(role: 'owner');
$profile = BaselineProfile::factory()->active()->create([
'workspace_id' => (int) $tenant->workspace_id,
]);
$snapshot = BaselineSnapshot::factory()->create([
'workspace_id' => (int) $tenant->workspace_id,
'baseline_profile_id' => (int) $profile->getKey(),
]);
$profile->update(['active_snapshot_id' => (int) $snapshot->getKey()]);
BaselineTenantAssignment::factory()->create([
'workspace_id' => (int) $tenant->workspace_id,
'managed_environment_id' => (int) $tenant->getKey(),
'baseline_profile_id' => (int) $profile->getKey(),
]);
\App\Models\OperationRun::factory()->create([
'managed_environment_id' => (int) $tenant->getKey(),
'workspace_id' => (int) $tenant->workspace_id,
'type' => OperationRunType::BaselineCompare->value,
'status' => OperationRunStatus::Completed->value,
'outcome' => OperationRunOutcome::Succeeded->value,
'completed_at' => now(),
'context' => [
'baseline_profile_id' => (int) $profile->getKey(),
'baseline_snapshot_id' => (int) $snapshot->getKey(),
'baseline_compare' => [
'reason_code' => BaselineCompareReasonCode::NoDriftDetected->value,
'coverage' => [
'effective_types' => ['deviceConfiguration'],
'covered_types' => ['deviceConfiguration'],
'uncovered_types' => [],
'proof' => true,
],
],
],
]);
return [$user, $tenant];
}
it('reuses one tenant-governance aggregate across the tenant dashboard summary widgets', function (): void {
[$user, $tenant] = createTenantGovernanceMemoizationTenant();
$this->actingAs($user);
Filament::setCurrentPanel(Filament::getPanel('tenant'));
Filament::setTenant($tenant, true);
Livewire::actingAs($user)->test(NeedsAttention::class);
Livewire::actingAs($user)->test(BaselineCompareNow::class);
expect(app(RequestScopedDerivedStateStore::class)->countStored(
DerivedStateFamily::TenantGovernanceAggregate,
ManagedEnvironment::class,
(string) $tenant->getKey(),
TenantGovernanceAggregateResolver::VARIANT_TENANT_GOVERNANCE_SUMMARY,
))->toBe(1);
});
it('keeps tenant switches from reusing another tenant aggregate in the same request scope', function (): void {
[, $tenantA] = createTenantGovernanceMemoizationTenant();
[, $tenantB] = createTenantGovernanceMemoizationTenant();
$resolver = app(TenantGovernanceAggregateResolver::class);
$aggregateA = $resolver->forTenant($tenantA);
$aggregateB = $resolver->forTenant($tenantB);
$store = app(RequestScopedDerivedStateStore::class);
expect($aggregateA)->not->toBeNull()
->and($aggregateB)->not->toBeNull()
->and($aggregateA?->tenantId)->toBe((int) $tenantA->getKey())
->and($aggregateB?->tenantId)->toBe((int) $tenantB->getKey())
->and($aggregateA?->tenantId)->not->toBe($aggregateB?->tenantId)
->and($store->countStored(
DerivedStateFamily::TenantGovernanceAggregate,
ManagedEnvironment::class,
(string) $tenantA->getKey(),
TenantGovernanceAggregateResolver::VARIANT_TENANT_GOVERNANCE_SUMMARY,
))->toBe(1)
->and($store->countStored(
DerivedStateFamily::TenantGovernanceAggregate,
ManagedEnvironment::class,
(string) $tenantB->getKey(),
TenantGovernanceAggregateResolver::VARIANT_TENANT_GOVERNANCE_SUMMARY,
))->toBe(1);
});
it('returns no aggregate and stores nothing when no tenant context exists', function (): void {
$aggregate = app(TenantGovernanceAggregateResolver::class)->forTenant(null);
expect($aggregate)->toBeNull()
->and(app(RequestScopedDerivedStateStore::class)->countStored(
DerivedStateFamily::TenantGovernanceAggregate,
))->toBe(0);
});