main
py 87 lines 3 KB
Raw
1 # Copyright 2026 Google LLC
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import base64
16 from unittest.mock import MagicMock, patch
17
18 import pytest
19 from rich.markdown import Markdown
20 from rich.text import Text
21
22 from colab_cli.utils import handle_image, print_kitty, render_display_data
23
24
25 @patch("colab_cli.utils.sys.stdout.isatty", return_value=True)
26 def test_print_kitty_emits_escape_sequence_on_tty(_mock_isatty, capsys):
27 print_kitty(b"fake-png-bytes")
28 captured = capsys.readouterr()
29 assert "_Ga=T,f=100;" in captured.out
30 b64 = base64.b64encode(b"fake-png-bytes").decode("ascii")
31 assert b64 in captured.out
32
33
34 @patch("colab_cli.utils.sys.stdout.isatty", return_value=False)
35 def test_print_kitty_silent_when_stdout_not_tty(_mock_isatty, capsys):
36 """Kitty graphics escape sequences are useless and visually corrupt the
37 output when stdout is redirected (e.g. `colab exec ... > log.txt`,
38 `colab exec ... | grep ...`, or any non-Kitty terminal). When stdout is
39 not a TTY, print_kitty must not emit anything.
40 """
41 print_kitty(b"fake-png-bytes")
42 captured = capsys.readouterr()
43 assert captured.out == "", (
44 f"Expected no output when stdout is not a TTY, got: {captured.out!r}"
45 )
46
47
48 @patch("colab_cli.utils.tempfile.NamedTemporaryFile")
49 @patch("colab_cli.utils.print_kitty")
50 def test_handle_image(mock_print_kitty, mock_tempfile, capsys):
51 mock_tmp = MagicMock()
52 mock_tmp.name = "/tmp/fake.png"
53 mock_tempfile.return_value = mock_tmp
54
55 handle_image(base64.b64encode(b"test").decode("ascii"), "image/png")
56
57 mock_print_kitty.assert_called_once_with(b"test")
58 mock_tmp.write.assert_called_once_with(b"test")
59 mock_tmp.close.assert_called_once()
60
61 captured = capsys.readouterr()
62 assert "/tmp/fake.png" in captured.out
63
64
65 @pytest.mark.parametrize(
66 "data, expected_markup",
67 [
68 ({"text/markdown": "**md**"}, "**md**"),
69 ({"text/html": "<b>hi</b>"}, "**hi**\n\n"),
70 ({"text/markdown": "**md**", "text/html": "<b>hi</b>"}, "**md**"),
71 ({"text/html": "<b>hi</b>", "text/plain": "plain"}, "**hi**\n\n"),
72 ],
73 )
74 def test_render_display_data_markdown(data, expected_markup):
75 result = render_display_data(data)
76 assert isinstance(result, Markdown)
77 assert result.markup == expected_markup
78
79
80 def test_render_display_data_plain():
81 result = render_display_data({"text/plain": "plain"})
82 assert isinstance(result, Text)
83 assert result.plain == "plain"
84
85
86 def test_render_display_data_none():
87 assert render_display_data({"image/png": "..."}) is None