Skip to content

gerodp/hermes-productivity-skills / calendar/calendar

calendar

Verified

Check your Google Calendar (read-only): today's / this week's / upcoming events as an agenda.

FileKindSize
SKILL.mdskill md6.2 KB
scripts/calendar_agenda.pyscript16.7 KB
scripts/test_calendar_agenda.pyscript8.5 KB

scripts/test_calendar_agenda.py

View on GitHub
#!/usr/bin/env python3
"""Offline unit tests for calendar_agenda — no subprocess, no network.

The builtin-running helper (`run_gws_json`) is monkeypatched with canned event
JSON shaped like google_api.py's `calendar list` output, so these tests are
hermetic and fast.

Run:  python3 test_calendar_agenda.py
"""
import argparse
import unittest
from datetime import date, datetime

import calendar_agenda as ca


# Canned events as google_api.py would print them (RFC3339 with tz, + an all-day).
RAW_EVENTS = [
    {"id": "a", "summary": "Standup",
     "start": "2026-06-29T09:00:00+02:00", "end": "2026-06-29T09:15:00+02:00",
     "location": "Zoom", "status": "confirmed"},
    {"id": "b", "summary": "Offsite (all day)",
     "start": "2026-06-30", "end": "2026-07-01", "location": ""},
    {"id": "c", "summary": "Lunch with Alex",
     "start": "2026-06-29T13:30:00+02:00", "end": "2026-06-29T14:30:00+02:00",
     "location": "Gracia"},
]


def make_args(**over):
    base = dict(period=None, days=None, next=None, start=None, end=None,
                calendar="primary", max=50, json=False,
                gws_script="google_api.py", setup_script="setup.py")
    base.update(over)
    return argparse.Namespace(**base)


class TestWindows(unittest.TestCase):
    TODAY = date(2026, 6, 29)  # Monday

    def test_default_today(self):
        self.assertEqual(ca.compute_range(self.TODAY),
                         (date(2026, 6, 29), date(2026, 6, 29)))

    def test_tomorrow(self):
        self.assertEqual(ca.compute_range(self.TODAY, period="tomorrow"),
                         (date(2026, 6, 30), date(2026, 6, 30)))

    def test_this_week_monday_start(self):
        self.assertEqual(ca.compute_range(self.TODAY, period="this-week"),
                         (date(2026, 6, 29), date(2026, 7, 5)))

    def test_next_week(self):
        self.assertEqual(ca.compute_range(self.TODAY, period="next-week"),
                         (date(2026, 7, 6), date(2026, 7, 12)))

    def test_next_days_forward(self):
        self.assertEqual(ca.compute_range(self.TODAY, nxt=3),
                         (date(2026, 6, 29), date(2026, 7, 1)))

    def test_days_forward(self):
        self.assertEqual(ca.compute_range(self.TODAY, days=2),
                         (date(2026, 6, 29), date(2026, 6, 30)))

    def test_explicit(self):
        self.assertEqual(
            ca.compute_range(self.TODAY, start="2026-07-01", end="2026-07-03"),
            (date(2026, 7, 1), date(2026, 7, 3)))

    def test_half_open(self):
        with self.assertRaises(ca.CalendarError):
            ca.compute_range(self.TODAY, start="2026-07-01")

    def test_bad_next(self):
        with self.assertRaises(ca.CalendarError):
            ca.compute_range(self.TODAY, nxt=0)


class TestIsoBounds(unittest.TestCase):
    def test_inclusive_end_day(self):
        lo, hi = ca.iso_bounds(date(2026, 6, 29), date(2026, 6, 29))
        # Same calendar day start; end is next-day midnight (whole day covered).
        self.assertTrue(lo.startswith("2026-06-29T00:00:00"))
        self.assertTrue(hi.startswith("2026-06-30T00:00:00"))
        # tz-aware (has offset or Z)
        self.assertTrue(("+" in lo[11:]) or ("-" in lo[11:]) or lo.endswith("+00:00"))


class TestNormalizeAndRender(unittest.TestCase):
    def test_normalize_sorts_and_flags(self):
        ev = ca.normalize_events(RAW_EVENTS)
        # Sorted by (day, start_local): the two 06-29 timed events before 06-30.
        self.assertEqual([e["id"] for e in ev], ["a", "c", "b"])
        a = next(e for e in ev if e["id"] == "a")
        self.assertFalse(a["all_day"])
        self.assertEqual(a["day"], "2026-06-29")
        b = next(e for e in ev if e["id"] == "b")
        self.assertTrue(b["all_day"])
        self.assertIsNone(b["start_local"])

    def test_render_agenda_groups_by_day(self):
        ev = ca.normalize_events(RAW_EVENTS)
        out = ca.render_agenda(ev, date(2026, 6, 29), date(2026, 6, 30))
        self.assertIn("Standup", out)
        self.assertIn("all-day", out)
        self.assertIn("Offsite", out)
        self.assertIn("@ Zoom", out)
        # day headers present
        self.assertIn("2026-06-29", out)
        self.assertIn("2026-06-30", out)

    def test_render_empty(self):
        self.assertIn("(no events)",
                      ca.render_agenda([], date(2026, 6, 29), date(2026, 6, 29)))

    def test_parse_all_day_vs_timed(self):
        d, t = ca._parse_event_dt("2026-06-30")
        self.assertEqual(d, date(2026, 6, 30))
        self.assertIsNone(t)
        d2, t2 = ca._parse_event_dt("2026-06-29T09:00:00+02:00")
        self.assertEqual(d2, date(2026, 6, 29))
        self.assertEqual(t2, "09:00")   # parsed in +02:00 → rendered in local tz


class TestAgendaCommand(unittest.TestCase):
    def setUp(self):
        self._orig = ca.run_gws_json
        ca.run_gws_json = lambda args, gws=None: {"data": list(RAW_EVENTS)}

    def tearDown(self):
        ca.run_gws_json = self._orig

    def test_json_output(self):
        import io
        from contextlib import redirect_stdout
        buf = io.StringIO()
        with redirect_stdout(buf):
            rc = ca.do_agenda(make_args(period="this-week", json=True))
        self.assertEqual(rc, 0)
        import json as _json
        data = _json.loads(buf.getvalue())
        self.assertEqual(data["count"], 3)
        self.assertEqual(data["events"][0]["id"], "a")

    def test_graceful_error(self):
        import io
        from contextlib import redirect_stdout
        ca.run_gws_json = lambda args, gws=None: {"error": "not authenticated"}
        with redirect_stdout(io.StringIO()):
            rc = ca.do_agenda(make_args(json=True))
        self.assertEqual(rc, 1)


class TestCheckSmoke(unittest.TestCase):
    """`check` contract, exercised hermetically.

    In the sandbox neither the Google client libs nor an OAuth token exist, so
    the configured-success path is unreachable — we only assert the failure
    contract (that's the one `check` documents for an unconfigured install). To
    keep the test independent of whatever the dev has installed locally, we point
    the gws-script override at a nonexistent path: `do_check` short-circuits on
    the missing script before any real subprocess/network runs.
    """
    def _run(self, argv):
        import io
        from contextlib import redirect_stdout, redirect_stderr
        out, err = io.StringIO(), io.StringIO()
        with redirect_stdout(out), redirect_stderr(err):
            rc = ca.main(argv)
        return rc, out.getvalue(), err.getvalue()

    # A nonexistent gws script makes do_check fail deterministically without
    # touching the network or spawning a child interpreter.
    NO_GWS = ["--gws-script", "/no/such/google_api.py"]

    def test_check_returns_nonzero_without_traceback(self):
        rc, out, err = self._run(["check"] + self.NO_GWS)
        self.assertEqual(rc, 1)
        # A leaked traceback would mention Traceback; the message must be clean.
        self.assertNotIn("Traceback", out)
        self.assertNotIn("Traceback", err)
        self.assertIn("✗", out)

    def test_check_prints_setup_steps_on_failure(self):
        # The not-ready path must guide the user, not just fail silently.
        rc, out, _ = self._run(["check"] + self.NO_GWS)
        self.assertEqual(rc, 1)
        self.assertIn("google-workspace", out.lower())

    def test_check_has_no_json_flag(self):
        # `check` is a text-only diagnostic here (like the other stdlib skills);
        # argparse rejects an unknown flag with SystemExit(2). Documents the
        # contract so nobody assumes a check --json envelope exists.
        with self.assertRaises(SystemExit) as cm:
            self._run(["check", "--json"] + self.NO_GWS)
        self.assertEqual(cm.exception.code, 2)


class TestFirstLine(unittest.TestCase):
    def test_strips_error_prefix(self):
        self.assertEqual(ca._first_line("error: boom\nx"), "boom")
        self.assertEqual(ca._first_line("\n  plain\n"), "plain")


class TestSanitizing(unittest.TestCase):
    def test_render_strips_terminal_escapes_from_invites(self):
        # Anyone can send an invite — its title/location must not be able to
        # smuggle ANSI/OSC sequences into the terminal.
        ev = [{"id": "x", "day": "2026-06-29", "start_local": "09:00",
               "end_local": "09:30", "all_day": False,
               "summary": "Stand\x1b[2Jup", "location": "Zo\x07om"}]
        out = ca.render_agenda(ev, date(2026, 6, 29), date(2026, 6, 29))
        self.assertNotIn("\x1b", out)
        self.assertNotIn("\x07", out)
        self.assertIn("up", out)


if __name__ == "__main__":
    unittest.main(verbosity=2)
slack-gif-creatoranthropics/skillsKnowledge and utilities for creating animated GIFs optimized for Slack. Provides constraints, validation tools, and animation concepts. Use when users request animated GIFs for Slack like "make me a GIF of X doing Y for Slack."Stars: 198kUpdated Claude CodeCodexVerifiedInstall
ponytailDietrichGebert/ponytailLazy senior dev mode for any coding task: the smallest change that fully solves it, and a reply a busy human understands in one read.Stars: 196kUpdated Claude CodeCodexVerifiedInstall
ponytail-debtDietrichGebert/ponytailHarvest every shortcut: comment into one debt ledger, so deferrals get tracked instead of forgotten. One-shot report.Stars: 196kUpdated Claude CodeCodexVerifiedInstall
ponytail-debtDietrichGebert/ponytailList every `shortcut:` comment (and older `ponytail:` ones) as a debt ledger. One-shot report, changes nothing. Use for "ponytail debt", "what did ponytail defer", "list the shortcuts", /ponytail-debt.Stars: 196kUpdated Claude CodeCodexVerifiedInstall