ckb/tests/unit/test_routines.py
Michał Kopeć cd7ae4dd44 Template 1.28.2: backport 2.0.1 fixes to the 1.x line
Python 3.8 compatibility, routines.py --mark YAML spacing and comments,
OKF --out and Starlight image paths confined to their folders, script
counts in AGENTS.md/READMEs/manuals, and the tests/unit/ suite.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 14:27:15 +01:00

78 lines
3 KiB
Python

"""routines.py edits ckb.yaml in place; every edit must leave valid YAML."""
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_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)
if __name__ == "__main__":
unittest.main()