Skip to content
18 changes: 17 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,32 @@

from __future__ import annotations

import json
import os
import shutil
from collections.abc import Mapping
from pathlib import Path
from unittest.mock import patch

import pytest
from hypothesis import settings

from app import create_app


def write_session(path: Path, lines: list[dict[str, object]]) -> None:
"""Write JSONL session *lines* to *path*, creating parent directories."""
path.parent.mkdir(parents=True, exist_ok=True)
with path.open("w", encoding="utf-8") as handle:
for line in lines:
handle.write(json.dumps(line, ensure_ascii=False) + "\n")


def index_patches(cache_root: Path):
"""Return the patch context managers that point the index at *cache_root*."""
return (patch("utils.search_index.cache_dir", return_value=cache_root),)


# Hypothesis profiles drive fuzz example counts/deadlines (deadline disabled to
# avoid timing flakiness on slow/CI runners). CI runs fewer examples for speed.
settings.register_profile("dev", max_examples=200, deadline=None)
Expand Down Expand Up @@ -67,7 +83,7 @@ def client(tmp_path, export_state_file):

@pytest.fixture
def client_single(tmp_path, export_state_file):
"""Flask test client with one seeded session ? for search/limit tests."""
"""Flask test client with one seeded session for search/limit tests."""
return _make_test_client(tmp_path, {"session_abc123.jsonl": "session_minimal.jsonl"})


Expand Down
17 changes: 5 additions & 12 deletions tests/test_search_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from __future__ import annotations

import json
import sqlite3
from contextlib import closing, contextmanager
from datetime import UTC, datetime, timedelta
Expand All @@ -12,6 +11,7 @@
import pytest

from api.search import _resolve_search_results, _search_via_index
from tests.conftest import index_patches as _index_patches, write_session as _write_session
from utils.exclusion_rules import load_rules
from utils.search_index import (
build_search_index,
Expand All @@ -26,17 +26,6 @@
)


def _write_session(path: Path, lines: list[dict[str, object]]) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
with path.open("w", encoding="utf-8") as handle:
for line in lines:
handle.write(json.dumps(line, ensure_ascii=False) + "\n")


def _index_patches(cache_root: Path):
return (patch("utils.search_index.cache_dir", return_value=cache_root),)


@pytest.fixture
def indexed_tree(tmp_path, monkeypatch):
"""Temp projects dir + isolated search index cache."""
Expand Down Expand Up @@ -381,11 +370,15 @@ def test_background_worker_starts_once(self, indexed_tree, monkeypatch):
patches[0],
patch("utils.search_index.threading.Thread") as mock_thread,
):
# The mock worker is not a real thread; report it as stopped so the
# teardown reset does not treat it as a live worker that won't join.
mock_thread.return_value.is_alive.return_value = False
start_search_index_background(indexed_tree["projects"], [])
start_search_index_background(indexed_tree["projects"], [])
assert mock_thread.call_count == 1
assert mock_thread.call_args.kwargs.get("daemon") is True
assert mock_thread.return_value.start.call_count == 1
reset_background_for_tests()


class TestIndexSearchCompleteness:
Expand Down
Loading
Loading