import { vi, describe, expect, it } from 'vitest';
import { getTotal } from 'src/composables/getTotal';

vi.mock('src/filters', () => ({
    toCurrency: vi.fn((value, currency) => `${currency} ${value.toFixed(2)}`),
}));

describe('getTotal()', () => {
    const rows = [
        { amount: 10.5, tax: 2.1 },
        { amount: 20.75, tax: 3.25 },
        { amount: 30.25, tax: 4.75 },
    ];

    it('should calculate the total for a given key', () => {
        const total = getTotal(rows, 'amount');
        expect(total).toBe('61.50');
    });

    it('should calculate the total with a callback function', () => {
        const total = getTotal(rows, null, { cb: (row) => row.amount + row.tax });
        expect(total).toBe('71.60');
    });

    it('should format the total as currency', () => {
        const total = getTotal(rows, 'amount', { currency: 'USD' });
        expect(total).toBe('USD 61.50');
    });

    it('should format the total as currency with default currency', () => {
        const total = getTotal(rows, 'amount', { currency: 'default' });
        expect(total).toBe('undefined 61.50');
    });

    it('should calculate the total with integer formatting', () => {
        const total = getTotal(rows, 'amount', { decimalPlaces: 0 });
        expect(total).toBe('62');
    });

    it('should calculate the total with custom decimal places', () => {
        const total = getTotal(rows, 'amount', { decimalPlaces: 1 });
        expect(total).toBe('61.5');
    });

    it('should handle rows with missing keys', () => {
        const rowsWithMissingKeys = [{ amount: 10.5 }, { amount: 20.75 }, {}];
        const total = getTotal(rowsWithMissingKeys, 'amount');
        expect(total).toBe('31.25');
    });

    it('should handle empty rows', () => {
        const total = getTotal([], 'amount');
        expect(total).toBe('0.00');
    });
});
