|
1 | 1 | import { describe, expect, it } from "vitest"; |
2 | 2 | import { |
| 3 | + decodeHtmlEntities, |
| 4 | + extractMSTeamsQuoteInfo, |
| 5 | + htmlToPlainText, |
3 | 6 | normalizeMSTeamsConversationId, |
4 | 7 | parseMSTeamsActivityTimestamp, |
5 | 8 | stripMSTeamsMentionTags, |
@@ -66,4 +69,153 @@ describe("msteams inbound", () => { |
66 | 69 | ).toBe(false); |
67 | 70 | }); |
68 | 71 | }); |
| 72 | + |
| 73 | + describe("decodeHtmlEntities", () => { |
| 74 | + it("decodes common entities", () => { |
| 75 | + expect(decodeHtmlEntities("&<>"'' ")).toBe("&<>\"'' "); |
| 76 | + }); |
| 77 | + |
| 78 | + it("leaves plain text unchanged", () => { |
| 79 | + expect(decodeHtmlEntities("hello world")).toBe("hello world"); |
| 80 | + }); |
| 81 | + |
| 82 | + it("prevents double-decoding: &lt; should become < not <", () => { |
| 83 | + // If & were decoded first, &lt; → < → < (wrong). |
| 84 | + // With & decoded last, &lt; stays as < (correct). |
| 85 | + expect(decodeHtmlEntities("&lt;b&gt;")).toBe("<b>"); |
| 86 | + }); |
| 87 | + }); |
| 88 | + |
| 89 | + describe("htmlToPlainText", () => { |
| 90 | + it("strips tags and decodes entities", () => { |
| 91 | + expect(htmlToPlainText("<strong>Hello & world</strong>")).toBe("Hello & world"); |
| 92 | + }); |
| 93 | + |
| 94 | + it("collapses whitespace from tag removal", () => { |
| 95 | + expect(htmlToPlainText("<p>foo</p><p>bar</p>")).toBe("foo bar"); |
| 96 | + }); |
| 97 | + |
| 98 | + it("trims leading and trailing whitespace", () => { |
| 99 | + expect(htmlToPlainText(" <span>hi</span> ")).toBe("hi"); |
| 100 | + }); |
| 101 | + }); |
| 102 | + |
| 103 | + describe("extractMSTeamsQuoteInfo", () => { |
| 104 | + const replyAttachment = (overrides?: { content?: string; contentType?: string }) => ({ |
| 105 | + contentType: overrides?.contentType ?? "text/html", |
| 106 | + content: |
| 107 | + overrides?.content ?? |
| 108 | + '<blockquote itemtype="http://schema.skype.com/Reply" itemscope>' + |
| 109 | + '<strong itemprop="mri">Alice</strong>' + |
| 110 | + '<p itemprop="copy">Hello world</p>' + |
| 111 | + "</blockquote>", |
| 112 | + }); |
| 113 | + |
| 114 | + it("extracts sender and body from a Teams reply attachment", () => { |
| 115 | + const result = extractMSTeamsQuoteInfo([replyAttachment()]); |
| 116 | + expect(result).toEqual({ sender: "Alice", body: "Hello world" }); |
| 117 | + }); |
| 118 | + |
| 119 | + it("returns undefined for empty attachments array", () => { |
| 120 | + expect(extractMSTeamsQuoteInfo([])).toBeUndefined(); |
| 121 | + }); |
| 122 | + |
| 123 | + it("returns undefined when no reply blockquote is present", () => { |
| 124 | + expect( |
| 125 | + extractMSTeamsQuoteInfo([{ contentType: "text/html", content: "<p>just a message</p>" }]), |
| 126 | + ).toBeUndefined(); |
| 127 | + }); |
| 128 | + |
| 129 | + it("uses 'unknown' as sender when sender element is absent", () => { |
| 130 | + const result = extractMSTeamsQuoteInfo([ |
| 131 | + { |
| 132 | + contentType: "text/html", |
| 133 | + content: |
| 134 | + '<blockquote itemtype="http://schema.skype.com/Reply" itemscope>' + |
| 135 | + '<p itemprop="copy">quoted text</p>' + |
| 136 | + "</blockquote>", |
| 137 | + }, |
| 138 | + ]); |
| 139 | + expect(result).toEqual({ sender: "unknown", body: "quoted text" }); |
| 140 | + }); |
| 141 | + |
| 142 | + it("returns undefined when body element is absent", () => { |
| 143 | + const result = extractMSTeamsQuoteInfo([ |
| 144 | + { |
| 145 | + contentType: "text/html", |
| 146 | + content: |
| 147 | + '<blockquote itemtype="http://schema.skype.com/Reply" itemscope>' + |
| 148 | + '<strong itemprop="mri">Alice</strong>' + |
| 149 | + "</blockquote>", |
| 150 | + }, |
| 151 | + ]); |
| 152 | + expect(result).toBeUndefined(); |
| 153 | + }); |
| 154 | + |
| 155 | + it("decodes HTML entities in body text", () => { |
| 156 | + const result = extractMSTeamsQuoteInfo([ |
| 157 | + { |
| 158 | + contentType: "text/html", |
| 159 | + content: |
| 160 | + '<blockquote itemtype="http://schema.skype.com/Reply" itemscope>' + |
| 161 | + '<strong itemprop="mri">Bob</strong>' + |
| 162 | + '<p itemprop="copy">2 < 3 & 4 > 1</p>' + |
| 163 | + "</blockquote>", |
| 164 | + }, |
| 165 | + ]); |
| 166 | + expect(result).toEqual({ sender: "Bob", body: "2 < 3 & 4 > 1" }); |
| 167 | + }); |
| 168 | + |
| 169 | + it("handles multiline body by collapsing whitespace", () => { |
| 170 | + const result = extractMSTeamsQuoteInfo([ |
| 171 | + { |
| 172 | + contentType: "text/html", |
| 173 | + content: |
| 174 | + '<blockquote itemtype="http://schema.skype.com/Reply" itemscope>' + |
| 175 | + '<strong itemprop="mri">Carol</strong>' + |
| 176 | + '<p itemprop="copy">line one\nline two</p>' + |
| 177 | + "</blockquote>", |
| 178 | + }, |
| 179 | + ]); |
| 180 | + expect(result?.body).toBe("line one line two"); |
| 181 | + }); |
| 182 | + |
| 183 | + it("skips non-string content values", () => { |
| 184 | + expect( |
| 185 | + extractMSTeamsQuoteInfo([{ contentType: "application/json", content: { foo: "bar" } }]), |
| 186 | + ).toBeUndefined(); |
| 187 | + }); |
| 188 | + |
| 189 | + it("handles object content with .text property containing the reply HTML", () => { |
| 190 | + const htmlContent = |
| 191 | + '<blockquote itemtype="http://schema.skype.com/Reply" itemscope>' + |
| 192 | + '<strong itemprop="mri">Dave</strong>' + |
| 193 | + '<p itemprop="copy">hello from object</p>' + |
| 194 | + "</blockquote>"; |
| 195 | + const result = extractMSTeamsQuoteInfo([ |
| 196 | + { contentType: "text/html", content: { text: htmlContent } }, |
| 197 | + ]); |
| 198 | + expect(result).toEqual({ sender: "Dave", body: "hello from object" }); |
| 199 | + }); |
| 200 | + |
| 201 | + it("handles object content with .body property containing the reply HTML", () => { |
| 202 | + const htmlContent = |
| 203 | + '<blockquote itemtype="http://schema.skype.com/Reply" itemscope>' + |
| 204 | + '<strong itemprop="mri">Eve</strong>' + |
| 205 | + '<p itemprop="copy">hello from body field</p>' + |
| 206 | + "</blockquote>"; |
| 207 | + const result = extractMSTeamsQuoteInfo([ |
| 208 | + { contentType: "text/html", content: { body: htmlContent } }, |
| 209 | + ]); |
| 210 | + expect(result).toEqual({ sender: "Eve", body: "hello from body field" }); |
| 211 | + }); |
| 212 | + |
| 213 | + it("finds quote in second attachment when first has no quote", () => { |
| 214 | + const result = extractMSTeamsQuoteInfo([ |
| 215 | + { contentType: "text/plain", content: "plain text" }, |
| 216 | + replyAttachment(), |
| 217 | + ]); |
| 218 | + expect(result).toEqual({ sender: "Alice", body: "Hello world" }); |
| 219 | + }); |
| 220 | + }); |
69 | 221 | }); |
0 commit comments