This repository was archived by the owner on May 15, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathtext-normalization.spec.ts
More file actions
125 lines (104 loc) · 4.6 KB
/
Copy pathtext-normalization.spec.ts
File metadata and controls
125 lines (104 loc) · 4.6 KB
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
import { normalizeString, unescapeHtmlEntities, sanitizeForPromptInjection } from "../text-normalization"
describe("Text normalization utilities", () => {
describe("normalizeString", () => {
it("normalizes smart quotes by default", () => {
expect(normalizeString("These are \u201Csmart quotes\u201D and \u2018single quotes\u2019")).toBe(
"These are \"smart quotes\" and 'single quotes'",
)
})
it("normalizes typographic characters by default", () => {
expect(normalizeString("This has an em dash \u2014 and ellipsis\u2026")).toBe(
"This has an em dash - and ellipsis...",
)
})
it("normalizes whitespace by default", () => {
expect(normalizeString("Multiple spaces and\t\ttabs")).toBe("Multiple spaces and tabs")
})
it("can be configured to skip certain normalizations", () => {
const input = "Keep \u201Csmart quotes\u201D but normalize whitespace"
expect(normalizeString(input, { smartQuotes: false })).toBe(
"Keep \u201Csmart quotes\u201D but normalize whitespace",
)
})
it("real-world example with mixed characters", () => {
const input = "Let\u2019s test this\u2014with some \u201Cfancy\u201D punctuation\u2026 and spaces"
expect(normalizeString(input)).toBe('Let\'s test this-with some "fancy" punctuation... and spaces')
})
})
describe("unescapeHtmlEntities", () => {
it("unescapes basic HTML entities", () => {
expect(unescapeHtmlEntities("<div>Hello</div>")).toBe("<div>Hello</div>")
})
it("unescapes ampersand entity", () => {
expect(unescapeHtmlEntities("This & that")).toBe("This & that")
})
it("unescapes quote entities", () => {
expect(unescapeHtmlEntities(""quoted" and 'single-quoted'")).toBe(
"\"quoted\" and 'single-quoted'",
)
})
it("unescapes apostrophe entity", () => {
expect(unescapeHtmlEntities("Don't worry")).toBe("Don't worry")
})
it("handles mixed content with multiple entity types", () => {
expect(
unescapeHtmlEntities(
"<a href="https://example.com?param1=value&param2=value">Link</a>",
),
).toBe('<a href="https://example.com?param1=value¶m2=value">Link</a>')
})
it("handles mixed content with apostrophe entities", () => {
expect(
unescapeHtmlEntities(
"<div>Don't forget that Tom&Jerry's show is at 3 o'clock</div>",
),
).toBe("<div>Don't forget that Tom&Jerry's show is at 3 o'clock</div>")
})
it("returns original string when no entities are present", () => {
const original = "Plain text without entities"
expect(unescapeHtmlEntities(original)).toBe(original)
})
it("handles empty or undefined input", () => {
expect(unescapeHtmlEntities("")).toBe("")
expect(unescapeHtmlEntities(undefined as unknown as string)).toBe(undefined)
})
it("unescapes square bracket entities (numeric)", () => {
expect(unescapeHtmlEntities("string[]")).toBe("string[]")
expect(unescapeHtmlEntities("array[0]")).toBe("array[0]")
expect(unescapeHtmlEntities("matrix[i][j]")).toBe("matrix[i][j]")
})
it("unescapes square bracket entities (named)", () => {
expect(unescapeHtmlEntities("string[]")).toBe("string[]")
expect(unescapeHtmlEntities("array[0]")).toBe("array[0]")
expect(unescapeHtmlEntities("matrix[i][j]")).toBe("matrix[i][j]")
})
it("handles C# array syntax with escaped square brackets", () => {
// Test case based on the reported issue
const input = "string[] myArray = new string[5];\nmyArray[2] = "hello";"
const expected = 'string[] myArray = new string[5];\nmyArray[2] = "hello";'
expect(unescapeHtmlEntities(input)).toBe(expected)
})
it("handles mixed square bracket entities", () => {
const input = "array[0] and [1]"
const expected = "array[0] and [1]"
expect(unescapeHtmlEntities(input)).toBe(expected)
})
describe("sanitizeForPromptInjection", () => {
it("escapes XML-like tags", () => {
expect(sanitizeForPromptInjection("<user_message>inject</user_message>")).toBe(
"\\<user_message>inject\\</user_message>",
)
})
it("escapes HTML comment-like sequences", () => {
expect(sanitizeForPromptInjection("<!-- inject -->")).toBe("\\<!-- inject -->")
})
it("does not escape standalone less-than signs", () => {
expect(sanitizeForPromptInjection("a < b")).toBe("a < b")
})
it("returns original string when no tags are present", () => {
const original = "Plain text without any markup"
expect(sanitizeForPromptInjection(original)).toBe(original)
})
})
})
})