Path: blob/main/extensions/copilot/src/extension/renameSuggestions/test/node/renameSuggestionsProvider.spec.tsx
13405 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 { outdent } from 'outdent';6import { expect, suite, test } from 'vitest';7import { RenameSuggestionsProvider } from '../../node/renameSuggestionsProvider';89suite('processReply', () => {10test('should handle JSON array of strings', () => {11const input = `["Hello", "world", "howAreYou?"]`;12const output = RenameSuggestionsProvider.parseResponse(input);13expect(output).toMatchInlineSnapshot(`14{15"redundantCharCount": 0,16"replyFormat": "jsonStringArray",17"symbolNames": [18"Hello",19"world",20"howAreYou?",21],22}23`);24});2526test('should handle ordered list with .', () => {27const input = `281. Hello292. world303. howAreYou?31`;32const output = RenameSuggestionsProvider.parseResponse(input);33expect(output).toMatchInlineSnapshot(`34{35"redundantCharCount": 21,36"replyFormat": "list",37"symbolNames": [38"Hello",39"world",40"howAreYou?",41],42}43`);44});4546test('should handle ordered list with )', () => {47const input = `481) Hello492) world503) howAreYou?51`;52const output = RenameSuggestionsProvider.parseResponse(input);53expect(output).toMatchInlineSnapshot(`54{55"redundantCharCount": 21,56"replyFormat": "list",57"symbolNames": [58"Hello",59"world",60"howAreYou?",61],62}63`);64});6566test('should handle unordered list with *', () => {67const input = `68* Hello69* world70* howAreYou?71`;72const output = RenameSuggestionsProvider.parseResponse(input);73expect(output).toMatchInlineSnapshot(`74{75"redundantCharCount": 18,76"replyFormat": "list",77"symbolNames": [78"Hello",79"world",80"howAreYou?",81],82}83`);84});8586test('should handle unordered list with -', () => {87const input = `88- Hello89- world90- howAreYou?91`;92const output = RenameSuggestionsProvider.parseResponse(input);93expect(output).toMatchInlineSnapshot(`94{95"redundantCharCount": 18,96"replyFormat": "list",97"symbolNames": [98"Hello",99"world",100"howAreYou?",101],102}103`);104});105106test('should handle invalid input', () => {107const input = 'Hello world howAreYou?';108const output = RenameSuggestionsProvider.parseResponse(input);109expect(output).toMatchInlineSnapshot(`110{111"redundantCharCount": 22,112"replyFormat": "unknown",113"symbolNames": [],114}115`);116});117118test('should handle empty input', () => {119const input = '';120const output = RenameSuggestionsProvider.parseResponse(input);121expect(output).toMatchInlineSnapshot(`122{123"redundantCharCount": 0,124"replyFormat": "unknown",125"symbolNames": [],126}127`);128});129130test('should handle free response', () => {131const input = outdent`132\`\`\`133// FILEPATH: /Users/foo/bar.ts134135private static _determinePrefix(name: string): string | undefined {136const prefix = name.match(/^([\\.\$\\_]+)/)?.[0];137return prefix;138}139\`\`\`140141The purpose of the \`_determinePrefix\` function is to extract the prefix from a given name string. It uses a regular expression to match any characters at the beginning of the string that are either a dot (.), dollar sign ($), or underscore (_), and returns that matched prefix.142143Here are some new names that reflect the purpose of the \`_determinePrefix\` function:144145\`\`\`json146[147"_extractPrefix",148"_findPrefix",149"_getPrefix",150"_identifyPrefix",151"_parsePrefix"152]153\`\`\`154`;155const output = RenameSuggestionsProvider.parseResponse(input);156expect(output).toMatchInlineSnapshot(`157{158"redundantCharCount": 552,159"replyFormat": "multiJsonStringArray",160"symbolNames": [161"_extractPrefix",162"_findPrefix",163"_getPrefix",164"_identifyPrefix",165"_parsePrefix",166],167}168`);169});170171// TODO@ulugbekna: handle this case172test('JSON object', () => {173const input = outdent`174{175"1": "fooBar",176"2": "bazQux",177"3": "steamBear",178}179`;180const output = RenameSuggestionsProvider.parseResponse(input);181expect(output).toMatchInlineSnapshot(`182{183"redundantCharCount": 54,184"replyFormat": "unknown",185"symbolNames": [],186}187`);188});189190test('handle free form + ordered list', () => {191const input = outdent`192Based on the name and the role it plays in the code, \`Baz\` is likely a class responsible for initializing or setting up the baz service.193194Here are a few alternative names that could reflect the purpose and functionality of \`Baz\`:1951961. \`FooBar\`1972. \`FooBarBaz\`1983. \`BarBazBear\`199`;200const output = RenameSuggestionsProvider.parseResponse(input);201expect(output).toMatchInlineSnapshot(`202{203"redundantCharCount": 248,204"replyFormat": "list",205"symbolNames": [206"FooBar",207"FooBarBaz",208"BarBazBear",209],210}211`);212});213});214215suite('preprocessSymbolNames', () => {216test('to camelCase', () => {217const result = RenameSuggestionsProvider.preprocessSymbolNames({218currentSymbolName: 'camelCase',219newSymbolNames: ['snake_case', 'kebab-case', 'PascalCase', 'UPPER_SNAKE_CASE', 'lower-kebab-case'],220languageId: 'javascript',221});222expect(result).toEqual(['snakeCase', 'kebabCase', 'pascalCase', 'upperSnakeCase', 'lowerKebabCase']);223});224225test('to snake_case', () => {226const result = RenameSuggestionsProvider.preprocessSymbolNames({227currentSymbolName: 'snake_case',228newSymbolNames: ['camelCase', 'kebab-case', 'PascalCase', 'UPPER_SNAKE_CASE', 'lower-kebab-case'],229languageId: 'python',230});231expect(result).toEqual(['camel_case', 'kebab_case', 'pascal_case', 'upper_snake_case', 'lower_kebab_case']);232});233234test('to PascalCase', () => {235const result = RenameSuggestionsProvider.preprocessSymbolNames({236currentSymbolName: 'PascalCase',237newSymbolNames: ['camelCase', 'kebab-case', 'snake_case', 'UPPER_SNAKE_CASE', 'lower-kebab-case'],238languageId: 'javascript',239});240expect(result).toEqual(['CamelCase', 'KebabCase', 'SnakeCase', 'UpperSnakeCase', 'LowerKebabCase']);241});242243test('to kebab-case', () => {244const result = RenameSuggestionsProvider.preprocessSymbolNames({245currentSymbolName: 'kebab-case',246newSymbolNames: ['camelCase', 'snake_case', 'PascalCase', 'UPPER_SNAKE_CASE', 'lower-kebab-case'],247languageId: 'javascript',248});249expect(result).toEqual(['camel-case', 'snake-case', 'pascal-case', 'upper-snake-case', 'lower-kebab-case']);250});251252test('as is', () => {253const result = RenameSuggestionsProvider.preprocessSymbolNames({254currentSymbolName: 'Unknown_Format',255newSymbolNames: ['camelCase', 'snake_case', 'PascalCase', 'UPPER_SNAKE_CASE', 'lower-kebab-case'],256languageId: 'javascript',257});258expect(result).toEqual(['camelCase', 'snake_case', 'PascalCase', 'UPPER_SNAKE_CASE', 'lower-kebab-case']);259});260});261262263