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

import {
  getDevLoginAccount,
  removeDevLoginQuery,
  shouldAttemptDevLogin,
} from '../dev-login';

describe('dev-login route helpers', () => {
  it('reads the admin account from the query string', () => {
    expect(getDevLoginAccount({ devLogin: 'admin' })).toBe('admin');
    expect(getDevLoginAccount({ devLogin: ['admin', 'other'] })).toBe('admin');
    expect(getDevLoginAccount({ devLogin: '   ' })).toBeNull();
  });

  it('removes only the devLogin query parameter', () => {
    expect(
      removeDevLoginQuery({
        devLogin: 'admin',
        redirect: '/ai/workflow',
      }),
    ).toEqual({
      redirect: '/ai/workflow',
    });
  });

  it('attempts dev login only in dev mode and without an existing token', () => {
    expect(
      shouldAttemptDevLogin({
        account: 'admin',
        hasAccessToken: false,
        isDev: true,
      }),
    ).toBe(true);
    expect(
      shouldAttemptDevLogin({
        account: 'admin',
        hasAccessToken: true,
        isDev: true,
      }),
    ).toBe(false);
    expect(
      shouldAttemptDevLogin({
        account: 'guest',
        hasAccessToken: false,
        isDev: true,
      }),
    ).toBe(false);
    expect(
      shouldAttemptDevLogin({
        account: 'admin',
        hasAccessToken: false,
        isDev: false,
      }),
    ).toBe(false);
  });
});
