Path: blob/main/extensions/copilot/src/extension/prompt/test/common/streamingGrammar.spec.ts
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 { describe, expect, it } from 'vitest';6import { StreamingGrammar } from '../../common/streamingGrammar';78const enum State {9Initial,10State1,11State2,12State3,13}1415describe('StreamingGrammar', () => {16it('should initialize with the correct state', () => {17const grammar = new StreamingGrammar(State.Initial, {18[State.Initial]: { 'token1': State.State1 },19[State.State1]: { 'token2': State.State2 },20});21expect(grammar.state).to.equal(State.Initial);22});2324it('should transition states correctly', () => {25const grammar = new StreamingGrammar(State.Initial, {26[State.Initial]: { 'token1': State.State1 },27[State.State1]: { 'token2': State.State2 },28});2930grammar.append('token1');31expect(grammar.state).to.equal(State.State1);32expect(grammar.tokens).to.deep.equal([33{ state: State.Initial, token: 'token1', transitionTo: State.State1 }34]);3536grammar.append('token2');37expect(grammar.state).to.equal(State.State2);38expect(grammar.tokens).to.deep.equal([39{ state: State.Initial, token: 'token1', transitionTo: State.State1 },40{ state: State.State1, token: 'token2', transitionTo: State.State2 }41]);42});4344it('should accumulate text correctly', () => {45const grammar = new StreamingGrammar(State.Initial, {46[State.Initial]: { 'token1': State.State1 },47[State.State1]: { 'token2': State.State2 },48});4950grammar.append('some text');51grammar.append(' to');52grammar.append('ken1');53expect(grammar.state).to.equal(State.State1);54expect(grammar.tokens).to.deep.equal([55{ state: State.Initial, token: 'som' },56{ state: State.Initial, token: 'e t' },57{ state: State.Initial, token: 'ext ' },58{ state: State.Initial, token: 'token1', transitionTo: State.State1 }59]);60});6162it('should handle multiple transitions', () => {63const grammar = new StreamingGrammar(State.Initial, {64[State.Initial]: { 'token1': State.State1 },65[State.State1]: { 'token2': State.State2 },66[State.State2]: { 'token3': State.State3 },67});6869grammar.append('token1token2token3');70expect(grammar.state).to.equal(State.State3);71expect(grammar.tokens).to.deep.equal([72{ state: State.Initial, token: 'token1', transitionTo: State.State1 },73{ state: State.State1, token: 'token2', transitionTo: State.State2 },74{ state: State.State2, token: 'token3', transitionTo: State.State3 }75]);76});7778it('should flush remaining text', () => {79const grammar = new StreamingGrammar(State.Initial, {80[State.Initial]: { 'token1': State.State1 },81[State.State1]: { 'token2': State.State2 },82});8384grammar.append('some text');85grammar.flush();86expect(grammar.tokens).to.deep.equal([87{ state: State.Initial, token: 'som' },88{ state: State.Initial, token: 'e text' },89]);90});9192it('should accumulate tokens correctly', () => {93const grammar = new StreamingGrammar(State.Initial, {94[State.Initial]: { 'token1': State.State1 },95[State.State1]: { 'token2': State.State2 },96});9798grammar.append('atoken1btoken2c');99expect(grammar.accumulate(0, 2)).to.equal('atoken1');100expect(grammar.accumulate(0, 2, State.Initial)).to.equal('a');101});102});103104105