* feat(src): migrate second-brain RAG CLI + tests * fix(embedder): strip hardcoded ai.slaid098.dev endpoint * refactor: rename slaid098/opencode to opencode-config * docs(handoff): add pr-5 handoff + ADR-001 * fix(docs): rebase handoff/ADR naming to PR number + drop dangling ADR-009 refs * fix(tests): update script paths + assertions for opencode-config migration * fix(pyproject): update cov + ruff paths config/scripts -> .opencode/scripts * docs: update project map + handoff + ADR --------- Co-authored-by: opencode-agent <agent@slaid098.dev>
58 lines
1.7 KiB
Python
58 lines
1.7 KiB
Python
import json
|
|
from argparse import Namespace
|
|
from pathlib import Path
|
|
from unittest.mock import patch
|
|
|
|
import numpy as np
|
|
import pytest
|
|
import src.memory.search as search_mod
|
|
from src.memory.search import _cosine_sim
|
|
|
|
|
|
class TestCosineSim:
|
|
def test_identical(self) -> None:
|
|
v = np.array([1.0, 2.0, 3.0])
|
|
assert _cosine_sim(v, v) == pytest.approx(1.0)
|
|
|
|
def test_orthogonal(self) -> None:
|
|
a = np.array([1.0, 0.0])
|
|
b = np.array([0.0, 1.0])
|
|
assert _cosine_sim(a, b) == pytest.approx(0.0)
|
|
|
|
def test_zero_vector(self) -> None:
|
|
a = np.array([0.0, 0.0])
|
|
b = np.array([1.0, 1.0])
|
|
assert _cosine_sim(a, b) == pytest.approx(0.0)
|
|
|
|
def test_parallel(self) -> None:
|
|
a = np.array([1.0, 2.0])
|
|
b = np.array([2.0, 4.0])
|
|
assert _cosine_sim(a, b) == pytest.approx(1.0)
|
|
|
|
def test_opposite(self) -> None:
|
|
a = np.array([1.0, 1.0])
|
|
b = np.array([-1.0, -1.0])
|
|
assert _cosine_sim(a, b) == pytest.approx(-1.0)
|
|
|
|
|
|
class TestSearchOutput:
|
|
def test_search_json_format(self, tmp_path: Path) -> None:
|
|
index_dir = tmp_path / ".rag"
|
|
index_dir.mkdir()
|
|
index = {
|
|
"files": [
|
|
{
|
|
"source": "test.md",
|
|
"text": "hello world",
|
|
"embedding": [1.0, 0.0, 0.0],
|
|
},
|
|
],
|
|
}
|
|
(index_dir / "index.json").write_text(json.dumps(index))
|
|
|
|
def fake_embed_texts(texts):
|
|
return [[1.0, 0.0, 0.0]]
|
|
|
|
with patch.object(search_mod, "embed_texts", fake_embed_texts):
|
|
args = Namespace(index_dir=str(index_dir), query="hello", k=5, json=True)
|
|
search_mod.run_search(args)
|