liblore 0.10.0__tar.gz → 0.10.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (25) hide show
  1. {liblore-0.10.0/src/liblore.egg-info → liblore-0.10.2}/PKG-INFO +6 -2
  2. {liblore-0.10.0 → liblore-0.10.2}/README.md +5 -1
  3. {liblore-0.10.0 → liblore-0.10.2}/pyproject.toml +1 -1
  4. {liblore-0.10.0 → liblore-0.10.2}/src/liblore/__init__.py +1 -1
  5. {liblore-0.10.0 → liblore-0.10.2}/src/liblore/node.py +35 -3
  6. {liblore-0.10.0 → liblore-0.10.2}/src/liblore/series.py +1 -3
  7. {liblore-0.10.0 → liblore-0.10.2}/src/liblore/utils.py +35 -11
  8. {liblore-0.10.0 → liblore-0.10.2/src/liblore.egg-info}/PKG-INFO +6 -2
  9. {liblore-0.10.0 → liblore-0.10.2}/tests/test_email_utils.py +87 -9
  10. {liblore-0.10.0 → liblore-0.10.2}/tests/test_node.py +12 -2
  11. {liblore-0.10.0 → liblore-0.10.2}/tests/test_partial.py +116 -0
  12. {liblore-0.10.0 → liblore-0.10.2}/LICENSES/GPL-2.0-or-later.txt +0 -0
  13. {liblore-0.10.0 → liblore-0.10.2}/setup.cfg +0 -0
  14. {liblore-0.10.0 → liblore-0.10.2}/src/liblore/py.typed +0 -0
  15. {liblore-0.10.0 → liblore-0.10.2}/src/liblore.egg-info/SOURCES.txt +0 -0
  16. {liblore-0.10.0 → liblore-0.10.2}/src/liblore.egg-info/dependency_links.txt +0 -0
  17. {liblore-0.10.0 → liblore-0.10.2}/src/liblore.egg-info/requires.txt +0 -0
  18. {liblore-0.10.0 → liblore-0.10.2}/src/liblore.egg-info/top_level.txt +0 -0
  19. {liblore-0.10.0 → liblore-0.10.2}/tests/test_auth_headers.py +0 -0
  20. {liblore-0.10.0 → liblore-0.10.2}/tests/test_cache.py +0 -0
  21. {liblore-0.10.0 → liblore-0.10.2}/tests/test_formatting.py +0 -0
  22. {liblore-0.10.0 → liblore-0.10.2}/tests/test_mbox.py +0 -0
  23. {liblore-0.10.0 → liblore-0.10.2}/tests/test_message.py +0 -0
  24. {liblore-0.10.0 → liblore-0.10.2}/tests/test_series.py +0 -0
  25. {liblore-0.10.0 → liblore-0.10.2}/tests/test_thread.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: liblore
3
- Version: 0.10.0
3
+ Version: 0.10.2
4
4
  Summary: Shared library for public-inbox / lore.kernel.org access
5
5
  Author-email: Konstantin Ryabitsev <konstantin@linuxfoundation.org>
6
6
  License-Expression: GPL-2.0-or-later
@@ -254,7 +254,11 @@ the node at the mirror, and it learns the rest.
254
254
  - **Searches stay local.** A search the mirror answers is final. The
255
255
  upstream archive is asked only when the mirror finds nothing. Set
256
256
  `partialsearch = upstream` to ask the upstream archive first, if you want
257
- every hit and don't mind the wait.
257
+ every hit and don't mind the wait. When you already know the mirror has
258
+ what you are searching for (for example, it just answered for the thread
259
+ the search is about), pass `local_sufficient=True`: then even "no
260
+ results" is final, and the upstream archive is asked only if the mirror
261
+ can't be reached.
258
262
  - **Polling stays local.** `get_thread_updates_since()` first asks the
259
263
  mirror whether it has the thread (a `HEAD` request). For a thread it has,
260
264
  its "nothing new" is final, and lore.kernel.org is not contacted. For a
@@ -230,7 +230,11 @@ the node at the mirror, and it learns the rest.
230
230
  - **Searches stay local.** A search the mirror answers is final. The
231
231
  upstream archive is asked only when the mirror finds nothing. Set
232
232
  `partialsearch = upstream` to ask the upstream archive first, if you want
233
- every hit and don't mind the wait.
233
+ every hit and don't mind the wait. When you already know the mirror has
234
+ what you are searching for (for example, it just answered for the thread
235
+ the search is about), pass `local_sufficient=True`: then even "no
236
+ results" is final, and the upstream archive is asked only if the mirror
237
+ can't be reached.
234
238
  - **Polling stays local.** `get_thread_updates_since()` first asks the
235
239
  mirror whether it has the thread (a `HEAD` request). For a thread it has,
236
240
  its "nothing new" is final, and lore.kernel.org is not contacted. For a
@@ -5,7 +5,7 @@ build-backend = "setuptools.build_meta"
5
5
 
6
6
  [project]
7
7
  name = "liblore"
8
- version = "0.10.0"
8
+ version = "0.10.2"
9
9
  description = "Shared library for public-inbox / lore.kernel.org access"
10
10
  readme = "README.md"
11
11
  requires-python = ">=3.9"
@@ -9,7 +9,7 @@ import email.policy
9
9
  import enum
10
10
  from email.message import EmailMessage
11
11
 
12
- __version__ = '0.10.0'
12
+ __version__ = '0.10.2'
13
13
 
14
14
  # Email policy used for parsing and serialising messages
15
15
  emlpolicy = email.policy.EmailPolicy(
@@ -2046,6 +2046,7 @@ class LoreNode:
2046
2046
  *,
2047
2047
  full_threads: bool = False,
2048
2048
  nocache: bool = False,
2049
+ local_sufficient: bool = False,
2049
2050
  ) -> bytes:
2050
2051
  """POST a search query and return the raw mbox bytes.
2051
2052
 
@@ -2058,6 +2059,15 @@ class LoreNode:
2058
2059
  doesn't have is then missed. With ``partial_search='upstream'``
2059
2060
  the upstream archive is asked first, and the mirror only when
2060
2061
  the upstream archive can't be reached.
2062
+
2063
+ With *local_sufficient*, the mirror's answer is final when the mirror
2064
+ answers, even when it finds nothing: the upstream archive is not
2065
+ asked, and ``partial_search='upstream'`` is ignored. Use it when
2066
+ you already know the mirror tracks what you search for, for
2067
+ example because it just answered for the thread the search is
2068
+ about. "No results" is then the server's 404
2069
+ (:class:`~liblore.RemoteError`), as on a full node. When the
2070
+ mirror can't be reached, the upstream archive still answers.
2061
2071
  """
2062
2072
 
2063
2073
  # A named function, not a lambda: mypy can't infer a lambda
@@ -2078,11 +2088,20 @@ class LoreNode:
2078
2088
  try:
2079
2089
  t_mbox, as_of = self._local_search(query, full_threads)
2080
2090
  except RemoteError as ex:
2091
+ if local_sufficient:
2092
+ raise
2081
2093
  # This answer may be the first to say the node is partial.
2082
2094
  upstream = self._fallback_node()
2083
2095
  if upstream is None or ex.status_code != 404:
2084
2096
  raise
2085
2097
  raise _Miss(upstream, missing=f'No results for {query!r}') from ex
2098
+ if local_sufficient:
2099
+ # Sufficient for the caller, but the mirror's answer may still
2100
+ # be incomplete: cache only what the default would.
2101
+ partial = self._get_upstream_url() is not None
2102
+ if not (partial and (upstream_first or not t_mbox)):
2103
+ self._cache_write(key, t_mbox)
2104
+ return t_mbox, as_of
2086
2105
  upstream = self._fallback_node()
2087
2106
  if upstream is not None and (upstream_first or not t_mbox):
2088
2107
  # The mirror's answer, which may be incomplete: never
@@ -2094,7 +2113,10 @@ class LoreNode:
2094
2113
  return t_mbox, as_of
2095
2114
 
2096
2115
  return self._answer(
2097
- f'Search {query!r}', local, remote, upstream_first=upstream_first
2116
+ f'Search {query!r}',
2117
+ local,
2118
+ remote,
2119
+ upstream_first=upstream_first and not local_sufficient,
2098
2120
  )
2099
2121
 
2100
2122
  def _local_search(self, query: str, full_threads: bool) -> tuple[bytes, float]:
@@ -2356,13 +2378,17 @@ class LoreNode:
2356
2378
  query: str,
2357
2379
  *,
2358
2380
  full_threads: bool = False,
2381
+ local_sufficient: bool = False,
2359
2382
  ) -> list[EmailMessage]:
2360
2383
  """POST a search query and return deduplicated messages.
2361
2384
 
2362
2385
  When *full_threads* is ``True``, the server expands results to
2363
2386
  include the full thread for every matching message.
2387
+ *local_sufficient* is passed on to :meth:`get_mbox_by_query`.
2364
2388
  """
2365
- t_mbox = self.get_mbox_by_query(query, full_threads=full_threads)
2389
+ t_mbox = self.get_mbox_by_query(
2390
+ query, full_threads=full_threads, local_sufficient=local_sufficient
2391
+ )
2366
2392
  if not t_mbox:
2367
2393
  raise LookupError(f'No results for query: {query}')
2368
2394
  msgs = split_and_dedupe(t_mbox)
@@ -2473,6 +2499,7 @@ class LoreNode:
2473
2499
  queries: list[str],
2474
2500
  *,
2475
2501
  full_threads: bool = False,
2502
+ local_sufficient: bool = False,
2476
2503
  ) -> list[list[EmailMessage]]:
2477
2504
  """Run multiple queries with rate limiting.
2478
2505
 
@@ -2481,6 +2508,7 @@ class LoreNode:
2481
2508
 
2482
2509
  When *full_threads* is ``True``, the server expands results to
2483
2510
  include the full thread for every matching message.
2511
+ *local_sufficient* is passed on to :meth:`get_mbox_by_query`.
2484
2512
 
2485
2513
  Returns a list of results in the same order as the input.
2486
2514
  :attr:`last_source` then reports the worst source of them all;
@@ -2492,7 +2520,11 @@ class LoreNode:
2492
2520
  for i, query in enumerate(queries):
2493
2521
  if i > 0:
2494
2522
  time.sleep(0.1)
2495
- results.append(self.get_thread_by_query(query, full_threads=full_threads))
2523
+ results.append(
2524
+ self.get_thread_by_query(
2525
+ query, full_threads=full_threads, local_sufficient=local_sufficient
2526
+ )
2527
+ )
2496
2528
  return results
2497
2529
 
2498
2530
  @_operation_scope
@@ -849,11 +849,9 @@ def _first_date(msgs: Sequence[EmailMessage]) -> datetime.datetime | None:
849
849
 
850
850
 
851
851
  def _sort_key(when: datetime.datetime | None) -> datetime.datetime:
852
- """Sort undated items last, and make naive/aware datetimes comparable."""
852
+ """Sort undated items last."""
853
853
  if when is None:
854
854
  return datetime.datetime.max.replace(tzinfo=datetime.timezone.utc)
855
- if when.tzinfo is None:
856
- return when.replace(tzinfo=datetime.timezone.utc)
857
855
  return when
858
856
 
859
857
 
@@ -629,13 +629,37 @@ def msg_get_recipients(msg: EmailMessage) -> set[str]:
629
629
  return {x[1].lower() for x in pairs}
630
630
 
631
631
 
632
+ def _parse_utc_date(value: str) -> datetime.datetime | None:
633
+ """Parse an RFC 5322 date into an aware UTC datetime, or ``None``.
634
+
635
+ ``parsedate_to_datetime()`` returns a naive value for ``-0000``, which
636
+ RFC 5322 defines as UTC with an unknown local zone, so reading it as
637
+ UTC is correct. It does the same when the zone is missing or not
638
+ recognised, and UTC is the only neutral reading there too. Converting
639
+ everything to UTC gives one canonical form that always compares and
640
+ serialises consistently.
641
+ """
642
+ try:
643
+ parsed = email.utils.parsedate_to_datetime(value)
644
+ if parsed.tzinfo is None:
645
+ return parsed.replace(tzinfo=datetime.timezone.utc)
646
+ # OverflowError: a date at the edge of year 9999 can parse and
647
+ # still have no representable UTC equivalent.
648
+ return parsed.astimezone(datetime.timezone.utc)
649
+ except (ValueError, TypeError, OverflowError):
650
+ return None
651
+
652
+
632
653
  def msg_get_date(msg: EmailMessage) -> datetime.datetime | None:
633
654
  """Return the message timestamp, preferring Received over Date.
634
655
 
635
656
  Uses the latest ``Received`` header timestamp when one is present,
636
657
  because those are stamped by the receiving servers rather than by
637
658
  the sender, and falls back to the client-set ``Date`` header.
638
- Returns ``None`` when neither yields a parsable timestamp.
659
+
660
+ Returns an aware datetime in UTC, or ``None`` when neither header
661
+ yields a parsable timestamp. Timestamps marked ``-0000``, or with no
662
+ usable zone, are treated as UTC.
639
663
  """
640
664
  latest_rdt: datetime.datetime | None = None
641
665
  for rhdr in msg.get_all('Received', []):
@@ -643,18 +667,18 @@ def msg_get_date(msg: EmailMessage) -> datetime.datetime | None:
643
667
  chunks = str(rhdr).rsplit(';', maxsplit=1)
644
668
  if len(chunks) < 2:
645
669
  continue
646
- rdate = chunks[1].strip()
647
- try:
648
- rdt = email.utils.parsedate_to_datetime(rdate)
649
- except (ValueError, TypeError):
650
- continue
651
- if not latest_rdt or latest_rdt < rdt:
670
+ rdt = _parse_utc_date(chunks[1].strip())
671
+ if rdt is not None and (latest_rdt is None or latest_rdt < rdt):
652
672
  latest_rdt = rdt
653
- if not latest_rdt and msg.get('Date'):
673
+ if latest_rdt is None:
654
674
  try:
655
- latest_rdt = email.utils.parsedate_to_datetime(str(msg.get('Date')))
656
- except (ValueError, TypeError):
657
- pass
675
+ date_hdr = msg.get('Date')
676
+ except TypeError:
677
+ # Python 3.9's DateHeader raises on a garbage value when the
678
+ # header is read; newer versions record a defect instead.
679
+ return None
680
+ if date_hdr:
681
+ latest_rdt = _parse_utc_date(str(date_hdr))
658
682
  return latest_rdt
659
683
 
660
684
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: liblore
3
- Version: 0.10.0
3
+ Version: 0.10.2
4
4
  Summary: Shared library for public-inbox / lore.kernel.org access
5
5
  Author-email: Konstantin Ryabitsev <konstantin@linuxfoundation.org>
6
6
  License-Expression: GPL-2.0-or-later
@@ -254,7 +254,11 @@ the node at the mirror, and it learns the rest.
254
254
  - **Searches stay local.** A search the mirror answers is final. The
255
255
  upstream archive is asked only when the mirror finds nothing. Set
256
256
  `partialsearch = upstream` to ask the upstream archive first, if you want
257
- every hit and don't mind the wait.
257
+ every hit and don't mind the wait. When you already know the mirror has
258
+ what you are searching for (for example, it just answered for the thread
259
+ the search is about), pass `local_sufficient=True`: then even "no
260
+ results" is final, and the upstream archive is asked only if the mirror
261
+ can't be reached.
258
262
  - **Polling stays local.** `get_thread_updates_since()` first asks the
259
263
  mirror whether it has the thread (a `HEAD` request). For a thread it has,
260
264
  its "nothing new" is final, and lore.kernel.org is not contacted. For a
@@ -3,6 +3,8 @@
3
3
 
4
4
  from __future__ import annotations
5
5
 
6
+ import datetime
7
+ import email
6
8
  from email.message import EmailMessage
7
9
 
8
10
  import pytest
@@ -11,6 +13,7 @@ from conftest import MsgFactory
11
13
  from liblore import emlpolicy
12
14
  from liblore.utils import (
13
15
  msg_get_author,
16
+ msg_get_date,
14
17
  msg_get_payload,
15
18
  msg_get_recipients,
16
19
  msg_get_subject,
@@ -18,6 +21,8 @@ from liblore.utils import (
18
21
  strip_reply_prefixes,
19
22
  )
20
23
 
24
+ UTC = datetime.timezone.utc
25
+
21
26
 
22
27
  class TestMsgGetSubject:
23
28
  def test_plain_subject(self, make_msg: MsgFactory) -> None:
@@ -179,15 +184,19 @@ class TestMsgGetRecipients:
179
184
 
180
185
 
181
186
  class TestSortMsgsByReceived:
182
- def test_sorts_by_date(self, make_msg: MsgFactory) -> None:
183
- msg1 = make_msg(
184
- msgid='older@x.com',
185
- date='Mon, 01 Jan 2024 00:00:00 +0000',
186
- )
187
- msg2 = make_msg(
188
- msgid='newer@x.com',
189
- date='Tue, 02 Jan 2024 00:00:00 +0000',
190
- )
187
+ @pytest.mark.parametrize(
188
+ ('older', 'newer'),
189
+ [
190
+ ('Mon, 01 Jan 2024 00:00:00 +0000', 'Tue, 02 Jan 2024 00:00:00 +0000'),
191
+ # A -0000 (naive) date must sort against an aware one rather
192
+ # than raising TypeError.
193
+ ('Mon, 02 Nov 2026 10:00:00 -0000', 'Mon, 02 Nov 2026 10:00:01 +0000'),
194
+ ('Mon, 02 Nov 2026 10:00:00 +0000', 'Mon, 02 Nov 2026 10:00:01 -0000'),
195
+ ],
196
+ )
197
+ def test_sorts_by_date(self, make_msg: MsgFactory, older: str, newer: str) -> None:
198
+ msg1 = make_msg(msgid='older@x.com', date=older)
199
+ msg2 = make_msg(msgid='newer@x.com', date=newer)
191
200
  # Feed them in reverse order
192
201
  result = sort_msgs_by_received([msg2, msg1])
193
202
  assert result[0]['Message-Id'] == '<older@x.com>'
@@ -200,3 +209,72 @@ class TestSortMsgsByReceived:
200
209
  del msg['Date']
201
210
  result = sort_msgs_by_received([msg])
202
211
  assert result == []
212
+
213
+
214
+ class TestMsgGetDate:
215
+ """msg_get_date() must always return an aware UTC datetime or None.
216
+
217
+ Callers sort, compare and convert the result to timestamps; a naive
218
+ value would crash comparisons and be read as local time.
219
+ """
220
+
221
+ @pytest.mark.parametrize(
222
+ ('date', 'received', 'expected'),
223
+ [
224
+ # RFC 5322: -0000 means UTC with an unknown local zone.
225
+ pytest.param(
226
+ 'Mon, 2 Nov 2026 10:00:00 -0000',
227
+ [],
228
+ datetime.datetime(2026, 11, 2, 10, 0, tzinfo=UTC),
229
+ id='date-minus0000',
230
+ ),
231
+ pytest.param(
232
+ 'Mon, 2 Nov 2026 12:00:00 +0200',
233
+ [],
234
+ datetime.datetime(2026, 11, 2, 10, 0, tzinfo=UTC),
235
+ id='date-offset',
236
+ ),
237
+ pytest.param(
238
+ 'Mon, 2 Nov 2026 08:00:00 +0000',
239
+ [
240
+ 'from a by b; Mon, 2 Nov 2026 10:00:00 -0000',
241
+ 'from c by a; Mon, 2 Nov 2026 09:59:00 +0000',
242
+ ],
243
+ datetime.datetime(2026, 11, 2, 10, 0, tzinfo=UTC),
244
+ id='received-mixed-latest-wins',
245
+ ),
246
+ pytest.param(
247
+ 'Mon, 2 Nov 2026 08:00:00 +0000',
248
+ ['from c by a; Mon, 2 Nov 2026 11:30:00 +0100'],
249
+ datetime.datetime(2026, 11, 2, 10, 30, tzinfo=UTC),
250
+ id='received-offset',
251
+ ),
252
+ pytest.param(None, [], None, id='no-headers'),
253
+ pytest.param('not a date', ['from x by y; garbage'], None, id='garbage'),
254
+ # Past year 9999 in UTC, so no datetime can hold it.
255
+ pytest.param(
256
+ 'Fri, 31 Dec 9999 23:59:59 -0500',
257
+ ['from x by y; Fri, 31 Dec 9999 23:59:59 -0500'],
258
+ None,
259
+ id='out-of-range',
260
+ ),
261
+ ],
262
+ )
263
+ def test_returns_utc(
264
+ self,
265
+ date: str | None,
266
+ received: list[str],
267
+ expected: datetime.datetime | None,
268
+ ) -> None:
269
+ # Parse from bytes, as real mail arrives: building an EmailMessage
270
+ # directly makes Python 3.9 reject a garbage Date before liblore
271
+ # ever sees it.
272
+ hdrs = [f'Received: {hdr}' for hdr in received]
273
+ if date is not None:
274
+ hdrs.append(f'Date: {date}')
275
+ raw = '\n'.join([*hdrs, 'Subject: x', '', 'body', ''])
276
+ msg = email.message_from_bytes(raw.encode(), policy=emlpolicy)
277
+ result = msg_get_date(msg)
278
+ assert result == expected
279
+ if result is not None:
280
+ assert result.tzinfo is UTC
@@ -681,7 +681,7 @@ class TestBatchGetThreadByQuery:
681
681
  class _Node(LoreNode):
682
682
  def __init__(self, results: list[list[EmailMessage]]) -> None:
683
683
  super().__init__()
684
- self.calls: list[tuple[str, bool]] = []
684
+ self.calls: list[tuple[str, bool, bool]] = []
685
685
  self._results = results
686
686
 
687
687
  @override
@@ -690,8 +690,9 @@ class TestBatchGetThreadByQuery:
690
690
  query: str,
691
691
  *,
692
692
  full_threads: bool = False,
693
+ local_sufficient: bool = False,
693
694
  ) -> list[EmailMessage]:
694
- self.calls.append((query, full_threads))
695
+ self.calls.append((query, full_threads, local_sufficient))
695
696
  return self._results.pop(0)
696
697
 
697
698
  def test_returns_ordered_results(self) -> None:
@@ -714,6 +715,15 @@ class TestBatchGetThreadByQuery:
714
715
  assert results == [result]
715
716
  mock_sleep.assert_not_called()
716
717
 
718
+ def test_passes_arguments(self) -> None:
719
+ node = self._Node([[EmailMessage()], [EmailMessage()]])
720
+ with patch('liblore.node.time.sleep'):
721
+ node.batch_get_thread_by_query(
722
+ ['q1', 'q2'], full_threads=True, local_sufficient=True
723
+ )
724
+
725
+ assert node.calls == [('q1', True, True), ('q2', True, True)]
726
+
717
727
  def test_sleep_count_matches_gaps(self) -> None:
718
728
  node = self._Node(
719
729
  [[EmailMessage()], [EmailMessage()], [EmailMessage()], [EmailMessage()]]
@@ -2682,3 +2682,119 @@ class TestFreshness(PartialNode):
2682
2682
  thread.join()
2683
2683
  assert seen == [UPDATED + 100]
2684
2684
  assert as_of_of(node) == UPDATED
2685
+
2686
+
2687
+ class TestLocalSufficient(PartialNode):
2688
+ """get_mbox_by_query(local_sufficient=True): the local result is final."""
2689
+
2690
+ def test_hit_stays_local(self, tmp_path: Path, sample_mbox: bytes) -> None:
2691
+ node = self.node(tmp_path)
2692
+ with responses.RequestsMock() as rsps:
2693
+ rsps.add(responses.POST, LOCAL_QUERY, body=gz(sample_mbox), headers=PARTIAL)
2694
+ assert node.get_mbox_by_query('test', local_sufficient=True) == sample_mbox
2695
+ assert urls(rsps) == [('POST', LOCAL_QUERY)]
2696
+ assert source_of(node) is Source.LOCAL
2697
+ assert cached(node, 'mbox_by_query', 'test', 'False')
2698
+
2699
+ def test_404_is_final(self, tmp_path: Path) -> None:
2700
+ # The same answer a full node gives for "no results".
2701
+ node = self.node(tmp_path)
2702
+ with responses.RequestsMock() as rsps:
2703
+ rsps.add(responses.POST, LOCAL_QUERY, status=404, headers=PARTIAL)
2704
+ with pytest.raises(RemoteError) as exc:
2705
+ node.get_mbox_by_query('test', local_sufficient=True)
2706
+ assert urls(rsps) == [('POST', LOCAL_QUERY)]
2707
+ assert exc.value.status_code == 404
2708
+ assert not isinstance(exc.value, NotOnMirrorError)
2709
+
2710
+ def test_empty_is_final(self, tmp_path: Path) -> None:
2711
+ node = self.node(tmp_path)
2712
+ with responses.RequestsMock() as rsps:
2713
+ rsps.add(responses.POST, LOCAL_QUERY, body=gz(b''), headers=PARTIAL)
2714
+ assert node.get_mbox_by_query('test', local_sufficient=True) == b''
2715
+ assert urls(rsps) == [('POST', LOCAL_QUERY)]
2716
+ assert source_of(node) is Source.LOCAL
2717
+
2718
+ def test_empty_is_not_cached(self, tmp_path: Path, sample_mbox: bytes) -> None:
2719
+ # A later call without local_sufficient must still ask the upstream
2720
+ # archive, and not find the sufficient empty answer in the cache.
2721
+ node = self.node(tmp_path)
2722
+ with responses.RequestsMock() as rsps:
2723
+ rsps.add(responses.POST, LOCAL_QUERY, body=gz(b''), headers=PARTIAL)
2724
+ rsps.add(responses.POST, UP_QUERY, body=gz(sample_mbox))
2725
+ node.get_mbox_by_query('test', local_sufficient=True)
2726
+ assert not cached(node, 'mbox_by_query', 'test', 'False')
2727
+ assert node.get_mbox_by_query('test') == sample_mbox
2728
+ assert urls(rsps) == [
2729
+ ('POST', LOCAL_QUERY),
2730
+ ('POST', LOCAL_QUERY),
2731
+ ('POST', UP_QUERY),
2732
+ ]
2733
+
2734
+ def test_upstream_first_is_ignored(
2735
+ self, tmp_path: Path, sample_mbox: bytes
2736
+ ) -> None:
2737
+ node = self.node(tmp_path, partial_search='upstream')
2738
+ with responses.RequestsMock() as rsps:
2739
+ rsps.add(responses.POST, LOCAL_QUERY, body=gz(sample_mbox), headers=PARTIAL)
2740
+ assert node.get_mbox_by_query('test', local_sufficient=True) == sample_mbox
2741
+ assert urls(rsps) == [('POST', LOCAL_QUERY)]
2742
+ assert source_of(node) is Source.LOCAL
2743
+ # Upstream-first never caches the mirror's answer: a later
2744
+ # call without local_sufficient must not get it from the cache.
2745
+ assert not cached(node, 'mbox_by_query', 'test', 'False')
2746
+
2747
+ def test_learned_from_same_answer(self, tmp_path: Path) -> None:
2748
+ node = self.node(tmp_path, learned=False)
2749
+ with responses.RequestsMock() as rsps:
2750
+ rsps.add(responses.POST, LOCAL_QUERY, status=404, headers=PARTIAL)
2751
+ with pytest.raises(RemoteError):
2752
+ node.get_mbox_by_query('test', local_sufficient=True)
2753
+ assert urls(rsps) == [('POST', LOCAL_QUERY)]
2754
+ assert node.upstream_url == UPSTREAM
2755
+
2756
+ @DOWN
2757
+ def test_mirror_down_uses_upstream(
2758
+ self, tmp_path: Path, sample_mbox: bytes, status: int | None
2759
+ ) -> None:
2760
+ # A sufficient local result needs the mirror to answer at all.
2761
+ node = self.node(tmp_path)
2762
+ with responses.RequestsMock() as rsps:
2763
+ add_down(rsps, responses.POST, LOCAL_QUERY, status)
2764
+ rsps.add(responses.POST, UP_QUERY, body=gz(sample_mbox))
2765
+ assert node.get_mbox_by_query('test', local_sufficient=True) == sample_mbox
2766
+ assert urls(rsps) == [('POST', LOCAL_QUERY), ('POST', UP_QUERY)]
2767
+ assert source_of(node) is Source.UPSTREAM
2768
+
2769
+ def test_bad_upstream_config_is_not_read(self, tmp_path: Path) -> None:
2770
+ # No fallback means no upstream node, so its git config can't fail.
2771
+ node = self.node(tmp_path)
2772
+ with (
2773
+ patch.object(LoreNode, 'from_git_config', side_effect=LibloreError('bad')),
2774
+ responses.RequestsMock() as rsps,
2775
+ ):
2776
+ rsps.add(responses.POST, LOCAL_QUERY, body=gz(b''), headers=PARTIAL)
2777
+ assert node.get_mbox_by_query('test', local_sufficient=True) == b''
2778
+
2779
+ def test_thread_by_query(self, tmp_path: Path) -> None:
2780
+ node = self.node(tmp_path)
2781
+ with responses.RequestsMock() as rsps:
2782
+ rsps.add(responses.POST, LOCAL_QUERY, status=404, headers=PARTIAL)
2783
+ with pytest.raises(RemoteError):
2784
+ node.get_thread_by_query('test', local_sufficient=True)
2785
+ assert urls(rsps) == [('POST', LOCAL_QUERY)]
2786
+
2787
+ def test_batch_thread_by_query(self, tmp_path: Path) -> None:
2788
+ node = self.node(tmp_path)
2789
+ with responses.RequestsMock() as rsps:
2790
+ rsps.add(responses.POST, LOCAL_QUERY, body=gz(b''), headers=PARTIAL)
2791
+ with pytest.raises(LookupError):
2792
+ node.batch_get_thread_by_query(['test'], local_sufficient=True)
2793
+ assert urls(rsps) == [('POST', LOCAL_QUERY)]
2794
+
2795
+ def test_full_node_unchanged(self, tmp_path: Path, sample_mbox: bytes) -> None:
2796
+ node = self.node(tmp_path, learned=False)
2797
+ with responses.RequestsMock() as rsps:
2798
+ rsps.add(responses.POST, LOCAL_QUERY, body=gz(sample_mbox))
2799
+ assert node.get_mbox_by_query('test', local_sufficient=True) == sample_mbox
2800
+ assert cached(node, 'mbox_by_query', 'test', 'False')
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes