refactor(browser): trim observe endpoints

This commit is contained in:
Peter Steinberger
2025-12-20 02:07:27 +00:00
parent 0e94c6b025
commit 479720c169
11 changed files with 12 additions and 405 deletions

View File

@@ -3,19 +3,11 @@ import { beforeEach, describe, expect, it, vi } from "vitest";
import type { BrowserRouteContext } from "../server-context.js";
const pw = vi.hoisted(() => ({
generateLocatorForRef: vi
.fn()
.mockImplementation((ref: string) => `locator('aria-ref=${ref}')`),
getConsoleMessagesViaPlaywright: vi.fn().mockResolvedValue([]),
getNetworkRequestsViaPlaywright: vi.fn().mockResolvedValue([]),
mouseClickViaPlaywright: vi.fn().mockResolvedValue(undefined),
mouseDragViaPlaywright: vi.fn().mockResolvedValue(undefined),
mouseMoveViaPlaywright: vi.fn().mockResolvedValue(undefined),
pdfViaPlaywright: vi.fn().mockResolvedValue({ buffer: Buffer.from("pdf") }),
startTracingViaPlaywright: vi.fn().mockResolvedValue(undefined),
stopTracingViaPlaywright: vi
.fn()
.mockResolvedValue({ buffer: Buffer.from("trace") }),
verifyElementVisibleViaPlaywright: vi.fn().mockResolvedValue(undefined),
verifyListVisibleViaPlaywright: vi.fn().mockResolvedValue(undefined),
verifyTextVisibleViaPlaywright: vi.fn().mockResolvedValue(undefined),
@@ -113,24 +105,6 @@ describe("handleBrowserActionExtra", () => {
},
expectBody: { ok: true, messages: [], targetId: "tab1" },
},
{
action: "network" as const,
args: { includeStatic: true },
fn: pw.getNetworkRequestsViaPlaywright,
expectArgs: {
cdpPort: 18792,
targetId: "tab1",
includeStatic: true,
},
expectBody: { ok: true, requests: [], targetId: "tab1" },
},
{
action: "traceStart" as const,
args: {},
fn: pw.startTracingViaPlaywright,
expectArgs: { cdpPort: 18792, targetId: "tab1" },
expectBody: { ok: true },
},
{
action: "verifyElement" as const,
args: { role: "button", accessibleName: "Submit" },
@@ -209,13 +183,6 @@ describe("handleBrowserActionExtra", () => {
},
expectBody: { ok: true },
},
{
action: "locator" as const,
args: { ref: "99" },
fn: pw.generateLocatorForRef,
expectArgs: "99",
expectBody: { ok: true, locator: "locator('aria-ref=99')" },
},
];
for (const item of cases) {
@@ -226,7 +193,7 @@ describe("handleBrowserActionExtra", () => {
}
});
it("stores PDF and trace outputs", async () => {
it("stores PDF output", async () => {
const { res: pdfRes } = await callAction("pdf");
expect(pw.pdfViaPlaywright).toHaveBeenCalledWith({
cdpPort: 18792,
@@ -240,18 +207,5 @@ describe("handleBrowserActionExtra", () => {
targetId: "tab1",
url: baseTab.url,
});
media.saveMediaBuffer.mockResolvedValueOnce({ path: "/tmp/fake.zip" });
const { res: traceRes } = await callAction("traceStop");
expect(pw.stopTracingViaPlaywright).toHaveBeenCalledWith({
cdpPort: 18792,
targetId: "tab1",
});
expect(traceRes.body).toMatchObject({
ok: true,
path: "/tmp/fake.zip",
targetId: "tab1",
url: baseTab.url,
});
});
});

View File

@@ -4,15 +4,11 @@ import type express from "express";
import { ensureMediaDir, saveMediaBuffer } from "../../media/store.js";
import {
generateLocatorForRef,
getConsoleMessagesViaPlaywright,
getNetworkRequestsViaPlaywright,
mouseClickViaPlaywright,
mouseDragViaPlaywright,
mouseMoveViaPlaywright,
pdfViaPlaywright,
startTracingViaPlaywright,
stopTracingViaPlaywright,
verifyElementVisibleViaPlaywright,
verifyListVisibleViaPlaywright,
verifyTextVisibleViaPlaywright,
@@ -21,7 +17,6 @@ import {
import type { BrowserRouteContext } from "../server-context.js";
import {
jsonError,
toBoolean,
toNumber,
toStringArray,
toStringOrEmpty,
@@ -37,14 +32,10 @@ function normalizeMouseButton(value: unknown): MouseButton | undefined {
export type BrowserActionExtra =
| "console"
| "locator"
| "mouseClick"
| "mouseDrag"
| "mouseMove"
| "network"
| "pdf"
| "traceStart"
| "traceStop"
| "verifyElement"
| "verifyList"
| "verifyText"
@@ -77,17 +68,6 @@ export async function handleBrowserActionExtra(
res.json({ ok: true, messages, targetId: tab.targetId });
return true;
}
case "network": {
const includeStatic = toBoolean(args.includeStatic) ?? false;
const tab = await ctx.ensureTabAvailable(target);
const requests = await getNetworkRequestsViaPlaywright({
cdpPort,
targetId: tab.targetId,
includeStatic,
});
res.json({ ok: true, requests, targetId: tab.targetId });
return true;
}
case "pdf": {
const tab = await ctx.ensureTabAvailable(target);
const pdf = await pdfViaPlaywright({
@@ -109,36 +89,6 @@ export async function handleBrowserActionExtra(
});
return true;
}
case "traceStart": {
const tab = await ctx.ensureTabAvailable(target);
await startTracingViaPlaywright({
cdpPort,
targetId: tab.targetId,
});
res.json({ ok: true });
return true;
}
case "traceStop": {
const tab = await ctx.ensureTabAvailable(target);
const trace = await stopTracingViaPlaywright({
cdpPort,
targetId: tab.targetId,
});
await ensureMediaDir();
const saved = await saveMediaBuffer(
trace.buffer,
"application/zip",
"browser",
trace.buffer.byteLength,
);
res.json({
ok: true,
path: path.resolve(saved.path),
targetId: tab.targetId,
url: tab.url,
});
return true;
}
case "verifyElement": {
const role = toStringOrEmpty(args.role);
const accessibleName = toStringOrEmpty(args.accessibleName);
@@ -269,16 +219,6 @@ export async function handleBrowserActionExtra(
res.json({ ok: true });
return true;
}
case "locator": {
const ref = toStringOrEmpty(args.ref);
if (!ref) {
jsonError(res, 400, "ref is required");
return true;
}
const locator = generateLocatorForRef(ref);
res.json({ ok: true, locator });
return true;
}
default:
return false;
}

View File

@@ -3,7 +3,7 @@ import type express from "express";
import type { BrowserRouteContext } from "../server-context.js";
import { handleBrowserActionCore } from "./actions-core.js";
import { handleBrowserActionExtra } from "./actions-extra.js";
import { jsonError, toBoolean, toStringOrEmpty } from "./utils.js";
import { jsonError, toStringOrEmpty } from "./utils.js";
function readBody(req: express.Request): Record<string, unknown> {
const body = req.body as Record<string, unknown> | undefined;
@@ -176,24 +176,6 @@ export function registerBrowserActionRoutes(
await runExtraAction(ctx, res, "console", args, targetId);
});
app.get("/network", async (req, res) => {
const targetId = readTargetId(req.query.targetId);
const includeStatic = toBoolean(req.query.includeStatic) ?? false;
await runExtraAction(ctx, res, "network", { includeStatic }, targetId);
});
app.post("/trace/start", async (req, res) => {
const body = readBody(req);
const targetId = readTargetId(body.targetId);
await runExtraAction(ctx, res, "traceStart", body, targetId);
});
app.post("/trace/stop", async (req, res) => {
const body = readBody(req);
const targetId = readTargetId(body.targetId);
await runExtraAction(ctx, res, "traceStop", body, targetId);
});
app.post("/pdf", async (req, res) => {
const body = readBody(req);
const targetId = readTargetId(body.targetId);
@@ -241,9 +223,4 @@ export function registerBrowserActionRoutes(
const targetId = readTargetId(body.targetId);
await runExtraAction(ctx, res, "mouseDrag", body, targetId);
});
app.post("/locator", async (req, res) => {
const body = readBody(req);
await runExtraAction(ctx, res, "locator", body, "");
});
}