1
+ import { expect } from "chai" ;
2
+ import { describe , it , beforeEach , afterEach } from "mocha" ;
3
+ import { transformValue , replaceAllOccurrences , getValuesToBeTransformed } from "../../../telemetry/utils" ;
4
+ import * as sinon from 'sinon' ;
5
+ import * as os from 'os' ;
6
+
7
+ const replacedValue = "_REM_" ;
8
+ const propertiesToTransform = [ "message" , "b" , "c" , "d" ] ;
9
+
10
+ describe ( "transformValue()" , ( ) => {
11
+ it ( "replaces top-level string if key is in propertiesToTransform" , ( ) => {
12
+ const result = transformValue ( "message" , [ "john" ] , propertiesToTransform , replacedValue , "hello john" ) ;
13
+ expect ( result ) . to . equal ( `hello ${ replacedValue } ` ) ;
14
+ } ) ;
15
+
16
+ it ( "does not replace if key is not in propertiesToTransform" , ( ) => {
17
+ const result = transformValue ( "greeting" , [ "john" ] , propertiesToTransform , replacedValue , "hello john" ) ;
18
+ expect ( result ) . to . equal ( "hello john" ) ;
19
+ } ) ;
20
+
21
+ it ( "replaces nested name values for matching keys only" , ( ) => {
22
+ const input = {
23
+ b : "john is here" ,
24
+ c : [ "john" , { d : "also john" } ]
25
+ } ;
26
+
27
+ const result = transformValue ( "a" , [ "john" ] , propertiesToTransform , replacedValue , input ) ;
28
+
29
+ expect ( result . b ) . to . equal ( `${ replacedValue } is here` ) ;
30
+ expect ( result . c [ 0 ] ) . to . equal ( `${ replacedValue } ` ) ;
31
+ expect ( result . c [ 1 ] . d ) . to . equal ( `also ${ replacedValue } ` ) ;
32
+ } ) ;
33
+
34
+ it ( "handles keys with deep nesting" , ( ) => {
35
+ const blocked = [ "john" , "doe" ] ;
36
+ const data = {
37
+ level1 : {
38
+ message : "john was here" ,
39
+ test : "john was here" ,
40
+ b : [ "hello john" , "doe speaks" , "java" ] ,
41
+ nestedArray : [
42
+ {
43
+ b : "john and doe" ,
44
+ other : "keep this"
45
+ } ,
46
+ {
47
+ b : "john is safe" ,
48
+ d : [ "john doe" , { d : "doe and john" } ]
49
+ }
50
+ ]
51
+ } ,
52
+ c : [
53
+ {
54
+ d : "test john" ,
55
+ random : "doe"
56
+ } ,
57
+ {
58
+ a : {
59
+ b : "john"
60
+ } ,
61
+ b : "john and jane" ,
62
+ d : null
63
+ }
64
+ ] ,
65
+ extra : "nothing here"
66
+ } ;
67
+
68
+ const result = transformValue ( null , blocked , propertiesToTransform , replacedValue , data ) ;
69
+
70
+ expect ( result . level1 . message ) . to . equal ( `${ replacedValue } was here` ) ;
71
+ expect ( result . level1 . test ) . to . equal ( "john was here" ) ;
72
+ expect ( result . level1 . b ) . to . deep . equal ( [ `hello ${ replacedValue } ` , `${ replacedValue } speaks` , "java" ] ) ;
73
+ expect ( result . level1 . nestedArray [ 0 ] . b ) . to . equal ( `${ replacedValue } and ${ replacedValue } ` ) ;
74
+ expect ( result . level1 . nestedArray [ 0 ] . other ) . to . equal ( "keep this" ) ;
75
+
76
+ expect ( result . level1 . nestedArray [ 1 ] . b ) . to . equal ( `${ replacedValue } is safe` ) ;
77
+ expect ( result . level1 . nestedArray [ 1 ] . d [ 0 ] ) . to . equal ( `${ replacedValue } ${ replacedValue } ` ) ;
78
+ expect ( result . level1 . nestedArray [ 1 ] . d [ 1 ] . d ) . to . equal ( `${ replacedValue } and ${ replacedValue } ` ) ;
79
+
80
+ expect ( result . c [ 0 ] . d ) . to . equal ( `test ${ replacedValue } ` ) ;
81
+ expect ( result . c [ 0 ] . random ) . to . equal ( "doe" ) ;
82
+
83
+ expect ( result . c [ 1 ] . a . b ) . to . equal ( `${ replacedValue } ` ) ;
84
+ expect ( result . c [ 1 ] . b ) . to . equal ( `${ replacedValue } and jane` ) ;
85
+ expect ( result . c [ 1 ] . d ) . to . equal ( null ) ;
86
+
87
+ expect ( result . extra ) . to . equal ( "nothing here" ) ;
88
+ } ) ;
89
+
90
+ it ( "does not transform primitive values" , ( ) => {
91
+ const data = {
92
+ count : 5 ,
93
+ flag : true ,
94
+ nothing : null
95
+ } ;
96
+ const result = transformValue ( null , [ "john" ] , propertiesToTransform , replacedValue , data ) ;
97
+
98
+ expect ( result . count ) . to . equal ( 5 ) ;
99
+ expect ( result . flag ) . to . equal ( true ) ;
100
+ expect ( result . nothing ) . to . equal ( null ) ;
101
+ } ) ;
102
+
103
+ it ( "returns original string if no blocked values" , ( ) => {
104
+ const result = transformValue ( "message" , [ ] , propertiesToTransform , replacedValue , "hello john" ) ;
105
+ expect ( result ) . to . equal ( "hello john" ) ;
106
+ } ) ;
107
+ } ) ;
108
+
109
+ describe ( "replaceAllOccurrences()" , ( ) => {
110
+ it ( "replaces all non-overlapping occurrences" , ( ) => {
111
+ const result = replaceAllOccurrences ( "john is john" , "john" , "_REM_" ) ;
112
+ expect ( result ) . to . equal ( "_REM_ is _REM_" ) ;
113
+ } ) ;
114
+
115
+ it ( "does not change string if valueString not found" , ( ) => {
116
+ const result = replaceAllOccurrences ( "hello world" , "john" , "_REM_" ) ;
117
+ expect ( result ) . to . equal ( "hello world" ) ;
118
+ } ) ;
119
+
120
+ it ( "replaces multiple adjacent matches" , ( ) => {
121
+ const result = replaceAllOccurrences ( "johnjohnjohn" , "john" , "_REM_" ) ;
122
+ expect ( result ) . to . equal ( "_REM__REM__REM_" ) ;
123
+ } ) ;
124
+
125
+ it ( "returns original string if valueString is empty" , ( ) => {
126
+ const result = replaceAllOccurrences ( "john is here" , "" , "_REM_" ) ;
127
+ expect ( result ) . to . equal ( "john is here" ) ;
128
+ } ) ;
129
+
130
+ it ( "can replace special characters (treated literally)" , ( ) => {
131
+ const result = replaceAllOccurrences ( "price is $5.00 and $5.00" , "$5.00" , "_REM_" ) ;
132
+ expect ( result ) . to . equal ( "price is _REM_ and _REM_" ) ;
133
+ } ) ;
134
+
135
+ it ( "is case-sensitive by default" , ( ) => {
136
+ const result = replaceAllOccurrences ( "John john JOHN" , "john" , "_REM_" ) ;
137
+ expect ( result ) . to . equal ( "John _REM_ JOHN" ) ;
138
+ } ) ;
139
+
140
+ it ( "handles overlapping cases (only first per loop)" , ( ) => {
141
+ const result = replaceAllOccurrences ( "aaa" , "aa" , "_X_" ) ;
142
+ expect ( result ) . to . equal ( "_X_a" ) ;
143
+ } ) ;
144
+ } ) ;
145
+
146
+ describe ( "transformValue() with getValuesToBeTransformed()" , ( ) => {
147
+ let stub : sinon . SinonStub ;
148
+
149
+ beforeEach ( ( ) => {
150
+ stub = sinon . stub ( os , "userInfo" ) . returns ( { username : "john" } as os . UserInfo < string > ) ;
151
+ } ) ;
152
+
153
+ afterEach ( ( ) => {
154
+ stub . restore ( ) ;
155
+ } ) ;
156
+
157
+ it ( "transforms string using values from getValuesToBeTransformed" , ( ) => {
158
+ const blocked = getValuesToBeTransformed ( ) ;
159
+
160
+ const result = transformValue ( "message" , blocked , propertiesToTransform , replacedValue , "hello john" ) ;
161
+
162
+ expect ( result ) . to . equal ( `hello ${ replacedValue } ` ) ;
163
+ } ) ;
164
+
165
+ it ( "does not transform if key is not in propertiesToTransform" , ( ) => {
166
+ const blocked = getValuesToBeTransformed ( ) ;
167
+
168
+ const result = transformValue ( "greeting" , blocked , propertiesToTransform , replacedValue , "hello john" ) ;
169
+
170
+ expect ( result ) . to . equal ( "hello john" ) ;
171
+ } ) ;
172
+
173
+ it ( "filters ignored and short env values correctly" , ( ) => {
174
+ const testEnv = {
175
+ SUDO_USER : "ja" ,
176
+ C9_USER : "java" ,
177
+ LOGNAME : "john" ,
178
+ USER : "oracle" ,
179
+ LNAME : "ab" ,
180
+ USERNAME : "johndoe" ,
181
+ HOSTNAME : undefined ,
182
+ COMPUTERNAME : "user" ,
183
+ NAME : "vscode"
184
+ } ;
185
+
186
+ const stubbed = sinon . stub ( process , "env" ) . value ( testEnv as any ) ;
187
+ stub . restore ( ) ;
188
+
189
+ const values = getValuesToBeTransformed ( ) ;
190
+
191
+ expect ( values ) . to . include ( "john" ) ;
192
+ expect ( values ) . to . include ( "johndoe" ) ;
193
+ expect ( values ) . to . not . include ( "java" ) ;
194
+ expect ( values ) . to . not . include ( "user" ) ;
195
+ expect ( values ) . to . not . include ( "ab" ) ;
196
+ expect ( values ) . to . not . include ( "vscode" ) ;
197
+
198
+ stubbed . restore ( ) ;
199
+ } ) ;
200
+ } ) ;
0 commit comments