11import 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'
66
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 ( )
1010 const prompt = removeCommonIndent ( `
1111 <step>
1212 Ensure truthfulness of the following statement, give a reason and a confidence score.
@@ -15,18 +15,22 @@ describe("step tags", async () => {
1515 <step>
1616 Now correct the statement if it is not true.
1717 </step>
18- ` ) ;
18+ ` )
1919
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+ `
2225 The statement is not true because it is fake. My confidence score is 100.
23- ` . trim ( ) } ) ;
26+ ` . trim ( ) ,
27+ } )
2428
25- expect ( mock ) . not . toHaveBeenCalled ( ) ;
26- } ) ;
29+ expect ( mock ) . not . toHaveBeenCalled ( )
30+ } )
2731
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 ( )
3034 const prompt = removeCommonIndent ( `
3135 <step as="analysis">
3236 Ensure truthfulness of the following statement, give a reason and a confidence score.
@@ -36,18 +40,24 @@ describe("step tags", async () => {
3640 {{ mock(analysis) }}
3741 Now correct the statement if it is not true.
3842 </step>
39- ` ) ;
43+ ` )
4044
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+ `
4350 The statement is not true because it is fake. My confidence score is 100.
44- ` . trim ( ) } ) ;
51+ ` . trim ( ) ,
52+ } )
4553
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+ } )
4858
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 ( )
5161 const prompt = removeCommonIndent ( `
5262 <step as="analysis" schema={{{type: "object", properties: {truthful: {type: "boolean"}, reason: {type: "string"}, confidence: {type: "integer"}}, required: ["truthful", "reason", "confidence"]}}}>
5363 Ensure truthfulness of the following statement, give a reason and a confidence score.
@@ -59,27 +69,33 @@ describe("step tags", async () => {
5969 Correct the statement taking into account the reason: '{{ analysis.reason }}'.
6070 {{ endif }}
6171 </step>
62- ` ) ;
72+ ` )
6373
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+ `
6679 {
6780 "truthful": false,
6881 "reason": "It is fake",
6982 "confidence": 100
7083 }
71- ` . trim ( ) } ) ;
84+ ` . trim ( ) ,
85+ } )
7286
7387 expect ( mock ) . toHaveBeenCalledWith ( {
7488 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+ } )
8096
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 ( )
8399 const prompt = removeCommonIndent ( `
84100 <step as="analysis" schema={{{type: "object", properties: {truthful: {type: "boolean"}, reason: {type: "string"}, confidence: {type: "integer"}}, required: ["truthful", "reason", "confidence"]}}}>
85101 Ensure truthfulness of the following statement, give a reason and a confidence score.
@@ -91,19 +107,29 @@ describe("step tags", async () => {
91107 Correct the statement taking into account the reason: '{{ analysis.reason }}'.
92108 {{ endif }}
93109 </step>
94- ` ) ;
110+ ` )
95111
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+ `
98119 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+ }
101126
102- expect ( mock ) . not . toHaveBeenCalled ( ) ;
103- } ) ;
127+ expect ( error ! . code ) . toBe ( 'invalid-step-response-format' )
128+ expect ( mock ) . not . toHaveBeenCalled ( )
129+ } )
104130
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 ( )
107133 const prompt = removeCommonIndent ( `
108134 <step raw="analysis">
109135 Ensure truthfulness of the following statement, give a reason and a confidence score.
@@ -113,21 +139,25 @@ describe("step tags", async () => {
113139 {{ mock(analysis) }}
114140 Now correct the statement if it is not true.
115141 </step>
116- ` ) ;
142+ ` )
117143
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+ `
120149 The statement is not true because it is fake. My confidence score is 100.
121- ` . trim ( ) } ) ;
150+ ` . trim ( ) ,
151+ } )
122152
123153 expect ( mock ) . toHaveBeenCalledWith ( {
124- role : " assistant" ,
154+ role : ' assistant' ,
125155 content : [
126156 {
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.' ,
129159 } ,
130160 ] ,
131- } ) ;
132- } ) ;
133- } ) ;
161+ } )
162+ } )
163+ } )
0 commit comments