## Summary - add canonical onboarding lifecycle and checkpoint fields plus optimistic locking versioning for managed tenant onboarding drafts - introduce centralized onboarding lifecycle and mutation services and route wizard mutations through version-checked writes - convert Verify Access and Bootstrap into live checkpoint-driven wizard states with conditional polling and updated browser/feature/unit coverage - add Spec Kit artifacts for feature 140, including spec, plan, tasks, research, data model, quickstart, checklist, and logical contract ## Validation - branch was committed and pushed cleanly - focused tests and formatting were updated during implementation work - full validation was not re-run as part of this final git/PR step ## Notes - base branch: `dev` - feature branch: `140-onboarding-lifecycle-operation-checkpoints-concurrency-mvp` - outstanding follow-up items, if any, remain tracked in `specs/140-onboarding-lifecycle-operation-checkpoints-concurrency-mvp/tasks.md` Co-authored-by: Ahmed Darrazi <ahmed.darrazi@live.de> Reviewed-on: #169
204 lines
7.9 KiB
PHP
204 lines
7.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
use App\Models\OperationRun;
|
|
use App\Models\Tenant;
|
|
use App\Services\Onboarding\OnboardingLifecycleService;
|
|
use App\Support\Onboarding\OnboardingCheckpoint;
|
|
use App\Support\Onboarding\OnboardingLifecycleState;
|
|
use App\Support\OperationRunOutcome;
|
|
use App\Support\OperationRunStatus;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
|
|
uses(RefreshDatabase::class);
|
|
|
|
it('marks a draft as action required when the provider connection changed before verification reruns', function (): void {
|
|
$tenant = Tenant::factory()->create();
|
|
|
|
$draft = createOnboardingDraft([
|
|
'workspace' => $tenant->workspace,
|
|
'tenant' => $tenant,
|
|
'current_step' => 'verify',
|
|
'state' => [
|
|
'entra_tenant_id' => (string) $tenant->tenant_id,
|
|
'tenant_name' => (string) $tenant->name,
|
|
'provider_connection_id' => 42,
|
|
'connection_recently_updated' => true,
|
|
],
|
|
]);
|
|
|
|
$snapshot = app(OnboardingLifecycleService::class)->snapshot($draft);
|
|
|
|
expect($snapshot['lifecycle_state'])->toBe(OnboardingLifecycleState::ActionRequired)
|
|
->and($snapshot['current_checkpoint'])->toBe(OnboardingCheckpoint::VerifyAccess)
|
|
->and($snapshot['last_completed_checkpoint'])->toBe(OnboardingCheckpoint::ConnectProvider)
|
|
->and($snapshot['reason_code'])->toBe('provider_connection_changed')
|
|
->and($snapshot['blocking_reason_code'])->toBe('provider_connection_changed');
|
|
});
|
|
|
|
it('marks a draft as ready for activation when verification succeeded for the selected connection', function (): void {
|
|
$tenant = Tenant::factory()->create();
|
|
|
|
$draft = createOnboardingDraft([
|
|
'workspace' => $tenant->workspace,
|
|
'tenant' => $tenant,
|
|
'current_step' => 'verify',
|
|
'state' => [
|
|
'entra_tenant_id' => (string) $tenant->tenant_id,
|
|
'tenant_name' => (string) $tenant->name,
|
|
'provider_connection_id' => 84,
|
|
],
|
|
]);
|
|
|
|
$run = OperationRun::factory()->create([
|
|
'tenant_id' => (int) $tenant->getKey(),
|
|
'workspace_id' => (int) $tenant->workspace_id,
|
|
'status' => OperationRunStatus::Completed->value,
|
|
'outcome' => OperationRunOutcome::Succeeded->value,
|
|
'context' => [
|
|
'provider_connection_id' => 84,
|
|
],
|
|
]);
|
|
|
|
$draft->forceFill([
|
|
'state' => array_merge($draft->state ?? [], [
|
|
'verification_operation_run_id' => (int) $run->getKey(),
|
|
]),
|
|
])->save();
|
|
|
|
$service = app(OnboardingLifecycleService::class);
|
|
$snapshot = $service->snapshot($draft->fresh());
|
|
|
|
expect($snapshot['lifecycle_state'])->toBe(OnboardingLifecycleState::ReadyForActivation)
|
|
->and($snapshot['current_checkpoint'])->toBe(OnboardingCheckpoint::CompleteActivate)
|
|
->and($snapshot['last_completed_checkpoint'])->toBe(OnboardingCheckpoint::VerifyAccess)
|
|
->and($service->isReadyForActivation($draft->fresh()))->toBeTrue();
|
|
});
|
|
|
|
it('marks a draft as bootstrapping while a selected bootstrap run is still active', function (): void {
|
|
$tenant = Tenant::factory()->create();
|
|
|
|
$verificationRun = OperationRun::factory()->create([
|
|
'tenant_id' => (int) $tenant->getKey(),
|
|
'workspace_id' => (int) $tenant->workspace_id,
|
|
'status' => OperationRunStatus::Completed->value,
|
|
'outcome' => OperationRunOutcome::Succeeded->value,
|
|
'context' => [
|
|
'provider_connection_id' => 126,
|
|
],
|
|
]);
|
|
|
|
$bootstrapRun = OperationRun::factory()->create([
|
|
'tenant_id' => (int) $tenant->getKey(),
|
|
'workspace_id' => (int) $tenant->workspace_id,
|
|
'status' => OperationRunStatus::Running->value,
|
|
'outcome' => OperationRunOutcome::Pending->value,
|
|
'type' => 'inventory_sync',
|
|
'context' => [
|
|
'provider_connection_id' => 126,
|
|
],
|
|
]);
|
|
|
|
$draft = createOnboardingDraft([
|
|
'workspace' => $tenant->workspace,
|
|
'tenant' => $tenant,
|
|
'current_step' => 'bootstrap',
|
|
'state' => [
|
|
'entra_tenant_id' => (string) $tenant->tenant_id,
|
|
'tenant_name' => (string) $tenant->name,
|
|
'provider_connection_id' => 126,
|
|
'verification_operation_run_id' => (int) $verificationRun->getKey(),
|
|
'bootstrap_operation_types' => ['inventory_sync'],
|
|
'bootstrap_operation_runs' => [
|
|
'inventory_sync' => (int) $bootstrapRun->getKey(),
|
|
],
|
|
],
|
|
]);
|
|
|
|
$service = app(OnboardingLifecycleService::class);
|
|
$snapshot = $service->snapshot($draft);
|
|
|
|
expect($snapshot['lifecycle_state'])->toBe(OnboardingLifecycleState::Bootstrapping)
|
|
->and($snapshot['current_checkpoint'])->toBe(OnboardingCheckpoint::Bootstrap)
|
|
->and($snapshot['last_completed_checkpoint'])->toBe(OnboardingCheckpoint::VerifyAccess)
|
|
->and($service->hasActiveCheckpoint($draft))->toBeTrue();
|
|
});
|
|
|
|
it('marks a draft as action required when a selected bootstrap run fails', function (): void {
|
|
$tenant = Tenant::factory()->create();
|
|
|
|
$verificationRun = OperationRun::factory()->create([
|
|
'tenant_id' => (int) $tenant->getKey(),
|
|
'workspace_id' => (int) $tenant->workspace_id,
|
|
'status' => OperationRunStatus::Completed->value,
|
|
'outcome' => OperationRunOutcome::Succeeded->value,
|
|
'context' => [
|
|
'provider_connection_id' => 256,
|
|
],
|
|
]);
|
|
|
|
$bootstrapRun = OperationRun::factory()->create([
|
|
'tenant_id' => (int) $tenant->getKey(),
|
|
'workspace_id' => (int) $tenant->workspace_id,
|
|
'status' => OperationRunStatus::Completed->value,
|
|
'outcome' => OperationRunOutcome::Failed->value,
|
|
'type' => 'inventory_sync',
|
|
'context' => [
|
|
'provider_connection_id' => 256,
|
|
],
|
|
]);
|
|
|
|
$draft = createOnboardingDraft([
|
|
'workspace' => $tenant->workspace,
|
|
'tenant' => $tenant,
|
|
'current_step' => 'bootstrap',
|
|
'state' => [
|
|
'entra_tenant_id' => (string) $tenant->tenant_id,
|
|
'tenant_name' => (string) $tenant->name,
|
|
'provider_connection_id' => 256,
|
|
'verification_operation_run_id' => (int) $verificationRun->getKey(),
|
|
'bootstrap_operation_types' => ['inventory_sync'],
|
|
'bootstrap_operation_runs' => [
|
|
'inventory_sync' => (int) $bootstrapRun->getKey(),
|
|
],
|
|
],
|
|
]);
|
|
|
|
$snapshot = app(OnboardingLifecycleService::class)->snapshot($draft);
|
|
|
|
expect($snapshot['lifecycle_state'])->toBe(OnboardingLifecycleState::ActionRequired)
|
|
->and($snapshot['current_checkpoint'])->toBe(OnboardingCheckpoint::Bootstrap)
|
|
->and($snapshot['last_completed_checkpoint'])->toBe(OnboardingCheckpoint::VerifyAccess)
|
|
->and($snapshot['reason_code'])->toBe('bootstrap_failed')
|
|
->and($snapshot['blocking_reason_code'])->toBe('bootstrap_failed');
|
|
});
|
|
|
|
it('applies the canonical lifecycle fields and normalizes the version floor', function (): void {
|
|
$tenant = Tenant::factory()->create();
|
|
|
|
$draft = createOnboardingDraft([
|
|
'workspace' => $tenant->workspace,
|
|
'tenant' => $tenant,
|
|
'version' => 0,
|
|
'lifecycle_state' => OnboardingLifecycleState::Draft->value,
|
|
'current_checkpoint' => OnboardingCheckpoint::Identify->value,
|
|
'last_completed_checkpoint' => null,
|
|
'current_step' => 'verify',
|
|
'state' => [
|
|
'entra_tenant_id' => (string) $tenant->tenant_id,
|
|
'tenant_name' => (string) $tenant->name,
|
|
'provider_connection_id' => 999,
|
|
'connection_recently_updated' => true,
|
|
],
|
|
]);
|
|
|
|
$changed = app(OnboardingLifecycleService::class)->applySnapshot($draft, false);
|
|
|
|
expect($changed)->toBeTrue()
|
|
->and($draft->version)->toBe(1)
|
|
->and($draft->lifecycle_state)->toBe(OnboardingLifecycleState::ActionRequired)
|
|
->and($draft->current_checkpoint)->toBe(OnboardingCheckpoint::VerifyAccess)
|
|
->and($draft->reason_code)->toBe('provider_connection_changed');
|
|
});
|