import { describe, expect, it } from 'vitest';
import {
  findFirstAccessiblePath,
  getRequiredPermission,
  canAccessPath,
} from './permission-navigation';

describe('permission navigation', () => {
  it('does not default to dashboard when dashboard permission is absent', () => {
    const permissions = ['class:view', 'schedule:view'];
    expect(findFirstAccessiblePath(permissions)).toBe('/classes');
  });

  it('lands teachers on the teacher workspace without global student or class access', () => {
    expect(
      findFirstAccessiblePath(['teacher-workspace:view', 'schedule:view', 'attendance:view']),
    ).toBe('/teacher-workspace');
    expect(canAccessPath('/students', ['teacher-workspace:view'])).toBe(false);
    expect(canAccessPath('/classes', ['teacher-workspace:view'])).toBe(false);
  });

  it('uses dashboard when it is the first accessible page', () => {
    expect(findFirstAccessiblePath(['dashboard:view', 'student:view'])).toBe('/dashboard');
  });

  it('returns null when the user has no page permissions', () => {
    expect(findFirstAccessiblePath(['profile:view'])).toBeNull();
  });

  it('keeps route permission lookup aligned for nested detail routes', () => {
    expect(getRequiredPermission('/classes/12')).toBe('class:view');
    expect(getRequiredPermission('/students/8/profile')).toBe('student:view');
    expect(getRequiredPermission('/exams/8')).toBe('exam:view');
    expect(canAccessPath('/ai-config', ['ai:config:read'])).toBe(true);
    expect(canAccessPath('/ai-config', ['integration:read'])).toBe(false);
  });
});
