/**
 * Unit test for the authentication boot warning.
 * Verifies that a prominent warning is logged when OAUTH_STRATEGY=none.
 */
import { Logger } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { Test } from '@nestjs/testing';
import { AuthenticationBootWarning } from './authentication-boot-warning';

describe('AuthenticationBootWarning', () => {
  let service: AuthenticationBootWarning;
  let configService: { get: jest.Mock };

  afterEach(() => {
    jest.restoreAllMocks();
  });

  describe('when OAUTH_STRATEGY=none', () => {
    beforeEach(async () => {
      configService = {
        get: jest.fn((key: string) => {
          if (key === 'OAUTH_STRATEGY') return 'none';
          return undefined;
        }),
      };

      const module = await Test.createTestingModule({
        providers: [AuthenticationBootWarning, { provide: ConfigService, useValue: configService }],
      }).compile();

      service = module.get(AuthenticationBootWarning);
    });

    it('emits a prominent warn log naming OAUTH_STRATEGY=none on onModuleInit', () => {
      const warnSpy = jest.spyOn(Logger.prototype, 'warn').mockImplementation(() => undefined);

      service.onModuleInit();

      expect(warnSpy).toHaveBeenCalled();
      const [message] = warnSpy.mock.calls[0];
      expect(String(message)).toMatch(/OAUTH_STRATEGY=none/i);
    });

    it('emits a log that mentions anonymous access', () => {
      const warnSpy = jest.spyOn(Logger.prototype, 'warn').mockImplementation(() => undefined);

      service.onModuleInit();

      const [message] = warnSpy.mock.calls[0];
      expect(String(message)).toMatch(/anonymous/i);
    });
  });

  describe('when OAUTH_STRATEGY=aurora-hub', () => {
    beforeEach(async () => {
      configService = {
        get: jest.fn((key: string) => {
          if (key === 'OAUTH_STRATEGY') return 'aurora-hub';
          return undefined;
        }),
      };

      const module = await Test.createTestingModule({
        providers: [AuthenticationBootWarning, { provide: ConfigService, useValue: configService }],
      }).compile();

      service = module.get(AuthenticationBootWarning);
    });

    it('does NOT emit a warn log when mode is aurora-hub', () => {
      const warnSpy = jest.spyOn(Logger.prototype, 'warn').mockImplementation(() => undefined);

      service.onModuleInit();

      expect(warnSpy).not.toHaveBeenCalled();
    });
  });
});
