import { describe, it, expect } from 'vitest';
import { OptimizationAnalyzer } from '../optimization-analyzer.js';
describe('OptimizationAnalyzer', () => {
    it('should detect large uncompressed files', () => {
        const state = {
            version: '1.0.0',
            buildTime: Date.now(),
            assets: {
                'large-file': {
                    id: 'large-file',
                    originalName: 'large-file.js',
                    originalPath: '/path/to/large-file.js',
                    originalSize: 200 * 1024, // 200KB
                    finalSize: 195 * 1024, // 只压缩了 5KB
                    totalCompressionRatio: 0.025, // 2.5% 压缩率
                    type: 'script',
                    processingHistory: [
                        {
                            stage: 'base-build',
                            name: 'large-file.js',
                            size: 195 * 1024,
                            optimizations: [],
                        },
                    ],
                },
            },
            stages: [],
        };
        const analyzer = new OptimizationAnalyzer(state);
        const result = analyzer.analyze();
        expect(result.totalSuggestions).toBeGreaterThan(0);
        expect(result.suggestions.some(s => s.type === 'large-uncompressed')).toBe(true);
    });
    it('should detect PNG images that can be converted to WebP', () => {
        const state = {
            version: '1.0.0',
            buildTime: Date.now(),
            assets: {
                'image': {
                    id: 'image',
                    originalName: 'image.png',
                    originalPath: '/path/to/image.png',
                    originalSize: 100 * 1024, // 100KB
                    finalSize: 100 * 1024,
                    totalCompressionRatio: 0,
                    type: 'texture',
                    processingHistory: [
                        {
                            stage: 'base-build',
                            name: 'image.png',
                            size: 100 * 1024,
                            optimizations: [],
                        },
                    ],
                },
            },
            stages: [],
        };
        const analyzer = new OptimizationAnalyzer(state);
        const result = analyzer.analyze();
        expect(result.suggestions.some(s => s.type === 'format-conversion')).toBe(true);
    });
    it('should detect missing minification', () => {
        const state = {
            version: '1.0.0',
            buildTime: Date.now(),
            assets: {
                'script': {
                    id: 'script',
                    originalName: 'script.js',
                    originalPath: '/path/to/script.js',
                    originalSize: 50 * 1024, // 50KB
                    finalSize: 50 * 1024,
                    totalCompressionRatio: 0,
                    type: 'script',
                    processingHistory: [
                        {
                            stage: 'base-build',
                            name: 'script.js',
                            size: 50 * 1024,
                            optimizations: [], // 没有 minify
                        },
                    ],
                },
            },
            stages: [],
        };
        const analyzer = new OptimizationAnalyzer(state);
        const result = analyzer.analyze();
        expect(result.suggestions.some(s => s.type === 'enable-minify')).toBe(true);
    });
    it('should group suggestions by severity', () => {
        const state = {
            version: '1.0.0',
            buildTime: Date.now(),
            assets: {
                'large-uncompressed': {
                    id: 'large-uncompressed',
                    originalName: 'large.js',
                    originalPath: '/path/to/large.js',
                    originalSize: 200 * 1024,
                    finalSize: 195 * 1024,
                    totalCompressionRatio: 0.025,
                    type: 'script',
                    processingHistory: [
                        {
                            stage: 'base-build',
                            name: 'large.js',
                            size: 195 * 1024,
                            optimizations: [],
                        },
                    ],
                },
                'png-image': {
                    id: 'png-image',
                    originalName: 'image.png',
                    originalPath: '/path/to/image.png',
                    originalSize: 100 * 1024,
                    finalSize: 100 * 1024,
                    totalCompressionRatio: 0,
                    type: 'texture',
                    processingHistory: [
                        {
                            stage: 'base-build',
                            name: 'image.png',
                            size: 100 * 1024,
                            optimizations: [],
                        },
                    ],
                },
            },
            stages: [],
        };
        const analyzer = new OptimizationAnalyzer(state);
        const result = analyzer.analyze();
        expect(result.bySeverity.high.length).toBeGreaterThan(0);
        expect(result.bySeverity.medium.length).toBeGreaterThan(0);
    });
    it('should calculate total estimated savings', () => {
        const state = {
            version: '1.0.0',
            buildTime: Date.now(),
            assets: {
                'large-file': {
                    id: 'large-file',
                    originalName: 'large-file.js',
                    originalPath: '/path/to/large-file.js',
                    originalSize: 200 * 1024,
                    finalSize: 195 * 1024,
                    totalCompressionRatio: 0.025,
                    type: 'script',
                    processingHistory: [
                        {
                            stage: 'base-build',
                            name: 'large-file.js',
                            size: 195 * 1024,
                            optimizations: [],
                        },
                    ],
                },
            },
            stages: [],
        };
        const analyzer = new OptimizationAnalyzer(state);
        const result = analyzer.analyze();
        expect(result.totalEstimatedSavings).toBeGreaterThan(0);
        expect(result.estimatedOptimizationRate).toBeGreaterThan(0);
    });
});
