@@ -337,14 +337,50 @@ describe("SessionV2.create", () => {
337337 expect ( yield * unavailable ( session . shell ( { sessionID : created . id , command : "pwd" } ) ) ) . toBe ( "shell" )
338338 expect ( yield * unavailable ( session . skill ( { sessionID : created . id , skill : "review" } ) ) ) . toBe ( "skill" )
339339 expect ( yield * unavailable ( session . switchAgent ( { sessionID : created . id , agent : "build" } ) ) ) . toBe ( "switchAgent" )
340+ } ) ,
341+ )
342+
343+ it . effect ( "switches the selected model through the durable Session event" , ( ) =>
344+ Effect . gen ( function * ( ) {
345+ const session = yield * SessionV2 . Service
346+ const created = yield * session . create ( { location } )
347+ const model = ModelV2 . Ref . make ( {
348+ id : ModelV2 . ID . make ( "sonnet" ) ,
349+ providerID : ProviderV2 . ID . anthropic ,
350+ variant : ModelV2 . VariantID . make ( "high" ) ,
351+ } )
352+
353+ yield * session . switchModel ( { sessionID : created . id , model } )
354+
355+ expect ( yield * session . get ( created . id ) ) . toMatchObject ( { model } )
356+ expect (
357+ Array . from ( yield * session . events ( { sessionID : created . id } ) . pipe ( Stream . take ( 1 ) , Stream . runCollect ) ) ,
358+ ) . toMatchObject ( [ { event : { type : "session.next.model.switched" , data : { model } } } ] )
359+
360+ yield * session . switchModel ( { sessionID : created . id , model } )
361+ const { db } = yield * Database . Service
362+ expect (
363+ yield * db . select ( ) . from ( EventTable ) . where ( eq ( EventTable . aggregate_id , created . id ) ) . all ( ) . pipe ( Effect . orDie ) ,
364+ ) . toHaveLength ( 2 )
365+ } ) ,
366+ )
367+
368+ it . effect ( "rejects a model switch for a missing Session" , ( ) =>
369+ Effect . gen ( function * ( ) {
370+ const session = yield * SessionV2 . Service
371+ const missing = SessionV2 . ID . make ( "ses_missing_model_switch" )
372+
340373 expect (
341- yield * unavailable (
342- session . switchModel ( {
343- sessionID : created . id ,
374+ yield * session
375+ . switchModel ( {
376+ sessionID : missing ,
344377 model : ModelV2 . Ref . make ( { id : ModelV2 . ID . make ( "sonnet" ) , providerID : ProviderV2 . ID . anthropic } ) ,
345- } ) ,
346- ) ,
347- ) . toBe ( "switchModel" )
378+ } )
379+ . pipe (
380+ Effect . flip ,
381+ Effect . map ( ( error ) => error . _tag ) ,
382+ ) ,
383+ ) . toBe ( "Session.NotFoundError" )
348384 } ) ,
349385 )
350386} )
0 commit comments