File size: 4,562 Bytes
fc93158 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | import { Command } from "commander";
import { afterEach, beforeAll, describe, expect, it, vi } from "vitest";
const gatewayMocks = vi.hoisted(() => ({
callGatewayFromCli: vi.fn(async () => ({
ok: true,
format: "ai",
targetId: "t1",
url: "https://example.com",
snapshot: "ok",
})),
}));
vi.mock("./gateway-rpc.js", () => ({
callGatewayFromCli: gatewayMocks.callGatewayFromCli,
}));
const configMocks = vi.hoisted(() => ({
loadConfig: vi.fn(() => ({ browser: {} })),
}));
vi.mock("../config/config.js", () => configMocks);
const sharedMocks = vi.hoisted(() => ({
callBrowserRequest: vi.fn(
async (_opts: unknown, params: { path?: string; query?: Record<string, unknown> }) => {
const format = params.query?.format === "aria" ? "aria" : "ai";
if (format === "aria") {
return {
ok: true,
format: "aria",
targetId: "t1",
url: "https://example.com",
nodes: [],
};
}
return {
ok: true,
format: "ai",
targetId: "t1",
url: "https://example.com",
snapshot: "ok",
};
},
),
}));
vi.mock("./browser-cli-shared.js", () => ({
callBrowserRequest: sharedMocks.callBrowserRequest,
}));
const runtime = {
log: vi.fn(),
error: vi.fn(),
exit: vi.fn(),
};
vi.mock("../runtime.js", () => ({
defaultRuntime: runtime,
}));
let registerBrowserInspectCommands: typeof import("./browser-cli-inspect.js").registerBrowserInspectCommands;
type SnapshotDefaultsCase = {
label: string;
args: string[];
expectMode: "efficient" | undefined;
};
describe("browser cli snapshot defaults", () => {
const runBrowserInspect = async (args: string[], withJson = false) => {
const program = new Command();
const browser = program.command("browser").option("--json", "JSON output", false);
registerBrowserInspectCommands(browser, () => ({}));
await program.parseAsync(withJson ? ["browser", "--json", ...args] : ["browser", ...args], {
from: "user",
});
const [, params] = sharedMocks.callBrowserRequest.mock.calls.at(-1) ?? [];
return params as { path?: string; query?: Record<string, unknown> } | undefined;
};
const runSnapshot = async (args: string[]) => await runBrowserInspect(["snapshot", ...args]);
beforeAll(async () => {
({ registerBrowserInspectCommands } = await import("./browser-cli-inspect.js"));
});
afterEach(() => {
vi.clearAllMocks();
configMocks.loadConfig.mockReturnValue({ browser: {} });
});
it.each<SnapshotDefaultsCase>([
{
label: "uses config snapshot defaults when mode is not provided",
args: [],
expectMode: "efficient",
},
{
label: "does not apply config snapshot defaults to aria snapshots",
args: ["--format", "aria"],
expectMode: undefined,
},
])("$label", async ({ args, expectMode }) => {
configMocks.loadConfig.mockReturnValue({
browser: { snapshotDefaults: { mode: "efficient" } },
});
if (args.includes("--format")) {
gatewayMocks.callGatewayFromCli.mockResolvedValueOnce({
ok: true,
format: "aria",
targetId: "t1",
url: "https://example.com",
snapshot: "ok",
});
}
const params = await runSnapshot(args);
expect(params?.path).toBe("/snapshot");
if (expectMode === undefined) {
expect((params?.query as { mode?: unknown } | undefined)?.mode).toBeUndefined();
} else {
expect(params?.query).toMatchObject({
format: "ai",
mode: expectMode,
});
}
});
it("does not set mode when config defaults are absent", async () => {
configMocks.loadConfig.mockReturnValue({ browser: {} });
const params = await runSnapshot([]);
expect((params?.query as { mode?: unknown } | undefined)?.mode).toBeUndefined();
});
it("applies explicit efficient mode without config defaults", async () => {
configMocks.loadConfig.mockReturnValue({ browser: {} });
const params = await runSnapshot(["--efficient"]);
expect(params?.query).toMatchObject({
format: "ai",
mode: "efficient",
});
});
it("sends screenshot request with trimmed target id and jpeg type", async () => {
const params = await runBrowserInspect(["screenshot", " tab-1 ", "--type", "jpeg"], true);
expect(params?.path).toBe("/screenshot");
expect((params as { body?: Record<string, unknown> } | undefined)?.body).toMatchObject({
targetId: "tab-1",
type: "jpeg",
fullPage: false,
});
});
});
|