@@ -3,6 +3,8 @@ import { AuthStandardClientScopes, EnvironmentId } from "@t3tools/contracts";
33import { expect , it } from "@effect/vitest" ;
44import * as Duration from "effect/Duration" ;
55import * as Effect from "effect/Effect" ;
6+ import * as Fiber from "effect/Fiber" ;
7+ import * as Deferred from "effect/Deferred" ;
68import * as FileSystem from "effect/FileSystem" ;
79import * as Layer from "effect/Layer" ;
810import * as Option from "effect/Option" ;
@@ -113,6 +115,57 @@ const layerFailingSessionLookupCredential = Layer.effect(
113115) ;
114116
115117it . layer ( NodeServices . layer ) ( "SessionStore.layer" , ( it ) => {
118+ it . effect . each ( [ "revoke" , "revokeAllExcept" , "replace" ] as const ) (
119+ "invalidates every connection for a session on %s and leaves other sessions active" ,
120+ ( operation ) =>
121+ Effect . gen ( function * ( ) {
122+ const sessions = yield * SessionStore . SessionStore ;
123+ const owner = yield * sessions . issue ( { subject : "owner" } ) ;
124+ const client = yield * sessions . issue ( relaySessionInput ) ;
125+ const ownerInvalidated = yield * Deferred . make < void > ( ) ;
126+ yield * sessions
127+ . awaitInvalidation ( owner . sessionId )
128+ . pipe (
129+ Effect . andThen ( Deferred . succeed ( ownerInvalidated , undefined ) ) ,
130+ Effect . forkScoped ( { startImmediately : true } ) ,
131+ ) ;
132+ const first = yield * sessions
133+ . awaitInvalidation ( client . sessionId )
134+ . pipe ( Effect . forkScoped ( { startImmediately : true } ) ) ;
135+ const second = yield * sessions
136+ . awaitInvalidation ( client . sessionId )
137+ . pipe ( Effect . forkScoped ( { startImmediately : true } ) ) ;
138+ if ( operation === "revoke" ) yield * sessions . revoke ( client . sessionId ) ;
139+ else if ( operation === "revokeAllExcept" ) yield * sessions . revokeAllExcept ( owner . sessionId ) ;
140+ else yield * sessions . issue ( { ...relaySessionInput , replaceSessionId : client . sessionId } ) ;
141+ yield * Fiber . join ( first ) ;
142+ yield * Fiber . join ( second ) ;
143+ expect ( yield * Deferred . isDone ( ownerInvalidated ) ) . toBe ( false ) ;
144+ // A connection whose upgrade authenticated just before revocation must also stop.
145+ yield * sessions . awaitInvalidation ( client . sessionId ) ;
146+ } ) . pipe ( Effect . scoped , Effect . provide ( Layer . merge ( layerSessionStore ( ) , TestClock . layer ( ) ) ) ) ,
147+ ) ;
148+
149+ it . effect ( "expires a connected session at its credential deadline" , ( ) =>
150+ Effect . gen ( function * ( ) {
151+ const sessions = yield * SessionStore . SessionStore ;
152+ const client = yield * sessions . issue ( relaySessionInput ) ;
153+ yield * sessions . markConnected ( client . sessionId ) ;
154+ const invalidated = yield * Deferred . make < void > ( ) ;
155+ const watcher = yield * sessions
156+ . awaitInvalidation ( client . sessionId )
157+ . pipe (
158+ Effect . andThen ( Deferred . succeed ( invalidated , undefined ) ) ,
159+ Effect . forkScoped ( { startImmediately : true } ) ,
160+ ) ;
161+ yield * TestClock . adjust ( Duration . minutes ( 59 ) ) ;
162+ expect ( yield * Deferred . isDone ( invalidated ) ) . toBe ( false ) ;
163+ yield * TestClock . adjust ( Duration . minutes ( 1 ) ) ;
164+ yield * Fiber . join ( watcher ) ;
165+ yield * sessions . awaitInvalidation ( client . sessionId ) ;
166+ } ) . pipe ( Effect . scoped , Effect . provide ( Layer . merge ( layerSessionStore ( ) , TestClock . layer ( ) ) ) ) ,
167+ ) ;
168+
116169 it . effect ( "keys remote cookies by environment identity instead of state directory" , ( ) =>
117170 Effect . gen ( function * ( ) {
118171 const cookieName = ( stateDir : string , environmentId : EnvironmentId ) =>
0 commit comments