You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/web/tests/CloudServiceCompensationPro...

95 lines
4.6 KiB
PHTML

<?php
$root = dirname(__DIR__);
$read = static fn (string $path): string => (string) file_get_contents(
$root . '/' . $path
);
$assert = static function (bool $condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$method = static function (
string $source,
string $start,
string $next
): string {
$offset = strpos($source, $start);
$end = $offset === false ? false : strpos($source, $next, $offset + 1);
if ($offset === false || $end === false || $end <= $offset) {
throw new RuntimeException('Unable to isolate compensation projection method.');
}
return substr($source, $offset, $end - $offset);
};
$invoice = $method(
$read('app/Services/Cloud/CloudInvoiceService.php'),
'public static function reconcileRenewalAfterCompensationLocked(',
'private static function refreshRenewalSnapshotLocked('
);
$invoiceLock = strpos($invoice, "->where('renews_expires_at', \$oldExpiry)");
$paymentLock = strpos($invoice, 'CloudInvoicePayment::where(');
$jobLock = strpos($invoice, 'CloudJobService::blockingQueryForInvoice(');
$void = strpos($invoice, "\$source->status = 'void'");
$replacement = strpos($invoice, 'self::createRenewalInvoiceLocked(');
$assert($invoiceLock !== false && $paymentLock !== false
&& $jobLock !== false && $void !== false && $replacement !== false
&& $invoiceLock < $paymentLock && $paymentLock < $jobLock
&& $jobLock < $void && $void < $replacement,
'Compensation renewal reconciliation lost its invoice/payment/job fence order.');
$assert(str_contains($invoice, "['failed', 'expired', 'not_created']")
&& !str_contains($invoice, '$source->renews_expires_at ='),
'Compensation renewal reconciliation may rewrite or cross settled payment evidence.');
$traffic = $method(
$read('app/Services/Cloud/CloudTrafficPoolAccounting.php'),
'public static function extendEntitlementWindowForCompensationLocked(',
'public static function settleInstanceUsage('
);
$pooled = substr($traffic, (int) strpos($traffic, '$pool = CloudTrafficPool::'));
$poolLock = strpos($pooled, '->lockForUpdate()->first()');
$periodLock = strpos($pooled, "CloudServicePeriod::where('id', \$servicePeriodId)");
$entitlementLock = strpos($pooled, 'CloudTrafficEntitlement::where(');
$event = strpos($pooled, "'event_type' => 'entitlement_window_extended'");
$recompute = strpos($pooled, 'self::recomputePoolProjectionLocked(');
$assert($poolLock !== false && $periodLock !== false
&& $entitlementLock !== false && $event !== false && $recompute !== false
&& $poolLock < $periodLock && $periodLock < $entitlementLock
&& $entitlementLock < $event && $event < $recompute,
'Traffic compensation lost pool-period-entitlement lock or ledger order.');
$assert(str_contains($traffic, "'amount_bytes' => 0")
&& str_contains($traffic, "['scheduled', 'active', 'exhausted']")
&& !str_contains($traffic, 'granted_bytes ='),
'Traffic compensation can grant bytes or revive a closed entitlement.');
$backup = $method(
$read('app/Services/Cloud/CloudAutomaticBackupBillingService.php'),
'public static function extendEntitlementWindowForCompensationLocked(',
'private static function backupItem('
);
$backupPeriod = strpos($backup, "CloudServicePeriod::where('id', \$servicePeriodId)");
$backupSubscription = strpos($backup, 'CloudBackupSubscription::where(');
$backupEntitlement = strpos($backup, 'CloudBackupPeriodEntitlement::where(');
$assert($backupPeriod !== false && $backupSubscription !== false
&& $backupEntitlement !== false
&& $backupPeriod < $backupSubscription
&& $backupSubscription < $backupEntitlement,
'Backup compensation lost period-subscription-entitlement lock order.');
$assert(!str_contains($backup, 'new CloudBackupPeriodEntitlement')
&& !str_contains($backup, 'new CloudBackupSubscription')
&& !str_contains($backup, '->enabled =')
&& str_contains($backup, "!== 'USD'")
&& str_contains($backup, '$fullPriceCents < 0')
&& str_contains($backup, '$entitlement->price_version < 1')
&& str_contains($backup, '$paidAmountCents > $fullPriceCents'),
'Backup compensation may create free cover, reopen scheduling, or alter paid terms.');
$assert(!str_contains($backup,
'$entitlement->offer_id !== (int) $subscription->offer_id')
&& !str_contains($backup,
'$entitlement->price_version !== (int) $subscription->price_version')
&& !str_contains($backup,
'$fullPriceCents !== $subscriptionPriceCents'),
'Historical paid backup terms were coupled to a later subscription price.');
echo "Service compensation projection contracts passed.\n";