@@ -1988,6 +1988,73 @@ describe("GatewayClient connect auth payload", () => {
19881988 client . stop ( ) ;
19891989 } ) ;
19901990
1991+ it . each ( [
1992+ { completion : "clear" , deviceAuthScope : undefined , store : "device" } ,
1993+ { completion : "overwrite" , deviceAuthScope : undefined , store : "device" } ,
1994+ { completion : "clear" , deviceAuthScope : "wss://one.example/rpc" , store : "origin" } ,
1995+ { completion : "overwrite" , deviceAuthScope : "wss://one.example/rpc" , store : "origin" } ,
1996+ ] as const ) (
1997+ "does not $completion rotated $store auth after the prepared snapshot" ,
1998+ async ( { completion, deviceAuthScope } ) => {
1999+ const preparedDeviceAuth = {
2000+ token : "prepared-device-token" ,
2001+ role : "operator" ,
2002+ scopes : [ "operator.read" ] ,
2003+ updatedAtMs : 123 ,
2004+ } ;
2005+ let durableToken : string | undefined = preparedDeviceAuth . token ;
2006+ const clearTokenMock = deviceAuthScope
2007+ ? clearOriginDeviceTokenMock
2008+ : clearDeviceAuthTokenMock ;
2009+ const storeTokenMock = deviceAuthScope
2010+ ? storeOriginDeviceTokenMock
2011+ : storeDeviceAuthTokenMock ;
2012+ clearTokenMock . mockImplementation ( ( { expectedToken } : { expectedToken ?: string } ) => {
2013+ if ( durableToken === expectedToken ) {
2014+ durableToken = undefined ;
2015+ }
2016+ } ) ;
2017+ storeTokenMock . mockImplementation (
2018+ ( { expectedToken, token } : { expectedToken ?: string ; token : string } ) => {
2019+ if ( durableToken === expectedToken ) {
2020+ durableToken = token ;
2021+ }
2022+ } ,
2023+ ) ;
2024+ const client = createClientWithIdentity ( "device-1" , ( ) => { } , {
2025+ preparedDeviceAuth,
2026+ ...( deviceAuthScope ? { deviceAuthScope } : { } ) ,
2027+ } ) ;
2028+
2029+ const { ws, connect } = startClientAndConnect ( { client } ) ;
2030+ durableToken = "rotated-device-token" ;
2031+ if ( completion === "clear" ) {
2032+ emitConnectFailure ( ws , connect . id , { code : "AUTH_DEVICE_TOKEN_MISMATCH" } ) ;
2033+ await waitForFast ( ( ) => expect ( clearTokenMock ) . toHaveBeenCalledOnce ( ) ) ;
2034+ } else {
2035+ ws . emitMessage (
2036+ JSON . stringify ( {
2037+ type : "res" ,
2038+ id : connect . id ,
2039+ ok : true ,
2040+ payload : {
2041+ type : "hello-ok" ,
2042+ auth : {
2043+ role : "operator" ,
2044+ scopes : [ "operator.write" ] ,
2045+ deviceToken : "stale-request-replacement" ,
2046+ } ,
2047+ } ,
2048+ } ) ,
2049+ ) ;
2050+ await waitForFast ( ( ) => expect ( storeTokenMock ) . toHaveBeenCalledOnce ( ) ) ;
2051+ }
2052+
2053+ expect ( durableToken ) . toBe ( "rotated-device-token" ) ;
2054+ client . stop ( ) ;
2055+ } ,
2056+ ) ;
2057+
19912058 it ( "keeps read-only origin auth loads and hello tokens off writable host callbacks" , async ( ) => {
19922059 loadOriginDeviceTokenReadOnlyMock . mockReturnValue ( {
19932060 token : "stored-origin-token" ,
0 commit comments