70 lines
2.7 KiB
PHP
70 lines
2.7 KiB
PHP
<?php
|
|
|
|
use app\common\service\chat\PrivateChatService;
|
|
use app\common\service\chat\WhisperAsrService;
|
|
|
|
require __DIR__ . '/../../server/vendor/autoload.php';
|
|
|
|
function fail(string $message): void
|
|
{
|
|
fwrite(STDERR, $message . PHP_EOL);
|
|
exit(1);
|
|
}
|
|
|
|
function assertTrue(bool $condition, string $message): void
|
|
{
|
|
if (!$condition) {
|
|
fail($message);
|
|
}
|
|
}
|
|
|
|
function assertSame($expected, $actual, string $message): void
|
|
{
|
|
if ($expected !== $actual) {
|
|
fail($message . ',期望: ' . var_export($expected, true) . ',实际: ' . var_export($actual, true));
|
|
}
|
|
}
|
|
|
|
if (!class_exists(WhisperAsrService::class)) {
|
|
fail('缺少 WhisperAsrService');
|
|
}
|
|
|
|
if (!defined(PrivateChatService::class . '::RELATIONSHIP_NOT_MUTUAL_TEXT')) {
|
|
fail('缺少非互关关系文案常量');
|
|
}
|
|
assertSame('对方还未添加你为好友', PrivateChatService::RELATIONSHIP_NOT_MUTUAL_TEXT, '非互关关系文案不符合预期');
|
|
|
|
$okFile = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'sport-era-voice-ok.m4a';
|
|
$largeFile = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'sport-era-voice-large.wav';
|
|
file_put_contents($okFile, 'fake-audio-content');
|
|
file_put_contents($largeFile, str_repeat('a', 1024 * 1024 + 1));
|
|
|
|
try {
|
|
$valid = WhisperAsrService::validateAudioFile($okFile, 'voice.m4a', 1);
|
|
assertTrue(!empty($valid['success']), 'm4a 音频应通过校验');
|
|
assertSame('m4a', $valid['data']['extension'] ?? '', '应返回归一化扩展名');
|
|
|
|
$fallbackExt = WhisperAsrService::validateAudioFile($okFile, 'file', 1);
|
|
assertTrue(!empty($fallbackExt['success']), '原始文件名缺少扩展名时应回退读取临时文件扩展名');
|
|
assertSame('m4a', $fallbackExt['data']['extension'] ?? '', '回退扩展名应来自临时文件路径');
|
|
|
|
$invalidExt = WhisperAsrService::validateAudioFile($okFile, 'voice.exe', 1);
|
|
assertTrue(empty($invalidExt['success']), '非音频扩展名应被拒绝');
|
|
|
|
$tooLarge = WhisperAsrService::validateAudioFile($largeFile, 'large.wav', 1);
|
|
assertTrue(empty($tooLarge['success']), '超过大小限制的音频应被拒绝');
|
|
|
|
$normal = WhisperAsrService::normalizeAsrResponse(['text' => ' 你好,世界 ', 'duration_ms' => 88]);
|
|
assertTrue(!empty($normal['success']), '带文字的 ASR 响应应归一化成功');
|
|
assertSame('你好,世界', $normal['data']['text'] ?? '', 'ASR 文本应去除首尾空白');
|
|
assertSame(88, $normal['data']['duration_ms'] ?? 0, 'ASR 耗时应保留');
|
|
|
|
$empty = WhisperAsrService::normalizeAsrResponse(['text' => ' ']);
|
|
assertTrue(empty($empty['success']), '空识别结果应返回失败');
|
|
} finally {
|
|
@unlink($okFile);
|
|
@unlink($largeFile);
|
|
}
|
|
|
|
echo 'Whisper ASR 服务校验测试通过' . PHP_EOL;
|