@@ -21,10 +21,12 @@ describe("Recipe Runner", () => {
2121 let runtime : Runtime ;
2222 let tx : IExtendedStorageTransaction ;
2323 let lift : ReturnType < typeof createBuilder > [ "commontools" ] [ "lift" ] ;
24+ let derive : ReturnType < typeof createBuilder > [ "commontools" ] [ "derive" ] ;
2425 let recipe : ReturnType < typeof createBuilder > [ "commontools" ] [ "recipe" ] ;
2526 let createCell : ReturnType < typeof createBuilder > [ "commontools" ] [ "createCell" ] ;
2627 let handler : ReturnType < typeof createBuilder > [ "commontools" ] [ "handler" ] ;
2728 let byRef : ReturnType < typeof createBuilder > [ "commontools" ] [ "byRef" ] ;
29+ let ifElse : ReturnType < typeof createBuilder > [ "commontools" ] [ "ifElse" ] ;
2830 let TYPE : ReturnType < typeof createBuilder > [ "commontools" ] [ "TYPE" ] ;
2931
3032 beforeEach ( ( ) => {
@@ -41,10 +43,12 @@ describe("Recipe Runner", () => {
4143 const { commontools } = createBuilder ( runtime ) ;
4244 ( {
4345 lift,
46+ derive,
4447 recipe,
4548 createCell,
4649 handler,
4750 byRef,
51+ ifElse,
4852 TYPE ,
4953 } = commontools ) ;
5054 } ) ;
@@ -1291,4 +1295,78 @@ describe("Recipe Runner", () => {
12911295 expect ( isCell ( listCell . get ( ) [ 0 ] ) ) . toBe ( true ) ;
12921296 expect ( listCell . get ( ) [ 0 ] . equals ( testCell . get ( ) ) ) . toBe ( true ) ;
12931297 } ) ;
1298+
1299+ it ( "correctly handles the ifElse values with nested derives" , async ( ) => {
1300+ const InputSchema = {
1301+ "type" : "object" ,
1302+ "properties" : {
1303+ "expandChat" : { "type" : "boolean" } ,
1304+ } ,
1305+ } as const satisfies JSONSchema ;
1306+
1307+ const StateSchema = {
1308+ "type" : "object" ,
1309+ "properties" : {
1310+ "expandChat" : { "type" : "boolean" } ,
1311+ "text" : { "type" : "string" } ,
1312+ } ,
1313+ "asCell" : true ,
1314+ } as const satisfies JSONSchema ;
1315+ const expandHandler = handler (
1316+ InputSchema ,
1317+ StateSchema ,
1318+ ( { expandChat } , state ) => {
1319+ state . key ( "expandChat" ) . set ( expandChat ) ;
1320+ } ,
1321+ ) ;
1322+
1323+ const ifElseRecipe = recipe < { expandChat : boolean } > (
1324+ "ifElse Recipe" ,
1325+ ( { expandChat } ) => {
1326+ const optionA = derive ( expandChat , ( t ) => t ? "A" : "a" ) ;
1327+ const optionB = derive ( expandChat , ( t ) => t ? "B" : "b" ) ;
1328+
1329+ return {
1330+ expandChat,
1331+ text : ifElse (
1332+ expandChat ,
1333+ optionA ,
1334+ optionB ,
1335+ ) ,
1336+ stream : expandHandler ( { expandChat } ) ,
1337+ } ;
1338+ } ,
1339+ ) ;
1340+
1341+ const charmCell = runtime . getCell <
1342+ { expandChat : boolean ; text : string ; stream : any }
1343+ > (
1344+ space ,
1345+ "ifElse should work" ,
1346+ ifElseRecipe . resultSchema ,
1347+ tx ,
1348+ ) ;
1349+
1350+ const charm = runtime . run (
1351+ tx ,
1352+ ifElseRecipe ,
1353+ { expandChat : true } ,
1354+ charmCell ,
1355+ ) ;
1356+
1357+ tx . commit ( ) ;
1358+
1359+ await runtime . idle ( ) ;
1360+
1361+ // Toggle
1362+ charm . key ( "stream" ) . send ( { expandChat : true } ) ;
1363+ await runtime . idle ( ) ;
1364+
1365+ expect ( charm . key ( "text" ) . get ( ) ) . toEqual ( "A" ) ;
1366+
1367+ charm . key ( "stream" ) . send ( { expandChat : false } ) ;
1368+ await runtime . idle ( ) ;
1369+
1370+ expect ( charm . key ( "text" ) . get ( ) ) . toEqual ( "b" ) ;
1371+ } ) ;
12941372} ) ;
0 commit comments