Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/util/node/test/anomalyDetection.spec.ts
13401 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import assert from 'assert';
7
import { suite, test } from 'vitest';
8
import { isRepetitive } from '../../common/anomalyDetection';
9
10
suite('Anomaly Repetition Tests', function () {
11
test('recognizes sequence consisting of single repeated token', function () {
12
const tokens = 'Bar Bar Bar Bar Bar Bar Bar Bar Bar Bar Bar Bar Bar Bar'.split(' ');
13
const repetitive = isRepetitive(tokens);
14
assert.strictEqual(repetitive, true, 'Repetition should be recognized.');
15
});
16
17
test('does nothing on a too short sequence of single repeated token', function () {
18
const tokens = 'Bar Bar Bar Bar Bar Bar Bar Bar Bar'.split(' ');
19
const repetitive = isRepetitive(tokens);
20
assert.strictEqual(repetitive, false, 'Repetition should not be recognized.');
21
});
22
23
test('recognizes single repeated token in proper suffix', function () {
24
const tokens = 'Baz Baz Baz Bar Bar Bar Bar Bar Bar Bar Bar Bar Bar Bar Bar Bar Bar'.split(' ');
25
const repetitive = isRepetitive(tokens);
26
assert.strictEqual(repetitive, true, 'Repetition should be recognized.');
27
});
28
29
test('recognizes repeated pattern', function () {
30
const tokens = (
31
'Bar Far Car Bar Far Car Bar Far Car Bar Far Car Bar Far Car Bar Far Car ' +
32
'Bar Far Car Bar Far Car Bar Far Car Bar Far Car Bar Far Car Bar Far Car'
33
).split(' ');
34
const repetitive = isRepetitive(tokens);
35
assert.strictEqual(repetitive, true, 'Repetition should be recognized.');
36
});
37
38
test('does nothing on a too short repeated pattern', function () {
39
const tokens = (
40
'Bar Far Car Bar Far Car Bar Far Car Bar Far Car Bar Far Car Bar Far Car ' +
41
'Bar Far Car Bar Far Car Bar Far Car'
42
).split(' ');
43
const repetitive = isRepetitive(tokens);
44
assert.strictEqual(repetitive, false, 'Repetition should not be recognized.');
45
});
46
47
test('does nothing in absence of a pattern', function () {
48
const tokens = (
49
'12 1 23 43 ac er gf gf 12 er gd 34 dg 35 ;o lo 34 xc ' +
50
'4t ggf gf 46 l7 dg qs 5y ku df 34 gr gr gr df er gr gr'
51
).split(' ');
52
const repetitive = isRepetitive(tokens);
53
assert.strictEqual(repetitive, false, 'No repetition should be claimed.');
54
});
55
56
test('does nothing on too long a pattern', function () {
57
const tokens = '12 1 23 43 ac er gf gf 12 er gd '.repeat(4).split(' ');
58
const repetitive = isRepetitive(tokens);
59
assert.strictEqual(repetitive, false, 'No repetition should be claimed.');
60
});
61
62
test('recognizes short real world example', function () {
63
const tokens = [
64
'C',
65
' LIM',
66
'IT',
67
' 1',
68
')',
69
'\n',
70
'\t',
71
'\t',
72
'\t',
73
'\t',
74
'\t',
75
'\t',
76
'\t',
77
'\t',
78
'\t',
79
'\t',
80
'\t',
81
'\t',
82
'\t',
83
'\t',
84
'\t',
85
];
86
const repetitive = isRepetitive(tokens);
87
assert.strictEqual(repetitive, true, 'Repetition should be found.');
88
});
89
90
test('recognizes long real world example', function () {
91
const tokens =
92
'Try to use the keyboard to navigate the website. Try to use the keyboard to navigate the website. Try to use the keyboard to navigate the website. Try to use the keyboard to navigate the website. Try to use the keyboard to navigate the website. Try to use the keyboard to navigate the website. Try to use the keyboard to navigate the website. Try to use the keyboard to navigate the website. Try to use the keyboard to navigate the website. Try to use the keyboard to navigate the website. Try to use the keyboard to navigate the website. Try to use the keyboard to navigate the website. Try to use the keyboard to navigate the website. Try to use the keyboard to navigate the website. Try to use the keyboard to navigate the website. Try to use the keyboard to navigate the website. Try to use the keyboard to navigate the website. Try to use the keyboard to navigate the website. Try to use the keyboard to navigate the website. Try to use the keyboard to navigate the website. Try to use the keyboard to navigate the website. Try to use the keyboard to navigate the website. Try to use the keyboard to navigate the website. Try to use the keyboard to navigate the website. Try to use the keyboard to navigate the website. Try to use the keyboard to navigate the website. Try to use the keyboard to navigate the website. Try to use the keyboard to navigate the website. Try to use the keyboard to navigate the website. Try to use the keyboard to navigate the website. Try to use the keyboard to navigate the website. Try to use the keyboard to navigate the website. Try to use the keyboard to navigate the website. Try to use the keyboard to navigate the website. Try to use the keyboard to navigate the website. Try to use the keyboard to navigate the website. Try to use the keyboard to navigate the website. Try to use the keyboard to navigate the website. Try to use the keyboard to navigate the website. Try to use the keyboard to navigate the website. Try to use the keyboard to navigate the website. Try to use the keyboard to navigate the website. Try to use the keyboard to navigate the website. Try to use the keyboard to navigate the website. Try to use the keyboard to navigate the website. Try to use the keyboard to navigate the website. Try to use the keyboard to navigate the website. Try to use the keyboard to navigate the'.split(
93
' '
94
);
95
const repetitive = isRepetitive(tokens);
96
assert.strictEqual(repetitive, true, 'Repetition should be found.');
97
});
98
99
test('recognizes repetitions with some prefix', function () {
100
const tokens = ['prefix', 'foo', 'foo', 'foo', 'foo', 'foo', 'foo', 'foo', 'foo', 'foo', 'foo'];
101
const repetitive = isRepetitive(tokens);
102
assert.strictEqual(repetitive, true, 'Repetition should be found.');
103
});
104
105
test('recognizes repetitions that differ only in whitespace tokens, with some prefix', function () {
106
const tokens = ['prefix', 'foo', 'foo', 'foo', 'foo', 'foo', 'foo', 'foo', 'foo', 'foo', ' ', 'foo'];
107
const repetitive = isRepetitive(tokens);
108
assert.strictEqual(repetitive, true, 'Repetition should be found.');
109
});
110
111
test('vscode-copilot-release #1662', function () {
112
/**
113
* This is a tokenized repetition of the following sequence:
114
"browser.safebrowsing.provider.google.reportURL": "",
115
"browser.safebrowsing.provider.google.reportPhishMistakeURL": "",
116
"browser.safebrowsing.provider.google.reportMalwareMistakeURL": "",
117
*/
118
const tokens = [
119
'"',
120
'browser',
121
'.s',
122
'af',
123
'eb',
124
'rows',
125
'ing',
126
'.provider',
127
'.google',
128
'.report',
129
'URL',
130
'":',
131
' "",',
132
'"',
133
'browser',
134
'.s',
135
'af',
136
'eb',
137
'rows',
138
'ing',
139
'.provider',
140
'.google',
141
'.report',
142
'Ph',
143
'ish',
144
'Mist',
145
'ake',
146
'URL',
147
'":',
148
' "",',
149
'"',
150
'browser',
151
'.s',
152
'af',
153
'eb',
154
'rows',
155
'ing',
156
'.provider',
157
'.google',
158
'.report',
159
'Mal',
160
'ware',
161
'Mist',
162
'ake',
163
'URL',
164
'":',
165
' "",',
166
'"',
167
'browser',
168
'.s',
169
'af',
170
'eb',
171
'rows',
172
'ing',
173
'.provider',
174
'.google',
175
'.report',
176
'URL',
177
'":',
178
' "",',
179
'"',
180
'browser',
181
'.s',
182
'af',
183
'eb',
184
'rows',
185
'ing',
186
'.provider',
187
'.google',
188
'.report',
189
'Ph',
190
'ish',
191
'Mist',
192
'ake',
193
'URL',
194
'":',
195
' "",',
196
'"',
197
'browser',
198
'.s',
199
'af',
200
'eb',
201
'rows',
202
'ing',
203
'.provider',
204
'.google',
205
'.report',
206
'Mal',
207
'ware',
208
'Mist',
209
'ake',
210
'URL',
211
'":',
212
' "",',
213
'"',
214
'browser',
215
'.s',
216
'af',
217
'eb',
218
'rows',
219
'ing',
220
'.provider',
221
'.google',
222
'.report',
223
'URL',
224
'":',
225
' "",',
226
'"',
227
'browser',
228
'.s',
229
'af',
230
'eb',
231
'rows',
232
'ing',
233
'.provider',
234
'.google',
235
'.report',
236
'Ph',
237
'ish',
238
'Mist',
239
'ake',
240
'URL',
241
'":',
242
' "",',
243
'"',
244
'browser',
245
'.s',
246
'af',
247
'eb',
248
'rows',
249
'ing',
250
'.provider',
251
'.google',
252
'.report',
253
'Mal',
254
'ware',
255
'Mist',
256
'ake',
257
'URL',
258
'":',
259
' "",',
260
'"',
261
'browser',
262
'.s',
263
'af',
264
'eb',
265
'rows',
266
'ing',
267
'.provider',
268
'.google',
269
'.report',
270
'URL',
271
'":',
272
' "",',
273
'"',
274
'browser',
275
'.s',
276
'af',
277
'eb',
278
'rows',
279
'ing',
280
'.provider',
281
'.google',
282
'.report',
283
'Ph',
284
'ish',
285
'Mist',
286
'ake',
287
'URL',
288
'":',
289
' "",',
290
'"',
291
'browser',
292
'.s',
293
'af',
294
'eb',
295
'rows',
296
'ing',
297
'.provider',
298
'.google',
299
'.report',
300
'Mal',
301
'ware',
302
'Mist',
303
'ake',
304
'URL',
305
'":',
306
' "",'
307
];
308
const repetitive = isRepetitive(tokens);
309
assert.strictEqual(repetitive, true, 'Repetition should be found.');
310
});
311
});
312
313