create([ 'workspace_id' => (int) $sourceTenant->workspace_id, ]); $user->tenants()->syncWithoutDetaching([ $targetTenant->getKey() => ['role' => 'owner'], ]); $profile = BaselineProfile::factory()->active()->create([ 'workspace_id' => (int) $sourceTenant->workspace_id, 'scope_jsonb' => ['policy_types' => ['deviceConfiguration'], 'foundation_types' => []], ]); $snapshot = BaselineSnapshot::factory()->create([ 'workspace_id' => (int) $sourceTenant->workspace_id, 'baseline_profile_id' => (int) $profile->getKey(), 'captured_at' => now()->subMinute(), ]); $displayName = 'Shared Policy'; $subjectKey = BaselineSubjectKey::fromDisplayName($displayName); expect($subjectKey)->not->toBeNull(); $baselineSubjectExternalId = BaselineSubjectKey::workspaceSafeSubjectExternalId( policyType: 'deviceConfiguration', subjectKey: (string) $subjectKey, ); $snapshotPayload = [ 'settings' => [ ['displayName' => 'SettingX', 'value' => 1], ], ]; $expectedContentHash = app(DriftHasher::class)->hashNormalized([ 'settings' => app(SettingsNormalizer::class)->normalizeForDiff($snapshotPayload, 'deviceConfiguration', 'windows'), 'assignments' => app(AssignmentsNormalizer::class)->normalizeForDiff([]), 'scope_tag_ids' => app(ScopeTagsNormalizer::class)->normalizeIds([]), ]); BaselineSnapshotItem::factory()->create([ 'baseline_snapshot_id' => (int) $snapshot->getKey(), 'subject_type' => 'policy', 'subject_external_id' => $baselineSubjectExternalId, 'subject_key' => (string) $subjectKey, 'policy_type' => 'deviceConfiguration', 'baseline_hash' => $expectedContentHash, 'meta_jsonb' => [ 'display_name' => $displayName, 'evidence' => [ 'fidelity' => 'content', 'source' => 'policy_version', 'observed_at' => now()->toIso8601String(), ], ], ]); $inventorySyncRun = createInventorySyncOperationRunWithCoverage( tenant: $targetTenant, statusByType: ['deviceConfiguration' => 'succeeded'], ); $policy = Policy::factory()->create([ 'tenant_id' => (int) $targetTenant->getKey(), 'policy_type' => 'deviceConfiguration', 'external_id' => 'tenant-policy-uuid', 'platform' => 'windows', 'display_name' => $displayName, ]); PolicyVersion::factory()->create([ 'tenant_id' => (int) $targetTenant->getKey(), 'policy_id' => (int) $policy->getKey(), 'policy_type' => (string) $policy->policy_type, 'platform' => (string) $policy->platform, 'captured_at' => now(), 'snapshot' => $snapshotPayload, 'assignments' => [], 'scope_tags' => [], ]); InventoryItem::factory()->create([ 'tenant_id' => (int) $targetTenant->getKey(), 'workspace_id' => (int) $targetTenant->workspace_id, 'external_id' => (string) $policy->external_id, 'policy_type' => (string) $policy->policy_type, 'display_name' => $displayName, 'meta_jsonb' => ['etag' => 'E1'], 'last_seen_operation_run_id' => (int) $inventorySyncRun->getKey(), 'last_seen_at' => now(), ]); $opService = app(OperationRunService::class); $run = $opService->ensureRunWithIdentity( tenant: $targetTenant, type: OperationRunType::BaselineCompare->value, identityInputs: ['baseline_profile_id' => (int) $profile->getKey()], context: [ 'baseline_profile_id' => (int) $profile->getKey(), 'baseline_snapshot_id' => (int) $snapshot->getKey(), 'effective_scope' => ['policy_types' => ['deviceConfiguration'], 'foundation_types' => []], ], initiator: $user, ); (new CompareBaselineToTenantJob($run))->handle( app(BaselineSnapshotIdentity::class), app(AuditLogger::class), $opService, ); $run->refresh(); expect($run->status)->toBe('completed'); expect($run->outcome)->toBe('succeeded'); expect( Finding::query() ->where('tenant_id', (int) $targetTenant->getKey()) ->where('source', 'baseline.compare') ->count(), )->toBe(0); });