Files
skills/tests/test_coach.py
T
Kevin Veen-BirkenbachandClaude Opus 5.5 6c63c364bd perf(skills): cut skill tokens and load hooks from cached bytecode
Hooks start their script as __main__ from cached bytecode instead of recompiling it on every call, and the installer migrates registered old hook commands to that form. A disabled plugin is written as false instead of deleted, because a deleted key in settings.local.json lets a true in settings.json win; the auto-compact window drops to 300000. The coach hour prints one ready line chosen by the time of day instead of asking the agent to load the skill, kvb-cleanup moves its procedure into plan.md, and the token counter reads subagent transcripts too and counts with Counter and defaultdict instead of .get defaults.

🎯 Fix: 88 %
💥 Break: 8 %
💸 Tokens: –
⏱️ Velocity: –
🛡️ Security: 100 %
🏆 Score: 81
🚦 Gate: ⛔ fix 88 % < 90, break 8 % > 5

Created-with: https://s.veen.world/skills
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 19:40:37 +00:00

123 lines
5.0 KiB
Python

"""Exercise coach.py: preferences flip between like and dislike, the log feeds hours since."""
from __future__ import annotations
import json
import subprocess
import sys
import tempfile
import unittest
from pathlib import Path
REPO_ROOT = Path(__file__).resolve().parents[1]
SCRIPT = REPO_ROOT / "skills" / "kvb-coach" / "scripts" / "coach.py"
HOOK = REPO_ROOT / "skills" / "kvb-coach" / "hooks" / "reminder.sh"
class TestCoach(unittest.TestCase):
def setUp(self):
self.tmp = tempfile.TemporaryDirectory()
self.addCleanup(self.tmp.cleanup)
self.profile = Path(self.tmp.name) / "state" / "coach.json"
def _run(self, *args: str) -> subprocess.CompletedProcess:
return subprocess.run(
[sys.executable, str(SCRIPT), "--profile", str(self.profile), *args],
check=False,
capture_output=True,
text=True,
)
def _show(self, now: str) -> dict:
result = self._run("show", "--now", now, "--json")
self.assertEqual(result.returncode, 0, result.stderr)
return {row["area"]: row for row in json.loads(result.stdout)}
def test_a_dislike_replaces_a_like_and_forget_drops_it(self):
self._run("like", "body", "Running")
self._run("like", "body", "Running")
self._run("like", "body", "Yoga")
self._run("dislike", "body", "Running")
body = self._show("2026-11-03T18:00:00+00:00")["body"]
self.assertEqual(body["likes"], ["Yoga"])
self.assertEqual(body["dislikes"], ["Running"])
self._run("forget", "body", "Running")
self.assertEqual(self._show("2026-11-03T18:00:00+00:00")["body"]["dislikes"], [])
def test_the_latest_done_per_area_gives_the_hours_since(self):
self._run("done", "food", "Breakfast", "--at", "2026-11-03T08:00:00+00:00")
self._run("done", "food", "Lunch", "--at", "2026-11-03T12:30:00+00:00")
self._run("done", "sleep", "Night", "--at", "2026-11-02T23:00:00+00:00")
rows = self._show("2026-11-03T18:00:00+00:00")
self.assertEqual((rows["food"]["last"], rows["food"]["hours_ago"]), ("Lunch", 5.5))
self.assertEqual(rows["sleep"]["hours_ago"], 19.0)
self.assertIsNone(rows["psyche"]["last"])
def test_invalid_input_exits_2_and_keeps_the_profile(self):
self.assertEqual(self._run("like", "work", "x").returncode, 2)
self.assertEqual(self._run("done", "body", "x", "--at", "yesterday").returncode, 2)
self.assertFalse(self.profile.exists())
self.profile.parent.mkdir(parents=True)
self.profile.write_text("{broken", encoding="utf-8")
self.assertEqual(self._run("show").returncode, 2)
self.assertEqual(self.profile.read_text(encoding="utf-8"), "{broken")
class TestCoachHour(unittest.TestCase):
def setUp(self):
self.tmp = tempfile.TemporaryDirectory()
self.addCleanup(self.tmp.cleanup)
self.hours = Path(self.tmp.name) / "hours"
def _run(self, hour: str) -> str:
return subprocess.run(
["bash", str(HOOK)],
check=True,
capture_output=True,
text=True,
env={
"PATH": "/usr/bin:/bin",
"HOME": self.tmp.name,
"COACH_STAMP_DIR": str(self.hours),
"COACH_HOUR": hour,
},
).stdout
def test_first_prompt_of_each_hour_triggers_the_coach_once(self):
self.assertIn("kvb-coach", self._run("2026-11-03T14"))
self.assertEqual(self._run("2026-11-03T14"), "")
self.assertIn("kvb-coach", self._run("2026-11-03T15"))
self.assertEqual(sorted(p.name for p in self.hours.iterdir()), ["2026-11-03T15"])
def test_the_hour_picks_one_fixed_tip_and_loads_no_skill(self):
tips = {hour: self._run(f"2026-11-03T{hour}").splitlines() for hour in ("23", "04", "12", "19", "09", "16")}
firsts = {hour: lines[1].split(" ", 1)[0] for hour, lines in tips.items()}
self.assertEqual(firsts, {"23": "😴", "04": "😴", "12": "🥗", "19": "🥗", "09": "🚶", "16": "🚶"})
self.assertTrue(all(len(lines) == 2 and "load no skill" in lines[0] for lines in tips.values()))
def test_a_binary_planted_on_the_inherited_path_never_runs(self):
planted = Path(self.tmp.name) / "planted"
planted.mkdir()
marker = Path(self.tmp.name) / "ran"
for name in ("mkdir", "find", "cat", "date"):
fake = planted / name
fake.write_text(f"#!/bin/sh\n/usr/bin/touch '{marker}'\n", encoding="utf-8")
fake.chmod(0o755)
subprocess.run(
["bash", str(HOOK)],
check=True,
capture_output=True,
text=True,
env={
"PATH": f"{planted}:/usr/bin:/bin",
"HOME": self.tmp.name,
"COACH_STAMP_DIR": str(self.hours),
"COACH_HOUR": "2026-11-03T14",
},
)
self.assertFalse(marker.exists())
if __name__ == "__main__":
unittest.main()