Files
xuetang/server/tests/QywxCustomerAcquisitionRetryPolicyTest.php
T
2026-09-08 11:40:15 +08:00

41 lines
1.8 KiB
PHP

<?php
declare(strict_types=1);
use app\common\service\qywx\QywxCustomerAcquisitionCustomerService;
require dirname(__DIR__) . '/vendor/autoload.php';
$assert = static function (bool $condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$now = 2_000_000_000;
$active = QywxCustomerAcquisitionCustomerService::retryDecision($now - 120, $now);
$assert($active['expired'] === false, '有效期内事件不应标记过期');
$assert($active['expire_time'] === $now + 1680, 'ChatKey 截止时间必须是事件时间 + 30 分钟');
$assert($active['next_retry'] === $now + 30, '失败事件应安排 30 秒后重试');
$nearDeadline = QywxCustomerAcquisitionCustomerService::retryDecision($now - 1790, $now);
$assert($nearDeadline['expired'] === false, '截止前事件仍应允许重试');
$assert($nearDeadline['next_retry'] === $now + 9, '重试时间不得越过 ChatKey 硬截止');
$expired = QywxCustomerAcquisitionCustomerService::retryDecision($now - 1800, $now);
$assert($expired['expired'] === true, '满 30 分钟必须明确过期');
$assert($expired['next_retry'] === 0, '过期事件不得继续安排重试');
$message = ['MsgId' => 'm1', 'LinkID' => 'l1', 'UserID' => 'u1'];
$keyA = QywxCustomerAcquisitionCustomerService::eventKey($message, 'message_from_customer', 'chat-1', $now);
$keyB = QywxCustomerAcquisitionCustomerService::eventKey($message, 'message_from_customer', 'chat-1', $now);
$assert($keyA === $keyB && strlen($keyA) === 64, '事件幂等键必须稳定且为 SHA-256');
$console = require dirname(__DIR__) . '/config/console.php';
$assert(
($console['commands']['qywx:retry-customer-acquisition-events'] ?? '') === 'app\\command\\QywxRetryCustomerAcquisitionEvents',
'获客回调重试命令未注册'
);
echo "QYWX_CUSTOMER_ACQUISITION_RETRY_POLICY_TEST_OK\n";