Skip to content

Commit d08ca43

Browse files
committed
fix: add test for search helpers
1 parent 4488963 commit d08ca43

1 file changed

Lines changed: 105 additions & 0 deletions

File tree

tests/test_search_helpers.py

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,35 @@ def test_workspace_without_legacy_data_skipped(self, tmp_workspace_root):
445445
assert results == []
446446

447447

448+
# ---------------------------------------------------------------------------
449+
# CLI session fixture helper
450+
# ---------------------------------------------------------------------------
451+
452+
453+
def _make_store_db(path: str, meta: dict, json_blobs: dict[str, dict]) -> None:
454+
"""Create a minimal ``store.db`` with *meta* and one or more JSON blobs.
455+
456+
The meta value is hex-encoded JSON, matching the real Cursor CLI format
457+
(see ``utils/cli_chat_reader._read_meta`` and ``traverse_blobs``).
458+
Blob IDs are arbitrary strings; no chain/binary blobs are needed for a
459+
single-message session since ``traverse_blobs`` collects the root blob
460+
directly when it is a JSON blob.
461+
"""
462+
with contextlib.closing(sqlite3.connect(path)) as conn:
463+
conn.execute("CREATE TABLE meta (key TEXT PRIMARY KEY, value TEXT)")
464+
conn.execute("CREATE TABLE blobs (id TEXT PRIMARY KEY, data BLOB)")
465+
conn.execute(
466+
"INSERT INTO meta VALUES ('0', ?)",
467+
(json.dumps(meta).encode("utf-8").hex(),),
468+
)
469+
for blob_id, msg in json_blobs.items():
470+
conn.execute(
471+
"INSERT INTO blobs VALUES (?, ?)",
472+
(blob_id, json.dumps(msg).encode("utf-8")),
473+
)
474+
conn.commit()
475+
476+
448477
# ---------------------------------------------------------------------------
449478
# search_cli_sessions
450479
# ---------------------------------------------------------------------------
@@ -470,3 +499,79 @@ def test_nonexistent_cli_dir_returns_empty(self):
470499
rules=[],
471500
)
472501
assert results == []
502+
503+
def test_seeded_session_found_by_content_match(self, tmp_workspace_root):
504+
"""Seed a real store.db session and verify search_cli_sessions finds it.
505+
506+
Directory layout mirrors the real Cursor CLI storage:
507+
cli_root/{project_id}/{session_id}/store.db
508+
509+
The store.db contains:
510+
- ``meta`` row: hex-encoded JSON with ``latestRootBlobId`` pointing
511+
to the single user-message blob.
512+
- ``blobs`` row: JSON bytes ``{"role": "user", "content": "<term>"}``
513+
where ``<term>`` is the unique query we search for.
514+
"""
515+
dirs = tmp_workspace_root
516+
cli_root = dirs["cli_root"]
517+
project_id = "proj-cli-test"
518+
session_id = "sess-cli-test"
519+
blob_id = "blob-msg-0001"
520+
search_term = "cli-session-unique-sentinel-xyz"
521+
522+
session_dir = os.path.join(cli_root, project_id, session_id)
523+
os.makedirs(session_dir, exist_ok=True)
524+
525+
_make_store_db(
526+
path=os.path.join(session_dir, "store.db"),
527+
meta={
528+
"latestRootBlobId": blob_id,
529+
"name": "CLI search test session",
530+
"createdAt": 1_715_100_000_000,
531+
},
532+
json_blobs={
533+
blob_id: {"role": "user", "content": f"Please help me with {search_term}"},
534+
},
535+
)
536+
537+
results = search_cli_sessions(
538+
cli_chats_path=cli_root,
539+
query=search_term,
540+
query_lower=search_term,
541+
rules=[],
542+
)
543+
544+
assert len(results) >= 1
545+
hit = next((r for r in results if r["chatId"] == session_id), None)
546+
assert hit is not None, f"session {session_id!r} not in results: {results}"
547+
assert hit["type"] == "cli_agent"
548+
assert hit["source"] == "cli"
549+
assert search_term in hit["matchingText"]
550+
551+
def test_seeded_session_not_returned_when_query_misses(self, tmp_workspace_root):
552+
"""Same store.db fixture; a non-matching query must return empty."""
553+
dirs = tmp_workspace_root
554+
cli_root = dirs["cli_root"]
555+
project_id = "proj-cli-miss"
556+
session_id = "sess-cli-miss"
557+
blob_id = "blob-msg-miss"
558+
559+
session_dir = os.path.join(cli_root, project_id, session_id)
560+
os.makedirs(session_dir, exist_ok=True)
561+
562+
_make_store_db(
563+
path=os.path.join(session_dir, "store.db"),
564+
meta={"latestRootBlobId": blob_id, "name": "Miss session", "createdAt": 0},
565+
json_blobs={
566+
blob_id: {"role": "user", "content": "completely unrelated content"},
567+
},
568+
)
569+
570+
results = search_cli_sessions(
571+
cli_chats_path=cli_root,
572+
query="xyzzy-no-match-cli",
573+
query_lower="xyzzy-no-match-cli",
574+
rules=[],
575+
)
576+
577+
assert results == []

0 commit comments

Comments
 (0)