Skip to content

Lightpanda: the headless browser designed for AI and automation

47.1 KB
Raw Blame
// Copyright (C) 2023-2026  Lightpanda (Selecy SAS)
//
// Francis Bouvier <[email protected]>
// Pierre Tachoire <[email protected]>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

const std = @import("std");
const lp = @import("lightpanda");

const CDP = @import("../CDP.zig");
const Base64Body = @import("network.zig").Base64Body;

const Robots = @import("../../../network/Robots.zig");
const DOMNode = @import("../../../browser/webapi/Node.zig");
const Selector = @import("../../../browser/webapi/selector/Selector.zig");
const NodeRegistry = @import("../../../NodeRegistry.zig");

const markdown = lp.markdown;
const SemanticTree = lp.SemanticTree;
const interactive = lp.interactive;
const structured_data = lp.structured_data;

pub fn processMessage(cmd: *CDP.Command) !void {
    const action = std.meta.stringToEnum(enum {
        dump,
        getMarkdown,
        getSemanticTree,
        getInteractiveElements,
        getNodeDetails,
        getStructuredData,
        getContentSignal,
        getPageStatus,
        detectForms,
        clickNode,
        fillNode,
        scrollNode,
        waitForSelector,
        handleJavaScriptDialog,
        configureCDP,
        configureLoading,
        version,
    }, cmd.input.action) orelse return error.UnknownMethod;

    switch (action) {
        .dump => return dump(cmd),
        .getMarkdown => return getMarkdown(cmd),
        .getSemanticTree => return getSemanticTree(cmd),
        .getInteractiveElements => return getInteractiveElements(cmd),
        .getNodeDetails => return getNodeDetails(cmd),
        .getStructuredData => return getStructuredData(cmd),
        .getContentSignal => return getContentSignal(cmd),
        .getPageStatus => return getPageStatus(cmd),
        .detectForms => return detectForms(cmd),
        .clickNode => return clickNode(cmd),
        .fillNode => return fillNode(cmd),
        .scrollNode => return scrollNode(cmd),
        .waitForSelector => return waitForSelector(cmd),
        .handleJavaScriptDialog => return handleJavaScriptDialog(cmd),
        .configureCDP => return configureCDP(cmd),
        .configureLoading => return configureLoading(cmd),
        .version => return version(cmd),
    }
}

pub fn downloadData(bc: *CDP.BrowserContext, msg: *const lp.Notification.DownloadData) !void {
    return bc.cdp.sendEvent("LP.downloadData", .{
        .guid = msg.guid,
        .data = Base64Body{ .data = msg.data },
    }, .{ .session_id = bc.download_session_id });
}

fn version(cmd: *CDP.Command) !void {
    return cmd.sendResult(.{
        .version = lp.build_config.version,
    }, .{});
}

fn configureCDP(cmd: *CDP.Command) !void {
    const params = (try cmd.params(struct {
        disableSetCacheDisabled: ?bool = null,
        obeyRobots: ?bool = null,
        httpVersion: ?lp.Config.HttpVersion = null,
    })) orelse return error.InvalidParams;

    if (params.disableSetCacheDisabled) |value| {
        cmd.cdp.disable_set_cache_disabled = value;
    }
    if (params.obeyRobots) |value| {
        try cmd.cdp.browser.http_client.obeyRobots(value);
    }
    if (params.httpVersion) |value| {
        cmd.cdp.browser.http_client.setHttpVersion(value);
    }

    return cmd.sendResult(null, .{});
}

fn configureLoading(cmd: *CDP.Command) !void {
    // Each field is optional so callers can flip one without resetting
    // the other to its default. Backwards-compatible: existing callers
    // that pass only `{ subFrame: bool }` are unchanged.
    const params = (try cmd.params(struct {
        subFrame: ?bool = null,
        worker: ?bool = null,
        externalStylesheets: ?bool = null,
        images: ?bool = null,
    })) orelse return error.InvalidParams;

    const bc = cmd.browser_context orelse return error.NoBrowserContext;
    if (params.images) |v| bc.session.load_resources.image = v;
    if (params.worker) |v| bc.session.load_resources.worker = v;
    if (params.subFrame) |v| bc.session.load_resources.iframe = v;
    if (params.externalStylesheets) |v| bc.session.load_resources.stylesheet = v;
    return cmd.sendResult(null, .{});
}

fn getSemanticTree(cmd: anytype) !void {
    const Params = struct {
        format: ?enum { text } = null,
        prune: ?bool = null,
        interactiveOnly: ?bool = null,
        backendNodeId: ?NodeRegistry.Id = null,
        maxDepth: ?u32 = null,
    };
    const params = (try cmd.params(Params)) orelse Params{};

    const bc = cmd.browser_context orelse return error.NoBrowserContext;
    const root = bc.mainFrame() orelse return error.FrameNotLoaded;

    const dom_node = if (params.backendNodeId) |nodeId|
        (bc.node_registry.lookup_by_id.get(nodeId) orelse return error.InvalidNodeId).dom
    else
        root.document.asNode();

    const st = SemanticTree.init(cmd.arena, dom_node, &bc.node_registry, root, .{
        .prune = params.prune orelse true,
        .interactive_only = params.interactiveOnly orelse false,
        .max_depth = params.maxDepth orelse std.math.maxInt(u32) - 1,
    }) catch return error.InvalidNodeId;

    if (params.format) |format| {
        if (format == .text) {
            var aw: std.Io.Writer.Allocating = .init(cmd.arena);
            defer aw.deinit();
            try st.textStringify(&aw.writer);

            return cmd.sendResult(.{
                .semanticTree = aw.written(),
            }, .{});
        }
    }

    return cmd.sendResult(.{
        .semanticTree = st,
    }, .{});
}

fn dump(cmd: anytype) !void {
    const Params = struct {
        format: enum { html, markdown, png, pdf },
        strip: lp.dump.Opts.Strip = .{},
        selector: ?[]const u8 = null,
        backendNodeId: ?NodeRegistry.Id = null,
        maxBytes: ?u32 = null,
    };
    const params = (try cmd.params(Params)) orelse return error.InvalidParams;

    const bc = cmd.browser_context orelse return error.NoBrowserContext;
    const frame = bc.mainFrame() orelse return error.FrameNotLoaded;

    const target = blk: {
        if (params.backendNodeId) |id| {
            break :blk (bc.node_registry.lookup_by_id.get(id) orelse return error.InvalidNodeId).dom;
        }
        if (params.selector) |selector| {
            const el = Selector.querySelector(frame.document.asNode(), selector, frame) catch return error.InvalidParams;
            break :blk (el orelse return error.InvalidParams).asNode();
        }
        break :blk frame.document.asNode();
    };

    const state = try lp.RenderTree.resolve(cmd.arena, target, params.strip, frame);

    switch (params.format) {
        .html, .markdown => {
            var aw: std.Io.Writer.Allocating = .init(cmd.arena);
            defer aw.deinit();
            if (params.format == .markdown) {
                try markdown.dump(state, .{ .max_bytes = params.maxBytes }, &aw.writer, frame);
            } else {
                try lp.dump.render(state, .{ .max_bytes = params.maxBytes }, &aw.writer, frame);
            }
            return cmd.sendResult(.{ .format = params.format, .content = aw.written() }, .{});
        },
        .png => {
            if (params.maxBytes != null) {
                return error.InvalidParams;
            }
            const opts: lp.screenshot.Opts = .fromViewport(cmd.cdp.browser.getViewport(), true);
            const prepared = try lp.screenshot.preparePng(cmd.arena, state, opts, frame);
            return cmd.sendResult(.{ .format = params.format, .content = prepared }, .{});
        },
        .pdf => {
            if (params.maxBytes != null) {
                return error.InvalidParams;
            }
            const prepared = try lp.pdf.prepare(cmd.arena, state, .{}, frame);
            return cmd.sendResult(.{ .format = params.format, .content = prepared }, .{});
        },
    }
}

// Deprecated: LP.dump with format "markdown". Kept for existing callers.
fn getMarkdown(cmd: anytype) !void {
    const Params = struct {
        nodeId: ?NodeRegistry.Id = null,
    };
    const params = (try cmd.params(Params)) orelse Params{};

    const bc = cmd.browser_context orelse return error.NoBrowserContext;
    const frame = bc.mainFrame() orelse return error.FrameNotLoaded;

    const dom_node = if (params.nodeId) |nodeId|
        (bc.node_registry.lookup_by_id.get(nodeId) orelse return error.InvalidNodeId).dom
    else
        frame.document.asNode();

    var aw: std.Io.Writer.Allocating = .init(cmd.arena);
    defer aw.deinit();
    try markdown.dump(.{ .root = dom_node }, .{}, &aw.writer, frame);

    return cmd.sendResult(.{
        .markdown = aw.written(),
    }, .{});
}

fn getInteractiveElements(cmd: anytype) !void {
    const Params = struct {
        nodeId: ?NodeRegistry.Id = null,
    };
    const params = (try cmd.params(Params)) orelse Params{};

    const bc = cmd.browser_context orelse return error.NoBrowserContext;
    const main_frame = bc.mainFrame() orelse return error.FrameNotLoaded;

    const root = if (params.nodeId) |nodeId|
        (bc.node_registry.lookup_by_id.get(nodeId) orelse return error.InvalidNodeId).dom
    else
        main_frame.document.asNode();
    const frame = root.ownerFrame(main_frame) orelse return error.InvalidNodeId;

    const elements = try interactive.collectInteractiveElements(root, cmd.arena, frame);
    try interactive.registerNodes(elements, &bc.node_registry);

    return cmd.sendResult(.{
        .elements = elements,
    }, .{});
}

fn getNodeDetails(cmd: anytype) !void {
    const Params = struct {
        backendNodeId: NodeRegistry.Id,
    };
    const params = (try cmd.params(Params)) orelse return error.InvalidParam;

    const bc = cmd.browser_context orelse return error.NoBrowserContext;
    const root = bc.mainFrame() orelse return error.FrameNotLoaded;

    const node = (bc.node_registry.lookup_by_id.get(params.backendNodeId) orelse return error.InvalidNodeId).dom;
    const st = SemanticTree.init(cmd.arena, node, &bc.node_registry, root, .{}) catch return error.InvalidNodeId;
    const details = st.nodeDetails() catch return error.InternalError;

    return cmd.sendResult(.{
        .nodeDetails = details,
    }, .{});
}

fn getStructuredData(cmd: anytype) !void {
    const bc = cmd.browser_context orelse return error.NoBrowserContext;
    const frame = bc.mainFrame() orelse return error.FrameNotLoaded;

    const data = try structured_data.collectStructuredData(
        frame.document.asNode(),
        cmd.arena,
        frame,
    );

    return cmd.sendResult(.{
        .structuredData = data,
    }, .{});
}

// Advisory `Content-Signal` robots.txt preferences for the current document's
// host (https://contentsignals.org). `available` is false when no robots.txt
// has been fetched for the host — note this is the case unless `obey_robots`
// is enabled, since the robots layer is what populates the store.
fn getContentSignal(cmd: anytype) !void {
    const bc = cmd.browser_context orelse return error.NoBrowserContext;
    const frame = bc.mainFrame() orelse return error.FrameNotLoaded;
    const network = bc.cdp.browser.http_client.network;

    const empty: []const Robots.ContentSignal = &.{};
    const robots_url = lp.URL.getRobotsUrl(cmd.arena, frame.url) catch {
        return cmd.sendResult(.{ .available = false, .contentSignals = empty }, .{});
    };

    const signals = try network.robot_store.getContentSignals(cmd.arena, robots_url);
    return cmd.sendResult(.{
        .available = signals != null,
        .contentSignals = signals orelse empty,
    }, .{});
}

fn getPageStatus(cmd: anytype) !void {
    const bc = cmd.browser_context orelse return error.NoBrowserContext;
    const frame = bc.mainFrame() orelse return error.FrameNotLoaded;
    const status = &frame.page.status;

    return cmd.sendResult(.{
        .url = frame.url,
        .httpStatus = frame._http_status,
        .botChallenge = frame._bot_challenge,
        .navigateError = if (frame._last_navigate_error) |err| @errorName(err) else null,
        .jsErrors = status.js_errors,
        .unsupported = status.unsupported,
        .skipped = status.skipped,
        .missingGlobals = status.missing_globals,
        .failed = status.failed,
        .blocked = status.blocked,
        .resourceChallenge = status.resource_challenge,
    }, .{});
}

fn detectForms(cmd: anytype) !void {
    const bc = cmd.browser_context orelse return error.NoBrowserContext;
    const frame = bc.mainFrame() orelse return error.FrameNotLoaded;

    const forms_data = try lp.forms.collectForms(
        cmd.arena,
        frame.document.asNode(),
        frame,
    );

    try lp.forms.registerNodes(forms_data, &bc.node_registry);

    return cmd.sendResult(.{
        .forms = forms_data,
    }, .{});
}

fn clickNode(cmd: anytype) !void {
    const Params = struct {
        nodeId: ?NodeRegistry.Id = null,
        backendNodeId: ?NodeRegistry.Id = null,
    };
    const params = (try cmd.params(Params)) orelse return error.InvalidParam;

    const bc = cmd.browser_context orelse return error.NoBrowserContext;
    const frame = bc.mainFrame() orelse return error.FrameNotLoaded;

    const node_id = params.nodeId orelse params.backendNodeId orelse return error.InvalidParam;
    const node = bc.node_registry.lookup_by_id.get(node_id) orelse return error.InvalidNodeId;

    lp.actions.click(node.dom, frame) catch |err| {
        if (err == error.InvalidNodeType) return error.InvalidParam;
        return error.InternalError;
    };

    return cmd.sendResult(.{}, .{});
}

fn fillNode(cmd: anytype) !void {
    const Params = struct {
        nodeId: ?NodeRegistry.Id = null,
        backendNodeId: ?NodeRegistry.Id = null,
        text: []const u8,
    };
    const params = (try cmd.params(Params)) orelse return error.InvalidParam;

    const bc = cmd.browser_context orelse return error.NoBrowserContext;
    const frame = bc.mainFrame() orelse return error.FrameNotLoaded;

    const node_id = params.nodeId orelse params.backendNodeId orelse return error.InvalidParam;
    const node = bc.node_registry.lookup_by_id.get(node_id) orelse return error.InvalidNodeId;

    lp.actions.fill(node.dom, params.text, frame) catch |err| {
        if (err == error.InvalidNodeType) return error.InvalidParam;
        return error.InternalError;
    };

    return cmd.sendResult(.{}, .{});
}

fn scrollNode(cmd: anytype) !void {
    const Params = struct {
        nodeId: ?NodeRegistry.Id = null,
        backendNodeId: ?NodeRegistry.Id = null,
        x: ?i32 = null,
        y: ?i32 = null,
    };
    const params = (try cmd.params(Params)) orelse return error.InvalidParam;

    const bc = cmd.browser_context orelse return error.NoBrowserContext;
    const frame = bc.mainFrame() orelse return error.FrameNotLoaded;

    const maybe_node_id = params.nodeId orelse params.backendNodeId;
    const target_node: ?*DOMNode = if (maybe_node_id) |node_id|
        (bc.node_registry.lookup_by_id.get(node_id) orelse return error.InvalidNodeId).dom
    else
        null;

    const result = lp.actions.scroll(target_node, params.x, params.y, frame) catch |err| {
        if (err == error.InvalidNodeType) return error.InvalidParam;
        return error.InternalError;
    };

    const target_id: ?NodeRegistry.Id = switch (result.target) {
        .window => null,
        .node, .container => |scrolled| (try bc.node_registry.register(scrolled)).id,
    };
    return cmd.sendResult(.{ .backendNodeId = target_id, .x = result.x, .y = result.y }, .{});
}

fn waitForSelector(cmd: anytype) !void {
    const Params = struct {
        selector: []const u8,
        timeout: ?u32 = null,
    };
    const params = (try cmd.params(Params)) orelse return error.InvalidParam;

    const bc = cmd.browser_context orelse return error.NoBrowserContext;
    const frame = bc.mainFrame() orelse return error.FrameNotLoaded;

    const timeout_ms = params.timeout orelse lp.tools.defaultWaitTimeout(frame);
    const selector_z = try cmd.arena.dupeSentinel(u8, params.selector, 0);

    const node = lp.actions.waitForSelector(selector_z, timeout_ms, frame._frame_id, bc.session) catch |err| {
        if (err == error.InvalidSelector) return error.InvalidParam;
        if (err == error.Timeout) return error.InternalError;
        return error.InternalError;
    };

    const registered = try bc.node_registry.register(node);
    return cmd.sendResult(.{
        .backendNodeId = registered.id,
    }, .{});
}

// Lightpanda-namespaced pre-arm for window.alert/confirm/prompt return values.
//
// Standard CDP drivers send Page.handleJavaScriptDialog *reactively* in
// response to a Page.javascriptDialogOpening event — the dialog suspends
// JS, the client picks accept/dismiss, the runtime resumes. Lightpanda's
// dialogs auto-dismiss in headless mode (window.alert/confirm/prompt
// return immediately rather than blocking V8), so by the time the event
// reaches the client, JS has already returned. A reactive call has
// nothing left to influence — full Chrome-faithful behavior would
// require V8 suspension, which #2082 / PR #2085 deferred.
//
// LP.handleJavaScriptDialog gives Lightpanda-aware clients a *proactive*
// opt-in: the client sets {accept, promptText} *before* triggering the JS
// that opens the dialog. The handler stashes the response on the
// BrowserContext; when the next dialog dispatches the
// `javascript_dialog_opening` notification, the listener in page.zig
// pops the stash and fills it into the dispatch's response output param.
// window.alert/confirm/prompt then return that value.
//
// Page.handleJavaScriptDialog continues to return -32000 "No dialog is
// showing" so reactive Chrome-style drivers see no semantic change.
//
// Without a pre-armed response, behavior is unchanged from PR #2085:
// confirm→false, prompt→null, alert→void.
fn handleJavaScriptDialog(cmd: anytype) !void {
    const params = (try cmd.params(struct {
        accept: bool,
        promptText: ?[]const u8 = null,
    })) orelse return error.InvalidParam;

    const bc = cmd.browser_context orelse return error.NoBrowserContext;

    // Duplicate promptText into the BrowserContext arena so it outlives the
    // CDP command's own message arena (the dialog may fire on a later command).
    const prompt_text: ?[]const u8 = if (params.promptText) |t|
        try bc.arena.dupe(u8, t)
    else
        null;

    bc.pending_dialog_response = .{
        .accept = params.accept,
        .prompt_text = prompt_text,
    };

    return cmd.sendResult(null, .{});
}

const testing = @import("../testing.zig");
test "cdp.lp: version" {
    var ctx = try testing.context();
    defer ctx.deinit();

    try ctx.processMessage(.{
        .id = 1,
        .method = "LP.version",
    });

    const result = (try ctx.getSentMessage(0)).?.object.get("result").?.object;
    try testing.expectEqualSlices(u8, lp.build_config.version, result.get("version").?.string);
}

test "cdp.lp: configureCDP toggles setCacheDisabled handling" {
    var ctx = try testing.context();
    defer ctx.deinit();

    try testing.expectEqual(false, ctx.cdp().disable_set_cache_disabled);

    try ctx.processMessage(.{ .id = 1, .method = "Target.attachToBrowserTarget" });
    try ctx.expectSentResult(.{ .sessionId = "BSID-1" }, .{ .id = 1 });

    try ctx.processMessage(.{
        .id = 2,
        .method = "LP.configureCDP",
        .sessionId = "BSID-1",
        .params = .{ .disableSetCacheDisabled = true },
    });
    try ctx.expectSentResult(null, .{ .id = 2, .session_id = "BSID-1" });
    try testing.expectEqual(true, ctx.cdp().disable_set_cache_disabled);

    try ctx.processMessage(.{
        .id = 3,
        .method = "LP.configureCDP",
        .sessionId = "BSID-1",
        .params = .{ .disableSetCacheDisabled = false },
    });
    try ctx.expectSentResult(null, .{ .id = 3, .session_id = "BSID-1" });
    try testing.expectEqual(false, ctx.cdp().disable_set_cache_disabled);
}

test "cdp.lp: configureCDP sets the HTTP version on this connection's client" {
    var ctx = try testing.context();
    defer ctx.deinit();

    const client = &ctx.cdp().browser.http_client;
    try testing.expectEqual(.auto, client.http_version);

    try ctx.processMessage(.{
        .id = 1,
        .method = "LP.configureCDP",
        .params = .{ .httpVersion = "1.1" },
    });
    try ctx.expectSentResult(null, .{ .id = 1 });
    try testing.expectEqual(.@"1.1", client.http_version);

    // Unsupported versions are rejected, not silently ignored.
    try ctx.processMessage(.{
        .id = 2,
        .method = "LP.configureCDP",
        .params = .{ .httpVersion = "3" },
    });
    try testing.expect((try ctx.getSentMessage(1)).?.object.get("error") != null);
    try testing.expectEqual(.@"1.1", client.http_version);

    try ctx.processMessage(.{
        .id = 3,
        .method = "LP.configureCDP",
        .params = .{ .httpVersion = "auto" },
    });
    try ctx.expectSentResult(null, .{ .id = 3 });
    try testing.expectEqual(.auto, client.http_version);
}

test "cdp.lp: getMarkdown" {
    var ctx = try testing.context();
    defer ctx.deinit();

    const bc = try ctx.loadBrowserContext(.{});
    _ = try bc.session.createPage();

    try ctx.processMessage(.{
        .id = 1,
        .method = "LP.getMarkdown",
    });

    const result = (try ctx.getSentMessage(0)).?.object.get("result").?.object;
    try testing.expect(result.get("markdown") != null);
}

test "cdp.lp: dump formats, strip and scoping" {
    var ctx = try testing.context();
    defer ctx.deinit();

    _ = try ctx.loadBrowserContext(.{ .id = "BID-S", .url = "cdp/strip.html", .target_id = "FID-000000000S".* });

    // markdown, no strip: chrome and content both present.
    try ctx.processMessage(.{ .id = 1, .method = "LP.dump", .params = .{ .format = "markdown" } });
    var content = try dumpContent(&ctx, 1, "markdown");
    try testing.expect(std.mem.find(u8, content, "Site menu") != null);
    try testing.expect(std.mem.find(u8, content, "Article body") != null);

    // markdown + shell: chrome gone, the article's own header kept.
    try ctx.processMessage(.{ .id = 2, .method = "LP.dump", .params = .{ .format = "markdown", .strip = .{ .shell = true } } });
    content = try dumpContent(&ctx, 2, "markdown");
    try testing.expectEqual("Byline\n\nArticle body\n", content);

    // html + shell, scoped by selector.
    try ctx.processMessage(.{ .id = 3, .method = "LP.dump", .params = .{ .format = "html", .selector = "main", .strip = .{ .shell = true, .js = true } } });
    content = try dumpContent(&ctx, 3, "html");
    try testing.expectEqual("<main><article><header>Byline</header><p>Article body</p></article></main>", content);

    // html, whole document, capped.
    try ctx.processMessage(.{ .id = 4, .method = "LP.dump", .params = .{ .format = "html", .maxBytes = 20 } });
    content = try dumpContent(&ctx, 4, "html");
    try testing.expect(std.mem.startsWith(u8, content, "<!DOCTYPE html>"));
    try testing.expect(content.len < 100);

    // png and pdf come back base64; maxBytes is a text-only option.
    try ctx.processMessage(.{ .id = 5, .method = "LP.dump", .params = .{ .format = "png", .strip = .{ .shell = true } } });
    content = try dumpContent(&ctx, 5, "png");
    try testing.expect(std.mem.startsWith(u8, content, "iVBOR"));

    try ctx.processMessage(.{ .id = 6, .method = "LP.dump", .params = .{ .format = "pdf" } });
    content = try dumpContent(&ctx, 6, "pdf");
    try testing.expect(std.mem.startsWith(u8, content, "JVBER"));

    try ctx.processMessage(.{ .id = 7, .method = "LP.dump", .params = .{ .format = "png", .maxBytes = 10 } });
    try testing.expect((try dumpReply(&ctx, 7)).get("error") != null);

    // Unknown selector and missing format are errors.
    try ctx.processMessage(.{ .id = 8, .method = "LP.dump", .params = .{ .format = "markdown", .selector = "#nope" } });
    try testing.expect((try dumpReply(&ctx, 8)).get("error") != null);
    try ctx.processMessage(.{ .id = 9, .method = "LP.dump", .params = .{ .strip = .{ .shell = true } } });
    try testing.expect((try dumpReply(&ctx, 9)).get("error") != null);
}

// A backendNodeId can name a node in a child frame while the handler only
// knows the root. Labels, datalists and styles must come from the node's own
// document: the parent reuses every id and hides `.probe`.
test "cdp.lp: semantic tree and node details read the node's own frame" {
    var ctx = try testing.context();
    defer ctx.deinit();

    const bc = try ctx.loadBrowserContext(.{ .id = "BID-T", .url = "cdp/semantic_tree_iframe.html", .target_id = "FID-000000000T".* });
    const root = bc.mainFrame() orelse unreachable;
    const child = root.child_frames.items[0];

    const html = (child.document.getDocumentElement() orelse unreachable).asNode();
    const input = (try child.document.querySelector(.wrap("input"), child)).?.asNode();
    const html_id = (try bc.node_registry.register(html)).id;
    const input_id = (try bc.node_registry.register(input)).id;

    try ctx.processMessage(.{ .id = 1, .method = "LP.getSemanticTree", .params = .{ .backendNodeId = html_id, .format = "text", .prune = false } });
    const tree = (try dumpReply(&ctx, 1)).get("result").?.object.get("semanticTree").?.string;
    try testing.expect(std.mem.find(u8, tree, "child-label") != null);
    try testing.expect(std.mem.find(u8, tree, "child-option") != null);
    try testing.expect(std.mem.find(u8, tree, "child-probe") != null);
    try testing.expect(std.mem.find(u8, tree, "parent-") == null);

    try ctx.processMessage(.{ .id = 2, .method = "LP.getNodeDetails", .params = .{ .backendNodeId = input_id } });
    const details = (try dumpReply(&ctx, 2)).get("result").?.object.get("nodeDetails").?.object;
    try testing.expectEqual("child-label", details.get("name").?.string);
    try testing.expectEqual("child-option", details.get("options").?.array.items[0].object.get("value").?.string);

    // JSON format and interactiveOnly walk the same frame as the text format.
    try ctx.processMessage(.{ .id = 5, .method = "LP.getSemanticTree", .params = .{ .backendNodeId = html_id, .interactiveOnly = true } });
    const json = try std.json.Stringify.valueAlloc(testing.allocator, (try dumpReply(&ctx, 5)).get("result").?.object.get("semanticTree").?, .{});
    defer testing.allocator.free(json);
    try testing.expect(std.mem.find(u8, json, "child-label") != null);
    try testing.expect(std.mem.find(u8, json, "parent-") == null);

    // A document with no frame has no styles or layout to describe.
    const frameless = try root._factory.genericDocument(.{});
    const frameless_id = (try bc.node_registry.register(frameless.asNode())).id;
    try ctx.processMessage(.{ .id = 3, .method = "LP.getSemanticTree", .params = .{ .backendNodeId = frameless_id } });
    try ctx.expectSentError(-31998, "InvalidNodeId", .{ .id = 3 });
    try ctx.processMessage(.{ .id = 4, .method = "LP.getNodeDetails", .params = .{ .backendNodeId = frameless_id } });
    try ctx.expectSentError(-31998, "InvalidNodeId", .{ .id = 4 });
}

test "cdp.lp: getInteractiveElements" {
    var ctx = try testing.context();
    defer ctx.deinit();

    const bc = try ctx.loadBrowserContext(.{});
    _ = try bc.session.createPage();

    try ctx.processMessage(.{
        .id = 1,
        .method = "LP.getInteractiveElements",
    });

    const result = (try ctx.getSentMessage(0)).?.object.get("result").?.object;
    try testing.expect(result.get("elements") != null);
}

test "cdp.lp: getInteractiveElements reads the node's own frame" {
    var ctx = try testing.context();
    defer ctx.deinit();

    const bc = try ctx.loadBrowserContext(.{ .id = "BID-T", .url = "cdp/interactive_iframe.html", .target_id = "FID-000000000T".* });
    const root = bc.mainFrame() orelse unreachable;
    const child = root.child_frames.items[0];

    const html = (child.document.getDocumentElement() orelse unreachable).asNode();
    const html_id = (try bc.node_registry.register(html)).id;

    try ctx.processMessage(.{ .id = 1, .method = "LP.getInteractiveElements", .params = .{ .nodeId = html_id } });
    const elements = (try dumpReply(&ctx, 1)).get("result").?.object.get("elements").?.array.items;

    var found_hook = false;
    var found_link = false;
    for (elements) |el| {
        const id = el.object.get("id") orelse continue;
        if (std.mem.eql(u8, id.string, "child-hook")) {
            found_hook = true;
            try testing.expectEqual("listener", el.object.get("type").?.string);
        } else if (std.mem.eql(u8, id.string, "child-link")) {
            found_link = true;
            try testing.expectEqual("http://127.0.0.1:9582/src/browser/tests/cdp/iframe/target.html", el.object.get("href").?.string);
        }
    }
    try testing.expect(found_link);
    try testing.expect(found_hook);
}

test "cdp.lp: getStructuredData" {
    var ctx = try testing.context();
    defer ctx.deinit();

    const bc = try ctx.loadBrowserContext(.{});
    _ = try bc.session.createPage();

    try ctx.processMessage(.{
        .id = 1,
        .method = "LP.getStructuredData",
    });

    const result = (try ctx.getSentMessage(0)).?.object.get("result").?.object;
    try testing.expect(result.get("structuredData") != null);
}

test "cdp.lp: getContentSignal" {
    var ctx = try testing.context();
    defer ctx.deinit();

    const bc = try ctx.loadBrowserContext(.{});
    _ = try bc.session.createPage();

    try ctx.processMessage(.{
        .id = 1,
        .method = "LP.getContentSignal",
    });

    // No robots.txt fetched for the page, so the directive is reported absent
    // rather than erroring — the command is still wired and returns the shape.
    const result = (try ctx.getSentMessage(0)).?.object.get("result").?.object;
    try testing.expectEqual(false, result.get("available").?.bool);
    try testing.expect(result.get("contentSignals") != null);
}

test "cdp.lp: getPageStatus" {
    testing.expectLog(&.{.page_error});

    var ctx = try testing.context();
    defer ctx.deinit();

    const bc = try ctx.loadBrowserContext(.{});
    const page = try bc.session.createPage();
    bc.session.load_resources = .{};
    // The iframe and worker skips warn; image and stylesheet are debug.
    testing.expectLog(&.{ .disabled, .disabled });

    const url = "http://localhost:9582/src/browser/tests/cdp/page_status.html";
    try page.navigate(url, .{ .reason = .address_bar, .kind = .{ .push = null } });
    try testing.waitForPage(bc);

    try ctx.processMessage(.{ .id = 1, .method = "LP.getPageStatus" });
    var result = (try dumpReply(&ctx, 1)).get("result").?.object;
    try testing.expectEqual(url, result.get("url").?.string);
    try testing.expectEqual(200, result.get("httpStatus").?.integer);
    try testing.expectEqual(null, result.get("botChallenge"));
    try testing.expectEqual(null, result.get("navigateError"));
    try testing.expectEqual(1, result.get("jsErrors").?.integer);

    // Two webgl probes, three paint calls; the 2d context itself and its
    // path setup don't count.
    var unsupported = result.get("unsupported").?.object;
    try testing.expectEqual(2, unsupported.get("webgl").?.integer);
    try testing.expectEqual(3, unsupported.get("canvas2d").?.integer);

    try testing.expectEqual(0, result.get("missingGlobals").?.array.items.len);

    var skipped = result.get("skipped").?.object;
    try testing.expectEqual(1, skipped.get("iframe").?.integer);
    try testing.expectEqual(1, skipped.get("worker").?.integer);
    try testing.expectEqual(1, skipped.get("image").?.integer);
    try testing.expectEqual(1, skipped.get("stylesheet").?.integer);

    try testing.expectEqual(0, result.get("failed").?.object.get("fetch").?.integer);
    try testing.expectEqual(0, result.get("blocked").?.object.get("fetch").?.integer);
    try testing.expectEqual(null, result.get("resourceChallenge"));

    // A new top-level navigation starts over.
    try bc.session.initiateRootNavigation(page.frame_id, "http://localhost:9582/src/browser/tests/cdp/dialog.html", .{ .reason = .address_bar, .kind = .{ .push = null } });
    try testing.waitForPage(bc);

    try ctx.processMessage(.{ .id = 2, .method = "LP.getPageStatus" });
    result = (try dumpReply(&ctx, 2)).get("result").?.object;
    try testing.expectEqual(0, result.get("jsErrors").?.integer);
    unsupported = result.get("unsupported").?.object;
    try testing.expectEqual(0, unsupported.get("webgl").?.integer);
    try testing.expectEqual(0, unsupported.get("canvas2d").?.integer);
    skipped = result.get("skipped").?.object;
    try testing.expectEqual(0, skipped.get("iframe").?.integer);
}

test "cdp.lp: action tools" {
    var ctx = try testing.context();
    defer ctx.deinit();

    const bc = try ctx.loadBrowserContext(.{});
    const page = try bc.session.createPage();
    const frame = page.frame().?;

    const url = "http://localhost:9582/src/browser/tests/mcp_actions.html";
    try frame.navigate(url, .{ .reason = .address_bar, .kind = .{ .push = null } });
    try testing.waitForPage(bc);

    // Test Click
    const btn = frame.document.getElementById("btn").?.asNode();
    const btn_id = (try bc.node_registry.register(btn)).id;
    try ctx.processMessage(.{
        .id = 1,
        .method = "LP.clickNode",
        .params = .{ .backendNodeId = btn_id },
    });

    // Test Fill Input
    const inp = frame.document.getElementById("inp").?.asNode();
    const inp_id = (try bc.node_registry.register(inp)).id;
    try ctx.processMessage(.{
        .id = 2,
        .method = "LP.fillNode",
        .params = .{ .backendNodeId = inp_id, .text = "hello" },
    });

    // Test Fill Select
    const sel = frame.document.getElementById("sel").?.asNode();
    const sel_id = (try bc.node_registry.register(sel)).id;
    try ctx.processMessage(.{
        .id = 3,
        .method = "LP.fillNode",
        .params = .{ .backendNodeId = sel_id, .text = "opt2" },
    });

    // Test Scroll
    const scrollbox = frame.document.getElementById("scrollbox").?.asNode();
    const scrollbox_id = (try bc.node_registry.register(scrollbox)).id;
    try ctx.processMessage(.{
        .id = 4,
        .method = "LP.scrollNode",
        .params = .{ .backendNodeId = scrollbox_id, .y = 50 },
    });
    try ctx.expectSentResult(.{ .backendNodeId = scrollbox_id, .x = 0, .y = 50 }, .{ .id = 4 });

    // A leaf inside a scroll container scrolls the container, not the leaf.
    const leaf = frame.document.getElementById("innerleaf").?.asNode();
    const leaf_id = (try bc.node_registry.register(leaf)).id;
    const outer = frame.document.getElementById("outerscroll").?.asNode();
    const outer_id = (try bc.node_registry.register(outer)).id;
    try ctx.processMessage(.{
        .id = 5,
        .method = "LP.scrollNode",
        .params = .{ .backendNodeId = leaf_id, .y = 30 },
    });
    try ctx.expectSentResult(.{ .backendNodeId = outer_id, .x = 0, .y = 30 }, .{ .id = 5 });

    // Scroll events are scheduled, not fired inline with the command.
    var runner = bc.session.runner(.{});
    try runner.waitForScript(frame._frame_id, "window.scrolled === true && window.outerScrolled === true", 1000);

    // Evaluate assertions
    var ls: lp.js.Local.Scope = undefined;
    frame.js.localScope(&ls);
    defer ls.deinit();

    var try_catch: lp.js.TryCatch = undefined;
    try_catch.init(&ls.local);
    defer try_catch.deinit();

    const result = try ls.local.compileAndRun("window.clicked === true && window.inputVal === 'hello' && window.changed === true && window.selChanged === 'opt2' && document.getElementById('outerscroll').scrollTop === 30 && document.getElementById('innerleaf').scrollTop === 0 && window.scrollY === 0", null);

    try testing.expect(result.isTrue());
}

test "cdp.lp: waitForSelector" {
    var ctx = try testing.context();
    defer ctx.deinit();

    const bc = try ctx.loadBrowserContext(.{});
    const page = try bc.session.createPage();
    const frame = page.frame().?;

    const url = "http://localhost:9582/src/browser/tests/mcp_wait_for_selector.html";
    try frame.navigate(url, .{ .reason = .address_bar, .kind = .{ .push = null } });
    try testing.waitForPage(bc);

    // 1. Existing element
    try ctx.processMessage(.{
        .id = 1,
        .method = "LP.waitForSelector",
        .params = .{ .selector = "#existing", .timeout = 2000 },
    });
    var result = (try ctx.getSentMessage(0)).?.object.get("result").?.object;
    try testing.expect(result.get("backendNodeId") != null);

    // 2. Delayed element
    try ctx.processMessage(.{
        .id = 2,
        .method = "LP.waitForSelector",
        .params = .{ .selector = "#delayed", .timeout = 5000 },
    });
    result = (try ctx.getSentMessage(1)).?.object.get("result").?.object;
    try testing.expect(result.get("backendNodeId") != null);

    // 3. Timeout error
    try ctx.processMessage(.{
        .id = 3,
        .method = "LP.waitForSelector",
        .params = .{ .selector = "#nonexistent", .timeout = 20 },
    });
    const err_obj = (try ctx.getSentMessage(2)).?.object.get("error").?.object;
    try testing.expect(err_obj.get("code") != null);
}

test "cdp.lp: handleJavaScriptDialog accepts/dismisses without an open dialog" {
    var ctx = try testing.context();
    defer ctx.deinit();

    {
        // Without a BrowserContext: error (matches other LP handlers' shape).
        try ctx.processMessage(.{ .id = 1, .method = "LP.handleJavaScriptDialog", .params = .{ .accept = true } });
        try ctx.expectSentError(-31998, "NoBrowserContext", .{ .id = 1 });
    }

    _ = try ctx.loadBrowserContext(.{ .id = "BID-D1", .url = "cdp/dialog.html", .target_id = "FID-000000000X".* });

    {
        // Pre-arming with accept=true succeeds. Headless browsers auto-dismiss,
        // so the CDP client sends LP.handleJavaScriptDialog *before* the JS
        // that opens the dialog — handler stashes the response on the
        // BrowserContext.
        try ctx.processMessage(.{ .id = 2, .method = "LP.handleJavaScriptDialog", .params = .{ .accept = true } });
        try ctx.expectSentResult(null, .{ .id = 2 });
    }

    {
        // Pre-arming with accept=false also succeeds.
        try ctx.processMessage(.{ .id = 3, .method = "LP.handleJavaScriptDialog", .params = .{ .accept = false } });
        try ctx.expectSentResult(null, .{ .id = 3 });
    }

    {
        // Pre-arming with a promptText also succeeds. The string is dup'd into
        // the BrowserContext arena so it survives until the dialog dispatches.
        try ctx.processMessage(.{ .id = 4, .method = "LP.handleJavaScriptDialog", .params = .{ .accept = true, .promptText = "hello" } });
        try ctx.expectSentResult(null, .{ .id = 4 });
    }
}

test "cdp.lp: handleJavaScriptDialog controls confirm/prompt/alert return values" {
    var ctx = try testing.context();
    defer ctx.deinit();

    var bc = try ctx.loadBrowserContext(.{ .id = "BID-D2", .url = "cdp/dialog.html", .target_id = "FID-000000000X".* });

    const frame = bc.mainFrame() orelse unreachable;
    var ls: lp.js.Local.Scope = undefined;
    frame.js.localScope(&ls);
    defer ls.deinit();

    // ---- confirm: accept=true makes confirm() return true ----
    try ctx.processMessage(.{ .id = 1, .method = "LP.handleJavaScriptDialog", .params = .{ .accept = true } });
    try ctx.expectSentResult(null, .{ .id = 1 });

    const c_accept = try ls.local.exec("confirm('proceed?')", null);
    try testing.expectEqual(true, c_accept.toBool());
    try ctx.expectSentEvent("Page.javascriptDialogOpening", .{
        .message = "proceed?",
        .type = "confirm",
        .hasBrowserHandler = false,
        .defaultPrompt = "",
    }, .{ .session_id = "SID-X" });

    // ---- confirm: accept=false makes confirm() return false ----
    try ctx.processMessage(.{ .id = 2, .method = "LP.handleJavaScriptDialog", .params = .{ .accept = false } });
    try ctx.expectSentResult(null, .{ .id = 2 });

    const c_dismiss = try ls.local.exec("confirm('again?')", null);
    try testing.expectEqual(false, c_dismiss.toBool());

    // ---- confirm: no pre-arm preserves PR #2085 default (false) ----
    const c_default = try ls.local.exec("confirm('default?')", null);
    try testing.expectEqual(false, c_default.toBool());

    // ---- prompt: accept=true with promptText returns the text ----
    try ctx.processMessage(.{ .id = 3, .method = "LP.handleJavaScriptDialog", .params = .{ .accept = true, .promptText = "hello" } });
    try ctx.expectSentResult(null, .{ .id = 3 });

    const p_text = try ls.local.exec("prompt('name?')", null);
    const p_text_str = try p_text.toStringSlice();
    try testing.expectEqualSlices(u8, "hello", p_text_str);

    // ---- prompt: accept=true without promptText AND no dialog defaultText returns "" ----
    try ctx.processMessage(.{ .id = 4, .method = "LP.handleJavaScriptDialog", .params = .{ .accept = true } });
    try ctx.expectSentResult(null, .{ .id = 4 });

    const p_empty = try ls.local.exec("prompt('name?')", null);
    const p_empty_str = try p_empty.toStringSlice();
    try testing.expectEqualSlices(u8, "", p_empty_str);

    // ---- prompt: accept=true without promptText falls back to dialog defaultText ----
    // Mirrors Chrome's accept-without-typing behavior: with no client-supplied
    // promptText, the prompt's return value is the second arg to window.prompt.
    try ctx.processMessage(.{ .id = 5, .method = "LP.handleJavaScriptDialog", .params = .{ .accept = true } });
    try ctx.expectSentResult(null, .{ .id = 5 });

    const p_default_text = try ls.local.exec("prompt('name?', 'John Smith')", null);
    const p_default_text_str = try p_default_text.toStringSlice();
    try testing.expectEqualSlices(u8, "John Smith", p_default_text_str);

    // ---- prompt: pre-armed promptText overrides the dialog defaultText ----
    try ctx.processMessage(.{ .id = 6, .method = "LP.handleJavaScriptDialog", .params = .{ .accept = true, .promptText = "typed" } });
    try ctx.expectSentResult(null, .{ .id = 6 });

    const p_override = try ls.local.exec("prompt('name?', 'John Smith')", null);
    const p_override_str = try p_override.toStringSlice();
    try testing.expectEqualSlices(u8, "typed", p_override_str);

    // ---- prompt: accept=false returns null regardless of dialog defaultText ----
    try ctx.processMessage(.{ .id = 7, .method = "LP.handleJavaScriptDialog", .params = .{ .accept = false } });
    try ctx.expectSentResult(null, .{ .id = 7 });

    const p_dismiss_with_default = try ls.local.exec("prompt('cancel?', 'John Smith')", null);
    try testing.expect(p_dismiss_with_default.isNull());

    // ---- prompt: accept=false makes prompt() return null ----
    try ctx.processMessage(.{ .id = 8, .method = "LP.handleJavaScriptDialog", .params = .{ .accept = false } });
    try ctx.expectSentResult(null, .{ .id = 8 });

    const p_dismiss = try ls.local.exec("prompt('cancel?')", null);
    try testing.expect(p_dismiss.isNull());

    // ---- prompt: no pre-arm preserves PR #2085 default (null) ----
    const p_default = try ls.local.exec("prompt('default?')", null);
    try testing.expect(p_default.isNull());

    // ---- alert: dispatches the event but has no return value to override ----
    try ctx.processMessage(.{ .id = 9, .method = "LP.handleJavaScriptDialog", .params = .{ .accept = true } });
    try ctx.expectSentResult(null, .{ .id = 9 });
    _ = try ls.local.exec("alert('important')", null);
    try ctx.expectSentEvent("Page.javascriptDialogOpening", .{
        .message = "important",
        .type = "alert",
    }, .{ .session_id = "SID-X" });

    // ---- pending response is consumed by exactly one dialog ----
    // After the alert above pops the pre-arm, the next confirm sees no pending
    // and falls back to the default (false) — the alert MUST clear pending so
    // the response doesn't leak across dialogs.
    const c_after_alert = try ls.local.exec("confirm('leak?')", null);
    try testing.expectEqual(false, c_after_alert.toBool());
}

test "cdp.lp: configureLoading toggles subFrame and worker independently" {
    var ctx = try testing.context();
    defer ctx.deinit();

    const bc = try ctx.loadBrowserContext(.{});
    _ = try bc.session.createPage();

    // Defaults: both loading types enabled.
    try testing.expectEqual(true, bc.session.load_resources.iframe);
    try testing.expectEqual(true, bc.session.load_resources.worker);

    // subFrame-only: leaves worker untouched.
    try ctx.processMessage(.{
        .id = 1,
        .method = "LP.configureLoading",
        .params = .{ .subFrame = false },
    });
    try ctx.expectSentResult(null, .{ .id = 1 });
    try testing.expectEqual(false, bc.session.load_resources.iframe);
    try testing.expectEqual(true, bc.session.load_resources.worker);

    // worker-only: leaves subFrame untouched.
    try ctx.processMessage(.{
        .id = 2,
        .method = "LP.configureLoading",
        .params = .{ .worker = false },
    });
    try ctx.expectSentResult(null, .{ .id = 2 });
    try testing.expectEqual(false, bc.session.load_resources.iframe);
    try testing.expectEqual(false, bc.session.load_resources.worker);

    // Both at once.
    try ctx.processMessage(.{
        .id = 3,
        .method = "LP.configureLoading",
        .params = .{ .subFrame = true, .worker = true },
    });
    try ctx.expectSentResult(null, .{ .id = 3 });
    try testing.expectEqual(true, bc.session.load_resources.iframe);
    try testing.expectEqual(true, bc.session.load_resources.worker);
}

test "cdp.lp: configureLoading toggles externalStylesheets independently" {
    var ctx = try testing.context();
    defer ctx.deinit();

    const bc = try ctx.loadBrowserContext(.{});
    _ = try bc.session.createPage();

    // Default is opt-in: off unless the CLI flag or CDP toggle enables it.
    try testing.expectEqual(false, bc.session.load_resources.stylesheet);

    // Enable via CDP; the other two loading toggles stay at their defaults.
    try ctx.processMessage(.{
        .id = 1,
        .method = "LP.configureLoading",
        .params = .{ .externalStylesheets = true },
    });
    try ctx.expectSentResult(null, .{ .id = 1 });
    try testing.expectEqual(true, bc.session.load_resources.stylesheet);
    try testing.expectEqual(true, bc.session.load_resources.iframe);
    try testing.expectEqual(true, bc.session.load_resources.worker);

    // Flip back off; partial params must not reset the other fields.
    try ctx.processMessage(.{
        .id = 2,
        .method = "LP.configureLoading",
        .params = .{ .externalStylesheets = false },
    });
    try ctx.expectSentResult(null, .{ .id = 2 });
    try testing.expectEqual(false, bc.session.load_resources.stylesheet);
    try testing.expectEqual(true, bc.session.load_resources.iframe);
    try testing.expectEqual(true, bc.session.load_resources.worker);
}

fn dumpReply(ctx: *testing.TestContext, id: i64) !std.json.ObjectMap {
    var i: usize = 0;
    while (try ctx.getSentMessage(i)) |m| : (i += 1) {
        const msg_id = m.object.get("id") orelse continue;
        if (msg_id.integer == id) {
            return m.object;
        }
    }
    return error.MissingReply;
}

fn dumpContent(ctx: *testing.TestContext, id: i64, format: []const u8) ![]const u8 {
    const result = (try dumpReply(ctx, id)).get("result").?.object;
    try testing.expectEqual(format, result.get("format").?.string);
    return result.get("content").?.string;
}