Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/extension/prompt/test/common/streamingGrammar.spec.ts
13405 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 { describe, expect, it } from 'vitest';
7
import { StreamingGrammar } from '../../common/streamingGrammar';
8
9
const enum State {
10
Initial,
11
State1,
12
State2,
13
State3,
14
}
15
16
describe('StreamingGrammar', () => {
17
it('should initialize with the correct state', () => {
18
const grammar = new StreamingGrammar(State.Initial, {
19
[State.Initial]: { 'token1': State.State1 },
20
[State.State1]: { 'token2': State.State2 },
21
});
22
expect(grammar.state).to.equal(State.Initial);
23
});
24
25
it('should transition states correctly', () => {
26
const grammar = new StreamingGrammar(State.Initial, {
27
[State.Initial]: { 'token1': State.State1 },
28
[State.State1]: { 'token2': State.State2 },
29
});
30
31
grammar.append('token1');
32
expect(grammar.state).to.equal(State.State1);
33
expect(grammar.tokens).to.deep.equal([
34
{ state: State.Initial, token: 'token1', transitionTo: State.State1 }
35
]);
36
37
grammar.append('token2');
38
expect(grammar.state).to.equal(State.State2);
39
expect(grammar.tokens).to.deep.equal([
40
{ state: State.Initial, token: 'token1', transitionTo: State.State1 },
41
{ state: State.State1, token: 'token2', transitionTo: State.State2 }
42
]);
43
});
44
45
it('should accumulate text correctly', () => {
46
const grammar = new StreamingGrammar(State.Initial, {
47
[State.Initial]: { 'token1': State.State1 },
48
[State.State1]: { 'token2': State.State2 },
49
});
50
51
grammar.append('some text');
52
grammar.append(' to');
53
grammar.append('ken1');
54
expect(grammar.state).to.equal(State.State1);
55
expect(grammar.tokens).to.deep.equal([
56
{ state: State.Initial, token: 'som' },
57
{ state: State.Initial, token: 'e t' },
58
{ state: State.Initial, token: 'ext ' },
59
{ state: State.Initial, token: 'token1', transitionTo: State.State1 }
60
]);
61
});
62
63
it('should handle multiple transitions', () => {
64
const grammar = new StreamingGrammar(State.Initial, {
65
[State.Initial]: { 'token1': State.State1 },
66
[State.State1]: { 'token2': State.State2 },
67
[State.State2]: { 'token3': State.State3 },
68
});
69
70
grammar.append('token1token2token3');
71
expect(grammar.state).to.equal(State.State3);
72
expect(grammar.tokens).to.deep.equal([
73
{ state: State.Initial, token: 'token1', transitionTo: State.State1 },
74
{ state: State.State1, token: 'token2', transitionTo: State.State2 },
75
{ state: State.State2, token: 'token3', transitionTo: State.State3 }
76
]);
77
});
78
79
it('should flush remaining text', () => {
80
const grammar = new StreamingGrammar(State.Initial, {
81
[State.Initial]: { 'token1': State.State1 },
82
[State.State1]: { 'token2': State.State2 },
83
});
84
85
grammar.append('some text');
86
grammar.flush();
87
expect(grammar.tokens).to.deep.equal([
88
{ state: State.Initial, token: 'som' },
89
{ state: State.Initial, token: 'e text' },
90
]);
91
});
92
93
it('should accumulate tokens correctly', () => {
94
const grammar = new StreamingGrammar(State.Initial, {
95
[State.Initial]: { 'token1': State.State1 },
96
[State.State1]: { 'token2': State.State2 },
97
});
98
99
grammar.append('atoken1btoken2c');
100
expect(grammar.accumulate(0, 2)).to.equal('atoken1');
101
expect(grammar.accumulate(0, 2, State.Initial)).to.equal('a');
102
});
103
});
104
105