ckb/tests/unit/test_routines.py
Michał Kopeć 45d2830aed Template 2.0.5: routines, redaction, OKF quoting, Unicode search, low-severity fixes
Fixes F20-F24 from the 2.0.0 script review: zero-indent routine lists,
escaped --do values, --remove continuation lines; <private> redaction
across content blocks; OKF scalar quoting and code-fence titles; Unicode
tokens in retrieve and consolidate; bad workload dates, frontmatter at
EOF, `wiki/` refs without a slash, Starlight route collisions, labelled
wikilinks, string source_files, real splash sections, <private> dropped
from both exports.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 17:19:48 +01:00

107 lines
4.6 KiB
Python

"""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_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()