<?php

namespace Tests\Feature;

use App\Master\Http\Resources\ApiUserView;
use App\Master\Models\ApiUser;
use Database\Seeders\Global\ApiUserRouteSeeder;
use Tests\DatabaseTestCase;

class ApiUserAvailableRoutesTest extends DatabaseTestCase
{
    public function testAdminEditPageOffersExactlyTheRoutesTheSeederGrants(): void
    {
        $seededRoutes = (new ApiUserRouteSeeder())
            ->getApiAuthenticatedRouteNames()
            ->sort()
            ->values();

        $apiUser = ApiUser::factory()->create();
        $view = (new ApiUserView($apiUser))->toArray(request());

        $offeredRoutes = collect($view['available_routes'])
            ->pluck('name')
            ->sort()
            ->values();

        // Guards against the list silently resolving to nothing (e.g. a broken
        // middleware lookup), which would make the equality assertion pass vacuously.
        $this->assertNotEmpty($seededRoutes, 'There should be at least one API authenticated route');

        $this->assertEquals(
            $seededRoutes->all(),
            $offeredRoutes->all(),
            'The admin edit page must offer exactly the routes the seeder grants'
        );
    }
}
