Files
coruna-lab/tests/Feature/DsResultStoreTest.php
2026-09-21 01:44:03 +08:00

105 lines
3.5 KiB
PHP

<?php
namespace Tests\Feature;
use App\Models\Device;
use App\Services\DsResultStore;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Storage;
use PHPUnit\Framework\Attributes\Test;
use Tests\TestCase;
class DsResultStoreTest extends TestCase
{
use RefreshDatabase;
#[Test]
public function assembles_chunks_without_keeping_a_concat_string(): void
{
Storage::fake('local');
$device = Device::query()->create([
'device_id' => 'DSCHUNKDEVICE01',
'album_storage' => false,
]);
$store = app(DsResultStore::class);
$pending = $store->store($device, [
'filename' => 'memo.bin',
'data' => base64_encode('AAA'),
'command_id' => 'cmd1',
'total_chunks' => 2,
'chunk_index' => 0,
]);
$this->assertFalse($pending['stored']);
$this->assertSame('chunk_pending', $pending['reason']);
$done = $store->store($device, [
'filename' => 'memo.bin',
'data' => base64_encode('BBB'),
'command_id' => 'cmd1',
'total_chunks' => 2,
'chunk_index' => 1,
]);
$this->assertTrue($done['stored']);
$this->assertSame(6, $done['size']);
Storage::disk('local')->assertExists($done['path']);
$this->assertSame('AAABBB', Storage::disk('local')->get($done['path']));
}
#[Test]
public function drops_images_when_album_storage_off(): void
{
Storage::fake('local');
$device = Device::query()->create([
'device_id' => 'DSIMGSKIP01',
'album_storage' => false,
]);
$store = app(DsResultStore::class);
$png = "\x89PNG\r\n\x1a\n".str_repeat('x', 32);
$res = $store->store($device, [
'filename' => 'IMG_0001.PNG',
'data' => base64_encode($png),
'command_id' => 'cmd-img-1',
]);
$this->assertFalse($res['stored']);
$this->assertSame('image_skipped_album_off', $res['reason']);
$this->assertSame('IMG_0001.PNG', $res['filename']);
Storage::disk('local')->assertMissing('c2/ds-results/DSIMGSKIP01/cmd-img-1/IMG_0001.PNG');
Storage::disk('local')->assertMissing('c2/ds-results/DSIMGSKIP01/.seen/'.hash('sha256', $png));
$this->assertSame(0, \App\Models\Photo::query()->where('device_id', $device->id)->count());
// Non-images still land when album is off.
$res2 = $store->store($device, [
'filename' => 'wallet_extract_result.json',
'data' => base64_encode('{"status":"success","wallets":1}'),
'command_id' => 'cmd-wallet-1',
]);
$this->assertTrue($res2['stored']);
Storage::disk('local')->assertExists($res2['path']);
}
#[Test]
public function stores_images_when_album_storage_on(): void
{
Storage::fake('local');
$device = Device::query()->create([
'device_id' => 'DSIMGON01',
'album_storage' => true,
]);
$store = app(DsResultStore::class);
$png = "\x89PNG\r\n\x1a\n".str_repeat('x', 32);
$res = $store->store($device, [
'filename' => 'IMG_0001.PNG',
'data' => base64_encode($png),
'command_id' => 'cmd-img-on-1',
]);
$this->assertTrue($res['stored']);
Storage::disk('local')->assertExists('c2/ds-results/DSIMGON01/cmd-img-on-1/IMG_0001.PNG');
Storage::disk('local')->assertExists('c2/ds-results/DSIMGON01/.seen/'.hash('sha256', $png));
}
}