Debian Codegraph @

tests/dcs_client_test.py

raw
1 #!/usr/bin/env python3
2 # Copyright: 2026, Maximiliano Curia <[email protected]>
3 #
4 # License: ISC
5 # Permission to use, copy, modify, and/or distribute this software for any
6 # purpose with or without fee is hereby granted, provided that the above
7 # copyright notice and this permission notice appear in all copies.
8 # .
9 # THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 # REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 # AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 # INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 # LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 # OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 # PERFORMANCE OF THIS SOFTWARE.
16
17 """Tests for scripts/dcs_client.py, the shared Debian Code Search client."""
18
19 from __future__ import annotations
20
21 import hashlib
22 import sys
23 import urllib.request
24 from pathlib import Path
25 from typing import Any
26
27 import pytest
28
29 ROOT = Path(__file__).resolve().parent.parent
30 sys.path.insert(0, str(ROOT / "scripts"))
31
32 import dcs_client # noqa: E402
33
34 from tests.dcs_helpers import FakeTransport # noqa: E402
35
36 CANNED = b'[{"package": "p", "path": "p/COPYING"}]'
37
38
39 def _client(cache_dir: Path, **kw: Any) -> Any:
40 kw.setdefault("user_agent", "test-agent")
41 config = dcs_client.DcsConfig(key="key", cache_dir=cache_dir, sleep=0.0, **kw)
42 return dcs_client.DcsClient(config)
43
44
45 def _serve(monkeypatch: pytest.MonkeyPatch, transport: FakeTransport) -> None:
46 monkeypatch.setattr(urllib.request, "urlopen", transport)
47
48
49 def test_cache_roundtrip(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
50 transport = FakeTransport([CANNED])
51 _serve(monkeypatch, transport)
52 client = _client(tmp_path)
53 first = client.search_live("q", "literal")
54 second = client.search_live("q", "literal") # served from cache
55 assert first.state is dcs_client.SearchState.HITS
56 assert first.hits == [{"package": "p", "path": "p/COPYING"}]
57 assert second == first
58 assert transport.calls == 1
59
60
61 def test_outcome_hits(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
62 transport = FakeTransport([CANNED])
63 _serve(monkeypatch, transport)
64 outcome = _client(tmp_path).search_live("q", "literal")
65 assert outcome.state is dcs_client.SearchState.HITS
66 assert outcome.hits == [{"package": "p", "path": "p/COPYING"}]
67
68
69 def test_outcome_broad(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
70 # A response larger than the cap is broad-by-design; shrink the cap so a
71 # tiny payload trips it rather than fabricating 25 MB.
72 monkeypatch.setattr(dcs_client, "MAX_RESPONSE_BYTES", 4)
73 _serve(monkeypatch, FakeTransport([CANNED]))
74 outcome = _client(tmp_path).search_live("q", "literal")
75 assert outcome.state is dcs_client.SearchState.BROAD
76 assert outcome.hits == []
77
78
79 def test_search_cached_pending_when_uncached(tmp_path: Path) -> None:
80 outcome = _client(tmp_path).search_cached("q", "literal")
81 assert outcome.state is dcs_client.SearchState.PENDING
82
83
84 def test_cache_key_prefix_disjoint(tmp_path: Path) -> None:
85 prefixed = _client(tmp_path, cache_prefix="rank")
86 plain = _client(tmp_path, cache_prefix="")
87 assert prefixed._cache_path("q", "literal") != plain._cache_path("q", "literal")
88
89
90 def test_rank_cache_key_matches_legacy_scheme(tmp_path: Path) -> None:
91 # The existing on-disk cache was written with this exact key; the prefixed
92 # client must keep resolving to the same filename or the cache is orphaned.
93 client = _client(tmp_path, cache_prefix="rank")
94 legacy = hashlib.sha256(b"rank\x00literal\x00q").hexdigest()[:20] + ".json"
95 assert client._cache_path("q", "literal").name == legacy
96
97
98 def test_plain_cache_key_matches_legacy_scheme(tmp_path: Path) -> None:
99 client = _client(tmp_path, cache_prefix="")
100 legacy = hashlib.sha256(b"literal\x00q").hexdigest()[:20] + ".json"
101 assert client._cache_path("q", "literal").name == legacy
102
103
104 def test_corrupt_cache_is_treated_as_miss(
105 tmp_path: Path, monkeypatch: pytest.MonkeyPatch
106 ) -> None:
107 transport = FakeTransport([CANNED])
108 _serve(monkeypatch, transport)
109 client = _client(tmp_path)
110 client._cache_path("q", "literal").write_text("{ not json", encoding="utf-8")
111 outcome = client.search_live("q", "literal")
112 assert outcome.state is dcs_client.SearchState.HITS # refetched, no crash
113 assert outcome.hits == [{"package": "p", "path": "p/COPYING"}]
114 assert transport.calls == 1