"""routines.py edits ckb.yaml in place; every edit must leave valid YAML.""" import json import re import subprocess import sys import tempfile import unittest from pathlib import Path ROOT = Path(__file__).resolve().parents[2] SCRIPT = ROOT / ".agents/skills/ckb-routine/scripts/routines.py" def run(root, *args): return subprocess.run([sys.executable, str(SCRIPT), "--root", str(root), "--today", "2026-11-11", *args], capture_output=True, text=True, encoding="utf-8") def yaml_problems(text): """A YAML subset check strong enough for ckb.yaml: every mapping line is `key:` or `key: value` (a space after the colon), never `key:value`.""" bad = [] for n, line in enumerate(text.splitlines(), 1): body = line.split(" #", 1)[0].rstrip() m = re.match(r"^\s*(?:-\s+)?([A-Za-z_][\w-]*):(\S.*)?$", body) if m and m.group(2) and not m.group(2).startswith("//"): bad.append(f"{n}: {line}") return bad class Mark(unittest.TestCase): def setUp(self): self.tmp = tempfile.TemporaryDirectory() self.root = Path(self.tmp.name) def tearDown(self): self.tmp.cleanup() def write(self, text): (self.root / "ckb.yaml").write_text(text, encoding="utf-8") def read(self): return (self.root / "ckb.yaml").read_text(encoding="utf-8") def test_add_then_mark_keeps_space_after_colon(self): self.write("template:\n branch: main\n") self.assertEqual(run(self.root, "--add", "--name", "wl", "--do", "Lint", "--every", "7d").returncode, 0) self.assertEqual(run(self.root, "--mark", "wl").returncode, 0) text = self.read() self.assertIn(" last_run: 2026-11-11", text) self.assertEqual(yaml_problems(text), []) def test_mark_keeps_trailing_comment(self): self.write("routines:\n - name: wl\n do: Lint\n every: 7d\n last_run: # never ran\n") self.assertEqual(run(self.root, "--mark", "wl").returncode, 0) self.assertIn(" last_run: 2026-11-11 # never ran\n", self.read()) due = run(self.root, "--due") self.assertNotIn("unreadable", due.stdout + due.stderr) def test_interrupted_run_stays_due(self): # Only --mark advances last_run. Reading what is due (session start does # this) must never write, so a run that dies before --mark is due again # next session instead of silently skipped (ai-memory b4fbe3c4). before = "routines:\n - name: wl\n do: Lint\n every: 7d\n last_run: 2026-01-01\n" self.write(before) for args in (("--due",), ("--list",), ("--due",)): r = run(self.root, *args) self.assertIn(r.returncode, (0, 1), r.stdout + r.stderr) # --due: 1 = something due self.assertEqual(self.read(), before, f"{args} wrote ckb.yaml") self.assertIn("wl", run(self.root, "--due").stdout) run(self.root, "--mark", "wl") self.assertNotIn("wl", run(self.root, "--due").stdout) def test_mark_replaces_existing_date_only(self): self.write("routines:\n - name: wl\n do: Lint\n every: 7d\n last_run: 2026-01-01 # weekly\n") run(self.root, "--mark", "wl") self.assertIn(" last_run: 2026-11-11 # weekly\n", self.read()) def test_mark_unknown_name_leaves_file_unchanged(self): before = "routines:\n - name: wl\n do: Lint\n every: 7d\n last_run:\n" self.write(before) self.assertNotEqual(run(self.root, "--mark", "nope").returncode, 0) self.assertEqual(self.read(), before) def test_bad_period_leaves_file_unchanged(self): before = "template:\n branch: main\n" self.write(before) self.assertNotEqual(run(self.root, "--add", "--name", "x", "--do", "Lint", "--every", "3m").returncode, 0) self.assertEqual(self.read(), before) def test_zero_indent_list_is_read_and_extended(self): self.write("routines:\n- name: a\n do: Lint\n every: 7d\n last_run:\nother: 1\n") due = run(self.root, "--due") self.assertIn("a", due.stdout) self.assertEqual(run(self.root, "--add", "--name", "b", "--do", "Consolidate", "--every", "14d").returncode, 0) text = self.read() self.assertIn("\n- name: b\n do: \"Consolidate\"\n", text) self.assertTrue(text.endswith("other: 1\n")) self.assertIn("b", run(self.root, "--list").stdout) def test_backslashes_and_quotes_in_do_round_trip(self): self.write("template:\n branch: main\n") do = 'Index C:\\docs and say "done"' self.assertEqual(run(self.root, "--add", "--name", "x", "--do", do, "--every", "7d").returncode, 0) line = next(l for l in self.read().splitlines() if l.strip().startswith("do:")) self.assertEqual(json.loads(line.split(":", 1)[1].strip()), do) self.assertIn("C:\\docs", run(self.root, "--list").stdout) def test_remove_takes_continuation_lines(self): # The multi-line value is the item's last field: its continuation lines # have no `key:` of their own. self.write("routines:\n - name: a\n every: 7d\n do: >\n Lint the wiki\n and report\n" " - name: b\n do: Lint\n every: 7d\n") self.assertEqual(run(self.root, "--remove", "a").returncode, 0) text = self.read() self.assertNotIn("report", text) self.assertIn("- name: b", text) if __name__ == "__main__": unittest.main()