1
1
import CompileError from '$promptl/error/error'
2
- import { complete , getExpectedError } from " $promptl/compiler/test/helpers" ;
3
- import { removeCommonIndent } from " $promptl/compiler/utils" ;
4
- import { Chain } from " $promptl/index" ;
5
- import { describe , expect , it , vi } from " vitest" ;
2
+ import { complete } from ' $promptl/compiler/test/helpers'
3
+ import { removeCommonIndent } from ' $promptl/compiler/utils'
4
+ import { Chain } from ' $promptl/index'
5
+ import { describe , expect , it , vi } from ' vitest'
6
6
7
- describe ( " step tags" , async ( ) => {
8
- it ( " does not create a variable from response if not specified" , async ( ) => {
9
- const mock = vi . fn ( ) ;
7
+ describe ( ' step tags' , async ( ) => {
8
+ it ( ' does not create a variable from response if not specified' , async ( ) => {
9
+ const mock = vi . fn ( )
10
10
const prompt = removeCommonIndent ( `
11
11
<step>
12
12
Ensure truthfulness of the following statement, give a reason and a confidence score.
@@ -15,18 +15,22 @@ describe("step tags", async () => {
15
15
<step>
16
16
Now correct the statement if it is not true.
17
17
</step>
18
- ` ) ;
18
+ ` )
19
19
20
- const chain = new Chain ( { prompt, parameters : { mock } } ) ;
21
- await complete ( { chain, callback : async ( ) => `
20
+ const chain = new Chain ( { prompt, parameters : { mock } } )
21
+ await complete ( {
22
+ chain,
23
+ callback : async ( ) =>
24
+ `
22
25
The statement is not true because it is fake. My confidence score is 100.
23
- ` . trim ( ) } ) ;
26
+ ` . trim ( ) ,
27
+ } )
24
28
25
- expect ( mock ) . not . toHaveBeenCalled ( ) ;
26
- } ) ;
29
+ expect ( mock ) . not . toHaveBeenCalled ( )
30
+ } )
27
31
28
- it ( " creates a text variable from response if specified" , async ( ) => {
29
- const mock = vi . fn ( ) ;
32
+ it ( ' creates a text variable from response if specified' , async ( ) => {
33
+ const mock = vi . fn ( )
30
34
const prompt = removeCommonIndent ( `
31
35
<step as="analysis">
32
36
Ensure truthfulness of the following statement, give a reason and a confidence score.
@@ -36,18 +40,24 @@ describe("step tags", async () => {
36
40
{{ mock(analysis) }}
37
41
Now correct the statement if it is not true.
38
42
</step>
39
- ` ) ;
43
+ ` )
40
44
41
- const chain = new Chain ( { prompt, parameters : { mock } } ) ;
42
- await complete ( { chain, callback : async ( ) => `
45
+ const chain = new Chain ( { prompt, parameters : { mock } } )
46
+ await complete ( {
47
+ chain,
48
+ callback : async ( ) =>
49
+ `
43
50
The statement is not true because it is fake. My confidence score is 100.
44
- ` . trim ( ) } ) ;
51
+ ` . trim ( ) ,
52
+ } )
45
53
46
- expect ( mock ) . toHaveBeenCalledWith ( "The statement is not true because it is fake. My confidence score is 100." ) ;
47
- } ) ;
54
+ expect ( mock ) . toHaveBeenCalledWith (
55
+ 'The statement is not true because it is fake. My confidence score is 100.' ,
56
+ )
57
+ } )
48
58
49
- it ( " creates an object variable from response if specified and schema is provided" , async ( ) => {
50
- const mock = vi . fn ( ) ;
59
+ it ( ' creates an object variable from response if specified and schema is provided' , async ( ) => {
60
+ const mock = vi . fn ( )
51
61
const prompt = removeCommonIndent ( `
52
62
<step as="analysis" schema={{{type: "object", properties: {truthful: {type: "boolean"}, reason: {type: "string"}, confidence: {type: "integer"}}, required: ["truthful", "reason", "confidence"]}}}>
53
63
Ensure truthfulness of the following statement, give a reason and a confidence score.
@@ -59,27 +69,33 @@ describe("step tags", async () => {
59
69
Correct the statement taking into account the reason: '{{ analysis.reason }}'.
60
70
{{ endif }}
61
71
</step>
62
- ` ) ;
72
+ ` )
63
73
64
- const chain = new Chain ( { prompt, parameters : { mock } } ) ;
65
- const { messages } = await complete ( { chain, callback : async ( ) => `
74
+ const chain = new Chain ( { prompt, parameters : { mock } } )
75
+ const { messages } = await complete ( {
76
+ chain,
77
+ callback : async ( ) =>
78
+ `
66
79
{
67
80
"truthful": false,
68
81
"reason": "It is fake",
69
82
"confidence": 100
70
83
}
71
- ` . trim ( ) } ) ;
84
+ ` . trim ( ) ,
85
+ } )
72
86
73
87
expect ( mock ) . toHaveBeenCalledWith ( {
74
88
truthful : false ,
75
- reason : "It is fake" ,
76
- confidence : 100
77
- } ) ;
78
- expect ( messages [ 2 ] ! . content ) . toEqual ( "Correct the statement taking into account the reason: 'It is fake'." ) ;
79
- } ) ;
89
+ reason : 'It is fake' ,
90
+ confidence : 100 ,
91
+ } )
92
+ expect ( messages [ 2 ] ! . content ) . toEqual (
93
+ "Correct the statement taking into account the reason: 'It is fake'." ,
94
+ )
95
+ } )
80
96
81
- it ( " fails creating an object variable from response if specified and schema is provided but response is invalid" , async ( ) => {
82
- const mock = vi . fn ( ) ;
97
+ it ( ' fails creating an object variable from response if specified and schema is provided but response is invalid' , async ( ) => {
98
+ const mock = vi . fn ( )
83
99
const prompt = removeCommonIndent ( `
84
100
<step as="analysis" schema={{{type: "object", properties: {truthful: {type: "boolean"}, reason: {type: "string"}, confidence: {type: "integer"}}, required: ["truthful", "reason", "confidence"]}}}>
85
101
Ensure truthfulness of the following statement, give a reason and a confidence score.
@@ -91,19 +107,29 @@ describe("step tags", async () => {
91
107
Correct the statement taking into account the reason: '{{ analysis.reason }}'.
92
108
{{ endif }}
93
109
</step>
94
- ` ) ;
110
+ ` )
95
111
96
- const chain = new Chain ( { prompt, parameters : { mock } } ) ;
97
- const error = await getExpectedError ( ( ) => complete ( { chain, callback : async ( ) => `
112
+ const chain = new Chain ( { prompt, parameters : { mock } } )
113
+ let error : CompileError
114
+ try {
115
+ await complete ( {
116
+ chain,
117
+ callback : async ( ) =>
118
+ `
98
119
Bad JSON.
99
- ` . trim ( ) } ) , CompileError )
100
- expect ( error . code ) . toBe ( 'invalid-step-response-format' )
120
+ ` . trim ( ) ,
121
+ } )
122
+ } catch ( e ) {
123
+ error = e as CompileError
124
+ expect ( e ) . toBeInstanceOf ( CompileError )
125
+ }
101
126
102
- expect ( mock ) . not . toHaveBeenCalled ( ) ;
103
- } ) ;
127
+ expect ( error ! . code ) . toBe ( 'invalid-step-response-format' )
128
+ expect ( mock ) . not . toHaveBeenCalled ( )
129
+ } )
104
130
105
- it ( " creates a raw variable from response if specified" , async ( ) => {
106
- const mock = vi . fn ( ) ;
131
+ it ( ' creates a raw variable from response if specified' , async ( ) => {
132
+ const mock = vi . fn ( )
107
133
const prompt = removeCommonIndent ( `
108
134
<step raw="analysis">
109
135
Ensure truthfulness of the following statement, give a reason and a confidence score.
@@ -113,21 +139,25 @@ describe("step tags", async () => {
113
139
{{ mock(analysis) }}
114
140
Now correct the statement if it is not true.
115
141
</step>
116
- ` ) ;
142
+ ` )
117
143
118
- const chain = new Chain ( { prompt, parameters : { mock } } ) ;
119
- await complete ( { chain, callback : async ( ) => `
144
+ const chain = new Chain ( { prompt, parameters : { mock } } )
145
+ await complete ( {
146
+ chain,
147
+ callback : async ( ) =>
148
+ `
120
149
The statement is not true because it is fake. My confidence score is 100.
121
- ` . trim ( ) } ) ;
150
+ ` . trim ( ) ,
151
+ } )
122
152
123
153
expect ( mock ) . toHaveBeenCalledWith ( {
124
- role : " assistant" ,
154
+ role : ' assistant' ,
125
155
content : [
126
156
{
127
- type : " text" ,
128
- text : " The statement is not true because it is fake. My confidence score is 100." ,
157
+ type : ' text' ,
158
+ text : ' The statement is not true because it is fake. My confidence score is 100.' ,
129
159
} ,
130
160
] ,
131
- } ) ;
132
- } ) ;
133
- } ) ;
161
+ } )
162
+ } )
163
+ } )
0 commit comments