Files
jackwener__opencli/clis/notebooklm/rpc.test.js
jakevin d2974a9ff6 refactor(adapters): convert adapter layer from TypeScript to JavaScript (#928)
* refactor(adapters): convert adapter layer from TypeScript to JavaScript

Core framework stays TypeScript; adapter layer moves to JS-first.
Adapters are essentially "executable config + browser scripts" that
barely use TS features — this simplifies the build/distribution pipeline
by removing the dist/clis/ intermediate compilation step.

Changes:
- Convert all 753 adapter files in clis/ from .ts to .js
- Update tsconfig to exclude clis/ from compilation
- Simplify build-manifest to scan clis/*.js directly (no dist/clis/)
- Update discovery, main, fetch-adapters to load JS adapters from clis/
- Update generate-verified to output .js artifacts
- Update package.json files field: dist/clis/ → clis/
- Fix all test files for the .ts → .js transition

* fix(main): use findPackageRoot for BUILTIN_CLIS path

The previous relative path (../../clis from __dirname) only worked for
dist/src/main.js but broke dev mode (tsx src/main.ts) where __dirname
is <repo>/src — resolving to /clis instead of <repo>/clis.

Use findPackageRoot() which works for both dev and prod paths.
2026-04-10 14:52:18 +08:00

106 lines
4.5 KiB
JavaScript

import { describe, expect, it, vi } from 'vitest';
import { AuthRequiredError } from '@jackwener/opencli/errors';
import { buildNotebooklmRpcBody, extractNotebooklmRpcResult, getNotebooklmPageAuth, parseNotebooklmChunkedResponse, } from './rpc.js';
describe('notebooklm rpc transport', () => {
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: '/',
});
expect(page.evaluate).toHaveBeenCalledTimes(1);
});
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',
});
});
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',
});
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');
}
});
});