<?php

use objects\subuser_grants_o;

it('keeps default subuser grant permissions JSON encodable', function (): void {
    $encoded = json_encode(subuser_grants_o::defaultPermissions, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);

    expect($encoded)->toBeString();
    expect(json_decode($encoded, true))
        ->toContain('VEHICLES_LIST')
        ->toContain('SELFSERVE_ADD')
        ->toContain('BOOKINGS_LIST');
});

it('normalizes stored subuser grant permission payloads', function (): void {
    expect(subuser_grants_o::normalizePermissionsValue(0))->toBe([]);
    expect(subuser_grants_o::normalizePermissionsValue('0'))->toBe([]);
    expect(subuser_grants_o::normalizePermissionsValue('["vehicles_list","BOOKINGS_ADD"]'))
        ->toBe(['VEHICLES_LIST', 'BOOKINGS_ADD']);
    expect(subuser_grants_o::normalizePermissionsValue([
        'VEHICLES_LIST' => true,
        'BOOKINGS_DELETE' => false,
        'UNKNOWN_PERMISSION' => true,
    ]))->toBe(['VEHICLES_LIST']);
});
