@@ -890,6 +890,36 @@ describe("GatewayClient connect auth payload", () => {
890890 } ) ;
891891 } ) ;
892892
893+ it ( "clears stale stored device tokens and does not reconnect on AUTH_DEVICE_TOKEN_MISMATCH" , async ( ) => {
894+ loadDeviceAuthTokenMock . mockReturnValue ( {
895+ token : "stored-device-token" ,
896+ scopes : [ "operator.read" ] ,
897+ } ) ;
898+ const onReconnectPaused = vi . fn ( ) ;
899+ const client = new GatewayClient ( {
900+ url : "ws://127.0.0.1:18789" ,
901+ onReconnectPaused,
902+ } ) ;
903+
904+ const { ws : ws1 , connect : firstConnect } = startClientAndConnect ( { client } ) ;
905+ expect ( firstConnect . params ?. auth ?. token ) . toBe ( "stored-device-token" ) ;
906+ await expectNoReconnectAfterConnectFailure ( {
907+ client,
908+ firstWs : ws1 ,
909+ connectId : firstConnect . id ,
910+ failureDetails : { code : "AUTH_DEVICE_TOKEN_MISMATCH" } ,
911+ } ) ;
912+ expect ( clearDeviceAuthTokenMock ) . toHaveBeenCalledWith ( {
913+ deviceId : expect . any ( String ) ,
914+ role : "operator" ,
915+ } ) ;
916+ expect ( onReconnectPaused ) . toHaveBeenCalledWith ( {
917+ code : 1008 ,
918+ reason : "connect failed" ,
919+ detailCode : "AUTH_DEVICE_TOKEN_MISMATCH" ,
920+ } ) ;
921+ } ) ;
922+
893923 it ( "does not auto-reconnect on token mismatch when retry is not trusted" , async ( ) => {
894924 loadDeviceAuthTokenMock . mockReturnValue ( { token : "stored-device-token" } ) ;
895925 const client = new GatewayClient ( {
0 commit comments