import 'reflect-metadata';
import { PERMISSION_KEY } from '../auth/decorators/permission.decorator';
import { ExpensesController } from './expenses.controller';

describe('ExpensesController purge routes', () => {
  it('requires expense:purge on permanent delete routes', () => {
    expect(
      Reflect.getMetadata(PERMISSION_KEY, ExpensesController.prototype.purgeRoomExpense),
    ).toEqual(['expense:purge']);
    expect(
      Reflect.getMetadata(PERMISSION_KEY, ExpensesController.prototype.batchPurgeRoomExpenses),
    ).toEqual(['expense:purge']);
    expect(
      Reflect.getMetadata(PERMISSION_KEY, ExpensesController.prototype.purgePersonalExpense),
    ).toEqual(['expense:purge']);
    expect(
      Reflect.getMetadata(PERMISSION_KEY, ExpensesController.prototype.batchPurgePersonalExpenses),
    ).toEqual(['expense:purge']);
  });

  it('writes permanent delete audit logs', async () => {
    const service = {
      purgeRoomExpense: jest.fn().mockResolvedValue({ message: '已永久删除宿舍费用（不可恢复）' }),
    };
    const log = jest.fn().mockResolvedValue(undefined);
    const controller = new ExpensesController(service as never, { log } as never);
    const req = { user: { id: 1, username: 'admin' }, ip: '127.0.0.1', headers: {} };
    await controller.purgeRoomExpense(1, req);
    expect(service.purgeRoomExpense).toHaveBeenCalledWith(1);
    expect(log).toHaveBeenCalledWith(
      expect.objectContaining({ module: '费用管理', action: '永久删除宿舍费用', targetId: 1 }),
    );
  });
});
