Files
Verde-Web/tests/Feature/Api/V1/LiveTracking/LiveTrackingTest.php

77 lines
2.8 KiB
PHP

<?php
namespace Tests\Feature\Api\V1\LiveTracking;
use App\Models\CollectionTeam;
use App\Models\Truck;
use App\Models\TruckLocationHistory;
use App\Models\User;
use Database\Seeders\RoleSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Laravel\Sanctum\Sanctum;
use MatanYadaev\EloquentSpatial\Objects\Point;
use Tests\TestCase;
class LiveTrackingTest extends TestCase
{
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
$this->seed(RoleSeeder::class);
}
public function test_assigned_driver_can_post_location(): void
{
$driver = User::factory()->create(['role' => User::ROLE_DRIVER, 'status' => 'active']);
$truck = Truck::create(['plate_number' => 'NCR-9999', 'status' => 'active']);
$team = CollectionTeam::create([
'name' => 'T1', 'driver_id' => $driver->id, 'truck_id' => $truck->id, 'status' => 'active',
]);
$truck->forceFill(['assigned_team_id' => $team->id])->save();
Sanctum::actingAs($driver);
$response = $this->postJson("/api/v1/driver/trucks/{$truck->uuid}/location", [
'lat' => 14.65, 'lng' => 121.07, 'speed_kmh' => 22,
]);
$response->assertOk()->assertJsonPath('data.recorded', true);
$this->assertSame(1, TruckLocationHistory::count());
$this->assertNotNull($truck->fresh()->last_known_coordinates);
}
public function test_non_team_driver_blocked(): void
{
$driver = User::factory()->create(['role' => User::ROLE_DRIVER, 'status' => 'active']);
$other = User::factory()->create(['role' => User::ROLE_DRIVER, 'status' => 'active']);
$truck = Truck::create(['plate_number' => 'NCR-1111', 'status' => 'active']);
$team = CollectionTeam::create([
'name' => 'T1', 'driver_id' => $other->id, 'truck_id' => $truck->id, 'status' => 'active',
]);
$truck->forceFill(['assigned_team_id' => $team->id])->save();
Sanctum::actingAs($driver);
$this->postJson("/api/v1/driver/trucks/{$truck->uuid}/location", [
'lat' => 14.65, 'lng' => 121.07,
])->assertStatus(403);
}
public function test_admin_sees_active_truck_positions(): void
{
$admin = User::factory()->create(['role' => User::ROLE_ADMIN, 'status' => 'active']);
$truck = Truck::create([
'plate_number' => 'NCR-2222', 'status' => 'active',
'last_known_coordinates' => new Point(14.65, 121.07, 4326),
'last_location_updated_at' => now(),
]);
Sanctum::actingAs($admin);
$response = $this->getJson('/api/v1/admin/live/trucks');
$response->assertOk();
$this->assertCount(1, $response->json('data.trucks'));
$this->assertSame('NCR-2222', $response->json('data.trucks.0.plate_number'));
}
}