Files
jackwener__opencli/clis/youtube/utils.test.js
jakevin 60dbbd4baa perf(adapters): hoist cookie reads to page.getCookies (Tier 1, 25 files) (#1450)
* perf: replace document.cookie reads with page.getCookies({domain}) (Tier 1 cookie API sweep)

Prior pattern in 25 adapter files round-tripped through `page.evaluate(\`document.cookie.split…\`)` to extract a single cookie value (CSRF token, session ID, etc.). CDP's `page.getCookies({domain})` reads the cookie store directly with zero JS-execution overhead.

Files touched (sites: twitter / linkedin / maimai / youtube):

- twitter (15): thread, timeline, list-add, bookmark-folders, following, list-tweets, bookmarks, list-remove, tweets, bookmark-folder, likes, lists, trending — direct 4-line replacement (cookie was outside `page.evaluate`); article, profile — hoisted ct0 read OUT of `page.evaluate` and threw `AuthRequiredError` upfront so unreachable in-evaluate auth branches got cleaned up too.

- linkedin/search.js — JSESSIONID was read inside the per-batch fetch loop's `page.evaluate`; hoisted once before the loop and pass `csrf` value into the template via `JSON.stringify`.

- maimai/search-talents.js — csrftoken cookie hoisted via getCookies; meta-tag fallback preserved inside `page.evaluate` (reached only when no cookie). Also converted the `page.evaluate(async (body) => …, body)` Playwright-style call to OpenCLI's template-string form so the helper actually runs.

- youtube — `SAPISID_HASH_FN` (used by like / unlike / subscribe / unsubscribe) reworked: sapisid is now passed in as a parameter; new `readYoutubeSapisid(page)` helper reads it via CDP. The HMAC-SHA1 compute still happens browser-side (Web Crypto), only the cookie read is hoisted.

Tests updated where mocks specifically referenced `document.cookie` (twitter following / bookmark-folder / bookmark-folders) to mock `getCookies` instead.

Verification:
- `npx tsc --noEmit` clean
- `npx vitest run clis/twitter clis/linkedin clis/youtube` → 264/264 pass
- typed-error-lint 189/189 (no new violations)
- silent-column-drop 103/103 (no new violations)

Scope notes (not in this PR):
- `goto + wait` redundancy and `browserSession: { reuse: 'site' }` rollout are scoped to follow-up PRs B and C per the #OpenCLI:3889b5cf thread plan.
- `document.cookie.match(...)` patterns (instagram 8 / xiaoe / qwen / hupu / tiktok / 1point3acres — ~13 files) are outside the original \`document.cookie.split\` audit scope and will follow as a Tier 1 expansion sweep.

* fix(adapters): read auth cookies by url scope
2026-05-11 01:06:48 +08:00

85 lines
3.6 KiB
JavaScript

import { describe, expect, it, vi } from 'vitest';
import { extractJsonAssignmentFromHtml, extractSubscriptionChannel, prepareYoutubeApiPage, readYoutubeSapisid } from './utils.js';
describe('youtube utils', () => {
it('extractJsonAssignmentFromHtml parses bootstrap objects with nested braces in strings', () => {
const html = `
<script>
var ytInitialPlayerResponse = {
"title": "brace { inside } string",
"nested": { "count": 2, "text": "quote \\"value\\"" }
};
</script>
`;
expect(extractJsonAssignmentFromHtml(html, 'ytInitialPlayerResponse')).toEqual({
title: 'brace { inside } string',
nested: { count: 2, text: 'quote "value"' },
});
});
it('extractJsonAssignmentFromHtml supports window assignments', () => {
const html = `
<script>
window["ytInitialData"] = {"contents":{"items":[1,2,3]}};
</script>
`;
expect(extractJsonAssignmentFromHtml(html, 'ytInitialData')).toEqual({
contents: { items: [1, 2, 3] },
});
});
it('prepareYoutubeApiPage loads the quiet API bootstrap page', async () => {
const page = {
goto: vi.fn().mockResolvedValue(undefined),
wait: vi.fn().mockResolvedValue(undefined),
};
await expect(prepareYoutubeApiPage(page)).resolves.toBeUndefined();
expect(page.goto).toHaveBeenCalledWith('https://www.youtube.com', { waitUntil: 'none' });
expect(page.wait).toHaveBeenCalledWith(2);
});
it('readYoutubeSapisid reads URL-scoped cookies and prefers secure SAPISID', async () => {
const page = {
getCookies: vi.fn().mockResolvedValue([
{ name: 'SAPISID', value: 'legacy' },
{ name: '__Secure-3PAPISID', value: 'secure' },
]),
};
await expect(readYoutubeSapisid(page)).resolves.toBe('secure');
expect(page.getCookies).toHaveBeenCalledWith({ url: 'https://www.youtube.com' });
});
it('readYoutubeSapisid falls back to legacy SAPISID', async () => {
const page = {
getCookies: vi.fn().mockResolvedValue([{ name: 'SAPISID', value: 'legacy' }]),
};
await expect(readYoutubeSapisid(page)).resolves.toBe('legacy');
});
it('extractSubscriptionChannel prefers explicit handle and subscriber count fields', () => {
expect(extractSubscriptionChannel({
title: { simpleText: 'OpenAI' },
channelHandleText: { runs: [{ text: '@openai' }] },
subscriberCountText: { simpleText: '1.23M subscribers' },
videoCountText: { simpleText: '123 videos' },
navigationEndpoint: { browseEndpoint: { canonicalBaseUrl: '/channel/UC123' } },
channelId: 'UC123',
})).toEqual({
name: 'OpenAI',
handle: '@openai',
subscribers: '1.23M subscribers',
url: 'https://www.youtube.com/channel/UC123',
});
});
it('extractSubscriptionChannel falls back when handle/count fields are overloaded', () => {
expect(extractSubscriptionChannel({
title: {
runs: [{ text: 'OpenAI' }],
},
subscriberCountText: { simpleText: '@openai' },
videoCountText: { simpleText: '1.23M subscribers' },
navigationEndpoint: { browseEndpoint: { canonicalBaseUrl: '/@openai' } },
channelId: 'UC123',
})).toEqual({
name: 'OpenAI',
handle: '@openai',
subscribers: '1.23M subscribers',
url: 'https://www.youtube.com/@openai',
});
});
});