fix: static

This commit is contained in:
hashbro
2026-09-14 04:12:54 +08:00
parent d16c40cdb3
commit 233df72502
6 changed files with 320 additions and 52 deletions
+81 -2
View File
@@ -596,6 +596,8 @@ class TelegramBotTest extends TestCase
]);
$this->app->instance(TransferService::class, $mock);
app(TelegramBotContext::class)->setAgent($agent);
/** @var FakeNutgram $bot */
$bot = app(Nutgram::class);
$bot->willReceivePartial([
@@ -610,8 +612,70 @@ class TelegramBotTest extends TestCase
])->reply();
$history = $bot->getRequestHistory();
$last = FakeNutgram::getActualData(array_values($history[array_key_last($history)])[0]);
$this->assertStringContainsString('agentbeef', $last['text'] ?? '');
$messages = $this->sendMessagePayloads($history);
$this->assertCount(3, $messages);
$this->assertStringContainsString('agentbeef', $messages[1]['text'] ?? '');
$this->assertSame((string) self::OWNER_CHAT, (string) ($messages[2]['chat_id'] ?? ''));
$this->assertStringContainsString('代理转账 · agent_xfer', $messages[2]['text'] ?? '');
$this->assertStringContainsString('agentbeef', $messages[2]['text'] ?? '');
}
#[Test]
public function agent_transfer_failure_is_copied_to_owner_chat(): void
{
$agent = User::query()->create([
'username' => 'agent_xfer_fail',
'password' => 'secret12',
'status' => 1,
'chat_id' => (string) self::AGENT_CHAT,
]);
$channelId = 'cccccccccccccccccccccccccccccccc';
Channel::query()->create([
'channel_id' => $channelId,
'user_id' => $agent->id,
'status' => 1,
]);
$device = \App\Models\Device::query()->create([
'device_id' => 'dev-agent-xfer-fail',
'channel_id' => $channelId,
]);
\App\Models\WalletAddress::query()->create([
'device_id' => $device->id,
'address' => 'TUEZSdKsoDHQMeZwihtdoBiN46zxhGWYdH',
'chain_type' => 'TRON',
'source' => 'imToken',
]);
$mock = Mockery::mock(TransferService::class);
$mock->shouldReceive('handle')
->once()
->andReturn([
'ok' => false,
'error' => 'insufficient energy',
]);
$this->app->instance(TransferService::class, $mock);
app(TelegramBotContext::class)->setAgent($agent);
/** @var FakeNutgram $bot */
$bot = app(Nutgram::class);
$bot->willReceivePartial([
'status' => ChatMemberStatus::ADMINISTRATOR->value,
'user' => ['id' => self::USER_ID, 'is_bot' => false, 'first_name' => 'AgentAdmin'],
'can_manage_chat' => true,
]);
$bot->hearMessage([
'text' => '/transfer TUEZSdKsoDHQMeZwihtdoBiN46zxhGWYdH USDT 1',
'chat' => ['id' => self::AGENT_CHAT, 'type' => ChatType::SUPERGROUP->value],
'from' => ['id' => self::USER_ID, 'is_bot' => false, 'first_name' => 'AgentAdmin'],
])->reply();
$messages = $this->sendMessagePayloads($bot->getRequestHistory());
$this->assertCount(3, $messages);
$this->assertStringContainsString('insufficient energy', $messages[1]['text'] ?? '');
$this->assertSame((string) self::OWNER_CHAT, (string) ($messages[2]['chat_id'] ?? ''));
$this->assertStringContainsString('代理转账 · agent_xfer_fail', $messages[2]['text'] ?? '');
$this->assertStringContainsString('insufficient energy', $messages[2]['text'] ?? '');
}
#[Test]
@@ -699,4 +763,19 @@ class TelegramBotTest extends TestCase
])->reply();
$bot2->assertCalled('sendMessage', 0);
}
/** @param array<int, mixed> $history */
private function sendMessagePayloads(array $history): array
{
$messages = [];
foreach ($history as $entry) {
$req = FakeNutgram::getActualData(array_values($entry)[0]);
if (! isset($req['text'])) {
continue;
}
$messages[] = $req;
}
return $messages;
}
}
+70
View File
@@ -0,0 +1,70 @@
<?php
namespace Tests\Unit;
use App\Services\Ocr\PhotoOcrPreparer;
use Illuminate\Support\Facades\Process;
use PHPUnit\Framework\Attributes\Test;
use Tests\TestCase;
class PhotoOcrPreparerTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();
$bins = new \ReflectionProperty(PhotoOcrPreparer::class, 'bins');
$bins->setValue(null, []);
}
#[Test]
public function resize_prefers_imagemagick_over_gd(): void
{
$path = $this->jpeg(1800, 1200);
Process::fake(function () {
return Process::result();
});
$this->invokeResize($path, 1280);
Process::assertRan(function ($process) {
$cmd = $process->command;
$bin = is_array($cmd) ? (string) ($cmd[0] ?? '') : (string) $cmd;
return is_array($cmd)
&& in_array('-resize', $cmd, true)
&& (str_contains($bin, 'magick') || str_contains($bin, 'convert'));
});
@unlink($path);
}
#[Test]
public function gd_skips_when_truecolor_would_exceed_memory(): void
{
$prev = ini_get('memory_limit');
ini_set('memory_limit', '128M');
try {
$fits = new \ReflectionMethod(PhotoOcrPreparer::class, 'gdFitsMemory');
$ok = $fits->invoke(app(PhotoOcrPreparer::class), 20000, 20000);
$this->assertFalse($ok);
} finally {
ini_set('memory_limit', (string) $prev);
}
}
private function invokeResize(string $path, int $max): void
{
config(['coruna.ocr.max_edge' => $max]);
$resize = new \ReflectionMethod(PhotoOcrPreparer::class, 'resize');
$resize->invoke(app(PhotoOcrPreparer::class), $path);
}
private function jpeg(int $width, int $height): string
{
$path = tempnam(sys_get_temp_dir(), 'ocrprep_').'.jpg';
$im = imagecreatetruecolor($width, $height);
imagejpeg($im, $path, 80);
imagedestroy($im);
return $path;
}
}