refactor: cut cyclomatic complexity of five hot spots and pin their behavior with tests

Risk labels, menu callback parsing, settings callbacks, table cell width and
markdown link targets now use tables and small helpers (CC <= 10).
This commit is contained in:
evandro committed 2026-10-10 03:59:46 -03:00
1 parent bf4a2021e8
commit 6a6e75fe32
8 files changed
+648 -294

No files matched your search

+76
View File
@@ -294,6 +294,82 @@ test("Markdown table padding handles CJK and combining marks", () => {
assert.match(chunks[0]?.text ?? "", /東京 \| city /u);
});
test("Markdown table padding measures wide, zero-width, and control code points", () => {
// [cell, expected display width]
const cells: Array<[string, number]> = [
["a", 1],
["\u{d55c}", 2], // Hangul syllable
["\u{ff21}", 2], // fullwidth Latin
["\u{2329}", 2], // angle bracket
["\u{f900}", 2], // CJK compatibility ideograph
["\u{fe10}", 2], // vertical form
["\u{fe30}", 2], // CJK compatibility form
["\u{ffe0}", 2], // fullwidth sign
["\u{1100}", 2], // Hangul Jamo
["é", 1], // combining mark
["a‍b", 2], // zero-width joiner
["a️", 1], // variation selector
["a\u0001b", 2], // control character
["ab", 2],
];
const markdown = [
"| W | N |",
"| --- | --- |",
...cells.map(([cell], index) => `| ${cell} | c${index} |`),
].join("\n");
const chunks = renderTelegramMessage(markdown, { mode: "markdown" });
assert.equal(chunks.length, 1);
const expectedRows = [
"W | N ",
"--- | ---",
...cells.map(
([cell, width], index) =>
`${cell}${" ".repeat(3 - width)} | ${`c${index}`.padEnd(3)}`,
),
];
assert.equal(
chunks[0]?.text,
`<pre><code class="language-markdown">${expectedRows.join("\n")}</code></pre>`,
);
});
test("Markdown link targets accept valid destinations and titles and reject malformed ones", () => {
const link = (href: string) => `<a href="${href}">a</a>`;
const cases: Array<[string, string]> = [
// plain destinations
["[a](https://x.com)", link("https://x.com")],
["[a]( https://x.com )", link("https://x.com")],
["[a](https://x.com/a_(b))", link("https://x.com/a_(b)")],
[String.raw`[a](https://x.com/a\)b)`, link(String.raw`https://x.com/a\)b`)],
[String.raw`[a](https://x.com/a\ b)`, link(String.raw`https://x.com/a\ b`)],
["[a](https://x.com/a b)", "[a](https://x.com/a b)"],
["[a](https://x.com/a(b)", "[a](https://x.com/a(b)"],
// angle-bracket destinations
["[a](<https://x.com/a b>)", link("https://x.com/a b")],
[String.raw`[a](<https://x.com/a\>b>)`, link(String.raw`https://x.com/a\&gt;b`)],
["[a](<https://x.com/a", "[a](&lt;https://x.com/a"],
["[a](<>)", "[a](&lt;&gt;)"],
// missing destination or end of input
["[a]()", "[a]()"],
["[a](", "[a]("],
["[a]( ", "[a]( "],
// titles
['[a](https://x.com "T")', link("https://x.com")],
["[a](https://x.com 'T')", link("https://x.com")],
["[a](https://x.com (T))", link("https://x.com")],
['[a](<https://x.com> "T")', link("https://x.com")],
["[a](https://x.com )", link("https://x.com")],
[String.raw`[a](https://x.com "T\"q")`, link("https://x.com")],
['[a](https://x.com "T)', '[a](https://x.com "T)'],
[String.raw`[a](https://x.com "T\")`, String.raw`[a](https://x.com "T\")`],
["[a](https://x.com T)", "[a](https://x.com T)"],
];
for (const [input, expected] of cases) {
const chunks = renderTelegramMessage(input, { mode: "markdown" });
assert.equal(chunks[0]?.text, expected, input);
}
});
test("Links, code spans, and underscore-heavy text coexist safely", () => {
const chunks = renderTelegramMessage(
"See [docs](https://example.com), run `foo_bar()` and keep foo_bar.txt literal",