openclaw/src/signal/format.links.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

59 lines
2.5 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { markdownToSignalText } from "./format.js";
describe("markdownToSignalText", () => {
describe("duplicate URL display", () => {
it("does not duplicate URL when label matches URL without protocol", () => {
// [selfh.st](http://selfh.st) should render as "selfh.st" not "selfh.st (http://selfh.st)"
const res = markdownToSignalText("[selfh.st](http://selfh.st)");
expect(res.text).toBe("selfh.st");
});
it("does not duplicate URL when label matches URL without https protocol", () => {
const res = markdownToSignalText("[example.com](https://example.com)");
expect(res.text).toBe("example.com");
});
it("does not duplicate URL when label matches URL without www prefix", () => {
const res = markdownToSignalText("[www.example.com](https://example.com)");
expect(res.text).toBe("www.example.com");
});
it("does not duplicate URL when label matches URL without trailing slash", () => {
const res = markdownToSignalText("[example.com](https://example.com/)");
expect(res.text).toBe("example.com");
});
it("does not duplicate URL when label matches URL with multiple trailing slashes", () => {
const res = markdownToSignalText("[example.com](https://example.com///)");
expect(res.text).toBe("example.com");
});
it("does not duplicate URL when label includes www but URL does not", () => {
const res = markdownToSignalText("[example.com](https://www.example.com)");
expect(res.text).toBe("example.com");
});
it("handles case-insensitive domain comparison", () => {
const res = markdownToSignalText("[EXAMPLE.COM](https://example.com)");
expect(res.text).toBe("EXAMPLE.COM");
});
it("still shows URL when label is meaningfully different", () => {
const res = markdownToSignalText("[click here](https://example.com)");
expect(res.text).toBe("click here (https://example.com)");
});
it("handles URL with path - should show URL when label is just domain", () => {
// Label is just domain, URL has path - these are meaningfully different
const res = markdownToSignalText("[example.com](https://example.com/page)");
expect(res.text).toBe("example.com (https://example.com/page)");
});
it("does not duplicate when label matches full URL with path", () => {
const res = markdownToSignalText("[example.com/page](https://example.com/page)");
expect(res.text).toBe("example.com/page");
});
});
});