import { describe, expect, it, vi } from 'vitest';

import { resolveNavigationUserInfo } from './navigation-user-info';

describe('navigation user info', () => {
  it('uses cached user info without issuing a request', async () => {
    const cachedUserInfo = { id: 1 };
    const fetchUserInfo = vi.fn();

    await expect(
      resolveNavigationUserInfo({
        cachedUserInfo,
        fetchUserInfo,
        getAccessToken: () => 'token',
      }),
    ).resolves.toBe(cachedUserInfo);
    expect(fetchUserInfo).not.toHaveBeenCalled();
  });

  it('returns null after an expired token has been cleared', async () => {
    await expect(
      resolveNavigationUserInfo({
        cachedUserInfo: null,
        fetchUserInfo: () => Promise.reject(new Error('expired')),
        getAccessToken: () => null,
      }),
    ).resolves.toBeNull();
  });

  it('keeps non-authentication failures visible', async () => {
    const error = new Error('network unavailable');
    await expect(
      resolveNavigationUserInfo({
        cachedUserInfo: null,
        fetchUserInfo: () => Promise.reject(error),
        getAccessToken: () => 'token',
      }),
    ).rejects.toBe(error);
  });
});
