Files
Verde-Web/tests/Feature/Api/V1/LiveTracking/LiveTrackingTest.php
admin 15d56d0e98 feat(backend): finish Module 13 sub-modules + flow fixes
Notifications: notification_preferences + Laravel notifications inbox.
SmsChannel adapter for our SmsService. RoutesByPreferences trait reads
per-user toggles. HouseholdApproved/Rejected, QrBalanceLow, and
CodesPurchased notifications wired in via auto-discovered listeners
or direct dispatch from controllers/StoreOperations.

Payments: payments table + PaymentDriver interface. ManualPaymentDriver
works out of the box; PayMongoDriver activates when
PAYMONGO_SECRET_KEY is set, falls back to manual otherwise. Resident
initiates code-purchase, admin can mark paid manually, webhook applies
real provider events. Fulfillment runs StoreOperations::sellToHousehold.

Live tracking (HTTP polling): truck_location_history (with SPATIAL
INDEX + 7-day retention plan). Driver POST /driver/trucks/{uuid}/location
writes history, updates trucks.last_known_coordinates, caches in Redis,
flags geofence-trigger when entering active trip dumpsite. Admin
GET /admin/live/trucks returns active truck positions. Reverb broadcast
deferred.

Flow corrections:
- QrAllocator now idempotent — re-approving a household no longer
  re-dispenses free codes.
- arrive-dumpsite enforces dumpsite geofence via ST_Contains; can be
  bypassed with override_geofence: true.

171 feature tests passing.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-01 14:01:38 +08:00

76 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 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 \MatanYadaev\EloquentSpatial\Objects\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'));
}
}