| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- #!/usr/bin/env python3
- import importlib.util
- import json
- import pathlib
- import unittest
- _SCRIPT_PATH = pathlib.Path(__file__).with_name("chrome-tab-switcher.py")
- _spec = importlib.util.spec_from_file_location("chrome_tab_switcher", _SCRIPT_PATH)
- mod = importlib.util.module_from_spec(_spec)
- assert _spec and _spec.loader
- _spec.loader.exec_module(mod)
- class ChromeTabSwitcherTest(unittest.TestCase):
- def test_parse_tabs_dict_payload(self) -> None:
- payload = {
- "windows": [
- {
- "activeTabIndex": 1,
- "tabs": [
- {"title": "Home", "url": "https://example.com"},
- {"title": "Docs", "url": "https://docs.example.com"},
- ],
- }
- ]
- }
- entries = mod.parse_tabs(json.dumps(payload))
- self.assertEqual(2, len(entries))
- self.assertEqual("W0T0", entries[0].token)
- self.assertFalse(entries[0].active)
- self.assertEqual("W0T1", entries[1].token)
- self.assertTrue(entries[1].active)
- self.assertEqual("Docs", entries[0].window_active_title)
- def test_parse_tabs_list_payload_with_selected(self) -> None:
- payload = [
- {
- "selected": 1,
- "tabs": [
- {"name": "First", "uri": "https://a.example"},
- {"name": "Second", "uri": "https://b.example"},
- ],
- }
- ]
- entries = mod.parse_tabs(json.dumps(payload))
- self.assertEqual(2, len(entries))
- self.assertTrue(entries[0].active)
- self.assertFalse(entries[1].active)
- def test_parse_menu_selection(self) -> None:
- token = mod.parse_menu_selection("S1W3T8\t[session w4 t9] Example")
- self.assertEqual("S1W3T8", token)
- def test_build_switch_commands(self) -> None:
- cmds = mod.build_switch_commands("0x01a00002", 3)
- self.assertEqual(["wmctrl", "-ia", "0x01a00002"], cmds[0])
- self.assertEqual(["xdotool", "key", "--clearmodifiers", "ctrl+1"], cmds[1])
- self.assertEqual(["xdotool", "key", "--clearmodifiers", "--repeat", "3", "ctrl+Tab"], cmds[2])
- def test_discover_active_session_dirs(self) -> None:
- ps_output = "\n".join(
- [
- '/opt/google/chrome/chrome --type=browser --user-data-dir="/tmp/chrome-a"',
- '/usr/bin/chromium --type=browser --user-data-dir=/tmp/chrome-b',
- '/opt/google/chrome/chrome --type=gpu-process',
- ]
- )
- dirs = mod.discover_active_session_dirs(ps_output, "/home/test")
- self.assertEqual(["/tmp/chrome-a", "/tmp/chrome-b"], dirs)
- def test_resolve_window_id_prefers_title_match(self) -> None:
- entry = mod.TabEntry(
- token="S0W1T2",
- window_index=1,
- tab_index=2,
- title="Target Tab",
- url="https://example.com",
- active=False,
- session_name="Profile 2",
- window_active_title="Sprint Board",
- )
- windows = [
- mod.ChromeWindow("0x100", "google-chrome.Google-chrome", "Inbox - Google Chrome"),
- mod.ChromeWindow("0x200", "google-chrome.Google-chrome", "Sprint Board - Google Chrome"),
- ]
- self.assertEqual("0x200", mod.resolve_window_id(entry, windows))
- def test_resolve_window_id_falls_back_to_index(self) -> None:
- entry = mod.TabEntry(
- token="S0W1T2",
- window_index=1,
- tab_index=2,
- title="Target Tab",
- url="https://example.com",
- active=False,
- window_active_title="",
- )
- windows = [
- mod.ChromeWindow("0x100", "google-chrome.Google-chrome", "Inbox - Google Chrome"),
- mod.ChromeWindow("0x200", "google-chrome.Google-chrome", "Sprint Board - Google Chrome"),
- ]
- self.assertEqual("0x200", mod.resolve_window_id(entry, windows))
- if __name__ == "__main__":
- unittest.main()
|