Path: blob/main/extensions/copilot/src/extension/prompts/node/test/fixtures/pseudoStartStopConversationCallbackTest.ts
13406 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation and GitHub. All rights reserved.2*--------------------------------------------------------------------------------------------*/34import assert from 'assert';5import type { ChatAgentContent, ChatAgentExtendedProgress, ChatAgentVulnerability } from 'vscode';6import { IResponseDelta } from '../../openai/fetch';7import { PseudoStopStartConversationCallback } from '../pseudoStartStopConversationCallback';8import sinon = require('sinon');910suite('Post Report Conversation Callback', () => {11const postReportFn = (deltas: IResponseDelta[]) => {12return ['<processed>', ...deltas.map(d => d.text), '</processed>'];13};14const annotations = [{ id: 123, details: { type: 'type', description: 'description' } }, { id: 456, details: { type: 'type2', description: 'description2' } }];1516test('Simple post-report', async () => {17const progress: ChatAgentExtendedProgress[] = [];18const testObj = new PseudoStopStartConversationCallback(19[{20start: 'end',21stop: 'start'22}],23{ report: p => progress.push(p) },24postReportFn25);2627testObj.apply('', { text: 'one' });28testObj.apply('', { text: ' start ' });29testObj.apply('', { text: 'two' });30testObj.apply('', { text: ' end' });3132assert.strictEqual(await testObj.appliedText, 'one start two end');3334assert.deepStrictEqual(35progress.map(p => (p as ChatAgentContent).content),36['one', ' ', '<processed>', ' ', 'two', ' ', '</processed>']);37});3839test('Partial stop word with extra text before', async () => {40const progress: ChatAgentExtendedProgress[] = [];41const testObj = new PseudoStopStartConversationCallback(42[{43start: 'end',44stop: 'start'45}],46{ report: p => progress.push(p) },47postReportFn48);4950testObj.apply('', { text: 'one sta' });51testObj.apply('', { text: 'rt' });52testObj.apply('', { text: ' two end' });5354assert.deepStrictEqual(55progress.map(p => (p as ChatAgentContent).content),56['one ', '<processed>', ' two ', '</processed>']57);58assert.strictEqual(await testObj.appliedText, 'one start two end');59});6061test('Partial stop word with extra text after', async () => {62const progress: ChatAgentExtendedProgress[] = [];63const testObj = new PseudoStopStartConversationCallback(64[{65start: 'end',66stop: 'start'67}],68{ report: p => progress.push(p) },69postReportFn70);7172testObj.apply('', { text: 'one ', annotations });73testObj.apply('', { text: 'sta' });74testObj.apply('', { text: 'rt two' });75testObj.apply('', { text: ' end' });7677assert.strictEqual(await testObj.appliedText, 'one start two end');78assert.deepStrictEqual((progress[0] as ChatAgentContent).vulnerabilities, annotations.map(a => ({ title: a.details.type, description: a.details.description } satisfies ChatAgentVulnerability)));7980assert.deepStrictEqual(81progress.map(p => (p as ChatAgentContent).content),82['one ', '<processed>', ' two', ' ', '</processed>']);83});8485test('no second stop word', async () => {86const progress: ChatAgentExtendedProgress[] = [];87const testObj = new PseudoStopStartConversationCallback(88[{89start: 'end',90stop: 'start'91}],92{ report: p => progress.push(p) },93postReportFn94);9596testObj.apply('', { text: 'one' });97testObj.apply('', { text: ' start ' });98testObj.apply('', { text: 'two' });99testObj.apply('', { text: ' ' });100101assert.strictEqual(await testObj.appliedText, 'one start two ');102assert.deepStrictEqual(103progress.map(p => (p as ChatAgentContent).content),104['one', ' ']);105});106107test('Text on same line as start', async () => {108const progress: ChatAgentExtendedProgress[] = [];109const testObj = new PseudoStopStartConversationCallback(110[{111start: 'end',112stop: 'start'113}],114{ report: p => progress.push(p) },115postReportFn116);117118testObj.apply('', { text: 'this is test text\n\n' });119testObj.apply('', { text: 'eeep start\n\n' });120testObj.apply('', { text: 'test test test test 123456' });121testObj.apply('', { text: 'end\n\nhello' });122123assert.strictEqual(await testObj.appliedText, 'this is test text\n\neeep start\n\ntest test test test 123456end\n\nhello');124125assert.deepStrictEqual((progress[0] as ChatAgentContent).content, 'this is test text\n\n');126assert.deepStrictEqual((progress[1] as ChatAgentContent).content, 'eeep ');127assert.deepStrictEqual((progress[2] as ChatAgentContent).content, '<processed>');128assert.deepStrictEqual((progress[3] as ChatAgentContent).content, '\n\n');129assert.deepStrictEqual((progress[4] as ChatAgentContent).content, 'test test test test 123456');130assert.deepStrictEqual((progress[5] as ChatAgentContent).content, '</processed>');131assert.deepStrictEqual((progress[6] as ChatAgentContent).content, '\n\nhello');132});133134135test('Start word without a stop word', () => {136const progress: ChatAgentExtendedProgress[] = [];137const testObj = new PseudoStopStartConversationCallback(138[{139start: '[RESPONSE END]',140stop: '[RESPONSE START]'141}],142{ report: p => progress.push(p) },143postReportFn144);145146testObj.apply('', { text: `I'm sorry, but as an AI programming assistant, I'm here to provide assistance with software development topics, specifically related to Visual Studio Code. I'm not equipped to provide a definition of a computer. [RESPONSE END]` });147148assert.strictEqual((progress[0] as ChatAgentContent).content, `I'm sorry, but as an AI programming assistant, I'm here to provide assistance with software development topics, specifically related to Visual Studio Code. I'm not equipped to provide a definition of a computer. [RESPONSE END]`);149});150151teardown(() => sinon.restore());152});153154155