"use strict";
// SPDX-License-Identifier: AGPL-3.0-or-later
// Copyright (C) 2024-2026 Gracker (Chris)
// This file is part of SmartPerfetto. See LICENSE for details.
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
/**
 * Scrolling Skill Test Runner with Real Frame Data
 *
 * Uses chrome_android_systrace.pftrace which contains actual frame data
 * to verify the complete scrolling analysis functionality.
 */
const skillAnalysisAdapter_1 = require("../services/skillEngine/skillAnalysisAdapter");
const traceProcessorService_1 = require("../services/traceProcessorService");
const htmlReportGenerator_1 = require("../services/htmlReportGenerator");
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
async function runScrollingTestWithRealData() {
    console.log('\n╔════════════════════════════════════════════════════════════════╗');
    console.log('║     Scrolling Skill Test - Real Frame Data                  ║');
    console.log('╚════════════════════════════════════════════════════════════════╝\n');
    // Use Chrome Android trace (43MB) - has actual frame data
    const testTracePath = path_1.default.join(process.cwd(), '../perfetto/test/data/chrome_android_systrace.pftrace');
    if (!fs_1.default.existsSync(testTracePath)) {
        console.error('❌ Test trace not found:', testTracePath);
        process.exit(1);
    }
    console.log('✓ Test trace found:', path_1.default.basename(testTracePath));
    console.log('  Size:', (fs_1.default.statSync(testTracePath).size / 1024 / 1024).toFixed(2), 'MB\n');
    try {
        const traceProcessor = (0, traceProcessorService_1.getTraceProcessorService)();
        const skillAdapter = (0, skillAnalysisAdapter_1.getSkillAnalysisAdapter)(traceProcessor);
        console.log('⏳ Loading trace into TraceProcessor...');
        const traceId = await traceProcessor.loadTraceFromFilePath(testTracePath);
        console.log('✓ Trace loaded. ID:', traceId, '\n');
        console.log('⏳ Executing Scrolling Skill...');
        console.log('─'.repeat(60));
        const startTime = Date.now();
        // Test with empty package to analyze all apps
        const result = await skillAdapter.analyze({
            traceId,
            question: '分析滑动性能',
        });
        const duration = Date.now() - startTime;
        console.log('─'.repeat(60));
        console.log(`✓ Skill execution completed in ${duration}ms\n`);
        // Check if frame data was detected
        const envData = result.layeredResult?.layers?.overview?.detect_environment?.data;
        if (envData && envData.length > 0) {
            const frameStatus = envData[0].frame_data_status;
            const totalFrames = envData[0].total_frames;
            console.log('═══════════════════════════════════════════════════════════');
            console.log('FRAME DATA DETECTION');
            console.log('═══════════════════════════════════════════════════════════');
            console.log(`Frame Data Status: ${frameStatus}`);
            console.log(`Total Frames: ${totalFrames}`);
            if (frameStatus === 'available' && totalFrames > 0) {
                console.log('✅ Frame data detected - analysis should have run successfully\n');
                // Check condition-dependent steps
                const overview = result.layeredResult?.layers?.overview || {};
                const conditionSteps = [
                    'get_jank_frames',
                    'jank_type_stats',
                    'frame_performance_summary',
                    'calculate_fps_by_phase',
                ];
                console.log('Condition-Dependent Steps Execution:');
                let allExecuted = true;
                for (const stepId of conditionSteps) {
                    const step = overview[stepId];
                    const executed = step?.success === true;
                    const status = executed ? '✅ Executed' : '❌ Skipped/Failed';
                    const dataCount = step?.data?.length || 0;
                    console.log(`  ${stepId}: ${status} (${dataCount} rows)`);
                    if (!executed)
                        allExecuted = false;
                }
                if (allExecuted) {
                    console.log('\n✅ ALL CONDITION STEPS EXECUTED - FIX VERIFIED!');
                }
                else {
                    console.log('\n⚠️  Some steps were skipped - check conditions');
                }
            }
            else {
                console.log('⚠️  No frame data found - this trace may not have scrolling events');
            }
        }
        // Generate HTML report
        console.log('\n═══════════════════════════════════════════════════════════');
        console.log('GENERATING REPORTS');
        console.log('═══════════════════════════════════════════════════════════');
        const reportGenerator = (0, htmlReportGenerator_1.getHTMLReportGenerator)();
        const htmlReport = reportGenerator.generateHTML({
            traceId,
            analysisResult: result,
            traceFile: path_1.default.basename(testTracePath),
        });
        const outputPath = path_1.default.join(process.cwd(), 'test-output/scrolling-test-real-data.html');
        fs_1.default.writeFileSync(outputPath, htmlReport);
        console.log('✓ HTML report saved to:', outputPath);
        const jsonPath = path_1.default.join(process.cwd(), 'test-output/scrolling-test-real-data.json');
        fs_1.default.writeFileSync(jsonPath, JSON.stringify(result, null, 2));
        console.log('✓ JSON result saved to:', jsonPath);
        console.log('\n═══════════════════════════════════════════════════════════');
        console.log('TEST COMPLETED');
        console.log('═══════════════════════════════════════════════════════════');
    }
    catch (error) {
        console.error('\n❌ Test failed:', error.message);
        console.error(error.stack);
        process.exit(1);
    }
    finally {
        await (0, traceProcessorService_1.getTraceProcessorService)().cleanup();
    }
}
runScrollingTestWithRealData();
//# sourceMappingURL=runScrollingTestRealData.js.map