Spaces:
Sleeping
Sleeping
File size: 10,277 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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 | """Tests for services.studio module."""
import json
import pytest
from unittest.mock import MagicMock, patch
from notebooklm_tools.services.studio import (
validate_artifact_type,
resolve_code,
create_artifact,
get_studio_status,
rename_artifact,
delete_artifact,
VALID_ARTIFACT_TYPES,
)
from notebooklm_tools.services.errors import ValidationError, ServiceError
@pytest.fixture
def mock_client():
client = MagicMock()
# Source resolution
client.get_notebook_sources_with_types.return_value = [
{"id": "src-1", "title": "Source 1"},
{"id": "src-2", "title": "Source 2"},
]
# Standard creation result
client.create_audio_overview.return_value = {"artifact_id": "art-1", "status": "in_progress"}
client.create_video_overview.return_value = {"artifact_id": "art-2", "status": "in_progress"}
client.create_infographic.return_value = {"artifact_id": "art-3", "status": "in_progress"}
client.create_slide_deck.return_value = {"artifact_id": "art-4", "status": "in_progress"}
client.create_report.return_value = {"artifact_id": "art-5", "status": "in_progress"}
client.create_flashcards.return_value = {"artifact_id": "art-6", "status": "in_progress"}
client.create_quiz.return_value = {"artifact_id": "art-7", "status": "in_progress"}
client.create_data_table.return_value = {"artifact_id": "art-8", "status": "in_progress"}
# Mind map
client.generate_mind_map.return_value = {
"mind_map_json": json.dumps({"name": "Root", "children": [{"name": "A"}, {"name": "B"}]}),
}
client.save_mind_map.return_value = {
"mind_map_id": "mm-1",
"title": "My Map",
"mind_map_json": json.dumps({"name": "Root", "children": [{"name": "A"}, {"name": "B"}]}),
}
# Status
client.poll_studio_status.return_value = [
{"artifact_id": "a1", "type": "audio", "status": "completed"},
{"artifact_id": "a2", "type": "report", "status": "in_progress"},
]
client.list_mind_maps.return_value = [
{"mind_map_id": "mm-1", "title": "Map 1"},
]
# Rename/delete
client.rename_studio_artifact.return_value = True
client.delete_studio_artifact.return_value = True
return client
class TestValidateArtifactType:
"""Test validate_artifact_type function."""
@pytest.mark.parametrize("artifact_type", sorted(VALID_ARTIFACT_TYPES))
def test_valid_types_pass(self, artifact_type):
validate_artifact_type(artifact_type) # should not raise
def test_invalid_type_raises(self):
with pytest.raises(ValidationError, match="Unknown artifact type"):
validate_artifact_type("podcast")
class TestResolveCode:
"""Test resolve_code function."""
def test_valid_code(self):
mapper = MagicMock()
mapper.get_code.return_value = 42
assert resolve_code(mapper, "deep_dive", "audio format") == 42
def test_invalid_code_raises(self):
mapper = MagicMock()
mapper.get_code.side_effect = ValueError("Unknown")
mapper.names = ["a", "b"]
with pytest.raises(ValidationError, match="Unknown audio format"):
resolve_code(mapper, "bad", "audio format")
class TestCreateArtifact:
"""Test create_artifact function."""
def test_create_audio(self, mock_client):
result = create_artifact(mock_client, "nb-1", "audio")
assert result["artifact_type"] == "audio"
assert result["artifact_id"] == "art-1"
assert "generation started" in result["message"].lower()
def test_create_video(self, mock_client):
result = create_artifact(mock_client, "nb-1", "video")
assert result["artifact_type"] == "video"
assert result["artifact_id"] == "art-2"
def test_create_infographic(self, mock_client):
result = create_artifact(mock_client, "nb-1", "infographic")
assert result["artifact_type"] == "infographic"
assert result["artifact_id"] == "art-3"
def test_create_slide_deck(self, mock_client):
result = create_artifact(mock_client, "nb-1", "slide_deck")
assert result["artifact_type"] == "slide_deck"
assert result["artifact_id"] == "art-4"
def test_create_report(self, mock_client):
result = create_artifact(mock_client, "nb-1", "report")
assert result["artifact_type"] == "report"
assert result["artifact_id"] == "art-5"
def test_create_flashcards(self, mock_client):
result = create_artifact(mock_client, "nb-1", "flashcards")
assert result["artifact_type"] == "flashcards"
assert result["artifact_id"] == "art-6"
def test_create_quiz(self, mock_client):
result = create_artifact(mock_client, "nb-1", "quiz")
assert result["artifact_type"] == "quiz"
assert result["artifact_id"] == "art-7"
def test_create_data_table(self, mock_client):
result = create_artifact(
mock_client, "nb-1", "data_table",
description="Compare features",
)
assert result["artifact_type"] == "data_table"
assert result["artifact_id"] == "art-8"
def test_create_data_table_missing_description(self, mock_client):
with pytest.raises(ValidationError, match="description is required"):
create_artifact(mock_client, "nb-1", "data_table")
def test_create_mind_map(self, mock_client):
result = create_artifact(mock_client, "nb-1", "mind_map")
assert result["artifact_type"] == "mind_map"
assert result["artifact_id"] == "mm-1"
assert result["root_name"] == "Root"
assert result["children_count"] == 2
def test_invalid_type(self, mock_client):
with pytest.raises(ValidationError, match="Unknown artifact type"):
create_artifact(mock_client, "nb-1", "podcast")
def test_uses_provided_source_ids(self, mock_client):
create_artifact(mock_client, "nb-1", "report", source_ids=["s1"])
mock_client.get_notebook_sources_with_types.assert_not_called()
mock_client.create_report.assert_called_once()
def test_fetches_source_ids_when_not_provided(self, mock_client):
create_artifact(mock_client, "nb-1", "report")
mock_client.get_notebook_sources_with_types.assert_called_once_with("nb-1")
def test_no_sources_in_notebook_raises(self, mock_client):
mock_client.get_notebook_sources_with_types.return_value = []
with pytest.raises(ValidationError, match="No sources found"):
create_artifact(mock_client, "nb-1", "report")
def test_no_artifact_id_raises(self, mock_client):
mock_client.create_report.return_value = {}
with pytest.raises(ServiceError, match="rejected"):
create_artifact(mock_client, "nb-1", "report")
def test_api_error_wraps(self, mock_client):
mock_client.create_report.side_effect = RuntimeError("boom")
with pytest.raises(ServiceError, match="Failed to create"):
create_artifact(mock_client, "nb-1", "report")
def test_mind_map_gen_failure(self, mock_client):
mock_client.generate_mind_map.return_value = {}
with pytest.raises(ServiceError, match="Failed to generate"):
create_artifact(mock_client, "nb-1", "mind_map")
def test_mind_map_save_failure(self, mock_client):
mock_client.save_mind_map.return_value = None
with pytest.raises(ServiceError, match="Failed to save mind map"):
create_artifact(mock_client, "nb-1", "mind_map")
class TestGetStudioStatus:
"""Test get_studio_status function."""
def test_returns_combined_artifacts(self, mock_client):
result = get_studio_status(mock_client, "nb-1")
assert result["total"] == 3 # 2 studio + 1 mind map
assert result["completed"] == 2 # 1 studio + 1 mind map
assert result["in_progress"] == 1
def test_mind_map_fetch_failure_ignored(self, mock_client):
mock_client.list_mind_maps.side_effect = RuntimeError("fail")
result = get_studio_status(mock_client, "nb-1")
assert result["total"] == 2 # only studio artifacts
def test_api_error(self, mock_client):
mock_client.poll_studio_status.side_effect = RuntimeError("fail")
with pytest.raises(ServiceError, match="Failed to poll"):
get_studio_status(mock_client, "nb-1")
class TestRenameArtifact:
"""Test rename_artifact function."""
def test_success(self, mock_client):
result = rename_artifact(mock_client, "art-1", "New Title")
assert result["artifact_id"] == "art-1"
assert result["new_title"] == "New Title"
def test_missing_artifact_id(self, mock_client):
with pytest.raises(ValidationError, match="artifact_id is required"):
rename_artifact(mock_client, "", "Title")
def test_missing_new_title(self, mock_client):
with pytest.raises(ValidationError, match="new_title is required"):
rename_artifact(mock_client, "art-1", "")
def test_falsy_result(self, mock_client):
mock_client.rename_studio_artifact.return_value = False
with pytest.raises(ServiceError, match="Rename returned falsy"):
rename_artifact(mock_client, "art-1", "Title")
def test_api_error(self, mock_client):
mock_client.rename_studio_artifact.side_effect = RuntimeError("fail")
with pytest.raises(ServiceError, match="Failed to rename"):
rename_artifact(mock_client, "art-1", "Title")
class TestDeleteArtifact:
"""Test delete_artifact function."""
def test_success(self, mock_client):
delete_artifact(mock_client, "art-1", "nb-1")
mock_client.delete_studio_artifact.assert_called_once_with("art-1", notebook_id="nb-1")
def test_falsy_result(self, mock_client):
mock_client.delete_studio_artifact.return_value = False
with pytest.raises(ServiceError, match="Delete returned falsy"):
delete_artifact(mock_client, "art-1", "nb-1")
def test_api_error(self, mock_client):
mock_client.delete_studio_artifact.side_effect = RuntimeError("fail")
with pytest.raises(ServiceError, match="Failed to delete"):
delete_artifact(mock_client, "art-1", "nb-1")
|