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