File size: 5,681 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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 | import { beforeEach, describe, expect, it, vi } from "vitest";
import { registerBrowserStateCommands } from "./browser-cli-state.js";
import { createBrowserProgram as createBrowserProgramShared } from "./browser-cli-test-helpers.js";
const mocks = vi.hoisted(() => ({
callBrowserRequest: vi.fn(async (..._args: unknown[]) => ({ ok: true })),
runBrowserResizeWithOutput: vi.fn(async (_params: unknown) => {}),
runtime: {
log: vi.fn(),
error: vi.fn(),
exit: vi.fn(),
},
}));
vi.mock("./browser-cli-shared.js", () => ({
callBrowserRequest: mocks.callBrowserRequest,
}));
vi.mock("./browser-cli-resize.js", () => ({
runBrowserResizeWithOutput: mocks.runBrowserResizeWithOutput,
}));
vi.mock("../runtime.js", () => ({
defaultRuntime: mocks.runtime,
}));
describe("browser state option collisions", () => {
const createStateProgram = ({ withGatewayUrl = false } = {}) => {
const { program, browser, parentOpts } = createBrowserProgramShared({ withGatewayUrl });
registerBrowserStateCommands(browser, parentOpts);
return program;
};
const getLastRequest = () => {
const call = mocks.callBrowserRequest.mock.calls.at(-1);
expect(call).toBeDefined();
if (!call) {
throw new Error("expected browser request call");
}
return call[1] as { body?: Record<string, unknown> };
};
const runBrowserCommand = async (argv: string[]) => {
const program = createStateProgram();
await program.parseAsync(["browser", ...argv], { from: "user" });
};
const runBrowserCommandAndGetRequest = async (argv: string[]) => {
await runBrowserCommand(argv);
return getLastRequest();
};
beforeEach(() => {
mocks.callBrowserRequest.mockClear();
mocks.runBrowserResizeWithOutput.mockClear();
mocks.runtime.log.mockClear();
mocks.runtime.error.mockClear();
mocks.runtime.exit.mockClear();
});
it("forwards parent-captured --target-id on `browser cookies set`", async () => {
const request = await runBrowserCommandAndGetRequest([
"cookies",
"set",
"session",
"abc",
"--url",
"https://example.com",
"--target-id",
"tab-1",
]);
expect((request as { body?: { targetId?: string } }).body?.targetId).toBe("tab-1");
});
it("resolves --url via parent when addGatewayClientOptions captures it", async () => {
const program = createStateProgram({ withGatewayUrl: true });
await program.parseAsync(
[
"browser",
"--url",
"ws://gw",
"cookies",
"set",
"session",
"abc",
"--url",
"https://example.com",
],
{ from: "user" },
);
const call = mocks.callBrowserRequest.mock.calls.at(-1);
expect(call).toBeDefined();
const request = call![1] as { body?: { cookie?: { url?: string } } };
expect(request.body?.cookie?.url).toBe("https://example.com");
});
it("inherits --url from parent when subcommand does not provide it", async () => {
const program = createStateProgram({ withGatewayUrl: true });
await program.parseAsync(
["browser", "--url", "https://inherited.example.com", "cookies", "set", "session", "abc"],
{ from: "user" },
);
const call = mocks.callBrowserRequest.mock.calls.at(-1);
expect(call).toBeDefined();
const request = call![1] as { body?: { cookie?: { url?: string } } };
expect(request.body?.cookie?.url).toBe("https://inherited.example.com");
});
it("accepts legacy parent `--json` by parsing payload via positional headers fallback", async () => {
const request = (await runBrowserCommandAndGetRequest([
"set",
"headers",
"--json",
'{"x-auth":"ok"}',
])) as {
body?: { headers?: Record<string, string> };
};
expect(request.body?.headers).toEqual({ "x-auth": "ok" });
});
it("filters non-string header values from JSON payload", async () => {
const request = (await runBrowserCommandAndGetRequest([
"set",
"headers",
"--json",
'{"x-auth":"ok","retry":3,"enabled":true}',
])) as {
body?: { headers?: Record<string, string> };
};
expect(request.body?.headers).toEqual({ "x-auth": "ok" });
});
it("errors when set offline receives an invalid value", async () => {
await runBrowserCommand(["set", "offline", "maybe"]);
expect(mocks.callBrowserRequest).not.toHaveBeenCalled();
expect(mocks.runtime.error).toHaveBeenCalledWith(expect.stringContaining("Expected on|off"));
expect(mocks.runtime.exit).toHaveBeenCalledWith(1);
});
it("errors when set media receives an invalid value", async () => {
await runBrowserCommand(["set", "media", "sepia"]);
expect(mocks.callBrowserRequest).not.toHaveBeenCalled();
expect(mocks.runtime.error).toHaveBeenCalledWith(
expect.stringContaining("Expected dark|light|none"),
);
expect(mocks.runtime.exit).toHaveBeenCalledWith(1);
});
it("errors when headers JSON is missing", async () => {
await runBrowserCommand(["set", "headers"]);
expect(mocks.callBrowserRequest).not.toHaveBeenCalled();
expect(mocks.runtime.error).toHaveBeenCalledWith(
expect.stringContaining("Missing headers JSON"),
);
expect(mocks.runtime.exit).toHaveBeenCalledWith(1);
});
it("errors when headers JSON is not an object", async () => {
await runBrowserCommand(["set", "headers", "--json", "[]"]);
expect(mocks.callBrowserRequest).not.toHaveBeenCalled();
expect(mocks.runtime.error).toHaveBeenCalledWith(
expect.stringContaining("Headers JSON must be a JSON object"),
);
expect(mocks.runtime.exit).toHaveBeenCalledWith(1);
});
});
|