"""Tests for check_text.py on made-up drafts. Run: python3 test_check_text.py
Not shipped in the skill zip; build.sh leaves test_* files out.
"""
# The drafts carry en and em dashes on purpose: they are what the checker hunts.
# ruff: noqa: RUF001
import contextlib
import io
import tempfile
import unittest
import zipfile
from pathlib import Path
import check_text
CV = """# Alex Example
Berlin | alex@example.com | +49 170 1234567
## Profile
Data engineer who builds streaming pipelines — and keeps them cheap. Results-driven team player.
## Experience
### Senior Data Engineer, Example GmbH
2019–2024, Berlin
- Built a Kafka pipeline for 2 billion events a day
- Cut cloud cost by 30% by optimizing storage tiers
- Led a team of four engineers through a migration from batch to streaming, covering design, rollout, monitoring, on-call handover, documentation, and the retirement of the old cron jobs
- Mentored two juniors
- Wrote the on-call runbook
- Ran the hiring loop
### Data Engineer, Sample AG
2016-2019, Munich
- Built ETL jobs in Python, SQL, and Spark
- Owned the data warehouse
- Ran the reporting
- Kept the lights on
- Did the budget
- Did the roadmap
- Wrote the docs
"""
CLEAN_CV = """# Alex Example
Berlin | alex@example.com | +49 170 1234567
## Experience
### Senior Data Engineer, Example GmbH
2019-2024, Berlin
- Built a Kafka pipeline for 2 billion events a day
- Cut cloud cost by 30% by tiering storage
### Data Engineer, Sample AG
Mar 2016-Dec 2018, Munich
- Built ETL jobs in Python, SQL, and Spark, with budgets of EUR 600k–1M
"""
def run(text, *argv, suffix=".md"):
with tempfile.TemporaryDirectory() as d:
path = Path(d) / f"draft{suffix}"
if isinstance(text, bytes):
path.write_bytes(text)
else:
path.write_text(text, encoding="utf-8")
buf = io.StringIO()
with contextlib.redirect_stdout(buf):
code = check_text.main([str(path), *argv])
return code, buf.getvalue()
def docx_bytes(paragraphs, header="", table=False):
w = "http://schemas.openxmlformats.org/wordprocessingml/2006/main"
def para(text, style=None, listed=False):
ppr = ""
if style or listed:
ppr = ""
ppr += f'' if style else ""
ppr += '' if listed else ""
ppr += ""
return f"{ppr}{text}"
body = "".join(para(*p) for p in paragraphs)
if table:
body += "" + para("cell") + ""
doc = f'{body}'
hdr = f'{para(header)}'
buf = io.BytesIO()
with zipfile.ZipFile(buf, "w") as z:
z.writestr("word/document.xml", doc)
z.writestr("word/header1.xml", hdr)
return buf.getvalue()
class CvChecks(unittest.TestCase):
def setUp(self):
self.code, self.out = run(CV)
def test_kind_and_exit(self):
self.assertIn("(kind: cv)", self.out)
self.assertEqual(self.code, 1)
def test_em_dash(self):
self.assertIn("em dash", self.out)
def test_en_dash_date_range(self):
self.assertIn("date range joined by U+2013", self.out)
def test_banned_phrases(self):
self.assertIn('banned phrase "Results-driven"', self.out)
self.assertIn('banned phrase "team player"', self.out)
def test_bullets_per_role(self):
self.assertIn("| 6 bullets |", self.out)
self.assertIn("| 7 bullets |", self.out)
self.assertIn("more than the role above it (6)", self.out)
def test_bullet_length(self):
self.assertIn("Bullets at most 2 lines", self.out)
def test_spelling_hint(self):
self.assertIn('American spelling "optimizing"', self.out)
self.assertIn("optimising", self.out)
class CleanCv(unittest.TestCase):
def test_no_errors(self):
code, out = run(CLEAN_CV)
self.assertEqual(code, 0, out)
self.assertNotIn("| error |", out)
self.assertNotIn("Oxford comma", out)
def test_numeric_range_without_year_is_not_a_date(self):
_, out = run(CLEAN_CV)
self.assertNotIn("U+2013", out)
class LetterChecks(unittest.TestCase):
def test_word_range_and_dash(self):
text = "Dear Ms Example,\n\nI think I could help -- with ingestion.\n\nKind regards,\nAlex\n"
code, out = run(text)
self.assertIn("(kind: letter)", out)
self.assertIn("Letter 250-300 words", out)
self.assertIn("'--' used as a dash", out)
self.assertIn('banned phrase "I think I could"', out)
self.assertEqual(code, 1)
def test_body_in_range(self):
body = " ".join(["word"] * 260)
code, out = run(f"Dear team,\n\n{body}.\n\nKind regards,\nAlex\n")
self.assertIn("260 words, target 250-300", out)
self.assertEqual(code, 0, out)
class FormChecks(unittest.TestCase):
def test_limits(self):
text = "## Why us\n\nI cut ingestion cost from 40k to 28k a month.\n\n## Pitch\n\nKafka at scale ✉\n"
code, out = run(text, "--kind", "form", "--limit", "Why us=20", "--word-limit", "Pitch=2")
self.assertIn("chars, limit 20", out)
self.assertIn("words, limit 2", out)
self.assertIn("icon glyph U+2709", out)
self.assertEqual(code, 1)
def test_whole_text_limit(self):
code, out = run("Short answer.", "--kind", "form", "--max-chars", "5")
self.assertIn("13 chars, limit 5", out)
self.assertEqual(code, 1)
def test_mailto_label(self):
_, out = run("Contact: [email me](mailto:alex@example.com)\n", "--kind", "post")
self.assertIn("contact only in a link target", out)
class DocxChecks(unittest.TestCase):
def test_docx(self):
data = docx_bytes(
[
("Alex Example", "Title"),
("Experience", "Heading1"),
("Data Engineer, Sample AG, 2016–2019", None),
("Built ETL jobs", None, True),
],
header="alex@example.com",
table=True,
)
code, out = run(data, suffix=".docx")
self.assertIn("(kind: cv)", out)
self.assertIn("only in the page header or footer", out)
self.assertIn("table found", out)
self.assertIn("date range joined by U+2013", out)
self.assertIn("| 1 |", out)
self.assertEqual(code, 1)
if __name__ == "__main__":
unittest.main()