"use strict";
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
const chai_1 = require("chai");
const path = __importStar(require("node:path"));
const events_js_1 = require("../../conductor/events.js");
const paths_js_1 = require("../../conductor/paths.js");
const performance_helpers_js_1 = require("../helpers/performance-helpers.js");
const frontend_helper_js_1 = require("../shared/frontend-helper.js");
async function loadEnhancedTrace(devToolsPage, inspectedPage, resource) {
    // Scripts in these traces happen to fail asserts in formatter_worker.
    (0, events_js_1.expectError)(/ScopeParser\.js|formatter_worker\.js/);
    await (0, performance_helpers_js_1.navigateToPerformanceTab)(undefined, devToolsPage, inspectedPage);
    const uploadProfileHandle = await devToolsPage.waitFor('input[type=file]');
    const tracePath = path.join(paths_js_1.GEN_DIR, 'test/e2e/resources', resource);
    // Open the popup and wait for the target to be created.
    const [newTarget] = await Promise.all([
        devToolsPage.page.browserContext().waitForTarget(target => target.url().includes('trace_app.html')),
        uploadProfileHandle.uploadFile(tracePath),
    ]);
    const newPage = await newTarget.page();
    if (!newPage) {
        throw new Error('Failed to get page from trace_app target');
    }
    // Wrap the new page in DevToolsPage.
    const tracePage = new frontend_helper_js_1.DevToolsPage(newPage);
    // Wait for Performance panel to open in the popup.
    // This creates a race condition where the event for
    // `traceload` has already happened so check the panel as well.
    await (0, performance_helpers_js_1.loadTraceAndWaitToFullyRender)(tracePage, async () => { }, true);
    // Performance and Sources.
    chai_1.assert.lengthOf(await tracePage.$$('.tabbed-pane-header[aria-label="Main toolbar"] .tabbed-pane-header-tab'), 2);
    return tracePage;
}
async function searchAndClickOnStackTrace(devToolsPage, searchTerm, expectedHighlightedContent, expectedSourceLocation) {
    // This is a function on a firebase script that has sourcemaps.
    await (0, performance_helpers_js_1.searchForComponent)(searchTerm, devToolsPage);
    await devToolsPage.raf();
    await devToolsPage.timeout(3000);
    // Prefer stack trace if present.
    const containerEl = await devToolsPage.$('.timeline-details-stack-values') ?? await devToolsPage.$('.timeline-details-view');
    chai_1.assert.isOk(containerEl);
    const topStackFrameLink = await devToolsPage.$('.devtools-link', containerEl);
    chai_1.assert.isOk(topStackFrameLink);
    chai_1.assert.strictEqual(await topStackFrameLink.evaluate(el => el.textContent), expectedSourceLocation);
    await devToolsPage.click('.devtools-link', { root: containerEl });
    // Now we're in the Sources panel.
    await devToolsPage.waitFor('.cm-content');
    await devToolsPage.waitFor('.cm-highlightedLine');
    chai_1.assert.include(await devToolsPage.getTextContent('.cm-highlightedLine'), expectedHighlightedContent);
}
describe('trace_app.html', function () {
    setup({ dockingMode: 'undocked' });
    if (this.timeout() > 0) {
        this.timeout(60000);
    }
    it('linkifies source mapped function calls', async ({ devToolsPage, inspectedPage }) => {
        const framePage = await loadEnhancedTrace(devToolsPage, inspectedPage, 'performance/timeline/enhanced-trace.json.gz');
        // This is a function on a firebase script that has sourcemaps.
        await searchAndClickOnStackTrace(framePage, 'createUserTimingTrace', 'createUserTimingTrace', 'oob_resources_service.ts:83:10');
    });
    it('linkifies function calls from inline scripts in HTML', async ({ devToolsPage, inspectedPage }) => {
        const framePage = await loadEnhancedTrace(devToolsPage, inspectedPage, 'performance/timeline/enhanced-trace.json.gz');
        // This is a function from an inline script in the HTML. Please excuse the Paul humor.
        await searchAndClickOnStackTrace(framePage, 'pooopInTheTrace', 'pooopInTheTrace', '(index):399:26');
    });
    it('linkifies to CSS resources', async ({ devToolsPage, inspectedPage }) => {
        const framePage = await loadEnhancedTrace(devToolsPage, inspectedPage, 'performance/timeline/enhanced-trace.json.gz');
        await searchAndClickOnStackTrace(framePage, 'fonts.googleapis.com', '/* latin */', 'css?family=PT+Serif:regular,italic,bold|PT+Sans:regular,italic,bold|Droid+Sans:400,700|Lato:700,900');
    });
});
//# sourceMappingURL=trace-app.test.js.map