mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-06-06 20:15:17 +02:00
refactor(tests): Update tests to remove summary references and adjust for embedding errors
This commit is contained in:
parent
e4d7b01b09
commit
e588782a9b
17 changed files with 69 additions and 148 deletions
|
|
@ -1,7 +1,7 @@
|
|||
import importlib
|
||||
import sys
|
||||
import uuid
|
||||
from unittest.mock import AsyncMock, MagicMock
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
import pytest
|
||||
import pytest_asyncio
|
||||
|
|
@ -123,18 +123,6 @@ async def db_search_space(db_session: AsyncSession, db_user: User) -> SearchSpac
|
|||
return space
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def patched_summarize(monkeypatch) -> AsyncMock:
|
||||
mock = AsyncMock(return_value="Mocked summary.")
|
||||
return mock
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def patched_summarize_raises(monkeypatch) -> AsyncMock:
|
||||
mock = AsyncMock(side_effect=RuntimeError("LLM unavailable"))
|
||||
return mock
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def patched_embed_texts(monkeypatch) -> MagicMock:
|
||||
mock = MagicMock(side_effect=lambda texts: [[0.1] * _EMBEDDING_DIM for _ in texts])
|
||||
|
|
@ -145,6 +133,16 @@ def patched_embed_texts(monkeypatch) -> MagicMock:
|
|||
return mock
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def patched_embed_texts_raises(monkeypatch) -> MagicMock:
|
||||
mock = MagicMock(side_effect=RuntimeError("Embedding unavailable"))
|
||||
monkeypatch.setattr(
|
||||
"app.indexing_pipeline.indexing_pipeline_service.embed_texts",
|
||||
mock,
|
||||
)
|
||||
return mock
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def patched_chunk_text(monkeypatch) -> MagicMock:
|
||||
mock = MagicMock(return_value=["Test chunk content."])
|
||||
|
|
|
|||
|
|
@ -32,8 +32,8 @@ async def test_sets_status_ready(db_session, db_search_space, db_user, mocker):
|
|||
@pytest.mark.usefixtures(
|
||||
"patched_embed_texts", "patched_chunk_text"
|
||||
)
|
||||
async def test_content_is_summary(db_session, db_search_space, db_user, mocker):
|
||||
"""Document content is set to the LLM-generated summary."""
|
||||
async def test_content_is_source_markdown(db_session, db_search_space, db_user, mocker):
|
||||
"""Document content is set to the extracted source markdown."""
|
||||
adapter = UploadDocumentAdapter(db_session)
|
||||
await adapter.index(
|
||||
markdown_content="## Hello\n\nSome content.",
|
||||
|
|
@ -48,7 +48,7 @@ async def test_content_is_summary(db_session, db_search_space, db_user, mocker):
|
|||
)
|
||||
document = result.scalars().first()
|
||||
|
||||
assert document.content == "Mocked summary."
|
||||
assert document.content == "## Hello\n\nSome content."
|
||||
|
||||
|
||||
@pytest.mark.usefixtures(
|
||||
|
|
@ -79,9 +79,7 @@ async def test_chunks_written_to_db(db_session, db_search_space, db_user, mocker
|
|||
assert chunks[0].content == "Test chunk content."
|
||||
|
||||
|
||||
@pytest.mark.usefixtures(
|
||||
"patched_summarize_raises", "patched_embed_texts", "patched_chunk_text"
|
||||
)
|
||||
@pytest.mark.usefixtures("patched_embed_texts_raises", "patched_chunk_text")
|
||||
async def test_raises_on_indexing_failure(db_session, db_search_space, db_user, mocker):
|
||||
"""RuntimeError is raised when the indexing step fails so the caller can fire a failure notification."""
|
||||
adapter = UploadDocumentAdapter(db_session)
|
||||
|
|
@ -92,7 +90,7 @@ async def test_raises_on_indexing_failure(db_session, db_search_space, db_user,
|
|||
etl_service="UNSTRUCTURED",
|
||||
search_space_id=db_search_space.id,
|
||||
user_id=str(db_user.id),
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
|
|
@ -104,7 +102,7 @@ async def test_raises_on_indexing_failure(db_session, db_search_space, db_user,
|
|||
"patched_embed_texts", "patched_chunk_text"
|
||||
)
|
||||
async def test_reindex_updates_content(db_session, db_search_space, db_user, mocker):
|
||||
"""Document content is updated to the new summary after reindexing."""
|
||||
"""Document content is updated to the new source markdown after reindexing."""
|
||||
adapter = UploadDocumentAdapter(db_session)
|
||||
await adapter.index(
|
||||
markdown_content="## Original\n\nOriginal content.",
|
||||
|
|
@ -125,7 +123,7 @@ async def test_reindex_updates_content(db_session, db_search_space, db_user, moc
|
|||
await adapter.reindex(document=document)
|
||||
|
||||
await db_session.refresh(document)
|
||||
assert document.content == "Mocked summary."
|
||||
assert document.content == "## Edited\n\nNew content after user edit."
|
||||
|
||||
|
||||
@pytest.mark.usefixtures(
|
||||
|
|
@ -256,7 +254,9 @@ async def test_reindex_clears_reindexing_flag(
|
|||
|
||||
|
||||
@pytest.mark.usefixtures("patched_embed_texts", "patched_chunk_text")
|
||||
async def test_reindex_raises_on_failure(db_session, db_search_space, db_user, mocker):
|
||||
async def test_reindex_raises_on_failure(
|
||||
db_session, db_search_space, db_user, patched_embed_texts, mocker
|
||||
):
|
||||
"""RuntimeError is raised when reindexing fails so the caller can handle it."""
|
||||
|
||||
adapter = UploadDocumentAdapter(db_session)
|
||||
|
|
@ -276,6 +276,8 @@ async def test_reindex_raises_on_failure(db_session, db_search_space, db_user, m
|
|||
document.source_markdown = "## Edited\n\nNew content after user edit."
|
||||
await db_session.flush()
|
||||
|
||||
patched_embed_texts.side_effect = RuntimeError("Embedding unavailable")
|
||||
|
||||
with pytest.raises(RuntimeError, match=r"Embedding failed|Reindexing failed"):
|
||||
await adapter.reindex(document=document)
|
||||
|
||||
|
|
|
|||
|
|
@ -259,16 +259,14 @@ async def test_reindex_replaces_old_chunks(
|
|||
assert len(chunks) == 1
|
||||
|
||||
|
||||
@pytest.mark.usefixtures(
|
||||
"patched_summarize_raises", "patched_embed_texts", "patched_chunk_text"
|
||||
)
|
||||
async def test_llm_error_sets_status_failed(
|
||||
@pytest.mark.usefixtures("patched_embed_texts_raises", "patched_chunk_text")
|
||||
async def test_embedding_error_sets_status_failed(
|
||||
db_session,
|
||||
db_search_space,
|
||||
make_connector_document,
|
||||
mocker,
|
||||
):
|
||||
"""Document status is FAILED when the LLM raises during indexing."""
|
||||
"""Document status is FAILED when embedding raises during indexing."""
|
||||
connector_doc = make_connector_document(search_space_id=db_search_space.id)
|
||||
service = IndexingPipelineService(session=db_session)
|
||||
|
||||
|
|
@ -286,10 +284,8 @@ async def test_llm_error_sets_status_failed(
|
|||
assert DocumentStatus.is_state(reloaded.status, DocumentStatus.FAILED)
|
||||
|
||||
|
||||
@pytest.mark.usefixtures(
|
||||
"patched_summarize_raises", "patched_embed_texts", "patched_chunk_text"
|
||||
)
|
||||
async def test_llm_error_leaves_no_partial_data(
|
||||
@pytest.mark.usefixtures("patched_embed_texts_raises", "patched_chunk_text")
|
||||
async def test_embedding_error_leaves_no_partial_data(
|
||||
db_session,
|
||||
db_search_space,
|
||||
make_connector_document,
|
||||
|
|
|
|||
|
|
@ -1271,7 +1271,7 @@ class TestIndexingProgressFlag:
|
|||
original_index = IndexingPipelineService.index
|
||||
flag_observed = []
|
||||
|
||||
async def patched_index(self_pipe, document, connector_doc, llm):
|
||||
async def patched_index(self_pipe, document, connector_doc):
|
||||
folder = (
|
||||
await db_session.execute(
|
||||
select(Folder).where(
|
||||
|
|
@ -1283,7 +1283,7 @@ class TestIndexingProgressFlag:
|
|||
if folder:
|
||||
meta = folder.folder_metadata or {}
|
||||
flag_observed.append(meta.get("indexing_in_progress", False))
|
||||
return await original_index(self_pipe, document, connector_doc, llm)
|
||||
return await original_index(self_pipe, document, connector_doc)
|
||||
|
||||
IndexingPipelineService.index = patched_index
|
||||
try:
|
||||
|
|
|
|||
|
|
@ -338,9 +338,7 @@ async def test_same_content_from_different_source_is_skipped(
|
|||
assert len(result.scalars().all()) == 1
|
||||
|
||||
|
||||
@pytest.mark.usefixtures(
|
||||
"patched_summarize_raises", "patched_embed_texts", "patched_chunk_text"
|
||||
)
|
||||
@pytest.mark.usefixtures("patched_embed_texts_raises", "patched_chunk_text")
|
||||
async def test_failed_document_with_unchanged_content_is_requeued(
|
||||
db_session,
|
||||
db_search_space,
|
||||
|
|
@ -351,7 +349,7 @@ async def test_failed_document_with_unchanged_content_is_requeued(
|
|||
doc = make_connector_document(search_space_id=db_search_space.id)
|
||||
service = IndexingPipelineService(session=db_session)
|
||||
|
||||
# First run: document is created and indexing crashes → status = failed
|
||||
# First run: document is created and indexing crashes, so status becomes failed.
|
||||
prepared = await service.prepare_for_indexing([doc])
|
||||
document_id = prepared[0].id
|
||||
await service.index(prepared[0], doc)
|
||||
|
|
|
|||
|
|
@ -87,18 +87,6 @@ async def test_build_connector_doc_produces_correct_fields():
|
|||
assert doc.metadata["connector_id"] == _CONNECTOR_ID
|
||||
assert doc.metadata["document_type"] == "Confluence Page"
|
||||
assert doc.metadata["connector_type"] == "Confluence"
|
||||
assert "Engineering Handbook" in doc.deterministic_preview
|
||||
assert markdown in doc.deterministic_preview
|
||||
|
||||
|
||||
async def test_build_connector_doc_summary_disabled():
|
||||
doc = _build_connector_doc(
|
||||
_make_page(),
|
||||
_to_markdown(_make_page()),
|
||||
connector_id=_CONNECTOR_ID,
|
||||
search_space_id=_SEARCH_SPACE_ID,
|
||||
user_id=_USER_ID,
|
||||
)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
|
|
|
|||
|
|
@ -294,12 +294,6 @@ def full_scan_mocks(mock_drive_client, monkeypatch):
|
|||
MagicMock(return_value=pipeline_mock),
|
||||
)
|
||||
|
||||
monkeypatch.setattr(
|
||||
_mod,
|
||||
"get_agent_llm",
|
||||
AsyncMock(return_value=MagicMock()),
|
||||
)
|
||||
|
||||
return {
|
||||
"drive_client": mock_drive_client,
|
||||
"session": mock_session,
|
||||
|
|
@ -480,12 +474,6 @@ async def test_delta_sync_removals_serial_rest_parallel(monkeypatch):
|
|||
"IndexingPipelineService",
|
||||
MagicMock(return_value=pipeline_mock),
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
_mod,
|
||||
"get_agent_llm",
|
||||
AsyncMock(return_value=MagicMock()),
|
||||
)
|
||||
|
||||
mock_session, _ = _make_page_limit_session()
|
||||
mock_task_logger = MagicMock()
|
||||
mock_task_logger.log_task_progress = AsyncMock()
|
||||
|
|
|
|||
|
|
@ -88,20 +88,6 @@ async def test_build_connector_doc_produces_correct_fields():
|
|||
assert doc.metadata["connector_id"] == _CONNECTOR_ID
|
||||
assert doc.metadata["document_type"] == "Linear Issue"
|
||||
assert doc.metadata["connector_type"] == "Linear"
|
||||
assert "ENG-42" in doc.deterministic_preview
|
||||
assert markdown in doc.deterministic_preview
|
||||
|
||||
|
||||
async def test_build_connector_doc_summary_disabled():
|
||||
"""When enable_vision_llm is False, deterministic_content is False."""
|
||||
doc = _build_connector_doc(
|
||||
_make_issue(),
|
||||
_make_formatted_issue(),
|
||||
"# content",
|
||||
connector_id=_CONNECTOR_ID,
|
||||
search_space_id=_SEARCH_SPACE_ID,
|
||||
user_id=_USER_ID,
|
||||
)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
|
|
|
|||
|
|
@ -55,19 +55,6 @@ async def test_build_connector_doc_produces_correct_fields():
|
|||
assert doc.metadata["connector_id"] == _CONNECTOR_ID
|
||||
assert doc.metadata["document_type"] == "Notion Page"
|
||||
assert doc.metadata["connector_type"] == "Notion"
|
||||
assert "My Notion Page" in doc.deterministic_preview
|
||||
assert markdown in doc.deterministic_preview
|
||||
|
||||
|
||||
async def test_build_connector_doc_summary_disabled():
|
||||
"""When enable_vision_llm is False, deterministic_content is False."""
|
||||
doc = _build_connector_doc(
|
||||
_make_page(),
|
||||
"# content",
|
||||
connector_id=_CONNECTOR_ID,
|
||||
search_space_id=_SEARCH_SPACE_ID,
|
||||
user_id=_USER_ID,
|
||||
)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
|
|
|
|||
|
|
@ -335,10 +335,6 @@ def gdrive_full_scan_mocks(monkeypatch):
|
|||
monkeypatch.setattr(
|
||||
_mod, "IndexingPipelineService", MagicMock(return_value=pipeline_mock)
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
_mod, "get_agent_llm", AsyncMock(return_value=MagicMock())
|
||||
)
|
||||
|
||||
return {
|
||||
"mod": _mod,
|
||||
"session": session,
|
||||
|
|
@ -452,10 +448,6 @@ async def test_gdrive_delta_sync_skips_over_quota(monkeypatch):
|
|||
monkeypatch.setattr(
|
||||
_mod, "IndexingPipelineService", MagicMock(return_value=pipeline_mock)
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
_mod, "get_agent_llm", AsyncMock(return_value=MagicMock())
|
||||
)
|
||||
|
||||
mock_task_logger = MagicMock()
|
||||
mock_task_logger.log_task_progress = AsyncMock()
|
||||
|
||||
|
|
|
|||
|
|
@ -69,6 +69,13 @@ def _signed_slack_request(payload: dict, *, secret: str = "signing-secret") -> R
|
|||
)
|
||||
|
||||
|
||||
def _enable_slack_gateway(monkeypatch):
|
||||
monkeypatch.setattr(routes.config, "GATEWAY_SLACK_ENABLED", True)
|
||||
monkeypatch.setattr(routes.config, "GATEWAY_SLACK_CLIENT_ID", "client-id")
|
||||
monkeypatch.setattr(routes.config, "GATEWAY_SLACK_CLIENT_SECRET", "client-secret")
|
||||
monkeypatch.setattr(routes.config, "GATEWAY_SLACK_SIGNING_SECRET", "signing-secret")
|
||||
|
||||
|
||||
async def _call_webhook(*, request: RequestStub, account_id: int, session):
|
||||
return await routes.telegram_webhook(
|
||||
request=request,
|
||||
|
|
@ -207,7 +214,7 @@ def test_verify_slack_signature_accepts_valid_signature():
|
|||
|
||||
@pytest.mark.asyncio
|
||||
async def test_slack_webhook_url_verification(monkeypatch, mocker):
|
||||
monkeypatch.setattr(routes.config, "GATEWAY_SLACK_SIGNING_SECRET", "signing-secret")
|
||||
_enable_slack_gateway(monkeypatch)
|
||||
request = _signed_slack_request({"type": "url_verification", "challenge": "abc123"})
|
||||
|
||||
response = await routes.slack_webhook(request=request, session=mocker.AsyncMock())
|
||||
|
|
@ -218,7 +225,7 @@ async def test_slack_webhook_url_verification(monkeypatch, mocker):
|
|||
|
||||
@pytest.mark.asyncio
|
||||
async def test_slack_webhook_persists_event(monkeypatch, mocker):
|
||||
monkeypatch.setattr(routes.config, "GATEWAY_SLACK_SIGNING_SECRET", "signing-secret")
|
||||
_enable_slack_gateway(monkeypatch)
|
||||
session = mocker.AsyncMock()
|
||||
monkeypatch.setattr(routes, "get_slack_account_by_team", mocker.AsyncMock(return_value=_slack_account()))
|
||||
persist = mocker.AsyncMock(return_value=100)
|
||||
|
|
@ -248,7 +255,7 @@ async def test_slack_webhook_persists_event(monkeypatch, mocker):
|
|||
|
||||
@pytest.mark.asyncio
|
||||
async def test_slack_webhook_ignores_self_event(monkeypatch, mocker):
|
||||
monkeypatch.setattr(routes.config, "GATEWAY_SLACK_SIGNING_SECRET", "signing-secret")
|
||||
_enable_slack_gateway(monkeypatch)
|
||||
session = mocker.AsyncMock()
|
||||
monkeypatch.setattr(routes, "get_slack_account_by_team", mocker.AsyncMock(return_value=_slack_account()))
|
||||
persist = mocker.AsyncMock(return_value=100)
|
||||
|
|
@ -275,7 +282,7 @@ async def test_slack_webhook_ignores_self_event(monkeypatch, mocker):
|
|||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_discord_gateway_install_returns_oauth_url(monkeypatch):
|
||||
async def test_discord_gateway_install_returns_oauth_url(monkeypatch, mocker):
|
||||
monkeypatch.setattr(routes.config, "DISCORD_CLIENT_ID", "discord-client")
|
||||
monkeypatch.setattr(
|
||||
routes.config,
|
||||
|
|
@ -283,10 +290,12 @@ async def test_discord_gateway_install_returns_oauth_url(monkeypatch):
|
|||
"http://localhost:8000/api/v1/gateway/discord/callback",
|
||||
)
|
||||
monkeypatch.setattr(routes.config, "SECRET_KEY", "test-secret")
|
||||
monkeypatch.setattr(routes, "check_search_space_access", mocker.AsyncMock())
|
||||
|
||||
response = await routes.install_discord_gateway(
|
||||
search_space_id=123,
|
||||
user=SimpleNamespace(id="00000000-0000-0000-0000-000000000001"),
|
||||
session=mocker.AsyncMock(),
|
||||
)
|
||||
|
||||
assert response["auth_url"].startswith("https://discord.com/api/oauth2/authorize?")
|
||||
|
|
|
|||
|
|
@ -37,12 +37,10 @@ async def test_calls_prepare_then_index_per_document(pipeline, make_connector_do
|
|||
orm2 = MagicMock(spec=Document)
|
||||
orm2.unique_identifier_hash = compute_unique_identifier_hash(doc2)
|
||||
|
||||
mock_llm = MagicMock()
|
||||
|
||||
pipeline.prepare_for_indexing = AsyncMock(return_value=[orm1, orm2])
|
||||
pipeline.index = AsyncMock(side_effect=lambda doc, cdoc, llm: doc)
|
||||
pipeline.index = AsyncMock(side_effect=lambda doc, cdoc: doc)
|
||||
|
||||
results = await pipeline.index_batch([doc1, doc2], mock_llm)
|
||||
results = await pipeline.index_batch([doc1, doc2])
|
||||
|
||||
pipeline.prepare_for_indexing.assert_awaited_once_with([doc1, doc2])
|
||||
assert pipeline.index.await_count == 2
|
||||
|
|
@ -53,7 +51,7 @@ async def test_empty_input_returns_empty(pipeline):
|
|||
"""Empty connector_docs list returns empty result."""
|
||||
pipeline.prepare_for_indexing = AsyncMock(return_value=[])
|
||||
|
||||
results = await pipeline.index_batch([], MagicMock())
|
||||
results = await pipeline.index_batch([])
|
||||
|
||||
assert results == []
|
||||
|
||||
|
|
@ -74,7 +72,7 @@ async def test_skips_document_without_matching_connector_doc(
|
|||
pipeline.prepare_for_indexing = AsyncMock(return_value=[orphan_orm])
|
||||
pipeline.index = AsyncMock()
|
||||
|
||||
results = await pipeline.index_batch([doc1], MagicMock())
|
||||
results = await pipeline.index_batch([doc1])
|
||||
|
||||
pipeline.index.assert_not_awaited()
|
||||
assert results == []
|
||||
|
|
|
|||
|
|
@ -183,19 +183,14 @@ async def test_batch_parallel_indexes_all_documents(
|
|||
|
||||
index_calls = []
|
||||
|
||||
async def fake_index(self, document, connector_doc, llm):
|
||||
async def fake_index(self, document, connector_doc):
|
||||
index_calls.append(document.id)
|
||||
document.status = DocumentStatus.ready()
|
||||
return document
|
||||
|
||||
monkeypatch.setattr(IndexingPipelineService, "index", fake_index)
|
||||
|
||||
async def mock_get_llm(session):
|
||||
return MagicMock()
|
||||
|
||||
_, indexed, failed = await pipeline.index_batch_parallel(
|
||||
docs, mock_get_llm, max_concurrency=2
|
||||
)
|
||||
_, indexed, failed = await pipeline.index_batch_parallel(docs, max_concurrency=2)
|
||||
|
||||
assert indexed == 3
|
||||
assert failed == 0
|
||||
|
|
@ -224,20 +219,15 @@ async def test_batch_parallel_one_failure_does_not_affect_others(
|
|||
_mock_session_factory(orm_by_id),
|
||||
)
|
||||
|
||||
async def failing_index(self, document, connector_doc, llm):
|
||||
async def failing_index(self, document, connector_doc):
|
||||
if document.id == 2:
|
||||
raise RuntimeError("LLM exploded")
|
||||
raise RuntimeError("Indexing exploded")
|
||||
document.status = DocumentStatus.ready()
|
||||
return document
|
||||
|
||||
monkeypatch.setattr(IndexingPipelineService, "index", failing_index)
|
||||
|
||||
async def mock_get_llm(session):
|
||||
return MagicMock()
|
||||
|
||||
_, indexed, failed = await pipeline.index_batch_parallel(
|
||||
docs, mock_get_llm, max_concurrency=4
|
||||
)
|
||||
_, indexed, failed = await pipeline.index_batch_parallel(docs, max_concurrency=4)
|
||||
|
||||
assert indexed == 2
|
||||
assert failed == 1
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue