Files
Verde-Web/tests/Feature/Api/V1/LiveTracking/AutoGeofenceTest.php
admin 58a7d3680a feat(backend): hardening sprint — docs, schedulers, validation, email + pickup
1. API docs via dedoc/scramble at /docs/api (scoped to api/v1).
   Linked from the admin sidebar Settings group.

2. Scheduled commands registered in routes/console.php:
   - reports:aggregate (02:00) — daily/weekly/monthly aggregations
   - qr:expire (02:30) — flips past-due allocated/active codes to expired
   - trucks:prune-locations (03:00) — drops history older than retention
     window (default 7 days, config('verde.location_retention_days'))
   All idempotent + withoutOverlapping. --dry flags on qr:expire and
   trucks:prune-locations for safe inspection.

3. Trip double-booking validation: AdminTripController::store rejects
   new trips when the team or truck already has a non-cancelled trip on
   the same date. override_conflicts: true bypasses for emergencies.
   Cancelled trips don't block rebooking.

4a. Email verification: User implements MustVerifyEmail.
    VerifyEmailNotification overrides verificationUrl() for our
    namespaced route. Register sends the link automatically (best
    effort, won't block signup). POST /auth/email/resend (auth) +
    GET /auth/email/verify/{id}/{hash} (signed URL).

4b. Password change while logged in: POST /me/password validates
    current_password, requires the new password to differ, revokes
    every other active token on success — current session stays.

5a. PickupImminent notification: when TripStop -> arrived,
    TripExecutor::notifyAssignedHouseholds() finds households whose
    assigned_drop_off_point_id matches and sends DB + SMS.

5b. Auto-geofence on truck location: TruckTracker::record() now
    auto-fires TripExecutor::arriveAtDumpsite() when an in-progress
    trip's truck pings inside its dumpsite boundary. The executor's
    status guard prevents duplicate timeline events if the driver also
    presses arrive-dumpsite manually.

190 feature tests passing.

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

62 lines
2.4 KiB
PHP

<?php
namespace Tests\Feature\Api\V1\LiveTracking;
use App\Models\CollectionTeam;
use App\Models\Dumpsite;
use App\Models\Route;
use App\Models\Trip;
use App\Models\Truck;
use App\Models\User;
use Database\Seeders\RoleSeeder;
use Database\Seeders\SampleDumpsitesSeeder;
use Database\Seeders\SampleDropOffPointsSeeder;
use Database\Seeders\SamplePsgcSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;
class AutoGeofenceTest extends TestCase
{
use RefreshDatabase;
public function test_truck_entering_dumpsite_auto_marks_arrived(): void
{
$this->seed([RoleSeeder::class, SamplePsgcSeeder::class, SampleDropOffPointsSeeder::class, SampleDumpsitesSeeder::class]);
$driver = User::factory()->create(['role' => User::ROLE_DRIVER, 'status' => 'active']);
$truck = Truck::create(['plate_number' => 'NCR-GF', 'status' => 'active']);
$team = CollectionTeam::create([
'name' => 'T-GF', 'driver_id' => $driver->id, 'truck_id' => $truck->id, 'status' => 'active',
]);
$truck->forceFill(['assigned_team_id' => $team->id])->save();
$dumpsite = Dumpsite::where('code', 'DS-PAYATAS-01')->firstOrFail();
$route = Route::create(['name' => 'R', 'code' => 'RT-GF', 'default_dumpsite_id' => $dumpsite->id, 'status' => 'active']);
$trip = Trip::create([
'trip_number' => 'TRIP-GF', 'route_id' => $route->id, 'team_id' => $team->id,
'dumpsite_id' => $dumpsite->id, 'scheduled_date' => now()->toDateString(),
'status' => 'in_progress', 'actual_start_time' => now(),
]);
Sanctum::actingAs($driver);
// First ping — outside the boundary
$this->postJson("/api/v1/driver/trucks/{$truck->uuid}/location", [
'lat' => 14.5, 'lng' => 121.0,
'trip_id' => $trip->uuid,
])->assertOk()->assertJsonPath('data.geofence_triggered', false);
$this->assertSame('in_progress', $trip->fresh()->status);
// Second ping — inside the seeded Payatas boundary
$this->postJson("/api/v1/driver/trucks/{$truck->uuid}/location", [
'lat' => 14.7155, 'lng' => 121.1083,
'trip_id' => $trip->uuid,
])->assertOk()->assertJsonPath('data.geofence_triggered', true);
$this->assertSame('at_dumpsite', $trip->fresh()->status);
$this->assertNotNull($trip->fresh()->dumpsite_arrival_time);
}
}