chrome-tab-switcher_test.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #!/usr/bin/env python3
  2. import importlib.util
  3. import json
  4. import pathlib
  5. import unittest
  6. _SCRIPT_PATH = pathlib.Path(__file__).with_name("chrome-tab-switcher.py")
  7. _spec = importlib.util.spec_from_file_location("chrome_tab_switcher", _SCRIPT_PATH)
  8. mod = importlib.util.module_from_spec(_spec)
  9. assert _spec and _spec.loader
  10. _spec.loader.exec_module(mod)
  11. class ChromeTabSwitcherTest(unittest.TestCase):
  12. def test_parse_tabs_dict_payload(self) -> None:
  13. payload = {
  14. "windows": [
  15. {
  16. "activeTabIndex": 1,
  17. "tabs": [
  18. {"title": "Home", "url": "https://example.com"},
  19. {"title": "Docs", "url": "https://docs.example.com"},
  20. ],
  21. }
  22. ]
  23. }
  24. entries = mod.parse_tabs(json.dumps(payload))
  25. self.assertEqual(2, len(entries))
  26. self.assertEqual("W0T0", entries[0].token)
  27. self.assertFalse(entries[0].active)
  28. self.assertEqual("W0T1", entries[1].token)
  29. self.assertTrue(entries[1].active)
  30. self.assertEqual("Docs", entries[0].window_active_title)
  31. def test_parse_tabs_list_payload_with_selected(self) -> None:
  32. payload = [
  33. {
  34. "selected": 1,
  35. "tabs": [
  36. {"name": "First", "uri": "https://a.example"},
  37. {"name": "Second", "uri": "https://b.example"},
  38. ],
  39. }
  40. ]
  41. entries = mod.parse_tabs(json.dumps(payload))
  42. self.assertEqual(2, len(entries))
  43. self.assertTrue(entries[0].active)
  44. self.assertFalse(entries[1].active)
  45. def test_parse_menu_selection(self) -> None:
  46. token = mod.parse_menu_selection("S1W3T8\t[session w4 t9] Example")
  47. self.assertEqual("S1W3T8", token)
  48. def test_build_switch_commands(self) -> None:
  49. cmds = mod.build_switch_commands("0x01a00002", 3)
  50. self.assertEqual(["wmctrl", "-ia", "0x01a00002"], cmds[0])
  51. self.assertEqual(["xdotool", "key", "--clearmodifiers", "ctrl+1"], cmds[1])
  52. self.assertEqual(["xdotool", "key", "--clearmodifiers", "--repeat", "3", "ctrl+Tab"], cmds[2])
  53. def test_discover_active_session_dirs(self) -> None:
  54. ps_output = "\n".join(
  55. [
  56. '/opt/google/chrome/chrome --type=browser --user-data-dir="/tmp/chrome-a"',
  57. '/usr/bin/chromium --type=browser --user-data-dir=/tmp/chrome-b',
  58. '/opt/google/chrome/chrome --type=gpu-process',
  59. ]
  60. )
  61. dirs = mod.discover_active_session_dirs(ps_output, "/home/test")
  62. self.assertEqual(["/tmp/chrome-a", "/tmp/chrome-b"], dirs)
  63. def test_resolve_window_id_prefers_title_match(self) -> None:
  64. entry = mod.TabEntry(
  65. token="S0W1T2",
  66. window_index=1,
  67. tab_index=2,
  68. title="Target Tab",
  69. url="https://example.com",
  70. active=False,
  71. session_name="Profile 2",
  72. window_active_title="Sprint Board",
  73. )
  74. windows = [
  75. mod.ChromeWindow("0x100", "google-chrome.Google-chrome", "Inbox - Google Chrome"),
  76. mod.ChromeWindow("0x200", "google-chrome.Google-chrome", "Sprint Board - Google Chrome"),
  77. ]
  78. self.assertEqual("0x200", mod.resolve_window_id(entry, windows))
  79. def test_resolve_window_id_falls_back_to_index(self) -> None:
  80. entry = mod.TabEntry(
  81. token="S0W1T2",
  82. window_index=1,
  83. tab_index=2,
  84. title="Target Tab",
  85. url="https://example.com",
  86. active=False,
  87. window_active_title="",
  88. )
  89. windows = [
  90. mod.ChromeWindow("0x100", "google-chrome.Google-chrome", "Inbox - Google Chrome"),
  91. mod.ChromeWindow("0x200", "google-chrome.Google-chrome", "Sprint Board - Google Chrome"),
  92. ]
  93. self.assertEqual("0x200", mod.resolve_window_id(entry, windows))
  94. if __name__ == "__main__":
  95. unittest.main()