Files
Moltbot/src/signal/format.test.ts
Hudson 1d6abddb9f fix(signal): outbound formatting and markdown IR rendering improvements (#9781)
* fix: Signal and markdown formatting improvements

Markdown IR fixes:
- Fix list-paragraph spacing (extra newline between list items and following paragraphs)
- Fix nested list indentation and newline handling
- Fix blockquote_close emitting redundant newline (inner content handles spacing)
- Render horizontal rules as visible ─── separator instead of silent drop
- Strip inner cell styles in code-mode tables to prevent overlapping with code_block span

Signal formatting fixes:
- Normalize URLs for dedup comparison (strip protocol, www., trailing slash)
- Render headings as bold text (headingStyle: 'bold')
- Add '> ' prefix to blockquotes for visual distinction
- Re-chunk after link expansion to respect chunk size limits

Tests:
- 51 new tests for markdown IR (spacing, lists, blockquotes, tables, HR)
- 18 new tests for Signal formatting (URL dedup, headings, blockquotes, HR, chunking)
- Update Slack nested list test expectation to match corrected IR output

* refactor: style-aware Signal text chunker

Replace indexOf-based chunk position tracking with deterministic
cursor tracking. The new splitSignalFormattedText:

- Splits at whitespace/newline boundaries within the limit
- Avoids breaking inside parentheses (preserves expanded link URLs)
- Slices style ranges at chunk boundaries with correct local offsets
- Tracks position via offset arithmetic instead of fragile indexOf

Removes dependency on chunkText from auto-reply/chunk.

Tests: 19 new tests covering style preservation across chunk boundaries,
edge cases (empty text, under limit, exact split points), and integration
with link expansion.

* fix: correct Signal style offsets with multiple link expansions

applyInsertionsToStyles() was using original coordinates for each
insertion without tracking cumulative shift from prior insertions.
This caused bold/italic/etc styles to drift to wrong text positions
when multiple markdown links expanded in a single message.

Added cumulative shift tracking and a regression test.

* test: clean up test noise and fix ineffective assertions

- Remove console.log from ir.list-spacing and ir.hr-spacing tests
- Fix ir.nested-lists.test.ts: remove ineffective regex assertion
- Fix ir.hr-spacing.test.ts: add actual assertions to edge case test

* refactor: split Signal formatting tests (#9781) (thanks @heyhudson)

---------

Co-authored-by: Hudson <258693705+hudson-rivera@users.noreply.github.com>
Co-authored-by: Peter Steinberger <steipete@gmail.com>
2026-02-14 16:57:20 +01:00

69 lines
2.4 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { markdownToSignalText } from "./format.js";
describe("markdownToSignalText", () => {
it("renders inline styles", () => {
const res = markdownToSignalText("hi _there_ **boss** ~~nope~~ `code`");
expect(res.text).toBe("hi there boss nope code");
expect(res.styles).toEqual([
{ start: 3, length: 5, style: "ITALIC" },
{ start: 9, length: 4, style: "BOLD" },
{ start: 14, length: 4, style: "STRIKETHROUGH" },
{ start: 19, length: 4, style: "MONOSPACE" },
]);
});
it("renders links as label plus url when needed", () => {
const res = markdownToSignalText("see [docs](https://example.com) and https://example.com");
expect(res.text).toBe("see docs (https://example.com) and https://example.com");
expect(res.styles).toEqual([]);
});
it("keeps style offsets correct with multiple expanded links", () => {
const markdown =
"[first](https://example.com/first) **bold** [second](https://example.com/second)";
const res = markdownToSignalText(markdown);
const expectedText =
"first (https://example.com/first) bold second (https://example.com/second)";
expect(res.text).toBe(expectedText);
expect(res.styles).toEqual([{ start: expectedText.indexOf("bold"), length: 4, style: "BOLD" }]);
});
it("applies spoiler styling", () => {
const res = markdownToSignalText("hello ||secret|| world");
expect(res.text).toBe("hello secret world");
expect(res.styles).toEqual([{ start: 6, length: 6, style: "SPOILER" }]);
});
it("renders fenced code blocks with monospaced styles", () => {
const res = markdownToSignalText("before\n\n```\nconst x = 1;\n```\n\nafter");
const prefix = "before\n\n";
const code = "const x = 1;\n";
const suffix = "\nafter";
expect(res.text).toBe(`${prefix}${code}${suffix}`);
expect(res.styles).toEqual([{ start: prefix.length, length: code.length, style: "MONOSPACE" }]);
});
it("renders lists without extra block markup", () => {
const res = markdownToSignalText("- one\n- two");
expect(res.text).toBe("• one\n• two");
expect(res.styles).toEqual([]);
});
it("uses UTF-16 code units for offsets", () => {
const res = markdownToSignalText("😀 **bold**");
const prefix = "😀 ";
expect(res.text).toBe(`${prefix}bold`);
expect(res.styles).toEqual([{ start: prefix.length, length: 4, style: "BOLD" }]);
});
});