/**
 * Tests for components/fallback/formatting.js
 *
 * Tests the fallback formatting utilities that provide basic
 * implementations when the main formatting module is unavailable.
 */

// Ensure main formatting is NOT loaded (so fallback activates)
// We need ResearchStates for formatStatus
window.RESEARCH_STATUS = {
    IN_PROGRESS: 'in_progress',
    COMPLETED: 'completed',
    FAILED: 'failed',
    SUSPENDED: 'suspended',
    CANCELLED: 'cancelled',
    QUEUED: 'queued',
    PENDING: 'pending',
    ERROR: 'error',
};
window.RESEARCH_TERMINAL_STATES = new Set([
    'completed', 'suspended', 'failed', 'error', 'cancelled',
]);

let fallbackFormatting;

beforeAll(async () => {
    // Load ResearchStates
    await import('@js/config/constants.js');

    // Delete any existing formatting to force fallback to activate
    delete window.formatting;

    // Load fallback
    await import('@js/components/fallback/formatting.js');
    fallbackFormatting = window.formatting;
});

describe('fallback formatting', () => {
    describe('formatDate', () => {
        it('returns N/A for falsy input', () => {
            expect(fallbackFormatting.formatDate(null)).toBe('N/A');
            expect(fallbackFormatting.formatDate('')).toBe('N/A');
            expect(fallbackFormatting.formatDate(undefined)).toBe('N/A');
        });

        it('formats a valid ISO date', () => {
            const result = fallbackFormatting.formatDate('2025-06-15T10:30:00Z');
            expect(result).toContain('2025');
            expect(result).toContain('15');
        });

        it('returns original string for invalid date', () => {
            const result = fallbackFormatting.formatDate('not-a-date');
            // The function returns the original string on error
            expect(result).toBeDefined();
        });
    });

    describe('formatMode', () => {
        it('maps known modes', () => {
            expect(fallbackFormatting.formatMode('quick')).toBe('Quick Summary');
            expect(fallbackFormatting.formatMode('detailed')).toBe('Detailed Report');
            expect(fallbackFormatting.formatMode('standard')).toBe('Standard Research');
            expect(fallbackFormatting.formatMode('advanced')).toBe('Advanced Research');
        });

        it('returns Unknown for falsy input', () => {
            expect(fallbackFormatting.formatMode(null)).toBe('Unknown');
            expect(fallbackFormatting.formatMode('')).toBe('Unknown');
        });

        it('returns raw value for unknown modes', () => {
            expect(fallbackFormatting.formatMode('custom')).toBe('custom');
        });
    });

    describe('formatDuration', () => {
        it('returns N/A for falsy input (except 0)', () => {
            expect(fallbackFormatting.formatDuration(null)).toBe('N/A');
            expect(fallbackFormatting.formatDuration(undefined)).toBe('N/A');
        });

        it('formats seconds only when < 60', () => {
            expect(fallbackFormatting.formatDuration(45)).toBe('45s');
        });

        it('formats minutes and seconds', () => {
            expect(fallbackFormatting.formatDuration(125)).toBe('2m 5s');
        });

        it('handles zero seconds', () => {
            expect(fallbackFormatting.formatDuration(0)).toBe('0s');
        });
    });

    describe('formatFileSize', () => {
        it('returns N/A for falsy input (except 0)', () => {
            expect(fallbackFormatting.formatFileSize(null)).toBe('N/A');
            expect(fallbackFormatting.formatFileSize(undefined)).toBe('N/A');
        });

        it('formats bytes', () => {
            expect(fallbackFormatting.formatFileSize(500)).toBe('500.0 B');
        });

        it('formats kilobytes', () => {
            expect(fallbackFormatting.formatFileSize(1024)).toBe('1.0 KB');
        });

        it('formats megabytes', () => {
            expect(fallbackFormatting.formatFileSize(1048576)).toBe('1.0 MB');
        });

        it('formats gigabytes', () => {
            expect(fallbackFormatting.formatFileSize(1073741824)).toBe('1.0 GB');
        });

        it('handles zero bytes', () => {
            expect(fallbackFormatting.formatFileSize(0)).toBe('0.0 B');
        });

        it('formats with decimal precision', () => {
            expect(fallbackFormatting.formatFileSize(1536)).toBe('1.5 KB');
        });
    });

    describe('formatStatus', () => {
        it('delegates to ResearchStates.formatStatus', () => {
            expect(fallbackFormatting.formatStatus('completed')).toBe('Completed');
            expect(fallbackFormatting.formatStatus('in_progress')).toBe('In Progress');
        });
    });
});
