Files
kongruity/backend/tests/slackText.test.ts
2026-08-02 06:13:01 -04:00

74 lines
2.7 KiB
TypeScript

import { describe, expect, it } from 'vitest';
import { cleanSlackText } from '../lib/slackText.js';
describe('cleanSlackText', () => {
it('should leave plain text untouched', () => {
expect(cleanSlackText('ship the thing')).toBe('ship the thing');
});
it('should keep a bare user mention readable when no label is supplied', () => {
expect(cleanSlackText('ping <@U0123> about it')).toBe('ping @U0123 about it');
});
it('should prefer the label on a user mention', () => {
expect(cleanSlackText('ping <@U0123|kevin> about it')).toBe('ping @kevin about it');
});
it('should render a channel reference by name', () => {
expect(cleanSlackText('see <#C0123|general>')).toBe('see #general');
});
it('should keep a channel reference without a label', () => {
expect(cleanSlackText('see <#C0123>')).toBe('see #C0123');
});
it('should convert broadcast mentions', () => {
expect(cleanSlackText('<!here> heads up')).toBe('@here heads up');
expect(cleanSlackText('<!channel> heads up')).toBe('@channel heads up');
});
it('should replace a labelled link with its label', () => {
expect(cleanSlackText('read <https://example.com|the docs>')).toBe('read the docs');
});
it('should keep the url when a link has no label', () => {
expect(cleanSlackText('read <https://example.com>')).toBe('read https://example.com');
});
it('should unwrap a mailto link', () => {
expect(cleanSlackText('mail <mailto:a@b.com|a@b.com>')).toBe('mail a@b.com');
expect(cleanSlackText('mail <mailto:a@b.com>')).toBe('mail a@b.com');
});
it('should unescape html entities', () => {
expect(cleanSlackText('tabs &amp; spaces')).toBe('tabs & spaces');
});
/**
* Slack escapes a literal angle bracket so it is not read as markup. If
* entities were unescaped first, this would be parsed as a link and the
* user's text would silently disappear.
*/
it('should not reparse an escaped angle bracket as markup', () => {
expect(cleanSlackText('if a &lt;b&gt; then stop')).toBe('if a <b> then stop');
});
it('should handle several references in one message', () => {
expect(
cleanSlackText('<@U1|amy> moved <#C1|ops> to <https://x.co|the wiki>')
).toBe('@amy moved #ops to the wiki');
});
it('should trim surrounding whitespace and trailing spaces on each line', () => {
expect(cleanSlackText(' first line \n second ')).toBe('first line\n second');
});
it('should return an empty string for whitespace-only input', () => {
expect(cleanSlackText(' \n ')).toBe('');
});
it('should leave an empty angle-bracket pair alone rather than throwing', () => {
expect(cleanSlackText('a <> b')).toBe('a b');
});
});