import unittest
from unittest.mock import patch
from bot.lib import notify


class TestNotify(unittest.TestCase):
    def test_uses_terminal_notifier_if_available(self):
        with patch("shutil.which", return_value="/usr/local/bin/terminal-notifier"), \
             patch("subprocess.run") as run:
            notify.notify(title="t", subtitle="s", body="b", open_path="/tmp/x.md")
            args = run.call_args.args[0]
            self.assertEqual(args[0], "/usr/local/bin/terminal-notifier")
            self.assertIn("-open", args)

    def test_falls_back_to_osascript(self):
        with patch("shutil.which", return_value=None), \
             patch("subprocess.run") as run:
            notify.notify(title="t", subtitle="s", body="b")
            args = run.call_args.args[0]
            self.assertEqual(args[0], "osascript")
            joined = " ".join(args)
            self.assertIn("display notification", joined)

    def test_escapes_double_quotes_in_osascript_body(self):
        with patch("shutil.which", return_value=None), \
             patch("subprocess.run") as run:
            notify.notify(title='a"b', subtitle="s", body='c"d')
            args = run.call_args.args[0]
            script = next(a for a in args if "display notification" in a)
            # Ensure no unescaped double quotes inside the strings.
            self.assertNotIn('"a"b"', script)
            self.assertNotIn('"c"d"', script)


if __name__ == "__main__":
    unittest.main()
