vomebook commited on
Commit
6686265
·
1 Parent(s): 3c9cfda

Cover OCR route boundaries and ratchet backend coverage

Browse files
backend-coverage-debt.json CHANGED
@@ -1,7 +1,7 @@
1
  {
2
  "backend/app.py": {
3
- "covered_lines": 2074,
4
- "covered_branches": 685,
5
  "lines": [
6
  3,
7
  4,
@@ -194,6 +194,12 @@
194
  257,
195
  258,
196
  261,
 
 
 
 
 
 
197
  269,
198
  270,
199
  271,
@@ -632,6 +638,8 @@
632
  967,
633
  968,
634
  969,
 
 
635
  972,
636
  973,
637
  974,
@@ -1392,6 +1400,12 @@
1392
  2109,
1393
  2110,
1394
  2111,
 
 
 
 
 
 
1395
  2118,
1396
  2119,
1397
  2120,
@@ -1596,6 +1610,7 @@
1596
  2426,
1597
  2433,
1598
  2434,
 
1599
  2436,
1600
  2437,
1601
  2438,
@@ -1603,10 +1618,12 @@
1603
  2440,
1604
  2441,
1605
  2442,
 
1606
  2446,
1607
  2447,
1608
  2448,
1609
  2449,
 
1610
  2452,
1611
  2453,
1612
  2454,
@@ -1616,6 +1633,7 @@
1616
  2458,
1617
  2461,
1618
  2465,
 
1619
  2467,
1620
  2468,
1621
  2470,
@@ -2159,6 +2177,22 @@
2159
  247,
2160
  248
2161
  ],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2162
  [
2163
  272,
2164
  273
@@ -2699,6 +2733,10 @@
2699
  959,
2700
  962
2701
  ],
 
 
 
 
2702
  [
2703
  969,
2704
  972
@@ -3839,6 +3877,10 @@
3839
  2089,
3840
  2090
3841
  ],
 
 
 
 
3842
  [
3843
  2091,
3844
  2093
@@ -3863,10 +3905,22 @@
3863
  2108,
3864
  2110
3865
  ],
 
 
 
 
3866
  [
3867
  2111,
3868
  2118
3869
  ],
 
 
 
 
 
 
 
 
3870
  [
3871
  2119,
3872
  2120
@@ -4119,14 +4173,26 @@
4119
  2419,
4120
  2420
4121
  ],
 
 
 
 
4122
  [
4123
  2434,
4124
  2436
4125
  ],
 
 
 
 
4126
  [
4127
  2442,
4128
  2446
4129
  ],
 
 
 
 
4130
  [
4131
  2449,
4132
  2452
@@ -4824,12 +4890,6 @@
4824
  223,
4825
  227,
4826
  253,
4827
- 262,
4828
- 263,
4829
- 264,
4830
- 265,
4831
- 266,
4832
- 267,
4833
  391,
4834
  416,
4835
  423,
@@ -4850,8 +4910,6 @@
4850
  760,
4851
  762,
4852
  774,
4853
- 970,
4854
- 971,
4855
  1175,
4856
  1176,
4857
  1177,
@@ -4931,12 +4989,6 @@
4931
  2092,
4932
  2097,
4933
  2098,
4934
- 2112,
4935
- 2113,
4936
- 2114,
4937
- 2115,
4938
- 2116,
4939
- 2117,
4940
  2140,
4941
  2141,
4942
  2296,
@@ -4948,10 +5000,6 @@
4948
  2369,
4949
  2376,
4950
  2378,
4951
- 2435,
4952
- 2445,
4953
- 2451,
4954
- 2466,
4955
  2484,
4956
  2487,
4957
  2488,
@@ -5002,22 +5050,6 @@
5002
  226,
5003
  227
5004
  ],
5005
- [
5006
- 263,
5007
- 264
5008
- ],
5009
- [
5010
- 263,
5011
- 265
5012
- ],
5013
- [
5014
- 265,
5015
- 266
5016
- ],
5017
- [
5018
- 265,
5019
- 267
5020
- ],
5021
  [
5022
  390,
5023
  391
@@ -5078,10 +5110,6 @@
5078
  773,
5079
  774
5080
  ],
5081
- [
5082
- 969,
5083
- 970
5084
- ],
5085
  [
5086
  1033,
5087
  1035
@@ -5302,10 +5330,6 @@
5302
  2074,
5303
  -2070
5304
  ],
5305
- [
5306
- 2089,
5307
- 2099
5308
- ],
5309
  [
5310
  2091,
5311
  2092
@@ -5314,18 +5338,6 @@
5314
  2095,
5315
  2097
5316
  ],
5317
- [
5318
- 2111,
5319
- 2112
5320
- ],
5321
- [
5322
- 2113,
5323
- 2114
5324
- ],
5325
- [
5326
- 2113,
5327
- 2116
5328
- ],
5329
  [
5330
  2124,
5331
  2126
@@ -5354,18 +5366,6 @@
5354
  2312,
5355
  2313
5356
  ],
5357
- [
5358
- 2434,
5359
- 2435
5360
- ],
5361
- [
5362
- 2442,
5363
- 2445
5364
- ],
5365
- [
5366
- 2449,
5367
- 2451
5368
- ],
5369
  [
5370
  2455,
5371
  2457
@@ -6825,8 +6825,8 @@
6825
  ]
6826
  },
6827
  "backend/search_indexes.py": {
6828
- "covered_lines": 134,
6829
- "covered_branches": 48,
6830
  "lines": [
6831
  8,
6832
  9,
@@ -6919,6 +6919,7 @@
6919
  119,
6920
  120,
6921
  121,
 
6922
  123,
6923
  124,
6924
  125,
@@ -6928,10 +6929,12 @@
6928
  136,
6929
  137,
6930
  138,
 
6931
  140,
6932
  141,
6933
  142,
6934
  143,
 
6935
  145,
6936
  146,
6937
  147,
@@ -6944,15 +6947,18 @@
6944
  163,
6945
  164,
6946
  165,
 
6947
  167,
6948
  168,
6949
  169,
6950
  170,
6951
  171,
6952
  172,
 
6953
  174,
6954
  175,
6955
  176,
 
6956
  178,
6957
  179,
6958
  180,
@@ -7108,6 +7114,10 @@
7108
  120,
7109
  124
7110
  ],
 
 
 
 
7111
  [
7112
  121,
7113
  123
@@ -7120,10 +7130,18 @@
7120
  136,
7121
  138
7122
  ],
 
 
 
 
7123
  [
7124
  138,
7125
  140
7126
  ],
 
 
 
 
7127
  [
7128
  143,
7129
  145
@@ -7136,14 +7154,26 @@
7136
  157,
7137
  161
7138
  ],
 
 
 
 
7139
  [
7140
  165,
7141
  167
7142
  ],
 
 
 
 
7143
  [
7144
  172,
7145
  174
7146
  ],
 
 
 
 
7147
  [
7148
  176,
7149
  178
@@ -7159,27 +7189,9 @@
7159
  ],
7160
  "missing_lines": [
7161
  15,
7162
- 16,
7163
- 122,
7164
- 139,
7165
- 144,
7166
- 166,
7167
- 173,
7168
- 177
7169
  ],
7170
  "missing_arcs": [
7171
- [
7172
- 121,
7173
- 122
7174
- ],
7175
- [
7176
- 138,
7177
- 139
7178
- ],
7179
- [
7180
- 143,
7181
- 144
7182
- ],
7183
  [
7184
  152,
7185
  -151
@@ -7187,18 +7199,6 @@
7187
  [
7188
  157,
7189
  -156
7190
- ],
7191
- [
7192
- 165,
7193
- 166
7194
- ],
7195
- [
7196
- 172,
7197
- 173
7198
- ],
7199
- [
7200
- 176,
7201
- 177
7202
  ]
7203
  ]
7204
  },
 
1
  {
2
  "backend/app.py": {
3
+ "covered_lines": 2092,
4
+ "covered_branches": 697,
5
  "lines": [
6
  3,
7
  4,
 
194
  257,
195
  258,
196
  261,
197
+ 262,
198
+ 263,
199
+ 264,
200
+ 265,
201
+ 266,
202
+ 267,
203
  269,
204
  270,
205
  271,
 
638
  967,
639
  968,
640
  969,
641
+ 970,
642
+ 971,
643
  972,
644
  973,
645
  974,
 
1400
  2109,
1401
  2110,
1402
  2111,
1403
+ 2112,
1404
+ 2113,
1405
+ 2114,
1406
+ 2115,
1407
+ 2116,
1408
+ 2117,
1409
  2118,
1410
  2119,
1411
  2120,
 
1610
  2426,
1611
  2433,
1612
  2434,
1613
+ 2435,
1614
  2436,
1615
  2437,
1616
  2438,
 
1618
  2440,
1619
  2441,
1620
  2442,
1621
+ 2445,
1622
  2446,
1623
  2447,
1624
  2448,
1625
  2449,
1626
+ 2451,
1627
  2452,
1628
  2453,
1629
  2454,
 
1633
  2458,
1634
  2461,
1635
  2465,
1636
+ 2466,
1637
  2467,
1638
  2468,
1639
  2470,
 
2177
  247,
2178
  248
2179
  ],
2180
+ [
2181
+ 263,
2182
+ 264
2183
+ ],
2184
+ [
2185
+ 263,
2186
+ 265
2187
+ ],
2188
+ [
2189
+ 265,
2190
+ 266
2191
+ ],
2192
+ [
2193
+ 265,
2194
+ 267
2195
+ ],
2196
  [
2197
  272,
2198
  273
 
2733
  959,
2734
  962
2735
  ],
2736
+ [
2737
+ 969,
2738
+ 970
2739
+ ],
2740
  [
2741
  969,
2742
  972
 
3877
  2089,
3878
  2090
3879
  ],
3880
+ [
3881
+ 2089,
3882
+ 2099
3883
+ ],
3884
  [
3885
  2091,
3886
  2093
 
3905
  2108,
3906
  2110
3907
  ],
3908
+ [
3909
+ 2111,
3910
+ 2112
3911
+ ],
3912
  [
3913
  2111,
3914
  2118
3915
  ],
3916
+ [
3917
+ 2113,
3918
+ 2114
3919
+ ],
3920
+ [
3921
+ 2113,
3922
+ 2116
3923
+ ],
3924
  [
3925
  2119,
3926
  2120
 
4173
  2419,
4174
  2420
4175
  ],
4176
+ [
4177
+ 2434,
4178
+ 2435
4179
+ ],
4180
  [
4181
  2434,
4182
  2436
4183
  ],
4184
+ [
4185
+ 2442,
4186
+ 2445
4187
+ ],
4188
  [
4189
  2442,
4190
  2446
4191
  ],
4192
+ [
4193
+ 2449,
4194
+ 2451
4195
+ ],
4196
  [
4197
  2449,
4198
  2452
 
4890
  223,
4891
  227,
4892
  253,
 
 
 
 
 
 
4893
  391,
4894
  416,
4895
  423,
 
4910
  760,
4911
  762,
4912
  774,
 
 
4913
  1175,
4914
  1176,
4915
  1177,
 
4989
  2092,
4990
  2097,
4991
  2098,
 
 
 
 
 
 
4992
  2140,
4993
  2141,
4994
  2296,
 
5000
  2369,
5001
  2376,
5002
  2378,
 
 
 
 
5003
  2484,
5004
  2487,
5005
  2488,
 
5050
  226,
5051
  227
5052
  ],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5053
  [
5054
  390,
5055
  391
 
5110
  773,
5111
  774
5112
  ],
 
 
 
 
5113
  [
5114
  1033,
5115
  1035
 
5330
  2074,
5331
  -2070
5332
  ],
 
 
 
 
5333
  [
5334
  2091,
5335
  2092
 
5338
  2095,
5339
  2097
5340
  ],
 
 
 
 
 
 
 
 
 
 
 
 
5341
  [
5342
  2124,
5343
  2126
 
5366
  2312,
5367
  2313
5368
  ],
 
 
 
 
 
 
 
 
 
 
 
 
5369
  [
5370
  2455,
5371
  2457
 
6825
  ]
6826
  },
6827
  "backend/search_indexes.py": {
6828
+ "covered_lines": 140,
6829
+ "covered_branches": 54,
6830
  "lines": [
6831
  8,
6832
  9,
 
6919
  119,
6920
  120,
6921
  121,
6922
+ 122,
6923
  123,
6924
  124,
6925
  125,
 
6929
  136,
6930
  137,
6931
  138,
6932
+ 139,
6933
  140,
6934
  141,
6935
  142,
6936
  143,
6937
+ 144,
6938
  145,
6939
  146,
6940
  147,
 
6947
  163,
6948
  164,
6949
  165,
6950
+ 166,
6951
  167,
6952
  168,
6953
  169,
6954
  170,
6955
  171,
6956
  172,
6957
+ 173,
6958
  174,
6959
  175,
6960
  176,
6961
+ 177,
6962
  178,
6963
  179,
6964
  180,
 
7114
  120,
7115
  124
7116
  ],
7117
+ [
7118
+ 121,
7119
+ 122
7120
+ ],
7121
  [
7122
  121,
7123
  123
 
7130
  136,
7131
  138
7132
  ],
7133
+ [
7134
+ 138,
7135
+ 139
7136
+ ],
7137
  [
7138
  138,
7139
  140
7140
  ],
7141
+ [
7142
+ 143,
7143
+ 144
7144
+ ],
7145
  [
7146
  143,
7147
  145
 
7154
  157,
7155
  161
7156
  ],
7157
+ [
7158
+ 165,
7159
+ 166
7160
+ ],
7161
  [
7162
  165,
7163
  167
7164
  ],
7165
+ [
7166
+ 172,
7167
+ 173
7168
+ ],
7169
  [
7170
  172,
7171
  174
7172
  ],
7173
+ [
7174
+ 176,
7175
+ 177
7176
+ ],
7177
  [
7178
  176,
7179
  178
 
7189
  ],
7190
  "missing_lines": [
7191
  15,
7192
+ 16
 
 
 
 
 
 
7193
  ],
7194
  "missing_arcs": [
 
 
 
 
 
 
 
 
 
 
 
 
7195
  [
7196
  152,
7197
  -151
 
7199
  [
7200
  157,
7201
  -156
 
 
 
 
 
 
 
 
 
 
 
 
7202
  ]
7203
  ]
7204
  },
tests/test_reader_ocr_validation.py CHANGED
@@ -1,8 +1,14 @@
1
  """Validation and search contracts for the bounded PDF text index."""
 
2
  import gzip
3
  import json
 
4
  import unittest
 
 
 
5
 
 
6
  from backend import app
7
 
8
 
@@ -76,5 +82,106 @@ class ReaderOcrValidationTests(unittest.TestCase):
76
  self.assertEqual((hits[0]["page"], hits[0]["start"], hits[0]["length"]), (3, 4, 1))
77
 
78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
  if __name__ == "__main__":
80
  unittest.main()
 
1
  """Validation and search contracts for the bounded PDF text index."""
2
+ import asyncio
3
  import gzip
4
  import json
5
+ import time
6
  import unittest
7
+ from collections import OrderedDict
8
+ from types import SimpleNamespace
9
+ from unittest.mock import AsyncMock, patch
10
 
11
+ from fastapi.responses import JSONResponse
12
  from backend import app
13
 
14
 
 
82
  self.assertEqual((hits[0]["page"], hits[0]["start"], hits[0]["length"]), (3, 4, 1))
83
 
84
 
85
+ class ReaderOcrRouteTests(unittest.IsolatedAsyncioTestCase):
86
+ async def test_resolution_error_is_forwarded_without_reading_the_bucket(self):
87
+ failure = JSONResponse({"error": "unknown id"}, status_code=404)
88
+ with patch.object(app, "api_reader_resolve", AsyncMock(return_value=failure)), \
89
+ patch.object(app, "get_reader_bucket_resource", AsyncMock()) as bucket:
90
+ response = await app.api_reader_ocr_search("a" * 13, "term", 1, 10)
91
+ self.assertIs(response, failure)
92
+ bucket.assert_not_awaited()
93
+
94
+ async def test_manifest_validation_rejects_untrusted_or_corrupt_paths(self):
95
+ root = "derived/test/" + "a" * 32
96
+ manifest = root + "/ocr-manifest.json"
97
+ url = "https://voiceofml-search.hf.space/api/reader-bucket-resource?path=" + manifest
98
+ for bad_url in ("https://example.org/not-reader?path=" + manifest,
99
+ "/api/reader-bucket-resource?path=" + manifest + "&extra=1",
100
+ "/api/reader-bucket-resource?path=../../ocr-manifest.json",
101
+ "/api/reader-bucket-resource?path=" + root + "/not-a-manifest.json"):
102
+ with self.subTest(url=bad_url), \
103
+ patch.object(app, "api_reader_resolve", AsyncMock(return_value=JSONResponse({"ocr_manifest": bad_url}))), \
104
+ patch.object(app, "get_reader_bucket_resource", AsyncMock()) as bucket:
105
+ response = await app.api_reader_ocr_search("a" * 13, "x", 1, 5)
106
+ self.assertEqual(response.status_code, 404)
107
+ bucket.assert_not_awaited()
108
+ for payload in ([], {}, {"book_text": {}}, {"book_text": {"path": "../book-text.json.gz"}},
109
+ {"book_text": {"path": root + "/wrong.json.gz"}}):
110
+ with self.subTest(payload=payload), \
111
+ patch.object(app, "api_reader_resolve", AsyncMock(return_value=JSONResponse({"ocr_manifest": url}))), \
112
+ patch.object(app, "get_reader_bucket_resource", AsyncMock(return_value=json.dumps(payload).encode())), \
113
+ patch.object(app, "get_reader_ocr_book", AsyncMock()) as load:
114
+ response = await app.api_reader_ocr_search("a" * 13, "x", 1, 5)
115
+ self.assertEqual(response.status_code, 502)
116
+ load.assert_not_awaited()
117
+
118
+ async def test_invalid_manifest_json_returns_corruption_error(self):
119
+ manifest = "derived/test/" + "a" * 32 + "/ocr-manifest.json"
120
+ url = "/api/reader-bucket-resource?path=" + manifest
121
+ with patch.object(app, "api_reader_resolve", AsyncMock(return_value=JSONResponse({"ocr_manifest": url}))), \
122
+ patch.object(app, "get_reader_bucket_resource", AsyncMock(return_value=b"not json")):
123
+ response = await app.api_reader_ocr_search("a" * 13, "x", 1, 5)
124
+ self.assertEqual(response.status_code, 502)
125
+ self.assertEqual(json.loads(response.body), {"error": "OCR 索引损坏"})
126
+
127
+ async def test_ocr_cache_hit_refreshes_lru_and_expiry_reloads_once(self):
128
+ original, fresh = [{"page": 1}], [{"page": 2}]
129
+ current = time.monotonic()
130
+ state = SimpleNamespace(reader_ocr_cache=OrderedDict([
131
+ ("book", (current, original, 5)), ("other", (current, [], 7))]),
132
+ reader_ocr_cache_bytes=12, reader_ocr_inflight={})
133
+ with patch.object(app, "app", SimpleNamespace(state=state)), \
134
+ patch.object(app, "_load_reader_ocr_book", AsyncMock(return_value=fresh)) as load:
135
+ self.assertIs(await app.get_reader_ocr_book("book"), original)
136
+ self.assertEqual(list(state.reader_ocr_cache), ["other", "book"])
137
+ load.assert_not_awaited()
138
+ state.reader_ocr_cache["book"] = (current - app.READER_BUCKET_CACHE_TTL_SECONDS - 1, original, 5)
139
+ with patch.object(app, "app", SimpleNamespace(state=state)), \
140
+ patch.object(app, "_load_reader_ocr_book", AsyncMock(return_value=fresh)) as load:
141
+ self.assertIs(await app.get_reader_ocr_book("book"), fresh)
142
+ self.assertNotIn("book", state.reader_ocr_cache)
143
+ self.assertEqual(state.reader_ocr_cache_bytes, 7)
144
+ load.assert_awaited_once_with("book")
145
+
146
+ async def test_cancelled_subscriber_does_not_cancel_shared_ocr_parse(self):
147
+ state = SimpleNamespace()
148
+ started, release = asyncio.Event(), asyncio.Event()
149
+ pages = [{"page": 1}]
150
+
151
+ async def load(_path):
152
+ started.set()
153
+ await release.wait()
154
+ return pages
155
+
156
+ with patch.object(app, "app", SimpleNamespace(state=state)), \
157
+ patch.object(app, "_load_reader_ocr_book", side_effect=load) as loader:
158
+ first = asyncio.create_task(app.get_reader_ocr_book("book"))
159
+ await asyncio.wait_for(started.wait(), 1)
160
+ second = asyncio.create_task(app.get_reader_ocr_book("book"))
161
+ try:
162
+ await asyncio.sleep(0)
163
+ first.cancel()
164
+ with self.assertRaises(asyncio.CancelledError):
165
+ await first
166
+ self.assertFalse(second.done())
167
+ finally:
168
+ release.set()
169
+ self.assertIs(await asyncio.wait_for(second, 1), pages)
170
+ await asyncio.sleep(0)
171
+ self.assertEqual(state.reader_ocr_inflight, {})
172
+ loader.assert_awaited_once_with("book")
173
+
174
+ async def test_oversized_parsed_book_does_not_enter_ocr_cache(self):
175
+ pages = [{"page": 1}]
176
+ state = SimpleNamespace(reader_ocr_cache=OrderedDict(), reader_ocr_cache_bytes=0)
177
+ with patch.object(app, "app", SimpleNamespace(state=state)), \
178
+ patch.object(app, "get_reader_bucket_resource", AsyncMock(return_value=b"data")), \
179
+ patch.object(app, "parse_reader_ocr_book", return_value=(pages, 101)), \
180
+ patch.object(app, "READER_OCR_CACHE_MAX_BYTES", 100):
181
+ self.assertIs(await app._load_reader_ocr_book("book"), pages)
182
+ self.assertEqual(state.reader_ocr_cache, {})
183
+ self.assertEqual(state.reader_ocr_cache_bytes, 0)
184
+
185
+
186
  if __name__ == "__main__":
187
  unittest.main()
tests/test_search_words_contract.py CHANGED
@@ -37,6 +37,16 @@ class SearchWordsContractTests(unittest.TestCase):
37
  self.assertEqual(app.word_index, {"alpha": {0}, "docs": {0, 1}, "beta": {1}})
38
  self.assertEqual(app.vocab_by_len, {5: ["alpha"], 4: ["docs", "beta"]})
39
 
 
 
 
 
 
 
 
 
 
 
40
  def test_failure_preserves_partially_built_index_and_previous_vocab(self):
41
  from backend import app
42
  records = [{"File": "alpha", "Folder": []}, {"File": "beta", "Folder": []}]
 
37
  self.assertEqual(app.word_index, {"alpha": {0}, "docs": {0, 1}, "beta": {1}})
38
  self.assertEqual(app.vocab_by_len, {5: ["alpha"], 4: ["docs", "beta"]})
39
 
40
+ def test_posting_count_uses_validated_packed_count_without_decoding(self):
41
+ from backend import app
42
+ index = {"materialized": {1, 3}, "packed": (7, 99, 4), "empty": set()}
43
+ with patch.object(app, "word_index", index), \
44
+ patch.object(app, "resolve_postings", side_effect=AssertionError("unnecessary decoding")):
45
+ self.assertEqual(app.word_posting_count("absent"), 0)
46
+ self.assertEqual(app.word_posting_count("empty"), 0)
47
+ self.assertEqual(app.word_posting_count("materialized"), 2)
48
+ self.assertEqual(app.word_posting_count("packed"), 4)
49
+
50
  def test_failure_preserves_partially_built_index_and_previous_vocab(self):
51
  from backend import app
52
  records = [{"File": "alpha", "Folder": []}, {"File": "beta", "Folder": []}]