import { describe, expect, it } from 'bun:test' import { parseTeamsContent, teamsHtmlToText } from './message-content' const AMS_IMAGE_URL = 'https://kr-prod.asyncgw.teams.microsoft.com/v1/objects/0-skr-d1-abc/views/imgo' const ATTACHMENT_URL = 'https://contoso-my.sharepoint.com/personal/alice/Documents/report.pdf' describe('parseTeamsContent', () => { it('keeps the raw html and lists inline images for rich messages', () => { const html = `
Look at this
Thoughts?
` const parsed = parseTeamsContent(html, { messagetype: 'RichText/Html' }) expect(parsed).toEqual({ content: 'Look at this\n\nThoughts?', html, files: [AMS_IMAGE_URL], }) }) it('omits html and files for plain text messages', () => { const parsed = parseTeamsContent('Tom & Jerry', { messagetype: 'Text' }) expect(parsed).toEqual({ content: 'Tom & Jerry', html: undefined, files: undefined }) }) it('lists inline images before attachments from properties.files', () => { const properties = { files: JSON.stringify([{ '@type': 'http://schema.skype.com/File', objectUrl: ATTACHMENT_URL }]), } const html = `See
report
', { messagetype: 'RichText/Html', properties }) expect(parsed.files).toEqual([ATTACHMENT_URL]) }) it('reads attachments when properties itself arrives as a JSON string', () => { const properties = JSON.stringify({ files: JSON.stringify([{ objectUrl: ATTACHMENT_URL }]) }) const parsed = parseTeamsContent('report
', { messagetype: 'RichText/Html', properties }) expect(parsed.files).toEqual([ATTACHMENT_URL]) }) it('ignores malformed properties.files', () => { const parsed = parseTeamsContent('hi
', { messagetype: 'RichText/Html', properties: { files: '{not json' } }) expect(parsed.files).toBeUndefined() }) it('does not treat emoji, stickers, or GIFs as files', () => { const html = 'hi 
' +
'
'
const parsed = parseTeamsContent(html, { messagetype: 'RichText/Html' })
expect(parsed.files).toBeUndefined()
expect(parsed.content).toBe('hi π')
})
it('treats channel content without a messagetype as html only when it has tags', () => {
expect(parseTeamsContent('a < b
')).toEqual({ content: 'a < b', html: 'a < b
', files: undefined, }) expect(parseTeamsContent('a < b')).toEqual({ content: 'a < b', html: undefined, files: undefined }) }) it('returns empty content for a missing body', () => { expect(parseTeamsContent(undefined, { messagetype: 'RichText/Html' }).content).toBe('') }) }) describe('teamsHtmlToText', () => { it('turns block elements and line breaks into newlines', () => { expect(teamsHtmlToText('one
two
\r\n
title
\r\nhttps://example.com
\r\nbody\r\ncontinues
으λ‘ 😀 "q" <
')).toBe('μΌλ‘ π "q" <') }) it('decodes common typographic named entities', () => { expect(teamsHtmlToText('© 2026 — “ok”…
')).toBe('Β© 2026 β βokββ¦') }) it('keeps link targets that differ from the link text', () => { expect(teamsHtmlToText('Docs & more')).toBe( 'Docs & more (https://example.com/a?x=1&y=2)', ) }) it('does not repeat a link target that matches the link text', () => { expect(teamsHtmlToText('https://example.com')).toBe('https://example.com') expect(teamsHtmlToText('alice@example.com')).toBe('alice@example.com') }) it('preserves whitespace inside pre blocks', () => { expect(teamsHtmlToText('code:
{\n "a": 1\n}done
')).toBe( 'code:\n\n{\n "a": 1\n}\n\ndone', ) }) it('leaves blank lines and leading spaces inside pre blocks untouched', () => { expect(teamsHtmlToText('indented\n\n\n\nafter gap')).toBe(' indented\n\n\n\nafter gap ') }) it('collapses runs of spaces within a line', () => { expect(teamsHtmlToText('
a b\t c
')).toBe('a b c') }) })