vomebook commited on
Commit ·
cdda7ab
1
Parent(s): 6a7cb2e
Gate search executor and strengthen reader metadata coverage
Browse files
backend-coverage-debt.json
CHANGED
|
@@ -6495,335 +6495,6 @@
|
|
| 6495 |
]
|
| 6496 |
]
|
| 6497 |
},
|
| 6498 |
-
"backend/record_metadata.py": {
|
| 6499 |
-
"covered_lines": 114,
|
| 6500 |
-
"covered_branches": 39,
|
| 6501 |
-
"lines": [
|
| 6502 |
-
2,
|
| 6503 |
-
3,
|
| 6504 |
-
4,
|
| 6505 |
-
5,
|
| 6506 |
-
8,
|
| 6507 |
-
12,
|
| 6508 |
-
15,
|
| 6509 |
-
16,
|
| 6510 |
-
17,
|
| 6511 |
-
18,
|
| 6512 |
-
19,
|
| 6513 |
-
20,
|
| 6514 |
-
23,
|
| 6515 |
-
24,
|
| 6516 |
-
27,
|
| 6517 |
-
28,
|
| 6518 |
-
31,
|
| 6519 |
-
33,
|
| 6520 |
-
34,
|
| 6521 |
-
35,
|
| 6522 |
-
38,
|
| 6523 |
-
39,
|
| 6524 |
-
42,
|
| 6525 |
-
43,
|
| 6526 |
-
46,
|
| 6527 |
-
47,
|
| 6528 |
-
48,
|
| 6529 |
-
49,
|
| 6530 |
-
50,
|
| 6531 |
-
53,
|
| 6532 |
-
54,
|
| 6533 |
-
55,
|
| 6534 |
-
58,
|
| 6535 |
-
59,
|
| 6536 |
-
60,
|
| 6537 |
-
63,
|
| 6538 |
-
64,
|
| 6539 |
-
65,
|
| 6540 |
-
66,
|
| 6541 |
-
67,
|
| 6542 |
-
73,
|
| 6543 |
-
74,
|
| 6544 |
-
75,
|
| 6545 |
-
76,
|
| 6546 |
-
77,
|
| 6547 |
-
78,
|
| 6548 |
-
79,
|
| 6549 |
-
83,
|
| 6550 |
-
84,
|
| 6551 |
-
85,
|
| 6552 |
-
86,
|
| 6553 |
-
89,
|
| 6554 |
-
90,
|
| 6555 |
-
91,
|
| 6556 |
-
94,
|
| 6557 |
-
95,
|
| 6558 |
-
96,
|
| 6559 |
-
97,
|
| 6560 |
-
98,
|
| 6561 |
-
99,
|
| 6562 |
-
100,
|
| 6563 |
-
101,
|
| 6564 |
-
102,
|
| 6565 |
-
103,
|
| 6566 |
-
104,
|
| 6567 |
-
105,
|
| 6568 |
-
106,
|
| 6569 |
-
107,
|
| 6570 |
-
110,
|
| 6571 |
-
111,
|
| 6572 |
-
114,
|
| 6573 |
-
115,
|
| 6574 |
-
116,
|
| 6575 |
-
117,
|
| 6576 |
-
118,
|
| 6577 |
-
119,
|
| 6578 |
-
120,
|
| 6579 |
-
121,
|
| 6580 |
-
123,
|
| 6581 |
-
124,
|
| 6582 |
-
125,
|
| 6583 |
-
128,
|
| 6584 |
-
129,
|
| 6585 |
-
133,
|
| 6586 |
-
134,
|
| 6587 |
-
135,
|
| 6588 |
-
136,
|
| 6589 |
-
137,
|
| 6590 |
-
139,
|
| 6591 |
-
140,
|
| 6592 |
-
141,
|
| 6593 |
-
144,
|
| 6594 |
-
147,
|
| 6595 |
-
148,
|
| 6596 |
-
149,
|
| 6597 |
-
150,
|
| 6598 |
-
154,
|
| 6599 |
-
155,
|
| 6600 |
-
158,
|
| 6601 |
-
160,
|
| 6602 |
-
161,
|
| 6603 |
-
165,
|
| 6604 |
-
166,
|
| 6605 |
-
167,
|
| 6606 |
-
168,
|
| 6607 |
-
171,
|
| 6608 |
-
172,
|
| 6609 |
-
173,
|
| 6610 |
-
179,
|
| 6611 |
-
180,
|
| 6612 |
-
181,
|
| 6613 |
-
182,
|
| 6614 |
-
183,
|
| 6615 |
-
184
|
| 6616 |
-
],
|
| 6617 |
-
"arcs": [
|
| 6618 |
-
[
|
| 6619 |
-
48,
|
| 6620 |
-
49
|
| 6621 |
-
],
|
| 6622 |
-
[
|
| 6623 |
-
48,
|
| 6624 |
-
50
|
| 6625 |
-
],
|
| 6626 |
-
[
|
| 6627 |
-
58,
|
| 6628 |
-
59
|
| 6629 |
-
],
|
| 6630 |
-
[
|
| 6631 |
-
58,
|
| 6632 |
-
60
|
| 6633 |
-
],
|
| 6634 |
-
[
|
| 6635 |
-
67,
|
| 6636 |
-
73
|
| 6637 |
-
],
|
| 6638 |
-
[
|
| 6639 |
-
74,
|
| 6640 |
-
75
|
| 6641 |
-
],
|
| 6642 |
-
[
|
| 6643 |
-
74,
|
| 6644 |
-
84
|
| 6645 |
-
],
|
| 6646 |
-
[
|
| 6647 |
-
78,
|
| 6648 |
-
79
|
| 6649 |
-
],
|
| 6650 |
-
[
|
| 6651 |
-
96,
|
| 6652 |
-
97
|
| 6653 |
-
],
|
| 6654 |
-
[
|
| 6655 |
-
96,
|
| 6656 |
-
98
|
| 6657 |
-
],
|
| 6658 |
-
[
|
| 6659 |
-
98,
|
| 6660 |
-
99
|
| 6661 |
-
],
|
| 6662 |
-
[
|
| 6663 |
-
98,
|
| 6664 |
-
100
|
| 6665 |
-
],
|
| 6666 |
-
[
|
| 6667 |
-
100,
|
| 6668 |
-
101
|
| 6669 |
-
],
|
| 6670 |
-
[
|
| 6671 |
-
100,
|
| 6672 |
-
106
|
| 6673 |
-
],
|
| 6674 |
-
[
|
| 6675 |
-
102,
|
| 6676 |
-
103
|
| 6677 |
-
],
|
| 6678 |
-
[
|
| 6679 |
-
102,
|
| 6680 |
-
104
|
| 6681 |
-
],
|
| 6682 |
-
[
|
| 6683 |
-
118,
|
| 6684 |
-
119
|
| 6685 |
-
],
|
| 6686 |
-
[
|
| 6687 |
-
118,
|
| 6688 |
-
121
|
| 6689 |
-
],
|
| 6690 |
-
[
|
| 6691 |
-
121,
|
| 6692 |
-
123
|
| 6693 |
-
],
|
| 6694 |
-
[
|
| 6695 |
-
123,
|
| 6696 |
-
124
|
| 6697 |
-
],
|
| 6698 |
-
[
|
| 6699 |
-
123,
|
| 6700 |
-
125
|
| 6701 |
-
],
|
| 6702 |
-
[
|
| 6703 |
-
125,
|
| 6704 |
-
128
|
| 6705 |
-
],
|
| 6706 |
-
[
|
| 6707 |
-
125,
|
| 6708 |
-
129
|
| 6709 |
-
],
|
| 6710 |
-
[
|
| 6711 |
-
129,
|
| 6712 |
-
133
|
| 6713 |
-
],
|
| 6714 |
-
[
|
| 6715 |
-
129,
|
| 6716 |
-
136
|
| 6717 |
-
],
|
| 6718 |
-
[
|
| 6719 |
-
133,
|
| 6720 |
-
134
|
| 6721 |
-
],
|
| 6722 |
-
[
|
| 6723 |
-
133,
|
| 6724 |
-
135
|
| 6725 |
-
],
|
| 6726 |
-
[
|
| 6727 |
-
137,
|
| 6728 |
-
139
|
| 6729 |
-
],
|
| 6730 |
-
[
|
| 6731 |
-
139,
|
| 6732 |
-
140
|
| 6733 |
-
],
|
| 6734 |
-
[
|
| 6735 |
-
139,
|
| 6736 |
-
144
|
| 6737 |
-
],
|
| 6738 |
-
[
|
| 6739 |
-
140,
|
| 6740 |
-
141
|
| 6741 |
-
],
|
| 6742 |
-
[
|
| 6743 |
-
155,
|
| 6744 |
-
158
|
| 6745 |
-
],
|
| 6746 |
-
[
|
| 6747 |
-
155,
|
| 6748 |
-
160
|
| 6749 |
-
],
|
| 6750 |
-
[
|
| 6751 |
-
160,
|
| 6752 |
-
161
|
| 6753 |
-
],
|
| 6754 |
-
[
|
| 6755 |
-
160,
|
| 6756 |
-
168
|
| 6757 |
-
],
|
| 6758 |
-
[
|
| 6759 |
-
161,
|
| 6760 |
-
165
|
| 6761 |
-
],
|
| 6762 |
-
[
|
| 6763 |
-
161,
|
| 6764 |
-
167
|
| 6765 |
-
],
|
| 6766 |
-
[
|
| 6767 |
-
165,
|
| 6768 |
-
161
|
| 6769 |
-
],
|
| 6770 |
-
[
|
| 6771 |
-
165,
|
| 6772 |
-
166
|
| 6773 |
-
]
|
| 6774 |
-
],
|
| 6775 |
-
"missing_lines": [
|
| 6776 |
-
68,
|
| 6777 |
-
69,
|
| 6778 |
-
70,
|
| 6779 |
-
71,
|
| 6780 |
-
72,
|
| 6781 |
-
81,
|
| 6782 |
-
82,
|
| 6783 |
-
122,
|
| 6784 |
-
138,
|
| 6785 |
-
142,
|
| 6786 |
-
143
|
| 6787 |
-
],
|
| 6788 |
-
"missing_arcs": [
|
| 6789 |
-
[
|
| 6790 |
-
67,
|
| 6791 |
-
68
|
| 6792 |
-
],
|
| 6793 |
-
[
|
| 6794 |
-
69,
|
| 6795 |
-
70
|
| 6796 |
-
],
|
| 6797 |
-
[
|
| 6798 |
-
69,
|
| 6799 |
-
72
|
| 6800 |
-
],
|
| 6801 |
-
[
|
| 6802 |
-
78,
|
| 6803 |
-
81
|
| 6804 |
-
],
|
| 6805 |
-
[
|
| 6806 |
-
121,
|
| 6807 |
-
122
|
| 6808 |
-
],
|
| 6809 |
-
[
|
| 6810 |
-
137,
|
| 6811 |
-
138
|
| 6812 |
-
],
|
| 6813 |
-
[
|
| 6814 |
-
140,
|
| 6815 |
-
142
|
| 6816 |
-
],
|
| 6817 |
-
[
|
| 6818 |
-
142,
|
| 6819 |
-
143
|
| 6820 |
-
],
|
| 6821 |
-
[
|
| 6822 |
-
142,
|
| 6823 |
-
144
|
| 6824 |
-
]
|
| 6825 |
-
]
|
| 6826 |
-
},
|
| 6827 |
"backend/search_indexes.py": {
|
| 6828 |
"covered_lines": 140,
|
| 6829 |
"covered_branches": 54,
|
|
|
|
| 6495 |
]
|
| 6496 |
]
|
| 6497 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6498 |
"backend/search_indexes.py": {
|
| 6499 |
"covered_lines": 140,
|
| 6500 |
"covered_branches": 54,
|
mutation-equivalent-search-execution.json
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"tool": "mutmut 3.8.1",
|
| 3 |
+
"source": "backend/search_execution.py",
|
| 4 |
+
"source_sha256": "a1706a4d6ff87302d0484f720a46b86121a912e7086ad17e3ba2e882433df447",
|
| 5 |
+
"equivalent": {}
|
| 6 |
+
}
|
scripts/check_search_words_mutation.py
CHANGED
|
@@ -34,6 +34,20 @@ MATCHING_TESTS = [
|
|
| 34 |
"test_wildcard_cache_reuses_literal_scans_across_matches",
|
| 35 |
"test_fuzzy_match_stops_at_the_two_edit_work_bound",
|
| 36 |
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
SPECS: dict[str, tuple[str, list[str], list[str]]] = {
|
| 38 |
"search_words": ("test_search_words_contract.py", SELECTED_TESTS, []),
|
| 39 |
"search_matching": ("test_search_words_contract.py", MATCHING_TESTS, ["search_words.py"]),
|
|
@@ -43,6 +57,7 @@ SPECS: dict[str, tuple[str, list[str], list[str]]] = {
|
|
| 43 |
"cache_core": ("test_cache_core_contract.py", [], []),
|
| 44 |
"reader_index": ("test_reader_index_contract.py", [], []),
|
| 45 |
"upstream_http": ("test_upstream_http.py", [], []),
|
|
|
|
| 46 |
}
|
| 47 |
EMPTY_STATUSES = ("no_tests", "skipped", "suspicious", "timeout", "check_was_interrupted_by_user", "segfault")
|
| 48 |
|
|
@@ -111,7 +126,8 @@ def main() -> int:
|
|
| 111 |
if module == "reader_identity":
|
| 112 |
shutil.copyfile(ROOT / "tests/reader-contract-vectors.json", temp / "tests/reader-contract-vectors.json")
|
| 113 |
(temp / "tests/__init__.py").write_text("")
|
| 114 |
-
|
|
|
|
| 115 |
if selected else [f"tests/{test_name}::PythonSearchIndexTests"
|
| 116 |
if module == "search_indexes" else f"tests/{test_name}"])
|
| 117 |
(temp / "pyproject.toml").write_text(
|
|
|
|
| 34 |
"test_wildcard_cache_reuses_literal_scans_across_matches",
|
| 35 |
"test_fuzzy_match_stops_at_the_two_edit_work_bound",
|
| 36 |
]
|
| 37 |
+
EXECUTION_TESTS = [
|
| 38 |
+
"test_background_executor_drains_before_context_exit",
|
| 39 |
+
"test_completed_job_registers_a_callable_cleanup",
|
| 40 |
+
"test_cancelling_background_wait_keeps_foreground_ownership",
|
| 41 |
+
"test_background_jobs_wait_for_admitted_foreground_jobs",
|
| 42 |
+
"test_active_cancellation_retains_slot_and_queued_cancellation_drops_work",
|
| 43 |
+
"test_capacity_failure_and_recovery",
|
| 44 |
+
"test_single_named_search_thread_and_real_millisecond_timings",
|
| 45 |
+
"test_repeated_foreground_completion_does_not_leave_stale_owners",
|
| 46 |
+
"test_close_waits_for_started_work_after_requester_cancels",
|
| 47 |
+
"test_abandoned_failures_are_consumed_and_executor_recovers",
|
| 48 |
+
"test_close_drains_failed_owned_job_before_shutting_down",
|
| 49 |
+
"test_shutdown_joins_worker_and_constructor_sets_boolean_state",
|
| 50 |
+
]
|
| 51 |
SPECS: dict[str, tuple[str, list[str], list[str]]] = {
|
| 52 |
"search_words": ("test_search_words_contract.py", SELECTED_TESTS, []),
|
| 53 |
"search_matching": ("test_search_words_contract.py", MATCHING_TESTS, ["search_words.py"]),
|
|
|
|
| 57 |
"cache_core": ("test_cache_core_contract.py", [], []),
|
| 58 |
"reader_index": ("test_reader_index_contract.py", [], []),
|
| 59 |
"upstream_http": ("test_upstream_http.py", [], []),
|
| 60 |
+
"search_execution": ("test_search_execution.py", EXECUTION_TESTS, []),
|
| 61 |
}
|
| 62 |
EMPTY_STATUSES = ("no_tests", "skipped", "suspicious", "timeout", "check_was_interrupted_by_user", "segfault")
|
| 63 |
|
|
|
|
| 126 |
if module == "reader_identity":
|
| 127 |
shutil.copyfile(ROOT / "tests/reader-contract-vectors.json", temp / "tests/reader-contract-vectors.json")
|
| 128 |
(temp / "tests/__init__.py").write_text("")
|
| 129 |
+
test_class = "SearchExecutionTests" if module == "search_execution" else "SearchWordsContractTests"
|
| 130 |
+
selections = ([f"tests/{test_name}::{test_class}::{name}" for name in selected]
|
| 131 |
if selected else [f"tests/{test_name}::PythonSearchIndexTests"
|
| 132 |
if module == "search_indexes" else f"tests/{test_name}"])
|
| 133 |
(temp / "pyproject.toml").write_text(
|
tests/test_quality_inventory.py
CHANGED
|
@@ -1,11 +1,12 @@
|
|
| 1 |
"""Regression tests for strict inventory and backend coverage ratchets."""
|
|
|
|
| 2 |
import json
|
| 3 |
from pathlib import Path
|
| 4 |
import tempfile
|
| 5 |
import unittest
|
| 6 |
|
| 7 |
from scripts.check_backend_coverage import coverage_errors
|
| 8 |
-
from scripts.check_search_words_mutation import EMPTY_STATUSES, generated_mutations, mutation_errors
|
| 9 |
from scripts.check_strict_inventory import inventory_errors
|
| 10 |
|
| 11 |
|
|
@@ -194,6 +195,17 @@ class Example:
|
|
| 194 |
recorded = {"backend.example.x_Example_sync__mutmut_1": 1}
|
| 195 |
self.assertEqual(set(nodes) - set(recorded), {"backend.example.x_Example_async__mutmut_1"})
|
| 196 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 197 |
|
| 198 |
if __name__ == "__main__":
|
| 199 |
unittest.main()
|
|
|
|
| 1 |
"""Regression tests for strict inventory and backend coverage ratchets."""
|
| 2 |
+
import ast
|
| 3 |
import json
|
| 4 |
from pathlib import Path
|
| 5 |
import tempfile
|
| 6 |
import unittest
|
| 7 |
|
| 8 |
from scripts.check_backend_coverage import coverage_errors
|
| 9 |
+
from scripts.check_search_words_mutation import EMPTY_STATUSES, EXECUTION_TESTS, generated_mutations, mutation_errors
|
| 10 |
from scripts.check_strict_inventory import inventory_errors
|
| 11 |
|
| 12 |
|
|
|
|
| 195 |
recorded = {"backend.example.x_Example_sync__mutmut_1": 1}
|
| 196 |
self.assertEqual(set(nodes) - set(recorded), {"backend.example.x_Example_async__mutmut_1"})
|
| 197 |
|
| 198 |
+
def test_execution_mutation_gate_selects_all_standalone_contracts(self):
|
| 199 |
+
test_file = Path(__file__).with_name("test_search_execution.py")
|
| 200 |
+
module = ast.parse(test_file.read_text())
|
| 201 |
+
classes = [node for node in module.body if isinstance(node, ast.ClassDef) and node.name == "SearchExecutionTests"]
|
| 202 |
+
self.assertEqual(len(classes), 1)
|
| 203 |
+
tests = {node.name for node in classes[0].body if isinstance(node, ast.AsyncFunctionDef)
|
| 204 |
+
and node.name.startswith("test_")}
|
| 205 |
+
self.assertEqual(len(EXECUTION_TESTS), len(set(EXECUTION_TESTS)))
|
| 206 |
+
self.assertEqual(tests - set(EXECUTION_TESTS), {"test_slow_search_does_not_block_http_ping_and_busy_requests_retry"})
|
| 207 |
+
self.assertTrue(set(EXECUTION_TESTS) <= tests)
|
| 208 |
+
|
| 209 |
|
| 210 |
if __name__ == "__main__":
|
| 211 |
unittest.main()
|
tests/test_reader_backend.py
CHANGED
|
@@ -52,6 +52,20 @@ def make_zip(files=None, comment=b"", zip64=False):
|
|
| 52 |
|
| 53 |
@unittest.skipIf(app is None, f"backend dependencies unavailable: {IMPORT_ERROR}")
|
| 54 |
class ReaderArchiveValidationTests(unittest.TestCase):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
def test_canonical_paths_preserve_character_encoder_semantics(self):
|
| 56 |
prefix = "https://huggingface.co/datasets/VoiceOfML/Test/resolve/main/"
|
| 57 |
def reference(value):
|
|
|
|
| 52 |
|
| 53 |
@unittest.skipIf(app is None, f"backend dependencies unavailable: {IMPORT_ERROR}")
|
| 54 |
class ReaderArchiveValidationTests(unittest.TestCase):
|
| 55 |
+
def test_reader_asset_decoding_discards_unknown_fields_and_preserves_status(self):
|
| 56 |
+
# Projection functions consume published, sanitized sidecar entries,
|
| 57 |
+
# not arbitrary unknown fields from the raw producer payload.
|
| 58 |
+
path = "documents/text/txt/" + "a" * 64 + "/document.txt"
|
| 59 |
+
entry = {"s": 2, "m": "t", "p": path, "b": "vomebook/reader-assets-v2",
|
| 60 |
+
"S": 3, "XXsXX": 3}
|
| 61 |
+
raw = gzip.compress(json.dumps({"v": 1, "f": {"book": entry}}).encode())
|
| 62 |
+
decoded = app.decode_reader_assets(raw)["f"]["book"]
|
| 63 |
+
self.assertEqual(decoded, {"s": 2, "m": "t", "p": path, "b": "vomebook/reader-assets-v2"})
|
| 64 |
+
projected = app.enrich_reader_record({"File": "book", "Extension": "txt"}, decoded)
|
| 65 |
+
self.assertEqual(projected["ReaderExtension"], "txt")
|
| 66 |
+
self.assertEqual(projected["ReaderLink"],
|
| 67 |
+
"https://voiceofml-search.hf.space/api/reader-bucket-resource?path=" + path)
|
| 68 |
+
|
| 69 |
def test_canonical_paths_preserve_character_encoder_semantics(self):
|
| 70 |
prefix = "https://huggingface.co/datasets/VoiceOfML/Test/resolve/main/"
|
| 71 |
def reference(value):
|
tests/test_record_metadata_contract.py
ADDED
|
@@ -0,0 +1,296 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Reader projections preserve source paths, existing overrides and sidecars."""
|
| 2 |
+
import copy
|
| 3 |
+
import unittest
|
| 4 |
+
from unittest.mock import patch
|
| 5 |
+
from urllib.parse import quote
|
| 6 |
+
|
| 7 |
+
from backend import record_metadata as metadata
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
class RecordMetadataTests(unittest.TestCase):
|
| 11 |
+
def test_path_helpers_keep_case_repeated_extensions_and_escaping(self):
|
| 12 |
+
record = {"Repo": "VoiceOfML/Test", "File": "书 名.PDF", "Extension": "pdf", "Folder": ["目录", "A#B"]}
|
| 13 |
+
path = "目录/A#B/书 名.PDF.pdf"
|
| 14 |
+
source = "https://huggingface.co/datasets/VoiceOfML/Test/resolve/main/" + quote(path, safe="/")
|
| 15 |
+
self.assertEqual(metadata.build_record_relative_path(record), path)
|
| 16 |
+
self.assertEqual(metadata.build_reader_asset_key(record), "VoiceOfML/Test\0" + path)
|
| 17 |
+
self.assertEqual(metadata.build_record_link(record), source)
|
| 18 |
+
self.assertEqual(metadata.build_record_path_url(record), source.replace("/resolve/", "/blob/"))
|
| 19 |
+
self.assertEqual(metadata.build_txt_relative_path(record), "目录/A#B/书 名.PDF.txt")
|
| 20 |
+
self.assertEqual(metadata.build_txt_relative_path({"File": "README", "Folder": []}), "README.txt")
|
| 21 |
+
self.assertEqual(metadata.build_record_relative_path({}), "")
|
| 22 |
+
self.assertEqual(metadata.dataset_file_urls("VoiceOfML/Test", path),
|
| 23 |
+
(source, source.replace("/resolve/", "/blob/")))
|
| 24 |
+
|
| 25 |
+
def test_folder_projection_preserves_source_and_explicit_overrides(self):
|
| 26 |
+
entry = {"current_path": "explicit", "folders": [{"name": "child"}, {"name": "x", "path": "keep"}],
|
| 27 |
+
"files": [{"name": "书 #", "ext": "pdf"}, {"name": "noext", "link": "custom"},
|
| 28 |
+
{"name": "x", "ext": "txt", "path": "custom-path"}]}
|
| 29 |
+
original = copy.deepcopy(entry)
|
| 30 |
+
result = metadata.enrich_browser_entry("VoiceOfML/Test", "docs", entry)
|
| 31 |
+
self.assertEqual(entry, original)
|
| 32 |
+
self.assertEqual(result["current_path"], "explicit")
|
| 33 |
+
self.assertEqual(result["folders"], [{"name": "child", "path": "docs/child"},
|
| 34 |
+
{"name": "x", "path": "keep"}])
|
| 35 |
+
files = result["files"]
|
| 36 |
+
self.assertEqual(files[0]["link"], "https://huggingface.co/datasets/VoiceOfML/Test/resolve/main/docs/%E4%B9%A6%20%23.pdf")
|
| 37 |
+
self.assertEqual(files[0]["path"], files[0]["link"].replace("/resolve/", "/blob/"))
|
| 38 |
+
self.assertEqual((files[1]["link"], files[1]["path"]),
|
| 39 |
+
("custom", "https://huggingface.co/datasets/VoiceOfML/Test/blob/main/docs/noext"))
|
| 40 |
+
self.assertEqual((files[2]["link"], files[2]["path"]),
|
| 41 |
+
("https://huggingface.co/datasets/VoiceOfML/Test/resolve/main/docs/x.txt", "custom-path"))
|
| 42 |
+
self.assertEqual(metadata.enrich_browser_entry("R", "", {"folders": [{"name": "root"}]})["folders"],
|
| 43 |
+
[{"name": "root", "path": "root"}])
|
| 44 |
+
self.assertEqual(metadata.build_browser_file_name({}), "file")
|
| 45 |
+
self.assertEqual(metadata.build_browser_file_name({"name": "poster.jpg", "ext": "jpg"}), "poster.jpg.jpg")
|
| 46 |
+
|
| 47 |
+
def test_sidecar_selection_retains_original_and_independent_ocr(self):
|
| 48 |
+
original = {"Repo": "VoiceOfML/Test", "File": "book", "Extension": "pdf", "Folder": ["docs"]}
|
| 49 |
+
source = metadata.build_record_link(original)
|
| 50 |
+
ocr = "derived/test/" + "a" * 32 + "/ocr-manifest.json"
|
| 51 |
+
pdf = "derived/test/" + "a" * 32 + "/document.pdf"
|
| 52 |
+
page = "derived/test/" + "a" * 32 + "/page-manifest.json"
|
| 53 |
+
ocr_url = metadata._reader_bucket_url(ocr)
|
| 54 |
+
ocr_only = {"s": 3, "m": "p", "o": ocr, "b": "vomebook/pdf-pages-v2", "om": "existing"}
|
| 55 |
+
self.assertEqual(metadata.reader_asset_fields(ocr_only, "pdf"), {"ocr_manifest": ocr_url, "ocr_mode": "existing"})
|
| 56 |
+
for bucket in ("vomebook/reader-assets-v2", "vomebook/pdf-pages-v2"):
|
| 57 |
+
asset = {"s": 2, "m": "p", "p": page, "pd": pdf, "pdb": bucket,
|
| 58 |
+
"o": ocr, "ob": "vomebook/pdf-pages-v2"}
|
| 59 |
+
fields = metadata.reader_asset_fields(asset, "pdf")
|
| 60 |
+
self.assertEqual(fields, {"ocr_manifest": ocr_url, "ocr_mode": "",
|
| 61 |
+
"url": metadata._reader_bucket_url(pdf), "extension": "pdf"})
|
| 62 |
+
resolved = metadata.reader_resolved_record(original, asset)
|
| 63 |
+
self.assertEqual((resolved["url"], resolved["download"], resolved["original_extension"]),
|
| 64 |
+
(metadata._reader_bucket_url(pdf), source, "pdf"))
|
| 65 |
+
djvu = {**original, "Extension": "djvu"}
|
| 66 |
+
self.assertEqual(metadata.reader_asset_fields(asset, "djvu"), {"ocr_manifest": ocr_url, "ocr_mode": ""})
|
| 67 |
+
self.assertEqual(metadata.reader_resolved_record(djvu, asset)["url"], metadata.build_record_link(djvu))
|
| 68 |
+
self.assertEqual(metadata.reader_asset_fields({"s": 2, "m": "p", "p": page}, "pdf"), {})
|
| 69 |
+
self.assertEqual(metadata.reader_resolved_record(original, {})["url"], source)
|
| 70 |
+
|
| 71 |
+
def test_chapter_and_pdf_companion_fields_preserve_bucket_contract(self):
|
| 72 |
+
chapter = "chapters/ebook/chm/" + "a" * 64 + "/" + "b" * 16 + "/chapter-manifest.json"
|
| 73 |
+
pdf = "derived/test/" + "a" * 32 + "/document.pdf"
|
| 74 |
+
asset = {"s": 2, "m": "e", "p": "native/ebook/chm/" + "a" * 64 + "/document.epub",
|
| 75 |
+
"c": chapter, "cb": "vomebook/reader-assets-v2", "pd": pdf, "pdb": "vomebook/pdf-pages-v2"}
|
| 76 |
+
fields = metadata.reader_asset_fields(asset, "chm")
|
| 77 |
+
self.assertEqual(fields["extension"], "epub-chapters")
|
| 78 |
+
self.assertEqual(fields["chapter_manifest"], metadata._reader_bucket_url(chapter))
|
| 79 |
+
self.assertEqual(fields["pdf_document"], metadata._reader_bucket_url(pdf))
|
| 80 |
+
for bucket, expected in (("vomebook/pdf-pages-v2", "/api/reader-bucket-resource?path=" + chapter),
|
| 81 |
+
("unknown", None)):
|
| 82 |
+
with self.subTest(bucket=bucket):
|
| 83 |
+
fields = metadata.reader_asset_fields({**asset, "cb": bucket}, "chm")
|
| 84 |
+
self.assertEqual(fields.get("chapter_manifest"), expected)
|
| 85 |
+
for filename, expected in (("book.AZW3", "azw3"), ("document.MOBI", "mobi"), ("unknown.bin", "fb2")):
|
| 86 |
+
self.assertEqual(metadata.reader_extension_for_asset({"m": "e", "p": "dir/" + filename}, ".FB2"), expected)
|
| 87 |
+
|
| 88 |
+
def test_payload_enrichment_is_copy_and_only_matches_exact_key(self):
|
| 89 |
+
first = {"Repo": "VoiceOfML/Test", "File": "book", "Extension": "pdf", "Folder": []}
|
| 90 |
+
second = {"Repo": "VoiceOfML/Test", "File": "file", "Extension": "txt", "Folder": []}
|
| 91 |
+
payload = {"results": [first, second], "total": 2}
|
| 92 |
+
asset = {"s": 2, "m": "e", "p": "native/book.epub"}
|
| 93 |
+
assets = {"f": {metadata.build_reader_asset_key(first): asset}}
|
| 94 |
+
original = copy.deepcopy((payload, assets))
|
| 95 |
+
result = metadata.add_reader_assets(payload, assets)
|
| 96 |
+
self.assertEqual((payload, assets), original)
|
| 97 |
+
self.assertEqual(result["results"][0]["ReaderExtension"], "epub")
|
| 98 |
+
self.assertEqual(result["results"][0]["DownloadLink"], metadata.build_record_link(first))
|
| 99 |
+
self.assertNotIn("ReaderLink", result["results"][1])
|
| 100 |
+
with patch.object(metadata, "quote", wraps=metadata.quote) as quote_url:
|
| 101 |
+
metadata.enrich_browser_entry("VoiceOfML/Test", "", {"files": [{"name": "a", "ext": "pdf"}]})
|
| 102 |
+
self.assertEqual(quote_url.call_count, 1)
|
| 103 |
+
|
| 104 |
+
def test_all_direct_reader_modes_preserve_source_and_download(self):
|
| 105 |
+
source = "https://huggingface.co/datasets/VoiceOfML/Test/resolve/main/custom"
|
| 106 |
+
for extension, expected in (("pdf", "pdf"), ("djvu", "djvu"), ("vcf", "text"), ("ini", "text"),
|
| 107 |
+
("tif", "image"), ("tiff", "image")):
|
| 108 |
+
for raw in (extension, "." + extension.upper()):
|
| 109 |
+
record = {"Repo": "VoiceOfML/Test", "File": "book", "Extension": raw, "Folder": [], "Link": source}
|
| 110 |
+
with self.subTest(extension=raw):
|
| 111 |
+
result = metadata.enrich_reader_record(record, {})
|
| 112 |
+
self.assertEqual(result["ReaderLink"], source)
|
| 113 |
+
self.assertEqual(result["ReaderExtension"], expected)
|
| 114 |
+
self.assertEqual(result["DownloadLink"], source)
|
| 115 |
+
self.assertEqual(record["Link"], source)
|
| 116 |
+
unsupported = {"Repo": "VoiceOfML/Test", "File": "book", "Extension": "zip", "Folder": []}
|
| 117 |
+
self.assertEqual(metadata.enrich_reader_record(unsupported, {}), unsupported)
|
| 118 |
+
|
| 119 |
+
def test_every_native_asset_mode_projects_its_exact_format(self):
|
| 120 |
+
for mode, expected in metadata.READER_ASSET_MODES.items():
|
| 121 |
+
asset = {"s": 2, "m": mode, "p": "objects/source/document.bin"}
|
| 122 |
+
with self.subTest(mode=mode):
|
| 123 |
+
fields = metadata.reader_asset_fields(asset, "other")
|
| 124 |
+
self.assertEqual(fields["extension"], expected)
|
| 125 |
+
self.assertEqual(fields["url"], "https://voiceofml-search.hf.space/api/reader-bucket-resource?path=objects/source/document.bin")
|
| 126 |
+
for mode, path, expected in (("i", "x/page-manifest.json", "image-pages"),
|
| 127 |
+
("i", "x/reading-manifest.json", "image-pages"),
|
| 128 |
+
("p", "x/page-manifest.json", "pdf-pages"),
|
| 129 |
+
("p", "x/reading-manifest.json", "pdf-pages")):
|
| 130 |
+
with self.subTest(mode=mode, path=path):
|
| 131 |
+
self.assertEqual(metadata.reader_extension_for_asset({"m": mode, "p": path}), expected)
|
| 132 |
+
for ext in metadata.READER_NATIVE_FOLIATE_EXTENSIONS:
|
| 133 |
+
with self.subTest(ext=ext):
|
| 134 |
+
self.assertEqual(metadata.reader_extension_for_asset({"m": "e", "p": "x/unknown"}, "." + ext.upper()), ext)
|
| 135 |
+
self.assertEqual(metadata.reader_extension_for_asset({"m": "e", "p": "x/document." + ext.upper()}), ext)
|
| 136 |
+
self.assertEqual(metadata.reader_extension_for_asset({"m": "e", "p": "x/unknown"}, "zip"), "epub")
|
| 137 |
+
self.assertEqual(metadata.reader_extension_for_asset({"m": None}), "")
|
| 138 |
+
self.assertEqual(metadata.reader_asset_url({}), "https://voiceofml-search.hf.space/api/reader-bucket-resource?path=")
|
| 139 |
+
|
| 140 |
+
def test_resolved_record_preserves_all_metadata_and_title_fallbacks(self):
|
| 141 |
+
source = "https://example.invalid/direct"
|
| 142 |
+
record = {"Repo": "VoiceOfML/Test", "File": "book.PDF", "Extension": "PDF", "Folder": ["docs", "子"], "Link": source}
|
| 143 |
+
self.assertEqual(metadata.reader_resolved_record(record, {}), {
|
| 144 |
+
"url": source, "download": source, "extension": "pdf", "original_extension": "pdf",
|
| 145 |
+
"title": "book.PDF", "filename": "book.PDF.PDF", "path": "Test/docs/子", "folder": "docs/子", "repo": "Test",
|
| 146 |
+
})
|
| 147 |
+
for record, title in (({"Name": "Fallback", "Folder": []}, "Fallback"), ({"Folder": []}, "在线阅读")):
|
| 148 |
+
with self.subTest(title=title):
|
| 149 |
+
resolved = metadata.reader_resolved_record(record, {})
|
| 150 |
+
self.assertEqual(resolved["title"], title)
|
| 151 |
+
self.assertEqual(resolved["repo"], "")
|
| 152 |
+
self.assertEqual(resolved["folder"], "")
|
| 153 |
+
self.assertEqual(resolved["original_extension"], "")
|
| 154 |
+
|
| 155 |
+
def test_default_inputs_and_ocr_only_do_not_invent_source_data(self):
|
| 156 |
+
self.assertEqual(metadata.build_reader_asset_key({"File": "book"}), "\0book")
|
| 157 |
+
self.assertEqual(metadata.build_record_link({"File": "book"}), "https://huggingface.co/datasets//resolve/main/book")
|
| 158 |
+
self.assertEqual(metadata.build_record_path_url({"File": "book"}), "https://huggingface.co/datasets//blob/main/book")
|
| 159 |
+
self.assertEqual(metadata.enrich_browser_entry("R", "docs", {}), {"current_path": "docs", "folders": [], "files": []})
|
| 160 |
+
self.assertEqual(metadata.enrich_browser_entry("R", "", {"folders": [{}]})["folders"], [{"path": ""}])
|
| 161 |
+
record = {"Repo": "VoiceOfML/Test", "File": "book", "Extension": "txt", "Folder": []}
|
| 162 |
+
ocr = "derived/test/" + "a" * 32 + "/ocr-manifest.json"
|
| 163 |
+
asset = {"s": 3, "o": ocr, "ob": "vomebook/pdf-pages-v2", "om": "accepted"}
|
| 164 |
+
result = metadata.enrich_reader_record(record, asset)
|
| 165 |
+
self.assertEqual(result["ReaderOcrManifest"], metadata._reader_bucket_url(ocr))
|
| 166 |
+
self.assertEqual(result["ReaderOcrMode"], "accepted")
|
| 167 |
+
self.assertEqual(result["DownloadLink"], metadata.build_record_link(record))
|
| 168 |
+
self.assertNotIn("ReaderLink", result)
|
| 169 |
+
self.assertEqual(metadata.add_reader_assets({}, {}), {"results": []})
|
| 170 |
+
|
| 171 |
+
def test_text_path_uses_last_component_not_dotted_folders(self):
|
| 172 |
+
record = {"File": "README", "Extension": "", "Folder": ["a.b", "c"]}
|
| 173 |
+
self.assertEqual(metadata.build_txt_relative_path(record), "a.b/c/README.txt")
|
| 174 |
+
record["Folder"] = ["a", "b.c"]
|
| 175 |
+
self.assertEqual(metadata.build_txt_relative_path(record), "a/b.c/README.txt")
|
| 176 |
+
|
| 177 |
+
def test_direct_source_fallback_for_djvu_and_epub_extension(self):
|
| 178 |
+
record = {"Repo": "VoiceOfML/Test", "File": "book", "Extension": "djvu", "Folder": ["docs"]}
|
| 179 |
+
asset = {"s": 2, "m": "p", "p": "derived/test/page-manifest.json"}
|
| 180 |
+
result = metadata.enrich_reader_record(record, asset)
|
| 181 |
+
self.assertEqual(result["ReaderLink"], metadata.build_record_link(record))
|
| 182 |
+
self.assertEqual(result["ReaderExtension"], "djvu")
|
| 183 |
+
self.assertEqual(result["DownloadLink"], metadata.build_record_link(record))
|
| 184 |
+
epub_record = {"Repo": "VoiceOfML/Test", "File": "book", "Extension": "fb2", "Folder": []}
|
| 185 |
+
epub_asset = {"s": 2, "m": "e", "p": "native/book.unknown"}
|
| 186 |
+
self.assertEqual(metadata.enrich_reader_record(epub_record, epub_asset)["ReaderExtension"], "fb2")
|
| 187 |
+
|
| 188 |
+
def test_reader_metadata_carries_companion_fields_without_changing_download(self):
|
| 189 |
+
source = "https://huggingface.co/datasets/VoiceOfML/Test/resolve/main/book.pdf"
|
| 190 |
+
chapter = "chapters/ebook/chm/" + "a" * 64 + "/" + "b" * 16 + "/chapter-manifest.json"
|
| 191 |
+
ocr = "derived/test/" + "a" * 32 + "/ocr-manifest.json"
|
| 192 |
+
pdf = "derived/test/" + "a" * 32 + "/document.pdf"
|
| 193 |
+
record = {"Repo": "VoiceOfML/Test", "File": "book", "Extension": "pdf", "Folder": [], "Link": source}
|
| 194 |
+
asset = {"s": 2, "m": "e", "p": "native/book.epub", "c": chapter, "cb": "vomebook/reader-assets-v2",
|
| 195 |
+
"pd": pdf, "pdb": "vomebook/reader-assets-v2", "o": ocr,
|
| 196 |
+
"ob": "vomebook/pdf-pages-v2", "om": "accepted"}
|
| 197 |
+
result = metadata.enrich_reader_record(record, asset)
|
| 198 |
+
self.assertEqual(result["ReaderChapterManifest"], metadata._reader_bucket_url(chapter))
|
| 199 |
+
self.assertEqual(result["ReaderPdfDocument"], metadata._reader_bucket_url(pdf))
|
| 200 |
+
self.assertEqual(result["ReaderOcrManifest"], metadata._reader_bucket_url(ocr))
|
| 201 |
+
self.assertEqual(result["ReaderOcrMode"], "accepted")
|
| 202 |
+
self.assertEqual(result["ReaderExtension"], "epub-chapters")
|
| 203 |
+
self.assertEqual(result["DownloadLink"], source)
|
| 204 |
+
|
| 205 |
+
def test_sidecars_with_missing_or_untrusted_companions_do_not_replace_original(self):
|
| 206 |
+
ocr = "derived/test/" + "a" * 32 + "/ocr-manifest.json"
|
| 207 |
+
page = "derived/test/" + "a" * 32 + "/page-manifest.json"
|
| 208 |
+
pdf = "derived/test/" + "a" * 32 + "/document.pdf"
|
| 209 |
+
for asset in ({"s": 2, "m": "p", "p": page, "pd": pdf, "pdb": "unknown"},
|
| 210 |
+
{"s": 2, "m": "p", "p": page, "pdb": "vomebook/pdf-pages-v2"},
|
| 211 |
+
{"s": 4, "m": "p", "p": pdf}, {"s": 2, "m": "unknown", "p": pdf},
|
| 212 |
+
{"s": 2, "m": "p"}, {"s": 3, "m": "e", "p": "native/book.epub"}):
|
| 213 |
+
with self.subTest(asset=asset):
|
| 214 |
+
self.assertEqual(metadata.reader_asset_fields(asset, "pdf"), {})
|
| 215 |
+
for asset in ({"s": 3, "o": ocr, "b": "unknown"}, {"s": 3, "o": "wrong", "ob": "vomebook/pdf-pages-v2"}):
|
| 216 |
+
with self.subTest(asset=asset):
|
| 217 |
+
self.assertEqual(metadata.reader_asset_fields(asset, "pdf"), {})
|
| 218 |
+
chapter_asset = {"s": 2, "m": "p", "p": page, "c": "chapters/test", "cb": "vomebook/reader-assets-v2"}
|
| 219 |
+
fields = metadata.reader_asset_fields(chapter_asset, "pdf")
|
| 220 |
+
self.assertEqual(fields["url"], metadata.reader_asset_url(chapter_asset))
|
| 221 |
+
self.assertEqual(fields["extension"], "epub-chapters")
|
| 222 |
+
self.assertEqual(fields["chapter_manifest"], metadata._reader_bucket_url("chapters/test"))
|
| 223 |
+
self.assertEqual(metadata.reader_extension_for_asset({"m": "unknown"}), "")
|
| 224 |
+
self.assertEqual(metadata.reader_extension_for_asset({"m": []}), "")
|
| 225 |
+
|
| 226 |
+
def test_explicit_mapping_defaults_do_not_invoke_custom_fallbacks(self):
|
| 227 |
+
class FallbackDict(dict):
|
| 228 |
+
def get(self, key, default=None):
|
| 229 |
+
if key not in self and default is None:
|
| 230 |
+
return {"Extension": "pdf", "Folder": ["legacy"], "name": "legacy", "ext": "pdf"}.get(key)
|
| 231 |
+
return super().get(key, default)
|
| 232 |
+
|
| 233 |
+
self.assertEqual(metadata.build_record_relative_path(FallbackDict({"File": "book"})), "book")
|
| 234 |
+
self.assertEqual(metadata.build_browser_file_name(FallbackDict()), "file")
|
| 235 |
+
self.assertEqual(metadata.build_browser_file_name(FallbackDict({"name": "book"})), "book")
|
| 236 |
+
with patch.object(metadata, "reader_asset_fields", return_value={}) as fields:
|
| 237 |
+
metadata.enrich_reader_record(FallbackDict({"File": "book"}), {})
|
| 238 |
+
fields.assert_called_once_with({}, "")
|
| 239 |
+
|
| 240 |
+
def test_missing_asset_map_and_missing_folder_defaults(self):
|
| 241 |
+
record = {"Repo": "VoiceOfML/Test", "File": "book", "Extension": "pdf"}
|
| 242 |
+
self.assertEqual(metadata.reader_resolved_record(record, {})["folder"], "")
|
| 243 |
+
enriched = metadata.add_reader_assets({"results": [record]}, {})
|
| 244 |
+
self.assertEqual(enriched["results"][0]["ReaderLink"], metadata.build_record_link(record))
|
| 245 |
+
|
| 246 |
+
def test_manifest_companions_require_both_path_and_allowed_bucket(self):
|
| 247 |
+
chapter = "chapters/ebook/chm/" + "a" * 64 + "/" + "b" * 16 + "/chapter-manifest.json"
|
| 248 |
+
asset = {"s": 2, "m": "e", "p": "native/book.epub", "c": chapter, "cb": "vomebook/reader-assets-v2"}
|
| 249 |
+
for fields in ({"pdb": "vomebook/pdf-pages-v2"}, {"pd": "derived/book.pdf", "pdb": "unknown"}):
|
| 250 |
+
with self.subTest(fields=fields):
|
| 251 |
+
self.assertNotIn("pdf_document", metadata.reader_asset_fields({**asset, **fields}, "epub"))
|
| 252 |
+
pdf = {"s": 2, "m": "p", "p": "derived/test/reading-manifest.json"}
|
| 253 |
+
self.assertNotIn("url", metadata.reader_asset_fields(pdf, ".DjVu"))
|
| 254 |
+
self.assertEqual(metadata.reader_resolved_record({"File": "x", "Extension": ".PDF", "Folder": []}, {})["extension"], "pdf")
|
| 255 |
+
|
| 256 |
+
def test_empty_root_path_does_not_gain_a_leading_slash(self):
|
| 257 |
+
entry = {"files": [{"name": "book", "ext": "pdf"}]}
|
| 258 |
+
result = metadata.enrich_browser_entry("VoiceOfML/Test", "", entry)
|
| 259 |
+
self.assertEqual(result["files"][0]["link"], "https://huggingface.co/datasets/VoiceOfML/Test/resolve/main/book.pdf")
|
| 260 |
+
self.assertEqual(result["files"][0]["path"], "https://huggingface.co/datasets/VoiceOfML/Test/blob/main/book.pdf")
|
| 261 |
+
|
| 262 |
+
def test_bucket_urls_are_exact_not_merely_consistent_with_each_other(self):
|
| 263 |
+
path = "derived/test/书 #/ocr-manifest.json"
|
| 264 |
+
expected = "https://voiceofml-search.hf.space/api/reader-bucket-resource?path=derived/test/%E4%B9%A6%20%23/ocr-manifest.json"
|
| 265 |
+
self.assertEqual(metadata._reader_bucket_url(path), expected)
|
| 266 |
+
fields = metadata.reader_asset_fields({"s": 3, "o": path, "ob": "vomebook/pdf-pages-v2"}, "pdf")
|
| 267 |
+
self.assertEqual(fields, {"ocr_manifest": expected, "ocr_mode": ""})
|
| 268 |
+
|
| 269 |
+
def test_missing_folder_does_not_coerce_malformed_source_filenames(self):
|
| 270 |
+
# Compact-corpus decoding passes File values through without a type
|
| 271 |
+
# check. Preserve the current root-path behavior for malformed data;
|
| 272 |
+
# joining an absent folder would turn these into a TypeError instead.
|
| 273 |
+
for value in (None, 0, False):
|
| 274 |
+
with self.subTest(filename=value):
|
| 275 |
+
self.assertIs(metadata.build_record_relative_path({"File": value, "Extension": ""}), value)
|
| 276 |
+
|
| 277 |
+
def test_unrelated_fields_do_not_override_canonical_status(self):
|
| 278 |
+
base = {"s": 2, "m": "t", "p": "documents/text/source/document.txt"}
|
| 279 |
+
expected = {"url": "https://voiceofml-search.hf.space/api/reader-bucket-resource?path=documents/text/source/document.txt",
|
| 280 |
+
"extension": "txt"}
|
| 281 |
+
# Internal callers receive sanitized dicts, but preserve the pure
|
| 282 |
+
# helper's existing behavior for other dictionary inputs as well.
|
| 283 |
+
for key in (None, "XXsXX", "S"):
|
| 284 |
+
with self.subTest(key=key):
|
| 285 |
+
self.assertEqual(metadata.reader_asset_fields({**base, key: 3}, "txt"), expected)
|
| 286 |
+
|
| 287 |
+
def test_malformed_djvu_suffixes_are_not_silently_normalized(self):
|
| 288 |
+
asset = {"s": 2, "m": "p", "p": "derived/test/page-manifest.json",
|
| 289 |
+
"pd": "derived/test/document.pdf", "pdb": "vomebook/pdf-pages-v2"}
|
| 290 |
+
expected = {"url": "https://voiceofml-search.hf.space/api/reader-bucket-resource?path=derived/test/document.pdf",
|
| 291 |
+
"extension": "pdf"}
|
| 292 |
+
# The corpus decoder preserves the raw extension, so a trailing dot or
|
| 293 |
+
# whitespace is not interchangeable with a real DjVu source suffix.
|
| 294 |
+
for extension in ("djvu.", " djvu"):
|
| 295 |
+
with self.subTest(extension=extension):
|
| 296 |
+
self.assertEqual(metadata.reader_asset_fields(asset, extension), expected)
|
tests/test_search_execution.py
CHANGED
|
@@ -6,6 +6,39 @@ from backend.search_execution import SearchBusy, SearchExecutor
|
|
| 6 |
|
| 7 |
|
| 8 |
class SearchExecutionTests(unittest.IsolatedAsyncioTestCase):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
async def test_completed_job_registers_a_callable_cleanup(self):
|
| 10 |
original = asyncio.create_task
|
| 11 |
noncallable = []
|
|
|
|
| 6 |
|
| 7 |
|
| 8 |
class SearchExecutionTests(unittest.IsolatedAsyncioTestCase):
|
| 9 |
+
async def asyncSetUp(self):
|
| 10 |
+
# Invalid completion callbacks can stop asyncio's Task scheduling before
|
| 11 |
+
# shielded requesters are resumed. Record them without registering the
|
| 12 |
+
# bad callback so the owning test fails instead of hanging indefinitely.
|
| 13 |
+
self._recorded_noncallable = []
|
| 14 |
+
self._loop_errors = []
|
| 15 |
+
loop = asyncio.get_running_loop()
|
| 16 |
+
previous_handler = loop.get_exception_handler()
|
| 17 |
+
loop.set_exception_handler(lambda _loop, context: self._loop_errors.append(context))
|
| 18 |
+
self.addCleanup(loop.set_exception_handler, previous_handler)
|
| 19 |
+
original = asyncio.create_task
|
| 20 |
+
|
| 21 |
+
def observed(coroutine, *args, **kwargs):
|
| 22 |
+
task = original(coroutine, *args, **kwargs)
|
| 23 |
+
add_done_callback = task.add_done_callback
|
| 24 |
+
|
| 25 |
+
def checked(callback, *, context=None):
|
| 26 |
+
if not callable(callback):
|
| 27 |
+
self._recorded_noncallable.append(callback)
|
| 28 |
+
return
|
| 29 |
+
add_done_callback(callback, context=context)
|
| 30 |
+
|
| 31 |
+
task.add_done_callback = checked
|
| 32 |
+
return task
|
| 33 |
+
|
| 34 |
+
create_task_patch = patch('backend.search_execution.asyncio.create_task', side_effect=observed)
|
| 35 |
+
create_task_patch.start()
|
| 36 |
+
self.addCleanup(create_task_patch.stop)
|
| 37 |
+
|
| 38 |
+
async def asyncTearDown(self):
|
| 39 |
+
self.assertEqual(self._recorded_noncallable, [], 'all owned completion callbacks must be callable')
|
| 40 |
+
self.assertEqual(self._loop_errors, [], 'owned callbacks must not raise event-loop errors')
|
| 41 |
+
|
| 42 |
async def test_completed_job_registers_a_callable_cleanup(self):
|
| 43 |
original = asyncio.create_task
|
| 44 |
noncallable = []
|