Files
wehub-resource-sync 9b395f5cc3
E2E Headed Chrome / e2e-headed (macos-15) (push) Has been cancelled
E2E Headed Chrome / e2e-headed (ubuntu-latest) (push) Has been cancelled
E2E Headed Chrome / e2e-headed (windows-latest) (push) Has been cancelled
CI / build (macos-latest) (push) Has been cancelled
CI / build (ubuntu-latest) (push) Has been cancelled
CI / build (windows-latest) (push) Has been cancelled
CI / unit-test (push) Has been cancelled
CI / bun-test (push) Has been cancelled
CI / adapter-test (push) Has been cancelled
CI / smoke-test (macos-latest) (push) Has been cancelled
CI / smoke-test (ubuntu-latest) (push) Has been cancelled
Security Audit / audit (push) Has been cancelled
Build Chrome Extension / build (push) Has been cancelled
Trigger Website Rebuild (Docs Updated) / dispatch (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:39:48 +08:00

132 lines
5.5 KiB
JavaScript

import { describe, expect, it, vi } from 'vitest';
import { AuthRequiredError } from '@jackwener/opencli/errors';
import { buildNotebooklmRpcBody, extractNotebooklmRpcResult, getNotebooklmPageAuth, parseNotebooklmChunkedResponse, unwrapNotebooklmEvaluateResult, } from './rpc.js';
describe('notebooklm rpc transport', () => {
it('unwraps Browser Bridge evaluate envelopes', () => {
const data = { ok: true };
expect(unwrapNotebooklmEvaluateResult({ session: 'site:notebooklm:abc', data })).toBe(data);
expect(unwrapNotebooklmEvaluateResult(data)).toBe(data);
});
it('extracts auth tokens from the page html via page evaluation', async () => {
const page = {
evaluate: vi.fn(async (script) => {
expect(script).toContain('document.documentElement.innerHTML');
return {
html: '<html>"SNlM0e":"csrf-123","FdrFJe":"sess-456"</html>',
sourcePath: '/',
};
}),
};
await expect(getNotebooklmPageAuth(page)).resolves.toEqual({
csrfToken: 'csrf-123',
sessionId: 'sess-456',
sourcePath: '/',
authuser: '',
});
expect(page.evaluate).toHaveBeenCalledTimes(1);
});
it('extracts auth tokens when page evaluation is wrapped in a Browser Bridge envelope', async () => {
const page = {
evaluate: vi.fn(async () => ({
session: 'site:notebooklm:abc',
data: {
html: '<html>"SNlM0e":"csrf-123","FdrFJe":"sess-456"</html>',
sourcePath: '/notebook/nb-demo',
},
})),
};
await expect(getNotebooklmPageAuth(page)).resolves.toEqual({
csrfToken: 'csrf-123',
sessionId: 'sess-456',
sourcePath: '/notebook/nb-demo',
authuser: '',
});
});
it('falls back to WIZ_global_data tokens when html regex data is missing', async () => {
const page = {
evaluate: vi.fn(async () => ({
html: '<html><body>NotebookLM</body></html>',
sourcePath: '/notebook/nb-demo',
readyState: 'complete',
csrfToken: 'csrf-wiz',
sessionId: 'sess-wiz',
})),
};
await expect(getNotebooklmPageAuth(page)).resolves.toEqual({
csrfToken: 'csrf-wiz',
sessionId: 'sess-wiz',
sourcePath: '/notebook/nb-demo',
authuser: '',
});
});
it('retries token extraction once when the first probe returns no tokens', async () => {
const page = {
evaluate: vi.fn()
.mockResolvedValueOnce({
html: '<html><body>Loading…</body></html>',
sourcePath: '/notebook/nb-demo',
readyState: 'interactive',
csrfToken: '',
sessionId: '',
})
.mockResolvedValueOnce({
html: '<html>"SNlM0e":"csrf-123","FdrFJe":"sess-456"</html>',
sourcePath: '/notebook/nb-demo',
readyState: 'complete',
csrfToken: '',
sessionId: '',
}),
wait: vi.fn(async () => undefined),
};
await expect(getNotebooklmPageAuth(page)).resolves.toEqual({
csrfToken: 'csrf-123',
sessionId: 'sess-456',
sourcePath: '/notebook/nb-demo',
authuser: '',
});
expect(page.evaluate).toHaveBeenCalledTimes(2);
});
it('builds the rpc body with the expected notebooklm payload shape', () => {
const body = buildNotebooklmRpcBody('wXbhsf', [null, 1, null, [2]], 'csrf-123');
expect(body).toContain('f.req=');
expect(body).toContain('at=csrf-123');
expect(body.endsWith('&')).toBe(true);
expect(decodeURIComponent(body)).toContain('"[null,1,null,[2]]"');
});
it('parses chunked batchexecute responses into json chunks', () => {
const raw = `)]}'\n107\n[["wrb.fr","wXbhsf","[[[\\\"Notebook One\\\",null,\\\"nb1\\\",null,null,[null,false,null,null,null,[1704067200]]]]]"]]`;
const chunks = parseNotebooklmChunkedResponse(raw);
expect(chunks).toHaveLength(1);
expect(Array.isArray(chunks[0])).toBe(true);
expect(chunks[0]).toEqual([
[
'wrb.fr',
'wXbhsf',
'[[["Notebook One",null,"nb1",null,null,[null,false,null,null,null,[1704067200]]]]]',
],
]);
});
it('extracts the rpc payload from wrb.fr responses', () => {
const raw = `)]}'\n107\n[["wrb.fr","wXbhsf","[[[\\\"Notebook One\\\",null,\\\"nb1\\\",null,null,[null,false,null,null,null,[1704067200]]]]]"]]`;
const result = extractNotebooklmRpcResult(raw, 'wXbhsf');
expect(result).toEqual([
[
['Notebook One', null, 'nb1', null, null, [null, false, null, null, null, [1704067200]]],
],
]);
});
it('classifies auth errors as AuthRequiredError', () => {
const raw = `)]}'\n25\n[["er",null,null,null,null,401,"generic"]]`;
expect(() => extractNotebooklmRpcResult(raw, 'wXbhsf')).toThrow(AuthRequiredError);
try {
extractNotebooklmRpcResult(raw, 'wXbhsf');
}
catch (error) {
expect(error).toBeInstanceOf(AuthRequiredError);
expect(error.domain).toBe('notebooklm.google.com');
expect(error.code).toBe('AUTH_REQUIRED');
}
});
});