|
| 1 | +# SPDX-License-Identifier: AGPL-3.0-or-later |
| 2 | +# Copyright (c) 2026 Hal Hildebrand. All rights reserved. |
| 3 | +"""nexus-p8nd5 — HttpCatalogClient.resolve_span_text service-mode parity. |
| 4 | +
|
| 5 | +The stub returned ``None`` unconditionally, so the majority topology |
| 6 | +(service mode) never saw the ib6uy distinguishability contract the local |
| 7 | +``Catalog.resolve_span_text`` honours: genuinely-unresolvable → ``None``, |
| 8 | +DEGRADED vector service → :class:`VectorServiceError` raised for the |
| 9 | +boundary to render. These tests pin the parity, including the underlying |
| 10 | +``t3._client`` seam fix in ``catalog_spans`` (HttpVectorClient deliberately |
| 11 | +has no ``_client`` attribute — the old code AttributeError'd into the |
| 12 | +broad except and masked every service-mode chash span to ``None``). |
| 13 | +""" |
| 14 | +from __future__ import annotations |
| 15 | + |
| 16 | +import hashlib |
| 17 | +from unittest.mock import patch |
| 18 | + |
| 19 | +import pytest |
| 20 | + |
| 21 | +from nexus.catalog.catalog import CatalogEntry |
| 22 | +from nexus.catalog.tumbler import Tumbler |
| 23 | +from nexus.catalog.http_catalog_client import HttpCatalogClient |
| 24 | +from nexus.db.http_vector_client import VectorServiceError |
| 25 | + |
| 26 | +_CHASH = hashlib.sha256(b"span target text").hexdigest() |
| 27 | + |
| 28 | + |
| 29 | +def _entry(**over) -> CatalogEntry: |
| 30 | + kw = dict( |
| 31 | + tumbler=Tumbler.parse("1.2.3"), |
| 32 | + title="Doc", |
| 33 | + author="", |
| 34 | + year=0, |
| 35 | + content_type="knowledge", |
| 36 | + file_path="", |
| 37 | + corpus="knowledge", |
| 38 | + physical_collection="knowledge__t__voyage-context-3__v1", |
| 39 | + chunk_count=1, |
| 40 | + head_hash="", |
| 41 | + indexed_at="", |
| 42 | + ) |
| 43 | + kw.update(over) |
| 44 | + return CatalogEntry(**kw) |
| 45 | + |
| 46 | + |
| 47 | +class _ServiceT3: |
| 48 | + """HttpVectorClient-shaped double: get_collection but NO _client attr.""" |
| 49 | + |
| 50 | + def __init__(self, docs=None, error: Exception | None = None): |
| 51 | + self._docs = docs or [] |
| 52 | + self._error = error |
| 53 | + self.where_seen: list[dict] = [] |
| 54 | + |
| 55 | + def get_collection(self, name): |
| 56 | + outer = self |
| 57 | + |
| 58 | + class _Col: |
| 59 | + def get(self, *, ids=None, where=None, include=None, **kw): |
| 60 | + if outer._error is not None: |
| 61 | + raise outer._error |
| 62 | + outer.where_seen.append(where or {"ids": ids}) |
| 63 | + if outer._docs: |
| 64 | + return {"ids": ["x"], "documents": list(outer._docs), |
| 65 | + "metadatas": [{}] * len(outer._docs)} |
| 66 | + return {"ids": [], "documents": [], "metadatas": []} |
| 67 | + |
| 68 | + return _Col() |
| 69 | + |
| 70 | + # deliberate: no _client attribute (pinned HttpVectorClient shape) |
| 71 | + |
| 72 | + |
| 73 | +def _client() -> HttpCatalogClient: |
| 74 | + return HttpCatalogClient(base_url="http://127.0.0.1:1", tenant="t", _token="test-token") |
| 75 | + |
| 76 | + |
| 77 | +def test_chash_span_resolves_through_the_service_shape(): |
| 78 | + c = _client() |
| 79 | + t3 = _ServiceT3(docs=["span target text"]) |
| 80 | + with patch.object(HttpCatalogClient, "resolve", return_value=_entry()), \ |
| 81 | + patch("nexus.db.make_t3", return_value=t3): |
| 82 | + out = c.resolve_span_text("1.2.3", f"chash:{_CHASH}") |
| 83 | + assert out == "span target text" |
| 84 | + assert t3.where_seen and t3.where_seen[0] == {"chunk_text_hash": _CHASH} |
| 85 | + |
| 86 | + |
| 87 | +def test_degraded_service_raises_never_masks_to_none(): |
| 88 | + """ib6uy: unreachable is never collapsed into not-found.""" |
| 89 | + c = _client() |
| 90 | + t3 = _ServiceT3(error=VectorServiceError("service unreachable", code=503)) |
| 91 | + with patch.object(HttpCatalogClient, "resolve", return_value=_entry()), \ |
| 92 | + patch("nexus.db.make_t3", return_value=t3): |
| 93 | + with pytest.raises(VectorServiceError): |
| 94 | + c.resolve_span_text("1.2.3", f"chash:{_CHASH}") |
| 95 | + |
| 96 | + |
| 97 | +def test_unknown_tumbler_is_none(): |
| 98 | + c = _client() |
| 99 | + with patch.object(HttpCatalogClient, "resolve", return_value=None): |
| 100 | + assert c.resolve_span_text("9.9.9", f"chash:{_CHASH}") is None |
| 101 | + |
| 102 | + |
| 103 | +def test_empty_span_is_none_without_resolving(): |
| 104 | + c = _client() |
| 105 | + with patch.object(HttpCatalogClient, "resolve") as res: |
| 106 | + assert c.resolve_span_text("1.2.3", "") is None |
| 107 | + res.assert_not_called() |
| 108 | + |
| 109 | + |
| 110 | +def test_missing_chunk_is_none_not_error(): |
| 111 | + c = _client() |
| 112 | + t3 = _ServiceT3(docs=[]) |
| 113 | + with patch.object(HttpCatalogClient, "resolve", return_value=_entry()), \ |
| 114 | + patch("nexus.db.make_t3", return_value=t3): |
| 115 | + assert c.resolve_span_text("1.2.3", f"chash:{_CHASH}") is None |
| 116 | + |
| 117 | + |
| 118 | +def test_shared_resolver_uses_handle_when_no_client_attr(): |
| 119 | + """The seam fix itself: resolve_span_text_for_entry must reach the T3 |
| 120 | + read through the HANDLE when ``_client`` is absent (service shape) — |
| 121 | + the old ``t3._client`` read AttributeError'd into the broad except and |
| 122 | + masked every service-mode chash span to None.""" |
| 123 | + from nexus.catalog.catalog_spans import resolve_span_text_for_entry |
| 124 | + |
| 125 | + t3 = _ServiceT3(docs=["via handle"]) |
| 126 | + with patch("nexus.db.make_t3", return_value=t3): |
| 127 | + out = resolve_span_text_for_entry(_entry(), f"chash:{_CHASH}") |
| 128 | + assert out == "via handle" |
0 commit comments