@@ -196,3 +196,76 @@ def test_ignorable_separator_view_does_not_expand_p7_or_p8(command: str) -> None
196196 )
197197 assert findings == []
198198 assert reason is None
199+
200+
201+ @pytest .mark .parametrize ("with_parent" , [False , True ], ids = ["window" , "parent" ])
202+ def test_absolute_source_mapping_does_no_per_character_work_at_construction (
203+ monkeypatch : pytest .MonkeyPatch , with_parent : bool
204+ ) -> None :
205+ class RecordingView (SecurityTextView ):
206+ def source_offset (self , derived_offset : int ) -> int :
207+ calls .append (derived_offset )
208+ return super ().source_offset (derived_offset )
209+
210+ calls : list [int ] = []
211+ view = SecurityTextView ("raw" , "x" * runner .SECURITY_VIEW_WINDOW_CHARS )
212+ parent = RecordingView ("parent" , view .text ) if with_parent else None
213+
214+ def forbidden_array (* args , ** kwargs ):
215+ pytest .fail ("Source mapping allocated an eager per-character array" )
216+
217+ monkeypatch .setattr (runner , "array" , forbidden_array )
218+ absolute = runner ._absolute_source_view (view , source_start = 123 , parent = parent )
219+ assert calls == []
220+ assert absolute .source_offset (17 ) == 140
221+ assert calls == ([17 ] if with_parent else [])
222+
223+
224+ @pytest .mark .parametrize (
225+ ("text" , "source_offsets" , "parent_offsets" , "expected" ),
226+ [
227+ ("abc" , None , None , [100 , 100 , 101 , 102 , 102 , 102 ]),
228+ ("abc" , [2 , 4 , 7 ], None , [102 , 102 , 104 , 107 , 107 , 107 ]),
229+ ("abc" , [0 , 1 , 1 ], [2 , 4 , 7 ], [102 , 102 , 104 , 104 , 104 , 104 ]),
230+ ("" , [], [2 , 4 , 7 ], [0 , 0 , 0 , 0 , 0 , 0 ]),
231+ ],
232+ ids = ["identity" , "derived" , "parent" , "empty" ],
233+ )
234+ def test_absolute_source_mapping_preserves_clamping_and_composition (
235+ text : str ,
236+ source_offsets : list [int ] | None ,
237+ parent_offsets : list [int ] | None ,
238+ expected : list [int ],
239+ ) -> None :
240+ from array import array
241+
242+ view = SecurityTextView (
243+ "derived" , text , None if source_offsets is None else array ("I" , source_offsets )
244+ )
245+ parent = (
246+ None
247+ if parent_offsets is None
248+ else SecurityTextView ("parent" , "abc" , array ("I" , parent_offsets ))
249+ )
250+ absolute = runner ._absolute_source_view (view , source_start = 100 , parent = parent )
251+ assert absolute .name == view .name
252+ assert absolute .text == view .text
253+ assert [absolute .source_offset (i ) for i in [- 3 , 0 , 1 , 2 , 3 , 99 ]] == expected
254+
255+
256+ def test_absolute_source_mapping_composes_bounded_derived_slice () -> None :
257+ text = "x" * (runner .SECURITY_VIEW_WINDOW_CHARS + 100 )
258+ second = list (runner ._bounded_view_slices (SecurityTextView ("raw" , text )))[1 ]
259+ absolute = runner ._absolute_source_view (second , source_start = 250_000 )
260+ expected_start = 250_000 + runner .SECURITY_VIEW_WINDOW_CHARS - runner ._WINDOW_OVERLAP_CHARS
261+ assert absolute .source_offset (0 ) == expected_start
262+ assert absolute .source_offset (len (second .text ) - 1 ) == 250_000 + len (text ) - 1
263+
264+
265+ def test_absolute_source_mapping_preserves_unshifted_identity_view () -> None :
266+ view = SecurityTextView ("raw" , "abc" )
267+ absolute = runner ._absolute_source_view (view )
268+ assert absolute is view
269+ assert absolute .source_offset (- 1 ) == 0
270+ assert absolute .source_offset (3 ) == 3
271+ assert absolute .source_offset (99 ) == 3
0 commit comments