|
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, |
@@ -63,4 +66,123 @@ describe("msteams inbound", () => { |
63 | 66 | ).toBe(false); |
64 | 67 | }); |
65 | 68 | }); |
| 69 | + |
| 70 | + describe("decodeHtmlEntities", () => { |
| 71 | + it("decodes common entities", () => { |
| 72 | + expect(decodeHtmlEntities("&<>"'' ")).toBe("&<>\"'' "); |
| 73 | + }); |
| 74 | + |
| 75 | + it("leaves plain text unchanged", () => { |
| 76 | + expect(decodeHtmlEntities("hello world")).toBe("hello world"); |
| 77 | + }); |
| 78 | + }); |
| 79 | + |
| 80 | + describe("htmlToPlainText", () => { |
| 81 | + it("strips tags and decodes entities", () => { |
| 82 | + expect(htmlToPlainText("<strong>Hello & world</strong>")).toBe("Hello & world"); |
| 83 | + }); |
| 84 | + |
| 85 | + it("collapses whitespace from tag removal", () => { |
| 86 | + expect(htmlToPlainText("<p>foo</p><p>bar</p>")).toBe("foo bar"); |
| 87 | + }); |
| 88 | + |
| 89 | + it("trims leading and trailing whitespace", () => { |
| 90 | + expect(htmlToPlainText(" <span>hi</span> ")).toBe("hi"); |
| 91 | + }); |
| 92 | + }); |
| 93 | + |
| 94 | + describe("extractMSTeamsQuoteInfo", () => { |
| 95 | + const replyAttachment = (overrides?: { content?: string; contentType?: string }) => ({ |
| 96 | + contentType: overrides?.contentType ?? "text/html", |
| 97 | + content: |
| 98 | + overrides?.content ?? |
| 99 | + '<blockquote itemtype="http://schema.skype.com/Reply" itemscope>' + |
| 100 | + '<strong itemprop="mri">Alice</strong>' + |
| 101 | + '<p itemprop="copy">Hello world</p>' + |
| 102 | + "</blockquote>", |
| 103 | + }); |
| 104 | + |
| 105 | + it("extracts sender and body from a Teams reply attachment", () => { |
| 106 | + const result = extractMSTeamsQuoteInfo([replyAttachment()]); |
| 107 | + expect(result).toEqual({ sender: "Alice", body: "Hello world" }); |
| 108 | + }); |
| 109 | + |
| 110 | + it("returns undefined for empty attachments array", () => { |
| 111 | + expect(extractMSTeamsQuoteInfo([])).toBeUndefined(); |
| 112 | + }); |
| 113 | + |
| 114 | + it("returns undefined when no reply blockquote is present", () => { |
| 115 | + expect( |
| 116 | + extractMSTeamsQuoteInfo([{ contentType: "text/html", content: "<p>just a message</p>" }]), |
| 117 | + ).toBeUndefined(); |
| 118 | + }); |
| 119 | + |
| 120 | + it("uses 'unknown' as sender when sender element is absent", () => { |
| 121 | + const result = extractMSTeamsQuoteInfo([ |
| 122 | + { |
| 123 | + contentType: "text/html", |
| 124 | + content: |
| 125 | + '<blockquote itemtype="http://schema.skype.com/Reply" itemscope>' + |
| 126 | + '<p itemprop="copy">quoted text</p>' + |
| 127 | + "</blockquote>", |
| 128 | + }, |
| 129 | + ]); |
| 130 | + expect(result).toEqual({ sender: "unknown", body: "quoted text" }); |
| 131 | + }); |
| 132 | + |
| 133 | + it("returns undefined when body element is absent", () => { |
| 134 | + const result = extractMSTeamsQuoteInfo([ |
| 135 | + { |
| 136 | + contentType: "text/html", |
| 137 | + content: |
| 138 | + '<blockquote itemtype="http://schema.skype.com/Reply" itemscope>' + |
| 139 | + '<strong itemprop="mri">Alice</strong>' + |
| 140 | + "</blockquote>", |
| 141 | + }, |
| 142 | + ]); |
| 143 | + expect(result).toBeUndefined(); |
| 144 | + }); |
| 145 | + |
| 146 | + it("decodes HTML entities in body text", () => { |
| 147 | + const result = extractMSTeamsQuoteInfo([ |
| 148 | + { |
| 149 | + contentType: "text/html", |
| 150 | + content: |
| 151 | + '<blockquote itemtype="http://schema.skype.com/Reply" itemscope>' + |
| 152 | + '<strong itemprop="mri">Bob</strong>' + |
| 153 | + '<p itemprop="copy">2 < 3 & 4 > 1</p>' + |
| 154 | + "</blockquote>", |
| 155 | + }, |
| 156 | + ]); |
| 157 | + expect(result).toEqual({ sender: "Bob", body: "2 < 3 & 4 > 1" }); |
| 158 | + }); |
| 159 | + |
| 160 | + it("handles multiline body by collapsing whitespace", () => { |
| 161 | + const result = extractMSTeamsQuoteInfo([ |
| 162 | + { |
| 163 | + contentType: "text/html", |
| 164 | + content: |
| 165 | + '<blockquote itemtype="http://schema.skype.com/Reply" itemscope>' + |
| 166 | + '<strong itemprop="mri">Carol</strong>' + |
| 167 | + '<p itemprop="copy">line one\nline two</p>' + |
| 168 | + "</blockquote>", |
| 169 | + }, |
| 170 | + ]); |
| 171 | + expect(result?.body).toBe("line one line two"); |
| 172 | + }); |
| 173 | + |
| 174 | + it("skips non-string content values", () => { |
| 175 | + expect( |
| 176 | + extractMSTeamsQuoteInfo([{ contentType: "application/json", content: { foo: "bar" } }]), |
| 177 | + ).toBeUndefined(); |
| 178 | + }); |
| 179 | + |
| 180 | + it("finds quote in second attachment when first has no quote", () => { |
| 181 | + const result = extractMSTeamsQuoteInfo([ |
| 182 | + { contentType: "text/plain", content: "plain text" }, |
| 183 | + replyAttachment(), |
| 184 | + ]); |
| 185 | + expect(result).toEqual({ sender: "Alice", body: "Hello world" }); |
| 186 | + }); |
| 187 | + }); |
66 | 188 | }); |
0 commit comments