"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 });
/**
 * Test Deep Layer Fix - Verify L4 frame details rendering
 */
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 testDeepLayerFix() {
    console.log('\n╔════════════════════════════════════════════════════════════════╗');
    console.log('║     Deep Layer Fix Verification Test                         ║');
    console.log('╚════════════════════════════════════════════════════════════════╝\n');
    // Use the scrolling test trace with actual jank data
    let testTracePath = path_1.default.join(process.cwd(), '../test-traces/scroll-demo-customer-scroll.pftrace');
    if (!fs_1.default.existsSync(testTracePath)) {
        // Fallback to the lighter trace
        const fallbackPath = path_1.default.join(process.cwd(), '../test-traces/scroll_Standard-AOSP-App-Without-PreAnimation.pftrace');
        if (fs_1.default.existsSync(fallbackPath)) {
            testTracePath = fallbackPath;
            console.log('Using fallback trace:', path_1.default.basename(fallbackPath));
        }
        else {
            console.error('❌ No test trace found');
            process.exit(1);
        }
    }
    console.log('✓ Test trace:', 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...');
        const traceId = await traceProcessor.loadTraceFromFilePath(testTracePath);
        console.log('✓ Trace loaded. ID:', traceId, '\n');
        console.log('⏳ Executing Scrolling Analysis...');
        const startTime = Date.now();
        const result = await skillAdapter.analyze({
            traceId,
            question: '分析滑动性能',
        });
        const duration = Date.now() - startTime;
        console.log(`✓ Analysis completed in ${duration}ms\n`);
        // Check deep layer data
        console.log('═══════════════════════════════════════════════════════════');
        console.log('DEEP LAYER DATA CHECK');
        console.log('═══════════════════════════════════════════════════════════');
        const deepLayer = result.layeredResult?.layers?.deep;
        if (deepLayer && Object.keys(deepLayer).length > 0) {
            console.log(`✅ Deep layer has ${Object.keys(deepLayer).length} sessions`);
            // Check first session's frames
            const firstSessionId = Object.keys(deepLayer)[0];
            const frames = deepLayer[firstSessionId];
            console.log(`   First session (${firstSessionId}) has ${Object.keys(frames).length} frames`);
            // Check frame detail data structure
            const firstFrameId = Object.keys(frames)[0];
            const firstFrame = frames[firstFrameId];
            console.log(`\n   Sample frame (${firstFrameId}):`);
            console.log(`     - Has diagnosis_summary: ${!!firstFrame.data?.diagnosis_summary}`);
            console.log(`     - Has full_analysis: ${!!firstFrame.data?.full_analysis}`);
            if (firstFrame.data?.full_analysis) {
                const analysis = firstFrame.data.full_analysis;
                console.log(`     - quadrants.main_thread: ${JSON.stringify(analysis.quadrants?.main_thread || {})}`);
                console.log(`     - binder_calls count: ${analysis.binder_calls?.length || 0}`);
                console.log(`     - main_thread_slices count: ${analysis.main_thread_slices?.length || 0}`);
                console.log(`     - cpu_frequency: ${JSON.stringify(analysis.cpu_frequency || {})}`);
                // Verify field mapping fix
                if (analysis.main_thread_slices?.length > 0) {
                    const slice = analysis.main_thread_slices[0];
                    console.log(`\n   Field mapping verification (main_thread_slices):`);
                    console.log(`     - Has 'total_ms' (expected): ${slice.total_ms !== undefined}`);
                    console.log(`     - Has 'dur_ms' (old): ${slice.dur_ms !== undefined}`);
                    if (slice.total_ms !== undefined) {
                        console.log('   ✅ Field mapping fix is working!');
                    }
                    else if (slice.dur_ms !== undefined) {
                        console.log('   ❌ Field mapping not applied - still using old field name');
                    }
                }
            }
        }
        else {
            console.log('⚠️ No deep layer data found');
        }
        // Generate HTML report
        console.log('\n═══════════════════════════════════════════════════════════');
        console.log('GENERATING HTML REPORT');
        console.log('═══════════════════════════════════════════════════════════');
        const reportGenerator = (0, htmlReportGenerator_1.getHTMLReportGenerator)();
        const htmlReport = reportGenerator.generateHTML({
            sessionId: 'test-session',
            traceId,
            question: '分析滑动性能',
            answer: result.summary || '分析完成',
            metrics: {
                totalDuration: result.executionTimeMs || 0,
                iterationsCount: 1,
                sqlQueriesCount: Object.keys(result.sections || {}).length,
            },
            collectedResults: [],
            skillEngineResult: {
                skillId: result.skillId,
                skillName: result.skillName,
                sections: result.sections || {},
                diagnostics: result.diagnostics || [],
                layeredResult: result.layeredResult,
            },
            timestamp: Date.now(),
        });
        const outputPath = path_1.default.join(process.cwd(), 'test-output/deep-layer-fix-test.html');
        fs_1.default.writeFileSync(outputPath, htmlReport);
        console.log('✓ HTML report saved to:', outputPath);
        const jsonPath = path_1.default.join(process.cwd(), 'test-output/deep-layer-fix-test.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 - Please check the HTML report');
        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();
    }
}
testDeepLayerFix();
//# sourceMappingURL=testDeepLayerFix.js.map