| 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 tempfile |
| 16 | import shutil |
| 17 | import unittest |
| 18 | from colab_cli.history import HistoryLogger |
| 19 | |
| 20 | |
| 21 | class TestHistory(unittest.TestCase): |
| 22 | def setUp(self): |
| 23 | self.test_dir = tempfile.mkdtemp() |
| 24 | self.logger = HistoryLogger(log_dir=self.test_dir) |
| 25 | |
| 26 | def tearDown(self): |
| 27 | shutil.rmtree(self.test_dir) |
| 28 | |
| 29 | def test_log_and_get_history(self): |
| 30 | self.logger.log_event("test-session", "session_created", {"variant": "DEFAULT"}) |
| 31 | self.logger.log_event( |
| 32 | "test-session", "execution", {"code": "print(1)", "outputs": []} |
| 33 | ) |
| 34 | |
| 35 | history = self.logger.get_history("test-session") |
| 36 | self.assertEqual(len(history), 2) |
| 37 | self.assertEqual(history[0]["event_type"], "session_created") |
| 38 | self.assertEqual(history[1]["event_type"], "execution") |
| 39 | self.assertEqual(history[1]["code"], "print(1)") |
| 40 | |
| 41 | def test_list_sessions(self): |
| 42 | self.logger.log_event("s1", "event", {}) |
| 43 | self.logger.log_event("s2", "event", {}) |
| 44 | |
| 45 | sessions = self.logger.list_sessions() |
| 46 | self.assertIn("s1", sessions) |
| 47 | self.assertIn("s2", sessions) |
| 48 | self.assertEqual(len(sessions), 2) |
| 49 | |
| 50 | |
| 51 | if __name__ == "__main__": |
| 52 | unittest.main() |