import { describe, it, expect } from 'vitest';
import {
    compare_access_levels,
    process_permission_checks,
    access_level_order
} from './ae_utils__perm_checks';

describe('Permission Hierarchy Tests', () => {
    describe('compare_access_levels', () => {
        it('should correctly identify upgrades', () => {
            // High to Low should return 1 (Upgrade if target is high, current is low)
            expect(compare_access_levels('super', 'manager')).toBe(1);
            expect(compare_access_levels('manager', 'administrator')).toBe(1);
            expect(compare_access_levels('administrator', 'trusted')).toBe(1);
            expect(compare_access_levels('trusted', 'authenticated')).toBe(1);
            expect(compare_access_levels('authenticated', 'anonymous')).toBe(1);
        });

        it('should correctly identify downgrades', () => {
            // Low to High should return -1
            expect(compare_access_levels('manager', 'super')).toBe(-1);
            expect(compare_access_levels('anonymous', 'authenticated')).toBe(
                -1
            );
        });

        it('should return 0 for equal levels', () => {
            expect(compare_access_levels('manager', 'manager')).toBe(0);
            expect(compare_access_levels('anonymous', 'anonymous')).toBe(0);
        });

        it('should handle null/empty as anonymous', () => {
            expect(compare_access_levels('trusted', '')).toBe(1);
            expect(compare_access_levels(null as any, 'authenticated')).toBe(
                -1
            );
        });
    });

    describe('process_permission_checks hierarchy', () => {
        it('manager should imply administrator and trusted', () => {
            const perms = process_permission_checks('manager');
            expect(perms.manager_access).toBe(true);
            expect(perms.administrator_access).toBe(true);
            expect(perms.trusted_access).toBe(true);
            expect(perms.authenticated_access).toBe(true);
            expect(perms.super_access).toBe(false);
        });

        it('administrator should imply trusted', () => {
            const perms = process_permission_checks('administrator');
            expect(perms.administrator_access).toBe(true);
            expect(perms.trusted_access).toBe(true);
            expect(perms.manager_access).toBe(false);
        });

        it('anonymous should imply nothing but anonymous_access', () => {
            const perms = process_permission_checks('anonymous');
            expect(perms.anonymous_access).toBe(true);
            expect(perms.authenticated_access).toBe(false);
            expect(perms.trusted_access).toBe(false);
        });
    });
});
