63 lines
2.5 KiB
Python
63 lines
2.5 KiB
Python
"""Red-first: #26 — shard connections must load sqlite-vec per connection.
|
|
get_shard() previously returned a raw connection: entries_vec invisible,
|
|
recall() crashed with 'no such table' in clean environments (CI canary)."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import importlib.util
|
|
import sqlite3
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
ROOT = Path(__file__).resolve().parent.parent
|
|
sys.path.insert(0, str(ROOT / "src"))
|
|
|
|
SPEC = importlib.util.spec_from_file_location("shard_manager", ROOT / "src" / "rune" / "shard_manager.py")
|
|
sm_mod = importlib.util.module_from_spec(SPEC)
|
|
sys.modules["shard_manager"] = sm_mod # register so importlib.reload() works
|
|
SPEC.loader.exec_module(sm_mod)
|
|
|
|
|
|
@pytest.fixture
|
|
def shard_env(tmp_path, monkeypatch):
|
|
"""Point ShardManager's SHARD_DIR/SHARD_INDEX at a temp home — no
|
|
importlib.reload (module-constant patching is deterministic and
|
|
side-effect free). SHARD_INDEX must be patched in the Rust-core mode too:
|
|
the kernel receives the index path explicitly (no _load_index indirection
|
|
to monkeypatch)."""
|
|
shard_dir = tmp_path / "shards"
|
|
shard_dir.mkdir()
|
|
monkeypatch.setattr(sm_mod, "SHARD_DIR", str(shard_dir))
|
|
monkeypatch.setattr(sm_mod, "SHARD_INDEX", str(tmp_path / "shard_index.json"))
|
|
# fresh index per test
|
|
monkeypatch.setattr(sm_mod.ShardManager, "_load_index", lambda self: {})
|
|
monkeypatch.setattr(sm_mod.ShardManager, "_save_index", lambda self: None)
|
|
yield sm_mod
|
|
|
|
|
|
def test_get_shard_connection_can_query_entries_vec(shard_env):
|
|
"""The exact CI canary failure: recall() SELECT ... FROM entries_vec on a
|
|
shard connection must not raise 'no such table'."""
|
|
mgr = sm_mod.ShardManager()
|
|
db = mgr.get_shard("guitar academy")
|
|
rows = db.execute("SELECT rowid FROM entries_vec LIMIT 1").fetchall()
|
|
assert rows == [] # table exists and is queryable — empty is fine
|
|
|
|
|
|
def test_create_shard_schema_includes_entries_vec(shard_env):
|
|
mgr = sm_mod.ShardManager()
|
|
path = mgr.create_shard("test topic")
|
|
db = sqlite3.connect(path)
|
|
tables = {r[0] for r in db.execute("SELECT name FROM sqlite_master WHERE type='table'")}
|
|
assert "entries_vec" in tables, "shard schema must include the vec table (recall routes here)"
|
|
|
|
|
|
def test_get_shard_reuses_existing_shard_file(shard_env):
|
|
mgr = sm_mod.ShardManager()
|
|
path1 = mgr.create_shard("reuse")
|
|
mgr.index["reuse"] = path1
|
|
db = mgr.get_shard("reuse")
|
|
db.execute("SELECT 1 FROM entries_vec").fetchall() # no raise
|