ckb/tests/unit/test_save_conversation.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

47 lines
1.9 KiB
Python

"""<private> spans never reach a saved transcript, even when a message splits
its text across several content blocks."""
import importlib.util
import json
import unittest
from pathlib import Path
ROOT = Path(__file__).resolve().parents[2]
SPEC = importlib.util.spec_from_file_location(
"save_conversation", str(ROOT / ".agents/skills/ckb-add-conversation/scripts/save_conversation.py"))
sc = importlib.util.module_from_spec(SPEC)
SPEC.loader.exec_module(sc)
def entry(*texts):
return {"type": "user", "message": {"role": "user", "content": [{"type": "text", "text": t} for t in texts]},
"uuid": "u-1", "timestamp": "2026-11-11T10:00:00Z"}
class Redaction(unittest.TestCase):
def test_unclosed_tag_redacts_the_rest_of_the_message(self):
out = json.dumps(sc.redact(entry("key: <private>sk-1", "23456</private> ok", "later")))
self.assertNotIn("sk-1", out)
self.assertNotIn("23456", out)
self.assertIn(" ok", out)
self.assertIn("later", out)
def test_unclosed_tag_never_closed_redacts_every_later_block(self):
out = sc.redact(entry("a <private>secret", "more secret", "still"))
texts = [b["text"] for b in out["message"]["content"]]
self.assertEqual(texts[1:], ["[redacted]", "[redacted]"])
self.assertEqual(out["uuid"], "u-1") # metadata is untouched
self.assertEqual(out["timestamp"], "2026-11-11T10:00:00Z")
def test_jsonl_line_round_trip(self):
line = json.dumps(entry("x <private>top", "secret</private> y")).encode()
got = sc.redact_jsonl(line + b"\n" + b'{"type":"system"}')
self.assertNotIn(b"secret", got)
self.assertTrue(got.endswith(b'{"type":"system"}'))
def test_closed_spans_in_one_block(self):
self.assertEqual(sc.redact("a <PRIVATE>b</private> c <private>d</private>"),
f"a {sc.PRIVATE_MARK} c {sc.PRIVATE_MARK}")
if __name__ == "__main__":
unittest.main()