Billing product model finalized (Deck #281): bill pages externally, record tokens internally. Rename the data-plane metric literals to match the now- canonical contract (Deck #284) — the control plane's METRIC_EVENT_NAMES is already renamed, so the old names would be unmapped and never sync to Stripe. Rename (values unchanged): - embeddings_queries → tokens_embedded (value = real token count, already emitted by this PR; the unit upstream providers bill on). - pages_chunks → pages_embedded (value kept as len(chunk_texts) interim; TODO(#282): real normalized "pages indexed" count — real pages for paginated types, chars/tokens-per-page constant otherwise — is deferred to the instrumentation card, this only lands the name/contract). - All literals, log strings, docstrings, comments, the migration comment, and tests renamed; grep confirms zero old strings remain. Observability (new): export embedding token cost to Prometheus as astrolabe_embedding_tokens_total{provider,operation} (operation = index|query) so the billed cost unit is visible in Grafana, not just the per-tenant billing DB. Dedicated counter (doesn't inflate the existing chunk/request metrics) and always-on (independent of USAGE_METERING_ENABLED, so OSS/self-host gets it). Wired on both the indexing batch embed and the search query embed (query inside the per-request cache-miss branch, so reused embeddings aren't double-counted). Note: the rename orphans any pre-existing embeddings_queries/pages_chunks rows in tenant app DBs (CP no longer maps them) — acceptable; pipeline is inert with throwaway dev/sandbox data. Deck #284 (folded into PR #875). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
105 lines
3.1 KiB
Python
105 lines
3.1 KiB
Python
"""Unit tests for the indexing-path usage-metering helper (Deck #67).
|
|
|
|
``record_indexing_usage`` records the two billable events (``pages_embedded`` +
|
|
``tokens_embedded``) after a document's chunks are embedded. These cover the
|
|
value mapping, the flag/zero-chunk no-ops, and the best-effort failure path
|
|
without standing up the full document pipeline.
|
|
"""
|
|
|
|
from unittest.mock import AsyncMock, MagicMock
|
|
|
|
import pytest
|
|
|
|
from nextcloud_mcp_server.vector import processor
|
|
|
|
|
|
@pytest.fixture
|
|
def store_spy(monkeypatch):
|
|
"""Patch UsageEventStore.shared() to return a spy store."""
|
|
store = MagicMock()
|
|
store.record_usage_event = AsyncMock()
|
|
monkeypatch.setattr(
|
|
processor.UsageEventStore, "shared", AsyncMock(return_value=store)
|
|
)
|
|
return store
|
|
|
|
|
|
@pytest.mark.unit
|
|
async def test_records_pages_embedded_and_token_count(store_spy):
|
|
"""Both events fire: pages_embedded = chunk count, tokens_embedded = tokens."""
|
|
await processor.record_indexing_usage(
|
|
enabled=True,
|
|
provider="mistral",
|
|
model="mistral-embed",
|
|
doc_type="file",
|
|
user_id="alice",
|
|
chunk_count=110,
|
|
token_count=4242,
|
|
total_chars=170826,
|
|
)
|
|
|
|
calls = store_spy.record_usage_event.await_args_list
|
|
by_metric = {c.kwargs["metric"]: c.kwargs["value"] for c in calls}
|
|
assert by_metric == {"pages_embedded": 110, "tokens_embedded": 4242}
|
|
for c in calls:
|
|
# Hot-path fast-gate + tenant-local attribution metadata.
|
|
assert c.kwargs["enabled"] is True
|
|
assert c.kwargs["metadata"]["provider"] == "mistral"
|
|
assert c.kwargs["metadata"]["model"] == "mistral-embed"
|
|
assert c.kwargs["metadata"]["user_id"] == "alice"
|
|
assert c.kwargs["metadata"]["doc_type"] == "file"
|
|
|
|
|
|
@pytest.mark.unit
|
|
async def test_disabled_is_noop(store_spy):
|
|
"""Flag off → no store access, no events."""
|
|
await processor.record_indexing_usage(
|
|
enabled=False,
|
|
provider="mistral",
|
|
model="mistral-embed",
|
|
doc_type="file",
|
|
user_id="alice",
|
|
chunk_count=10,
|
|
token_count=20,
|
|
total_chars=5,
|
|
)
|
|
store_spy.record_usage_event.assert_not_awaited()
|
|
|
|
|
|
@pytest.mark.unit
|
|
async def test_zero_chunks_is_noop(store_spy):
|
|
"""A document with no chunks records nothing (no zero-value rows)."""
|
|
await processor.record_indexing_usage(
|
|
enabled=True,
|
|
provider="mistral",
|
|
model="mistral-embed",
|
|
doc_type="file",
|
|
user_id="alice",
|
|
chunk_count=0,
|
|
token_count=0,
|
|
total_chars=0,
|
|
)
|
|
store_spy.record_usage_event.assert_not_awaited()
|
|
|
|
|
|
@pytest.mark.unit
|
|
async def test_store_failure_is_swallowed(monkeypatch):
|
|
"""A store-construction failure is logged, never raised into indexing."""
|
|
monkeypatch.setattr(
|
|
processor.UsageEventStore,
|
|
"shared",
|
|
AsyncMock(side_effect=RuntimeError("boom")),
|
|
)
|
|
|
|
# Must not raise.
|
|
await processor.record_indexing_usage(
|
|
enabled=True,
|
|
provider="mistral",
|
|
model="mistral-embed",
|
|
doc_type="file",
|
|
user_id="alice",
|
|
chunk_count=3,
|
|
token_count=7,
|
|
total_chars=9,
|
|
)
|