import { DepositsService } from './deposits.service';
import { Deposit } from '../entities/deposit.entity';

describe('DepositsService — direct refund', () => {
  it('refunds the full available balance and stores audit fields', async () => {
    const deposit = {
      id: 1,
      amount: 500,
      status: 'paid',
    } as Deposit;
    const repo = {
      findOne: jest.fn().mockResolvedValue(deposit),
      save: jest.fn().mockImplementation(async (value: Deposit) => value),
    };
    const service = new DepositsService(repo as never, {} as never, {} as never);

    const result = await service.refund(
      1,
      { refundDate: '2026-07-13', notes: '退还剩余押金' },
      42,
    );

    expect(result).toMatchObject({
      refundDate: '2026-07-13',
      amount: 0,
      refundAmount: 500,
      notes: '退还剩余押金',
      status: 'refunded',
      refundedBy: 42,
    });
    expect(result.refundedAt).toBeInstanceOf(Date);
    expect(repo.save).toHaveBeenCalledWith(deposit);
  });
});
