File size: 5,442 Bytes
83a59ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import assert from 'node:assert/strict'
import { test } from 'node:test'

import { formatWhatsappText, isUrlNotificationToken } from './whatsapp-format'

test('isUrlNotificationToken', async (t) => {
  await t.test('returns true for an https:// token', () => {
    assert.equal(isUrlNotificationToken('https://example.com/notify'), true)
  })

  await t.test('returns false for an FCM token', () => {
    assert.equal(isUrlNotificationToken('some-fcm-token'), false)
  })

  await t.test('returns false for http:// (non-https) tokens', () => {
    assert.equal(isUrlNotificationToken('http://example.com/notify'), false)
  })

  await t.test('returns false for null/undefined/empty tokens', () => {
    assert.equal(isUrlNotificationToken(undefined), false)
    assert.equal(isUrlNotificationToken(null), false)
    assert.equal(isUrlNotificationToken(''), false)
  })

  await t.test('trims whitespace and is case-insensitive', () => {
    assert.equal(isUrlNotificationToken('  HTTPS://example.com  '), true)
  })
})

test('formatWhatsappText - boundaries', async (t) => {
  await t.test('bolds text surrounded by whitespace', () => {
    assert.equal(
      formatWhatsappText('*boldme* test'),
      '<strong>boldme</strong> test',
    )
  })

  await t.test('does not bold a marker with no boundary at the end', () => {
    assert.equal(
      formatWhatsappText('*donotboldme*no space or end of string'),
      '*donotboldme*no space or end of string',
    )
  })

  await t.test('bolds text bounded by punctuation', () => {
    assert.equal(
      formatWhatsappText('your *IUC/Smartcard number*.'),
      'your <strong>IUC/Smartcard number</strong>.',
    )
  })

  await t.test('does not format a marker embedded mid-word', () => {
    assert.equal(
      formatWhatsappText('(parenthesis*bold*test)'),
      '(parenthesis*bold*test)',
    )
  })

  await t.test('formats italic and strikethrough', () => {
    assert.equal(formatWhatsappText('_it_ and ~s~'), '<em>it</em> and <s>s</s>')
  })
})

test('formatWhatsappText - quotes', async (t) => {
  await t.test('wraps "> " prefixed lines in a quote span', () => {
    assert.equal(
      formatWhatsappText('> quoted text'),
      '<span class="d-block pl-2" style="border-left: 3px solid currentColor; opacity: 0.8;">quoted text</span>',
    )
  })

  await t.test('leaves a bare ">" with no following space untouched', () => {
    assert.equal(formatWhatsappText('>threshold'), '&gt;threshold')
  })
})

test('formatWhatsappText - code', async (t) => {
  await t.test('wraps inline code', () => {
    assert.equal(formatWhatsappText('`code`'), '<code>code</code>')
  })

  await t.test('wraps multiline triple-backtick code blocks', () => {
    assert.equal(
      formatWhatsappText('```\nline one\nline two\n```'),
      '<code>\nline one\nline two\n</code>',
    )
  })

  await t.test('does not reformat markdown characters inside code', () => {
    assert.equal(formatWhatsappText('`a *b* c`'), '<code>a *b* c</code>')
  })
})

test('formatWhatsappText - links', async (t) => {
  await t.test('linkifies a raw URL', () => {
    assert.equal(
      formatWhatsappText('visit https://example.com today'),
      'visit <a href="https://example.com" target="_blank" rel="noopener noreferrer">https://example.com</a> today',
    )
  })

  await t.test('strips unbalanced trailing punctuation from a URL', () => {
    assert.equal(
      formatWhatsappText('Check this out (https://example.com).'),
      'Check this out (<a href="https://example.com" target="_blank" rel="noopener noreferrer">https://example.com</a>).',
    )
  })

  await t.test(
    'keeps a balanced trailing parenthesis as part of the URL',
    () => {
      assert.equal(
        formatWhatsappText('See https://example.com/page_(v2) for details.'),
        'See <a href="https://example.com/page_(v2)" target="_blank" rel="noopener noreferrer">https://example.com/page_(v2)</a> for details.',
      )
    },
  )

  await t.test(
    'preserves a legitimate trailing marker character that is part of the URL',
    () => {
      assert.equal(
        formatWhatsappText('Download https://example.com/file_name_ now'),
        'Download <a href="https://example.com/file_name_" target="_blank" rel="noopener noreferrer">https://example.com/file_name_</a> now',
      )
    },
  )

  await t.test('bolds a URL wrapped in asterisks', () => {
    assert.equal(
      formatWhatsappText('*https://example.com*'),
      '<strong><a href="https://example.com" target="_blank" rel="noopener noreferrer">https://example.com</a></strong>',
    )
  })
})

test('formatWhatsappText - escaping and placeholders', async (t) => {
  await t.test('escapes HTML special characters', () => {
    assert.equal(
      formatWhatsappText('<script>alert(1)</script>'),
      '&lt;script&gt;alert(1)&lt;/script&gt;',
    )
  })

  await t.test(
    'does not let literal placeholder-like text leak a link into the output',
    () => {
      const input = 'no links here, just text'
      assert.equal(formatWhatsappText(input), input)
    },
  )

  await t.test('two calls do not interfere with each other', () => {
    const first = formatWhatsappText('visit https://a.example today')
    const second = formatWhatsappText('visit https://b.example today')
    assert.match(first, /href="https:\/\/a\.example"/)
    assert.match(second, /href="https:\/\/b\.example"/)
    assert.doesNotMatch(first, /https:\/\/b\.example/)
  })
})