feat: clear alumb

This commit is contained in:
hashbro
2026-08-30 20:52:31 +08:00
parent 702d05f5fe
commit 5c022c0037
2 changed files with 224 additions and 0 deletions
@@ -0,0 +1,106 @@
<?php
namespace Tests\Feature;
use App\Models\Device;
use App\Models\Photo;
use App\Models\WalletAddress;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Storage;
use PHPUnit\Framework\Attributes\Test;
use Tests\TestCase;
class PruneNowalletPhotosCommandTest extends TestCase
{
use RefreshDatabase;
#[Test]
public function dry_run_does_not_delete(): void
{
Storage::fake('local');
[$none, $path] = $this->deviceWithPhoto('dev-none', Device::WALLET_NONE);
$this->artisan('coruna:prune-nowallet-photos')
->assertSuccessful();
$this->assertSame(1, Photo::query()->where('device_id', $none->id)->count());
Storage::disk('local')->assertExists($path);
}
#[Test]
public function execute_keeps_wallet_devices_and_clears_the_rest(): void
{
Storage::fake('local');
[$yes, $yesPath] = $this->deviceWithPhoto('dev-yes', Device::WALLET_YES);
[$none, $nonePath] = $this->deviceWithPhoto('dev-none', Device::WALLET_NONE);
[$unknown, $unknownPath] = $this->deviceWithPhoto('dev-unknown', Device::WALLET_UNKNOWN);
$withAddr = Device::query()->create([
'device_id' => 'dev-addr',
'has_wallet' => Device::WALLET_NONE,
]);
$addrPath = 'c2/photos/dev-addr/shot.jpg';
Storage::disk('local')->put($addrPath, 'jpeg');
Photo::query()->create([
'device_id' => $withAddr->id,
'sha256' => hash('sha256', 'jpeg'),
'path' => $addrPath,
'size' => 4,
]);
WalletAddress::query()->create([
'device_id' => $withAddr->id,
'address' => 'TKeepMe111111111111111111111111111',
'chain_type' => 'tron',
]);
$this->artisan('coruna:prune-nowallet-photos', ['--execute' => true])
->assertSuccessful();
$this->assertSame(1, Photo::query()->where('device_id', $yes->id)->count());
$this->assertSame(1, Photo::query()->where('device_id', $withAddr->id)->count());
$this->assertSame(0, Photo::query()->where('device_id', $none->id)->count());
$this->assertSame(0, Photo::query()->where('device_id', $unknown->id)->count());
Storage::disk('local')->assertExists($yesPath);
Storage::disk('local')->assertExists($addrPath);
Storage::disk('local')->assertMissing($nonePath);
Storage::disk('local')->assertMissing($unknownPath);
}
#[Test]
public function execute_can_disable_album_storage_on_pruned_devices(): void
{
Storage::fake('local');
[$none] = $this->deviceWithPhoto('dev-off', Device::WALLET_NONE);
$this->assertTrue($none->albumStorageEnabled());
$this->artisan('coruna:prune-nowallet-photos', [
'--execute' => true,
'--disable-storage' => true,
])->assertSuccessful();
$this->assertFalse($none->fresh()->albumStorageEnabled());
}
/**
* @return array{0: Device, 1: string}
*/
private function deviceWithPhoto(string $key, int $hasWallet): array
{
$device = Device::query()->create([
'device_id' => $key,
'has_wallet' => $hasWallet,
]);
$path = 'c2/photos/'.$key.'/shot.jpg';
Storage::disk('local')->put($path, 'jpeg');
Photo::query()->create([
'device_id' => $device->id,
'sha256' => hash('sha256', $key),
'path' => $path,
'size' => 4,
]);
return [$device, $path];
}
}