import { GUARDS_METADATA } from '@nestjs/common/constants';
import { RoleEnum } from '../roles/roles.enum';
import { RolesGuard } from '../roles/roles.guard';
import { RecurringTransactionsController } from './recurring-transactions.controller';

describe('RecurringTransactionsController.runDueRecurringTransactionsNow', () => {
  const service = {
    getTeamRecurringTransactions: jest.fn(),
    createRecurringTransaction: jest.fn(),
    updateRecurringTransaction: jest.fn(),
    deleteRecurringTransaction: jest.fn(),
  };
  const scheduler = { runDueRecurringTransactions: jest.fn() };
  const controller = new RecurringTransactionsController(service as any, scheduler as any);

  beforeEach(() => jest.clearAllMocks());

  it('is guarded by the global admin role', () => {
    expect(
      Reflect.getMetadata(
        'roles',
        RecurringTransactionsController.prototype.runDueRecurringTransactionsNow,
      ),
    ).toEqual([RoleEnum.admin]);
    expect(
      Reflect.getMetadata(
        GUARDS_METADATA,
        RecurringTransactionsController.prototype.runDueRecurringTransactionsNow,
      ),
    ).toContain(RolesGuard);
  });

  it('delegates to the scheduler', async () => {
    await controller.runDueRecurringTransactionsNow();

    expect(scheduler.runDueRecurringTransactions).toHaveBeenCalledTimes(1);
  });
});
