import { ARSession, ARSessionState } from '../src/core/ARSession';
import { Camera } from 'expo-camera';

describe('ARSession', () => {
  beforeEach(() => {
    jest.clearAllMocks();
  });

  it('initializes successfully if camera permission is granted', async () => {
    (Camera.requestCameraPermissionsAsync as jest.Mock).mockResolvedValue({ status: 'granted' });
    const session = new ARSession({ cameraType: 'back' });
    const initializedHandler = jest.fn();
    session.on('initialized', initializedHandler);
    await session.initialize();
    expect(initializedHandler).toHaveBeenCalled();
    expect(session.getState()).toBe(ARSessionState.INITIALIZED);
  });

  it('emits error if camera permission is denied', async () => {
    (Camera.requestCameraPermissionsAsync as jest.Mock).mockResolvedValue({ status: 'denied' });
    const session = new ARSession();
    const errorHandler = jest.fn();
    session.on('error', errorHandler);
    await expect(session.initialize()).rejects.toThrow('Camera permission not granted');
    expect(errorHandler).toHaveBeenCalled();
    expect(session.getState()).not.toBe(ARSessionState.INITIALIZED);
  });

  it('starts and stops the AR session correctly', async () => {
    (Camera.requestCameraPermissionsAsync as jest.Mock).mockResolvedValue({ status: 'granted' });
    const session = new ARSession();
    await session.initialize();
    session.start();
    expect(session.getState()).toBe(ARSessionState.RUNNING);
    session.pause();
    expect(session.getState()).toBe(ARSessionState.PAUSED);
    session.resume();
    expect(session.getState()).toBe(ARSessionState.RUNNING);
    session.stop();
    expect(session.getState()).toBe(ARSessionState.STOPPED);
  });
});