@@ -133,3 +133,62 @@ def test_question_history_opportunistic_expire():
133133
134134 assert fresh in history ._history
135135 assert len (history ._history ) == 1
136+
137+
138+ def _make_known_answers (count : int ) -> set [r .DNSRecord ]:
139+ """Build a set of ``count`` distinct PTR records for use as known-answers."""
140+ return {
141+ r .DNSPointer (
142+ "_svc._tcp.local." ,
143+ const ._TYPE_PTR ,
144+ const ._CLASS_IN ,
145+ 10000 ,
146+ f"target{ i } ._svc._tcp.local." ,
147+ )
148+ for i in range (count )
149+ }
150+
151+
152+ def test_question_history_oversized_known_answers_dropped ():
153+ """Known-answer sets above the per-entry cap are not stored."""
154+ history = QuestionHistory ()
155+ now = r .current_time_millis ()
156+ question = r .DNSQuestion ("_svc._tcp.local." , const ._TYPE_PTR , const ._CLASS_IN )
157+
158+ oversized = _make_known_answers (const ._MAX_KNOWN_ANSWERS_PER_HISTORY_ENTRY + 1 )
159+ history .add_question_at_time (question , now , oversized )
160+
161+ assert question not in history ._history
162+
163+
164+ def test_question_history_oversized_preserves_existing_entry ():
165+ """An oversized payload must not displace a pre-existing small entry."""
166+ history = QuestionHistory ()
167+ now = r .current_time_millis ()
168+ question = r .DNSQuestion ("_svc._tcp.local." , const ._TYPE_PTR , const ._CLASS_IN )
169+
170+ small = _make_known_answers (2 )
171+ history .add_question_at_time (question , now , small )
172+ assert history .suppresses (question , now , small )
173+
174+ # An oversized follow-up must be ignored; the small entry stays and
175+ # continues to drive suppression.
176+ oversized = _make_known_answers (const ._MAX_KNOWN_ANSWERS_PER_HISTORY_ENTRY + 1 )
177+ history .add_question_at_time (question , now , oversized )
178+
179+ stored_set = history ._history [question ][1 ]
180+ assert stored_set is small
181+ assert history .suppresses (question , now , small )
182+
183+
184+ def test_question_history_at_cap_known_answers_is_stored ():
185+ """A known-answer set exactly at the per-entry cap is retained."""
186+ history = QuestionHistory ()
187+ now = r .current_time_millis ()
188+ question = r .DNSQuestion ("_svc._tcp.local." , const ._TYPE_PTR , const ._CLASS_IN )
189+
190+ at_cap = _make_known_answers (const ._MAX_KNOWN_ANSWERS_PER_HISTORY_ENTRY )
191+ history .add_question_at_time (question , now , at_cap )
192+
193+ assert question in history ._history
194+ assert history ._history [question ][1 ] is at_cap
0 commit comments