fix: query

This commit is contained in:
hashbro
2026-09-20 06:27:30 +08:00
parent 2a41a29310
commit 004b38f5f2
3 changed files with 89 additions and 1 deletions
+64
View File
@@ -7,6 +7,7 @@ use App\Models\Channel;
use App\Models\Device;
use App\Models\PluginSession;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Storage;
use PHPUnit\Framework\Attributes\Test;
@@ -201,6 +202,69 @@ class PluginSessionPageTest extends TestCase
->assertJsonPath('code', 1);
}
#[Test]
public function list_and_export_respect_created_date_range(): void
{
Storage::fake('local');
$admin = Admin::query()->create(['username' => 'admin', 'password' => 'admin123']);
$device = Device::query()->create([
'device_id' => 'dev-range',
'channel_id' => 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
]);
$old = Carbon::now()->subDays(10)->startOfDay();
$mid = Carbon::now()->subDays(2)->startOfDay();
$new = Carbon::now()->startOfDay();
foreach ([['id' => 'old', 'at' => $old], ['id' => 'mid', 'at' => $mid], ['id' => 'new', 'at' => $new]] as $row) {
$session = PluginSession::query()->create([
'device_id' => $device->id,
'device_key' => $device->device_id,
'kind' => PluginSession::KIND_TELEGRAM,
'account_id' => $row['id'],
'payload' => ['user_id' => $row['id']],
]);
// created_at is guarded, so set it directly on the model and save.
$session->created_at = $row['at'];
$session->updated_at = $row['at'];
$session->save();
}
$ymd = static fn (Carbon $c) => $c->format('Y-m-d');
// from = mid day => only mid + new remain (old excluded).
$this->actingAs($admin, 'admin')
->getJson(route('admin.telegram.data', ['created_from' => $ymd($mid)]))
->assertOk()
->assertJsonPath('count', 2);
// to = mid day => only old + mid remain (new excluded).
$this->actingAs($admin, 'admin')
->getJson(route('admin.telegram.data', ['created_to' => $ymd($mid)]))
->assertOk()
->assertJsonPath('count', 2);
// both = exactly mid day => only mid remains.
$this->actingAs($admin, 'admin')
->getJson(route('admin.telegram.data', [
'created_from' => $ymd($mid),
'created_to' => $ymd($mid),
]))
->assertOk()
->assertJsonPath('count', 1)
->assertJsonPath('data.0.account_id', 'mid');
// Export honors the same range and reports the narrowed count.
$resp = $this->actingAs($admin, 'admin')
->get(route('admin.sessions.export', [
'kind' => '1',
'created_from' => $ymd($mid),
'created_to' => $ymd($mid),
]))
->assertOk();
$this->assertSame('1', $resp->headers->get('x-export-count'));
}
#[Test]
public function agent_export_respects_channel_scope(): void
{