"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const test_1 = require("@playwright/test");
const playwright_1 = require("playwright");
const v3_js_1 = require("../../lib/v3/v3.js");
const v3_config_js_1 = require("./v3.config.js");
test_1.test.describe("CDP session detach handling", () => {
    let v3;
    test_1.test.beforeEach(async () => {
        v3 = new v3_js_1.V3(v3_config_js_1.v3TestConfig);
        await v3.init();
    });
    test_1.test.afterEach(async () => {
        await v3?.close?.().catch(() => { });
    });
    (0, test_1.test)("rejects inflight CDP calls when a target is closed", async () => {
        const unhandled = [];
        const onUnhandled = (reason) => {
            unhandled.push(reason);
        };
        process.on("unhandledRejection", onUnhandled);
        let pwBrowser = null;
        try {
            pwBrowser = await playwright_1.chromium.connectOverCDP(v3.connectURL());
            const pwContext = pwBrowser.contexts()[0];
            const pwPage = pwContext.pages()[0];
            const v3Page = v3.context.pages()[0];
            await v3Page.goto("data:text/html,<html><body>cdp</body></html>");
            const pending = v3Page.sendCDP("Runtime.evaluate", {
                expression: "new Promise(r => setTimeout(() => r('done'), 5000))",
                awaitPromise: true,
                returnByValue: true,
            });
            await pwPage.close();
            await (0, test_1.expect)(pending).rejects.toThrow(/CDP session detached/);
            await new Promise((r) => setTimeout(r, 50));
            (0, test_1.expect)(unhandled).toHaveLength(0);
        }
        finally {
            process.off("unhandledRejection", onUnhandled);
            await pwBrowser?.close().catch(() => { });
        }
    });
});
//# sourceMappingURL=cdp-session-detached.spec.js.map