Spaces:
Sleeping
Sleeping
File size: 5,590 Bytes
7c87115 | 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 | # tests/core/test_base.py
"""Tests for BaseClient infrastructure class."""
from unittest.mock import patch, MagicMock
import pytest
def test_base_client_import():
"""Test that BaseClient can be imported."""
from notebooklm_tools.core.base import BaseClient
assert BaseClient is not None
def test_base_client_init():
"""Test BaseClient initialization with minimal args."""
from notebooklm_tools.core.base import BaseClient
with patch.object(BaseClient, '_refresh_auth_tokens'):
client = BaseClient(cookies={"test": "cookie"})
assert client.cookies == {"test": "cookie"}
assert client._client is None
def test_base_client_init_with_csrf():
"""Test BaseClient initialization with CSRF token (skips refresh)."""
from notebooklm_tools.core.base import BaseClient
# When csrf_token is provided, _refresh_auth_tokens should NOT be called
with patch.object(BaseClient, '_refresh_auth_tokens') as mock_refresh:
client = BaseClient(cookies={"test": "cookie"}, csrf_token="test_token")
mock_refresh.assert_not_called()
assert client.csrf_token == "test_token"
def test_build_request_body():
"""Test building RPC request body."""
from notebooklm_tools.core.base import BaseClient
with patch.object(BaseClient, '_refresh_auth_tokens'):
client = BaseClient(cookies={}, csrf_token="test_token")
body = client._build_request_body("testRpc", ["param1"])
assert "f.req=" in body
assert "at=" in body
assert "testRpc" in body
def test_build_url():
"""Test building batchexecute URL."""
from notebooklm_tools.core.base import BaseClient
with patch.object(BaseClient, '_refresh_auth_tokens'):
client = BaseClient(cookies={}, csrf_token="test_token", session_id="test_sid")
url = client._build_url("testRpc", "/notebook/123")
assert "rpcids=testRpc" in url
assert "source-path=" in url
assert "f.sid=test_sid" in url
def test_get_httpx_cookies_from_dict():
"""Test converting dict cookies to httpx.Cookies."""
from notebooklm_tools.core.base import BaseClient
with patch.object(BaseClient, '_refresh_auth_tokens'):
client = BaseClient(cookies={"SID": "abc123"}, csrf_token="token")
cookies = client._get_httpx_cookies()
# Should have cookies for both domains
assert cookies.get("SID", domain=".google.com") == "abc123"
assert cookies.get("SID", domain=".googleusercontent.com") == "abc123"
def test_get_httpx_cookies_from_list():
"""Test converting list of cookie dicts to httpx.Cookies."""
from notebooklm_tools.core.base import BaseClient
with patch.object(BaseClient, '_refresh_auth_tokens'):
cookie_list = [
{"name": "SID", "value": "abc123", "domain": ".google.com", "path": "/"}
]
client = BaseClient(cookies=cookie_list, csrf_token="token")
cookies = client._get_httpx_cookies()
assert cookies.get("SID", domain=".google.com") == "abc123"
# Should also duplicate to googleusercontent.com
assert cookies.get("SID", domain=".googleusercontent.com") == "abc123"
def test_parse_response():
"""Test parsing batchexecute response."""
from notebooklm_tools.core.base import BaseClient
with patch.object(BaseClient, '_refresh_auth_tokens'):
client = BaseClient(cookies={}, csrf_token="token")
# Simulate a typical batchexecute response
response_text = """)]}'
42
[["wrb.fr","testRpc","{\\"data\\":\\"value\\"}"]]
"""
result = client._parse_response(response_text)
assert len(result) == 1
assert result[0][0][0] == "wrb.fr"
assert result[0][0][1] == "testRpc"
def test_extract_rpc_result():
"""Test extracting RPC result from parsed response."""
from notebooklm_tools.core.base import BaseClient
with patch.object(BaseClient, '_refresh_auth_tokens'):
client = BaseClient(cookies={}, csrf_token="token")
parsed = [[["wrb.fr", "testRpc", '{"status": "ok"}']]]
result = client._extract_rpc_result(parsed, "testRpc")
assert result == {"status": "ok"}
def test_context_manager():
"""Test BaseClient as context manager."""
from notebooklm_tools.core.base import BaseClient
with patch.object(BaseClient, '_refresh_auth_tokens'):
with BaseClient(cookies={}, csrf_token="token") as client:
assert client is not None
# After exiting, client should be closed
assert client._client is None
def test_close():
"""Test explicit close."""
from notebooklm_tools.core.base import BaseClient
with patch.object(BaseClient, '_refresh_auth_tokens'):
client = BaseClient(cookies={}, csrf_token="token")
# Create but don't use _client
client._client = MagicMock()
client.close()
assert client._client is None
def test_constants_available():
"""Test that RPC and API constants are available on BaseClient."""
from notebooklm_tools.core.base import BaseClient
# Check RPC IDs
assert hasattr(BaseClient, 'RPC_LIST_NOTEBOOKS')
assert hasattr(BaseClient, 'RPC_GET_NOTEBOOK')
assert hasattr(BaseClient, 'RPC_CREATE_STUDIO')
# Check URLs
assert hasattr(BaseClient, 'BASE_URL')
assert hasattr(BaseClient, 'BATCHEXECUTE_URL')
# Check constant re-exports
assert hasattr(BaseClient, 'STUDIO_TYPE_AUDIO')
assert hasattr(BaseClient, 'AUDIO_FORMAT_DEEP_DIVE')
|