bcbd1bdb22
Integ / changes (push) Has been skipped
Pre-commit / pre-commit (push) Failing after 1s
CLI exit codes / changes (push) Has been skipped
Test (Install) / changes (push) Has been skipped
Test (Python) / changes (push) Has been skipped
Test (TypeScript) / changes (push) Has been skipped
CLI exit codes / cli-gate (push) Has been cancelled
Test (Install) / test-install-gate (push) Has been cancelled
Integ / integ-gate (push) Has been cancelled
Test (Python) / test-python-gate (push) Has been cancelled
Test (TypeScript) / test-typescript-gate (push) Has been cancelled
Test (Install) / python-minimal (3.12) (push) Has been cancelled
Test (Install) / python-minimal (3.11) (push) Has been cancelled
Test (Install) / python-extra (agno, mirage.agents.agno) (push) Has been cancelled
Test (Install) / python-extra (chroma, mirage.resource.chroma) (push) Has been cancelled
Test (Install) / python-extra (pdf, mirage.core.filetype.pdf) (push) Has been cancelled
Integ / integ (push) Has been cancelled
Integ / integ-database (push) Has been cancelled
Integ / integ-database-ts (push) Has been cancelled
Integ / integ-data (push) Has been cancelled
Integ / integ-ssh (push) Has been cancelled
Integ / integ-ssh-ts (push) Has been cancelled
Test (Python) / audit (push) Has been cancelled
Test (TypeScript) / test (push) Has been cancelled
Test (TypeScript) / python-fs-shim (push) Has been cancelled
CLI exit codes / Python CLI (push) Has been cancelled
CLI exit codes / TypeScript CLI (push) Has been cancelled
CLI exit codes / Cross-language snapshot interop (push) Has been cancelled
Test (Python) / test (push) Has been cancelled
Test (Python) / import-isolation (deepagents, openai, mirage.agents.openai_agents) (push) Has been cancelled
Test (Python) / import-isolation (deepagents, pydantic-ai, mirage.agents.pydantic_ai) (push) Has been cancelled
Integ / integ-ts (push) Has been cancelled
Integ / integ-fuse (push) Has been cancelled
Test (Install) / python-extra (databricks, mirage.resource.databricks_volume) (push) Has been cancelled
Test (Install) / python-extra (deepagents, mirage.agents.langchain) (push) Has been cancelled
Test (Install) / python-extra (email, mirage.resource.email) (push) Has been cancelled
Test (Install) / python-extra (fuse, mirage.fuse.mount) (push) Has been cancelled
Test (Install) / python-extra (hdf5, mirage.core.filetype.hdf5) (push) Has been cancelled
Test (Install) / python-extra (hf, mirage.resource.hf_buckets) (push) Has been cancelled
Test (Install) / python-extra (lancedb, mirage.resource.lancedb) (push) Has been cancelled
Test (Install) / python-extra (langfuse, mirage.resource.langfuse) (push) Has been cancelled
Test (Install) / python-extra (mongodb, mirage.resource.mongodb) (push) Has been cancelled
Test (Install) / python-extra (nextcloud, mirage.resource.nextcloud) (push) Has been cancelled
Test (Install) / python-extra (openai, mirage.agents.openai_agents) (push) Has been cancelled
Test (Install) / python-extra (openhands, mirage.agents.openhands, 3.12) (push) Has been cancelled
Test (Install) / python-extra (parquet, mirage.core.filetype.parquet) (push) Has been cancelled
Test (Install) / python-extra (postgres, mirage.resource.postgres) (push) Has been cancelled
Test (Install) / python-extra (pydantic-ai, mirage.agents.pydantic_ai) (push) Has been cancelled
Test (Install) / python-extra (qdrant, mirage.resource.qdrant) (push) Has been cancelled
Test (Install) / python-extra (redis, mirage.resource.redis) (push) Has been cancelled
Test (Install) / python-extra (s3, mirage.resource.s3) (push) Has been cancelled
Test (Install) / python-extra (ssh, mirage.resource.ssh) (push) Has been cancelled
Test (Install) / ts-minimal (push) Has been cancelled
149 lines
4.4 KiB
Python
149 lines
4.4 KiB
Python
import json
|
|
from functools import partial
|
|
from types import SimpleNamespace
|
|
|
|
import pytest
|
|
|
|
from mirage.cache.index import RAMIndexCacheStore
|
|
from mirage.types import PathSpec
|
|
from mirage.utils.key_prefix import mount_key
|
|
|
|
|
|
class FakeCollection:
|
|
|
|
def __init__(self) -> None:
|
|
self.documents: dict[str, str] = {}
|
|
self.chunks: dict[str, list[dict]] = {}
|
|
self.get_calls: list[dict] = []
|
|
self.queries: list[dict] = []
|
|
self.contains_queries: list[dict] = []
|
|
|
|
async def get(self, **kwargs):
|
|
self.get_calls.append(kwargs)
|
|
ids = kwargs.get("ids")
|
|
if ids == ["__path_tree__"]:
|
|
return {"documents": [self.documents["__path_tree__"]]}
|
|
|
|
where = kwargs.get("where") or {}
|
|
slug = where.get("page_slug")
|
|
if isinstance(slug, dict):
|
|
slug = slug.get("$eq")
|
|
if slug is not None:
|
|
chunks = self.chunks.get(slug, [])
|
|
offset = kwargs.get("offset") or 0
|
|
limit = kwargs.get("limit")
|
|
if limit is not None:
|
|
chunks = chunks[offset:offset + limit]
|
|
elif offset:
|
|
chunks = chunks[offset:]
|
|
return {
|
|
"documents": [item["document"] for item in chunks],
|
|
"metadatas": [item["metadata"] for item in chunks],
|
|
}
|
|
|
|
where_document = kwargs.get("where_document") or {}
|
|
if "$contains" in where_document or "$regex" in where_document:
|
|
self.contains_queries.append(kwargs)
|
|
candidates = where.get("page_slug", {}).get("$in", [])
|
|
pattern = where_document.get("$contains") or where_document.get(
|
|
"$regex")
|
|
docs: list[str] = []
|
|
metadatas: list[dict] = []
|
|
for slug_item in candidates:
|
|
for chunk in self.chunks.get(slug_item, []):
|
|
if pattern in chunk["document"]:
|
|
docs.append(chunk["document"])
|
|
metadatas.append(chunk["metadata"])
|
|
return {"documents": docs, "metadatas": metadatas}
|
|
|
|
return {"documents": [], "metadatas": []}
|
|
|
|
async def query(self, **kwargs):
|
|
self.queries.append(kwargs)
|
|
return {
|
|
"documents": [["quickstart chunk", "api chunk"]],
|
|
"metadatas": [[{
|
|
"page_slug": "guides/quickstart"
|
|
}, {
|
|
"page_slug": "api/reference"
|
|
}]],
|
|
"distances": [[0.1, 0.25]],
|
|
}
|
|
|
|
|
|
def path_tree_document() -> str:
|
|
return json.dumps({
|
|
"guides/quickstart": {
|
|
"size": 12,
|
|
"created_at": "2026-01-01T00:00:00Z",
|
|
"updated_at": "2026-02-01T00:00:00Z",
|
|
},
|
|
"api/reference": {
|
|
"size": None,
|
|
"created_at": None,
|
|
"updated_at": None,
|
|
},
|
|
})
|
|
|
|
|
|
@pytest.fixture
|
|
def chroma_collection() -> FakeCollection:
|
|
collection = FakeCollection()
|
|
collection.documents["__path_tree__"] = path_tree_document()
|
|
collection.chunks["guides/quickstart"] = [
|
|
{
|
|
"document": "second",
|
|
"metadata": {
|
|
"page_slug": "guides/quickstart",
|
|
"chunk_index": 2,
|
|
},
|
|
},
|
|
{
|
|
"document": "first",
|
|
"metadata": {
|
|
"page_slug": "guides/quickstart",
|
|
"chunk_index": 1,
|
|
},
|
|
},
|
|
]
|
|
collection.chunks["api/reference"] = [{
|
|
"document": "api",
|
|
"metadata": {
|
|
"page_slug": "api/reference",
|
|
"chunk_index": 0,
|
|
},
|
|
}]
|
|
return collection
|
|
|
|
|
|
async def _get_collection(collection):
|
|
return collection
|
|
|
|
|
|
@pytest.fixture
|
|
def chroma_accessor(chroma_collection) -> SimpleNamespace:
|
|
return SimpleNamespace(
|
|
config=SimpleNamespace(slug_field="page_slug",
|
|
chunk_index_field="chunk_index"),
|
|
collection=chroma_collection,
|
|
get_collection=partial(_get_collection, chroma_collection))
|
|
|
|
|
|
@pytest.fixture
|
|
def chroma_index() -> RAMIndexCacheStore:
|
|
return RAMIndexCacheStore()
|
|
|
|
|
|
@pytest.fixture
|
|
def knowledge_root() -> PathSpec:
|
|
return PathSpec(resource_path=mount_key("/knowledge", "/knowledge"),
|
|
virtual="/knowledge",
|
|
directory="/knowledge")
|
|
|
|
|
|
@pytest.fixture
|
|
def quickstart_path() -> PathSpec:
|
|
return PathSpec.from_str_path(
|
|
"/knowledge/guides/quickstart",
|
|
mount_key("/knowledge/guides/quickstart", "/knowledge"))
|