@@ -6,6 +6,129 @@ const TEST_STRINGS = {
6
6
PARAGRAPH_MIXED : 'This is a test paragraph with various symbols: 💥, $$$, 123, and more.' ,
7
7
}
8
8
9
+ const LANGUAGES_TEST_STRINGS = {
10
+ FRENCH_MIXED_SENTENCE : "C'est un test avec des mots français et des symboles: 💥, $$$, 123, et plus. Bonjour!" ,
11
+ SPANISH_MIXED_SENTENCE : 'Este es un test con palabras en español y símbolos: 💥, $$$, 123, y más. ¡Hola!' ,
12
+ GERMAN_MIXED_SENTENCE : 'Das ist ein Test mit deutschen Wörtern und Symbolen: 💥, $$$, 123, und mehr. Hallo!' ,
13
+ ITALIAN_MIXED_SENTENCE : 'Questo è un test con parole in italiano e simboli: 💥, $$$, 123, e altro. Ciao!' ,
14
+ PORTUGUESE_MIXED_SENTENCE : 'Este é um teste com palavras em português e símbolos: 💥, $$$, 123, e mais. Olá!' ,
15
+ }
16
+
17
+ describe ( 'getMatchRegex' , ( ) => {
18
+ it ( 'should handle emojis when Browser supports unicode regex' , ( ) => {
19
+ const matchRegex = getMatchRegex ( )
20
+ if ( matchRegex . flags . includes ( 'gu' ) ) {
21
+ const paragraphMixedMatches = TEST_STRINGS . PARAGRAPH_MIXED . match ( matchRegex )
22
+ expect ( paragraphMixedMatches ) . toContain ( '💥' )
23
+ expect ( paragraphMixedMatches ) . toContain ( '$$$' )
24
+ expect ( paragraphMixedMatches ) . toContain ( '123' )
25
+ }
26
+ } )
27
+
28
+ /**
29
+ * This test is to ensure that the match regex is working as expected in all browsers.
30
+ * With unicode regex, we can support symbols and emojis OOTB.
31
+ * But in older versions of browsers, we need to use a minimal fallback regex which does
32
+ * not support many symbols, to avoid bloating the bundle size.
33
+ *
34
+ * Only European languages (Except Russian) are tested here.
35
+ * We can't test Russian because it's not supported by the fallback regex.
36
+ * Asian languages are not supported by our current tokenizer strategy.
37
+ */
38
+ it ( 'MATCH_REGEX matches words and symbols in TEST_STRINGS' , ( ) => {
39
+ const complexMixedMatches = TEST_STRINGS . COMPLEX_MIXED . match ( getMatchRegex ( ) )
40
+ const expectedComplexMixed = [ 'test' , 'user' , 'name' , 'test' , 'user' , 'id' , 'hello' , 'world' ]
41
+ expectedComplexMixed . forEach ( ( expected ) => {
42
+ expect ( complexMixedMatches ) . toContain ( expected )
43
+ } )
44
+
45
+ const paragraphMixedMatches = TEST_STRINGS . PARAGRAPH_MIXED . match ( getMatchRegex ( ) )
46
+ const expectedParagraphMixed = [ 'This' , 'is' , 'a' , 'test' , 'paragraph' , 'with' , 'various' , 'symbols' , 'and' , 'more' ]
47
+ expectedParagraphMixed . forEach ( ( expected ) => {
48
+ expect ( paragraphMixedMatches ) . toContain ( expected )
49
+ } )
50
+ const frenchMatches = LANGUAGES_TEST_STRINGS . FRENCH_MIXED_SENTENCE . match ( getMatchRegex ( ) )
51
+ const expectedFrench = [
52
+ 'C' ,
53
+ 'est' ,
54
+ 'un' ,
55
+ 'test' ,
56
+ 'avec' ,
57
+ 'des' ,
58
+ 'mots' ,
59
+ 'français' ,
60
+ 'et' ,
61
+ 'des' ,
62
+ 'symboles' ,
63
+ 'et' ,
64
+ 'plus' ,
65
+ 'Bonjour' ,
66
+ ]
67
+ expectedFrench . forEach ( ( expected ) => {
68
+ expect ( frenchMatches ) . toContain ( expected )
69
+ } )
70
+
71
+ const spanishMatches = LANGUAGES_TEST_STRINGS . SPANISH_MIXED_SENTENCE . match ( getMatchRegex ( ) )
72
+ const expectedSpanish = [
73
+ 'Este' ,
74
+ 'es' ,
75
+ 'un' ,
76
+ 'test' ,
77
+ 'con' ,
78
+ 'palabras' ,
79
+ 'en' ,
80
+ 'español' ,
81
+ 'y' ,
82
+ 'símbolos' ,
83
+ 'y' ,
84
+ 'más' ,
85
+ 'Hola' ,
86
+ ]
87
+ expectedSpanish . forEach ( ( expected ) => {
88
+ expect ( spanishMatches ) . toContain ( expected )
89
+ } )
90
+
91
+ const germanMatches = LANGUAGES_TEST_STRINGS . GERMAN_MIXED_SENTENCE . match ( getMatchRegex ( ) )
92
+ const expectedGerman = [
93
+ 'Das' ,
94
+ 'ist' ,
95
+ 'ein' ,
96
+ 'Test' ,
97
+ 'mit' ,
98
+ 'deutschen' ,
99
+ 'Wörtern' ,
100
+ 'und' ,
101
+ 'Symbolen' ,
102
+ 'und' ,
103
+ 'mehr' ,
104
+ 'Hallo' ,
105
+ ]
106
+ expectedGerman . forEach ( ( expected ) => {
107
+ expect ( germanMatches ) . toContain ( expected )
108
+ } )
109
+
110
+ const portugueseMatches = LANGUAGES_TEST_STRINGS . PORTUGUESE_MIXED_SENTENCE . match ( getMatchRegex ( ) )
111
+ const expectedPortuguese = [
112
+ 'Este' ,
113
+ 'é' ,
114
+ 'um' ,
115
+ 'teste' ,
116
+ 'com' ,
117
+ 'palavras' ,
118
+ 'em' ,
119
+ 'português' ,
120
+ 'e' ,
121
+ 'símbolos' ,
122
+ 'e' ,
123
+ 'mais' ,
124
+ 'Olá' ,
125
+ ]
126
+ expectedPortuguese . forEach ( ( expected ) => {
127
+ expect ( portugueseMatches ) . toContain ( expected )
128
+ } )
129
+ } )
130
+ } )
131
+
9
132
describe ( 'createActionAllowList' , ( ) => {
10
133
beforeAll ( ( ) => {
11
134
window . $DD_ALLOW = new Set ( [ TEST_STRINGS . COMPLEX_MIXED , TEST_STRINGS . PARAGRAPH_MIXED ] )
@@ -17,7 +140,7 @@ describe('createActionAllowList', () => {
17
140
18
141
it ( 'should create an action name dictionary' , ( ) => {
19
142
const actionNameDictionary = createActionAllowList ( )
20
- expect ( actionNameDictionary . allowlist . size ) . toBe ( 20 )
143
+ expect ( actionNameDictionary . allowlist . size ) . toBeGreaterThan ( 0 )
21
144
expect ( actionNameDictionary . rawStringIterator ) . toBeDefined ( )
22
145
} )
23
146
@@ -48,41 +171,14 @@ describe('actionNameDictionary processing', () => {
48
171
clearActionNameDictionary ( )
49
172
} )
50
173
51
- it ( 'MATCH_REGEX matches words and symbols in TEST_STRINGS' , ( ) => {
52
- expect ( TEST_STRINGS . COMPLEX_MIXED . match ( getMatchRegex ( ) ) ) . toEqual (
53
- jasmine . arrayContaining ( [ 'test' , 'user' , 'name' , '💥$$$' , 'test' , 'user' , 'id' , 'hello' , '>=42' , 'world' ] )
54
- )
55
- expect ( TEST_STRINGS . PARAGRAPH_MIXED . match ( getMatchRegex ( ) ) ) . toEqual (
56
- jasmine . arrayContaining ( [
57
- 'This' ,
58
- 'is' ,
59
- 'a' ,
60
- 'test' ,
61
- 'paragraph' ,
62
- 'with' ,
63
- 'various' ,
64
- 'symbols' ,
65
- '💥' ,
66
- '$$$' ,
67
- '123' ,
68
- 'and' ,
69
- 'more' ,
70
- ] )
71
- )
72
- } )
73
-
74
174
it ( 'initializes allowlist with normalized words from $DD_ALLOW' , ( ) => {
75
- // EMOJI and EMOJI_WITH_NUMBERS
76
- expect ( actionNameDictionary . allowlist . has ( '123' ) ) . toBeTrue ( )
77
- // COMPLEX_MIXED
78
175
expect ( actionNameDictionary . allowlist . has ( 'test' ) ) . toBeTrue ( )
79
176
expect ( actionNameDictionary . allowlist . has ( 'hello' ) ) . toBeTrue ( )
80
- expect ( actionNameDictionary . allowlist . has ( '>=42' ) ) . toBeTrue ( )
81
177
expect ( actionNameDictionary . allowlist . has ( 'world' ) ) . toBeTrue ( )
82
178
} )
83
179
84
180
it ( 'updates dictionary when $DD_ALLOW changes' , ( ) => {
85
- expect ( actionNameDictionary . allowlist . size ) . toBe ( 20 )
181
+ const initialAllowlistSize = actionNameDictionary . allowlist . size
86
182
87
183
// Simulate a change in $DD_ALLOW
88
184
window . $DD_ALLOW ?. add ( 'new-Word' )
@@ -95,7 +191,7 @@ describe('actionNameDictionary processing', () => {
95
191
expect ( actionNameDictionary . allowlist . has ( 'new' ) ) . toBeTrue ( )
96
192
expect ( actionNameDictionary . allowlist . has ( 'another' ) ) . toBeTrue ( )
97
193
// Old words should still be present
98
- expect ( actionNameDictionary . allowlist . size ) . toBe ( 23 )
194
+ expect ( actionNameDictionary . allowlist . size ) . toBe ( initialAllowlistSize + 3 )
99
195
} )
100
196
} )
101
197
@@ -122,12 +218,23 @@ describe('maskActionName', () => {
122
218
} )
123
219
124
220
it ( 'masks words not in allowlist (with dictionary from $DD_ALLOW)' , ( ) => {
221
+ const matchRegex = getMatchRegex ( )
222
+ let expected = 'test-💥-xxxxxx-xxx'
223
+ if ( ! matchRegex . flags . includes ( 'gu' ) ) {
224
+ expected = 'test-💥-$>xxxx-xxx'
225
+ }
226
+
125
227
const testString1 = maskActionName ( 'test-💥-$>=123-pii' , actionNameDictionary . allowlist )
126
228
expect ( testString1 . masked ) . toBeTrue ( )
127
- expect ( testString1 . name ) . toBe ( 'test-💥-***-***' )
229
+ expect ( testString1 . name ) . toBe ( expected )
230
+
231
+ expected = 'test-xxxxxx*hello xxxx'
232
+ if ( ! matchRegex . flags . includes ( 'gu' ) ) {
233
+ expected = 'test-💥xxxx*hello xxxx'
234
+ }
128
235
const testString2 = maskActionName ( 'test-💥+123*hello wild' , actionNameDictionary . allowlist )
129
236
expect ( testString2 . masked ) . toBeTrue ( )
130
- expect ( testString2 . name ) . toBe ( 'test-****hello ***' )
237
+ expect ( testString2 . name ) . toBe ( expected )
131
238
} )
132
239
133
240
it ( 'handles empty string' , ( ) => {
0 commit comments