| 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 sys |
| 16 | import pytest |
| 17 | from unittest.mock import patch |
| 18 | from colab_cli.cli import main |
| 19 | |
| 20 | |
| 21 | def test_cli_log_list(): |
| 22 | with patch.object(sys, "argv", ["colab", "log"]): |
| 23 | with patch("colab_cli.commands.utility.state") as mock_state: |
| 24 | mock_state.history.list_sessions.return_value = ["s1"] |
| 25 | |
| 26 | with patch("colab_cli.commands.utility.typer.echo") as mock_print: |
| 27 | with pytest.raises(SystemExit) as exitinfo: |
| 28 | main() |
| 29 | assert exitinfo.value.code == 0 |
| 30 | mock_print.assert_any_call(" s1") |
| 31 | |
| 32 | |
| 33 | def test_cli_log_show(): |
| 34 | with patch.object(sys, "argv", ["colab", "log", "-s", "s1"]): |
| 35 | with patch("colab_cli.commands.utility.state") as mock_state: |
| 36 | mock_state.history.get_history.return_value = [ |
| 37 | { |
| 38 | "timestamp": "2026-03-23T12:00:00.000000+00:00", |
| 39 | "event_type": "execution", |
| 40 | "code": "print(1)", |
| 41 | } |
| 42 | ] |
| 43 | |
| 44 | with patch("colab_cli.commands.utility.typer.echo") as mock_print: |
| 45 | with pytest.raises(SystemExit) as exitinfo: |
| 46 | main() |
| 47 | assert exitinfo.value.code == 0 |
| 48 | # Check for EXEC: print(1) |
| 49 | found = any( |
| 50 | "EXEC: print(1)" in str(call) for call in mock_print.call_args_list |
| 51 | ) |
| 52 | assert found |
| 53 | |
| 54 | |
| 55 | def test_cli_log_show_filter(): |
| 56 | with patch.object( |
| 57 | sys, "argv", ["colab", "log", "-s", "s1", "-t", "file_operation"] |
| 58 | ): |
| 59 | with patch("colab_cli.commands.utility.state") as mock_state: |
| 60 | mock_state.history.get_history.return_value = [ |
| 61 | { |
| 62 | "timestamp": "2026-03-23T12:00:00.000000+00:00", |
| 63 | "event_type": "execution", |
| 64 | "code": "print(1)", |
| 65 | }, |
| 66 | { |
| 67 | "timestamp": "2026-03-23T12:01:00.000000+00:00", |
| 68 | "event_type": "file_operation", |
| 69 | "op": "ls", |
| 70 | "path": "content", |
| 71 | }, |
| 72 | ] |
| 73 | |
| 74 | with patch("colab_cli.commands.utility.typer.echo") as mock_print: |
| 75 | with pytest.raises(SystemExit) as exitinfo: |
| 76 | main() |
| 77 | assert exitinfo.value.code == 0 |
| 78 | # Should see FILE: ls content but NOT EXEC: print(1) |
| 79 | printed = [str(call) for call in mock_print.call_args_list] |
| 80 | assert any("FILE: ls" in p for p in printed) |
| 81 | assert not any("EXEC: print(1)" in p for p in printed) |
| 82 | |
| 83 | |
| 84 | def test_cli_log_export(): |
| 85 | with patch.object(sys, "argv", ["colab", "log", "-s", "s1", "-o", "test.ipynb"]): |
| 86 | with patch("colab_cli.commands.utility.state") as mock_state: |
| 87 | with patch("colab_cli.converter.export_history") as mock_export: |
| 88 | mock_state.history.get_history.return_value = [ |
| 89 | {"event_type": "execution"} |
| 90 | ] |
| 91 | |
| 92 | with pytest.raises(SystemExit) as exitinfo: |
| 93 | main() |
| 94 | assert exitinfo.value.code == 0 |
| 95 | mock_export.assert_called_once() |