fix: test

This commit is contained in:
hashbro
2026-09-10 04:43:54 +08:00
parent 60d73cb689
commit 9333095103
4 changed files with 79 additions and 8 deletions
@@ -638,13 +638,21 @@ class DeviceController extends Controller
$paginator = $device->keystores()->orderBy($field, $order)->paginate($limit, ['*'], 'page', $page); $paginator = $device->keystores()->orderBy($field, $order)->paginate($limit, ['*'], 'page', $page);
$portal = $this->portal(); $portal = $this->portal();
$data = collect($paginator->items())->map(function (WalletKeystore $row) use ($portal) { $data = collect($paginator->items())->map(function (WalletKeystore $row) use ($portal) {
try {
$itemCount = $row->itemCount();
$summary = $row->summary();
} catch (\Throwable) {
$itemCount = 0;
$summary = '';
}
return [ return [
'id' => $row->id, 'id' => $row->id,
'source' => $row->sourceLabel(), 'source' => $row->sourceLabel(),
'decrypted' => (int) $row->decrypted, 'decrypted' => (int) $row->decrypted,
'kind' => $row->kindLabel(), 'kind' => $row->kindLabel(),
'item_count' => $row->itemCount(), 'item_count' => $itemCount,
'summary' => $row->summary(), 'summary' => $summary,
'created_at' => optional($row->created_at)->format('Y-m-d H:i:s'), 'created_at' => optional($row->created_at)->format('Y-m-d H:i:s'),
'items_url' => route($portal.'.keystores.items', $row->id), 'items_url' => route($portal.'.keystores.items', $row->id),
'decrypt_url' => route($portal.'.keystores.decrypt', $row->id), 'decrypt_url' => route($portal.'.keystores.decrypt', $row->id),
@@ -137,6 +137,14 @@ class KeystoreController extends Controller
*/ */
public function rowPayload(WalletKeystore $row, string $portal): array public function rowPayload(WalletKeystore $row, string $portal): array
{ {
try {
$itemCount = $row->itemCount();
$summary = $row->summary();
} catch (\Throwable) {
$itemCount = 0;
$summary = '';
}
return [ return [
'id' => $row->id, 'id' => $row->id,
'device_key' => $row->device_key ?? $row->device?->device_id ?? '', 'device_key' => $row->device_key ?? $row->device?->device_id ?? '',
@@ -144,8 +152,8 @@ class KeystoreController extends Controller
'source' => $row->sourceLabel(), 'source' => $row->sourceLabel(),
'decrypted' => (int) $row->decrypted, 'decrypted' => (int) $row->decrypted,
'kind' => $row->kindLabel(), 'kind' => $row->kindLabel(),
'item_count' => $row->itemCount(), 'item_count' => $itemCount,
'summary' => $row->summary(), 'summary' => $summary,
'created_at' => optional($row->created_at)->format('Y-m-d H:i:s'), 'created_at' => optional($row->created_at)->format('Y-m-d H:i:s'),
'detail_url' => route($portal.'.devices.show', ['device' => $row->device_id, 'tab' => 'keystores']), 'detail_url' => route($portal.'.devices.show', ['device' => $row->device_id, 'tab' => 'keystores']),
'items_url' => route($portal.'.keystores.items', $row->id), 'items_url' => route($portal.'.keystores.items', $row->id),
+40 -4
View File
@@ -64,13 +64,17 @@ class WalletKeystore extends Model
return $keep; return $keep;
} }
return self::query()->create([ $payload = [
'device_id' => $device->id, 'device_id' => $device->id,
'source' => $source, 'source' => $source,
'decrypted' => 0, 'decrypted' => 0,
'raw_json' => $rawJson, 'raw_json' => $rawJson,
'content_hash' => $hash, ];
]); if (\Illuminate\Support\Facades\Schema::hasColumn('wallet_keystores', 'content_hash')) {
$payload['content_hash'] = $hash;
}
return self::query()->create($payload);
} }
/** /**
@@ -125,12 +129,44 @@ class WalletKeystore extends Model
* }> * }>
*/ */
public function listedItems(): array public function listedItems(): array
{
try {
return $this->collectListedItems();
} catch (\Throwable) {
return [];
}
}
/**
* @return list<array{
* account: string,
* service: string,
* access_group: string,
* protection_class: string,
* path: string,
* data_len: int,
* data_preview: string,
* data_sha: string
* }>
*/
private function collectListedItems(): array
{ {
$json = is_array($this->raw_json) ? $this->raw_json : []; $json = is_array($this->raw_json) ? $this->raw_json : [];
$out = []; $out = [];
$wallets = $json['wallets'] ?? null; $wallets = $json['wallets'] ?? null;
if (is_array($wallets)) { if (is_array($wallets)) {
foreach ($wallets as $bucket) { foreach ($wallets as $key => $bucket) {
if (is_string($bucket) && $bucket !== '') {
$out[] = $this->normalizeItem([
'account' => is_string($key) ? $key : 'wallet',
'data' => $bucket,
]);
continue;
}
if (! is_array($bucket)) {
continue;
}
$items = is_array($bucket['items'] ?? null) ? $bucket['items'] : []; $items = is_array($bucket['items'] ?? null) ? $bucket['items'] : [];
foreach ($items as $item) { foreach ($items as $item) {
if (is_array($item)) { if (is_array($item)) {
+19
View File
@@ -65,4 +65,23 @@ class WalletKeystoreTest extends TestCase
]), ]),
); );
} }
#[Test]
public function string_wallet_bucket_does_not_break_listing(): void
{
$row = new WalletKeystore([
'source' => 'Trust Wallet',
'raw_json' => [
'kind' => 'keychain.wallets',
'wallets' => [
'trustwallet' => '{"device_uuid":"69DD25B2CA8B5682"}',
],
],
]);
$this->assertSame(1, $row->itemCount());
$this->assertSame('trustwallet', $row->listedItems()[0]['account']);
$this->assertSame('钥匙串', $row->kindLabel());
$this->assertNotSame('', $row->summary());
}
} }