@@ -130,6 +130,73 @@ def test_do_execute_handles_error_result(self):
130130 finally :
131131 kernel_module ._HAS_IPYKERNEL = original
132132
133+ def test_do_execute_suppresses_pure_command_echo (self ):
134+ """A cell with no textual output (e.g. a graph) must not stream the
135+ echoed source back — that read as a useless repeat of the code."""
136+ from stata_code .kernel import kernel as kernel_module
137+
138+ original = kernel_module ._HAS_IPYKERNEL
139+ kernel_module ._HAS_IPYKERNEL = True
140+ echo_only = LogInfo (
141+ head = '\n . * 3) fit\n . twoway (scatter price mpg) (lfit price mpg)\n \n . \n ' ,
142+ tail = "" ,
143+ lines_total = 5 ,
144+ bytes_total = 60 ,
145+ )
146+ mock_result = _make_run_result (ok = True , log = echo_only )
147+ try :
148+ from stata_code .kernel import StataKernel
149+
150+ kb = StataKernel ()
151+ streamed : list [tuple [str , str ]] = []
152+ with patch .object (
153+ kb , "_stream" , side_effect = lambda n , t : streamed .append ((n , t ))
154+ ):
155+ with patch (
156+ "stata_code.kernel.kernel.execute" , return_value = mock_result
157+ ):
158+ reply = kb .do_execute (
159+ "* 3) fit\n twoway (scatter price mpg) (lfit price mpg)" ,
160+ silent = False ,
161+ )
162+ assert reply ["status" ] == "ok"
163+ assert not any (name == "stdout" for name , _ in streamed )
164+ finally :
165+ kernel_module ._HAS_IPYKERNEL = original
166+
167+ def test_do_execute_streams_output_without_echo (self ):
168+ """Genuine command output is streamed, but the leading `. cmd` echo is
169+ stripped from it."""
170+ from stata_code .kernel import kernel as kernel_module
171+
172+ original = kernel_module ._HAS_IPYKERNEL
173+ kernel_module ._HAS_IPYKERNEL = True
174+ mixed = LogInfo (
175+ head = "\n . summarize price\n \n price | 74\n \n . \n " ,
176+ tail = "" ,
177+ lines_total = 6 ,
178+ bytes_total = 40 ,
179+ )
180+ mock_result = _make_run_result (ok = True , log = mixed )
181+ try :
182+ from stata_code .kernel import StataKernel
183+
184+ kb = StataKernel ()
185+ streamed : list [tuple [str , str ]] = []
186+ with patch .object (
187+ kb , "_stream" , side_effect = lambda n , t : streamed .append ((n , t ))
188+ ):
189+ with patch (
190+ "stata_code.kernel.kernel.execute" , return_value = mock_result
191+ ):
192+ kb .do_execute ("summarize price" , silent = False )
193+ stdout = [t for n , t in streamed if n == "stdout" ]
194+ assert len (stdout ) == 1
195+ assert ". summarize price" not in stdout [0 ]
196+ assert "price | 74" in stdout [0 ]
197+ finally :
198+ kernel_module ._HAS_IPYKERNEL = original
199+
133200 def test_do_complete_returns_stata_keywords (self ):
134201 """do_complete should return Stata keyword matches."""
135202 from stata_code .kernel import StataKernel
@@ -332,6 +399,52 @@ def install_kernel_spec(
332399 assert flags == [False ]
333400
334401
402+ class TestCommandEchoStripping :
403+ """Unit tests for `_strip_command_echo` (pure, no Stata required)."""
404+
405+ def test_pure_echo_graph_cell_becomes_empty (self ):
406+ from stata_code .kernel .kernel import _strip_command_echo
407+
408+ log = "\n . * 3) fit\n . twoway (scatter price mpg) (lfit price mpg)\n \n . \n "
409+ assert _strip_command_echo (log ) == ""
410+
411+ def test_wrapped_continuation_lines_stripped (self ):
412+ from stata_code .kernel .kernel import _strip_command_echo
413+
414+ # Stata wraps a long command onto a `> ` continuation line.
415+ log = (
416+ '\n . twoway scatter price mpg, title("A long title that wraps\n '
417+ '> onto another line")\n \n . \n '
418+ )
419+ assert _strip_command_echo (log ) == ""
420+
421+ def test_real_output_preserved_echo_removed (self ):
422+ from stata_code .kernel .kernel import _strip_command_echo
423+
424+ log = "\n . summarize price\n \n price | 74\n \n . \n "
425+ out = _strip_command_echo (log )
426+ assert ". summarize" not in out
427+ assert out == " price | 74"
428+
429+ def test_consecutive_blank_lines_collapsed (self ):
430+ from stata_code .kernel .kernel import _strip_command_echo
431+
432+ assert _strip_command_echo ("line1\n \n \n \n line2" ) == "line1\n \n line2"
433+
434+ def test_log_without_echo_unchanged (self ):
435+ from stata_code .kernel .kernel import _strip_command_echo
436+
437+ log = " Variable | Obs\n price | 74"
438+ assert _strip_command_echo (log ) == log
439+
440+ def test_missing_value_dot_output_not_stripped (self ):
441+ from stata_code .kernel .kernel import _strip_command_echo
442+
443+ # A bare "." (e.g. `display .`) is real output, not an echoed prompt
444+ # (which is always ". " — dot-space). It must survive.
445+ assert _strip_command_echo ("." ) == "."
446+
447+
335448# NOTE: TestStataGraphDataUri was removed in v0.2. The legacy `StataGraph`
336449# dataclass (with .to_base64() / .to_data_uri()) is gone; the v1.0 `GraphInfo`
337450# schema returns refs by default and inline base64 only when explicitly
0 commit comments