Skip to content

Commit e997ecd

Browse files
committed
test(browser): cover cookies --url path and empty-scope envelope
Only forward the truthy scope option so getCookies receives the same shape internal callers already use.
1 parent 2660f60 commit e997ecd

2 files changed

Lines changed: 30 additions & 4 deletions

File tree

src/cli.test.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2227,15 +2227,38 @@ describe('browser cookies command', () => {
22272227

22282228
await program.parseAsync(['node', 'opencli', 'browser', '--session', 'test', 'cookies', '--domain', 'example.com']);
22292229

2230-
expect(browserState.page!.getCookies).toHaveBeenCalledWith({ domain: 'example.com', url: undefined });
2230+
expect(browserState.page!.getCookies).toHaveBeenCalledWith({ domain: 'example.com' });
22312231
const out = lastJsonLog();
2232-
expect(out.count).toBe(2);
2232+
expect(out).toMatchObject({
2233+
session: 'test',
2234+
count: 2,
2235+
captured_at: expect.any(String),
2236+
});
22332237
expect(out.cookies).toEqual(expect.arrayContaining([
22342238
expect.objectContaining({ name: 'session', httpOnly: true }),
22352239
expect.objectContaining({ name: 'theme', httpOnly: false }),
22362240
]));
22372241
});
22382242

2243+
it('passes --url through to getCookies without sending an unset --domain', async () => {
2244+
const program = createProgram('', '');
2245+
2246+
await program.parseAsync(['node', 'opencli', 'browser', '--session', 'test', 'cookies', '--url', 'https://example.com/login']);
2247+
2248+
expect(browserState.page!.getCookies).toHaveBeenCalledWith({ url: 'https://example.com/login' });
2249+
});
2250+
2251+
it('emits an empty cookies envelope with count 0 when the scope matches nothing', async () => {
2252+
(browserState.page!.getCookies as ReturnType<typeof vi.fn>).mockResolvedValueOnce([]);
2253+
const program = createProgram('', '');
2254+
2255+
await program.parseAsync(['node', 'opencli', 'browser', '--session', 'test', 'cookies', '--domain', 'nothing.example']);
2256+
2257+
const out = lastJsonLog();
2258+
expect(out.count).toBe(0);
2259+
expect(out.cookies).toEqual([]);
2260+
});
2261+
22392262
it('errors with missing_scope when neither --domain nor --url is provided', async () => {
22402263
const program = createProgram('', '');
22412264

src/cli.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1270,15 +1270,18 @@ Examples:
12701270

12711271
addBrowserTabOption(browser.command('cookies'))
12721272
.option('--domain <domain>', 'Cookie domain scope (e.g. example.com)')
1273-
.option('--url <url>', 'URL scope (use domain or url, not both)')
1273+
.option('--url <url>', 'URL scope (may be combined with --domain to narrow)')
12741274
.description('Read scoped cookies (includes HttpOnly)')
12751275
.action(browserAction(async (page, opts) => {
12761276
if (!opts.domain && !opts.url) {
12771277
console.log(JSON.stringify({ error: { code: 'missing_scope', message: 'Provide --domain or --url to scope the cookie read' } }, null, 2));
12781278
process.exitCode = EXIT_CODES.USAGE_ERROR;
12791279
return;
12801280
}
1281-
const cookies = await page.getCookies({ domain: opts.domain, url: opts.url });
1281+
const cookies = await page.getCookies({
1282+
...(opts.domain ? { domain: opts.domain } : {}),
1283+
...(opts.url ? { url: opts.url } : {}),
1284+
});
12821285
console.log(JSON.stringify({
12831286
session: getPageSession(page),
12841287
captured_at: new Date().toISOString(),

0 commit comments

Comments
 (0)