@@ -24,6 +24,7 @@ import SettingsStore from "../src/settings/SettingsStore";
2424import Modal from "../src/Modal" ;
2525import PlatformPeg from "../src/PlatformPeg" ;
2626import { SettingLevel } from "../src/settings/SettingLevel" ;
27+ import { Features } from "../src/settings/Settings" ;
2728
2829jest . useFakeTimers ( ) ;
2930
@@ -91,41 +92,96 @@ describe("MatrixClientPeg", () => {
9192 } ) ;
9293 } ) ;
9394
94- it ( "should initialise client crypto" , async ( ) => {
95- const mockInitCrypto = jest . spyOn ( testPeg . safeGet ( ) , "initCrypto" ) . mockResolvedValue ( undefined ) ;
96- const mockSetTrustCrossSignedDevices = jest
97- . spyOn ( testPeg . safeGet ( ) , "setCryptoTrustCrossSignedDevices" )
98- . mockImplementation ( ( ) => { } ) ;
99- const mockStartClient = jest . spyOn ( testPeg . safeGet ( ) , "startClient" ) . mockResolvedValue ( undefined ) ;
95+ describe ( "legacy crypto" , ( ) => {
96+ beforeEach ( ( ) => {
97+ const originalGetValue = SettingsStore . getValue ;
98+ jest . spyOn ( SettingsStore , "getValue" ) . mockImplementation (
99+ ( settingName : string , roomId : string | null = null , excludeDefault = false ) => {
100+ if ( settingName === "feature_rust_crypto" ) {
101+ return false ;
102+ }
103+ return originalGetValue ( settingName , roomId , excludeDefault ) ;
104+ } ,
105+ ) ;
106+ } ) ;
100107
101- await testPeg . start ( ) ;
102- expect ( mockInitCrypto ) . toHaveBeenCalledTimes ( 1 ) ;
103- expect ( mockSetTrustCrossSignedDevices ) . toHaveBeenCalledTimes ( 1 ) ;
104- expect ( mockStartClient ) . toHaveBeenCalledTimes ( 1 ) ;
108+ it ( "should initialise client crypto" , async ( ) => {
109+ const mockInitCrypto = jest . spyOn ( testPeg . safeGet ( ) , "initCrypto" ) . mockResolvedValue ( undefined ) ;
110+ const mockSetTrustCrossSignedDevices = jest
111+ . spyOn ( testPeg . safeGet ( ) , "setCryptoTrustCrossSignedDevices" )
112+ . mockImplementation ( ( ) => { } ) ;
113+ const mockStartClient = jest . spyOn ( testPeg . safeGet ( ) , "startClient" ) . mockResolvedValue ( undefined ) ;
114+
115+ await testPeg . start ( ) ;
116+ expect ( mockInitCrypto ) . toHaveBeenCalledTimes ( 1 ) ;
117+ expect ( mockSetTrustCrossSignedDevices ) . toHaveBeenCalledTimes ( 1 ) ;
118+ expect ( mockStartClient ) . toHaveBeenCalledTimes ( 1 ) ;
119+ } ) ;
120+
121+ it ( "should carry on regardless if there is an error initialising crypto" , async ( ) => {
122+ const e2eError = new Error ( "nope nope nope" ) ;
123+ const mockInitCrypto = jest . spyOn ( testPeg . safeGet ( ) , "initCrypto" ) . mockRejectedValue ( e2eError ) ;
124+ const mockSetTrustCrossSignedDevices = jest
125+ . spyOn ( testPeg . safeGet ( ) , "setCryptoTrustCrossSignedDevices" )
126+ . mockImplementation ( ( ) => { } ) ;
127+ const mockStartClient = jest . spyOn ( testPeg . safeGet ( ) , "startClient" ) . mockResolvedValue ( undefined ) ;
128+ const mockWarning = jest . spyOn ( logger , "warn" ) . mockReturnValue ( undefined ) ;
129+
130+ await testPeg . start ( ) ;
131+ expect ( mockInitCrypto ) . toHaveBeenCalledTimes ( 1 ) ;
132+ expect ( mockSetTrustCrossSignedDevices ) . not . toHaveBeenCalled ( ) ;
133+ expect ( mockStartClient ) . toHaveBeenCalledTimes ( 1 ) ;
134+ expect ( mockWarning ) . toHaveBeenCalledWith ( expect . stringMatching ( "Unable to initialise e2e" ) , e2eError ) ;
135+ } ) ;
136+
137+ it ( "should reload when store database closes for a guest user" , async ( ) => {
138+ testPeg . safeGet ( ) . isGuest = ( ) => true ;
139+ const emitter = new EventEmitter ( ) ;
140+ testPeg . safeGet ( ) . store . on = emitter . on . bind ( emitter ) ;
141+ const platform : any = { reload : jest . fn ( ) } ;
142+ PlatformPeg . set ( platform ) ;
143+ await testPeg . assign ( ) ;
144+ emitter . emit ( "closed" as any ) ;
145+ expect ( platform . reload ) . toHaveBeenCalled ( ) ;
146+ } ) ;
147+
148+ it ( "should show error modal when store database closes" , async ( ) => {
149+ testPeg . safeGet ( ) . isGuest = ( ) => false ;
150+ const emitter = new EventEmitter ( ) ;
151+ const platform : any = { getHumanReadableName : jest . fn ( ) } ;
152+ PlatformPeg . set ( platform ) ;
153+ testPeg . safeGet ( ) . store . on = emitter . on . bind ( emitter ) ;
154+ const spy = jest . spyOn ( Modal , "createDialog" ) ;
155+ await testPeg . assign ( ) ;
156+ emitter . emit ( "closed" as any ) ;
157+ expect ( spy ) . toHaveBeenCalled ( ) ;
158+ } ) ;
105159 } ) ;
106160
107- it ( "should carry on regardless if there is an error initialising crypto" , async ( ) => {
108- const e2eError = new Error ( "nope nope nope" ) ;
109- const mockInitCrypto = jest . spyOn ( testPeg . safeGet ( ) , "initCrypto" ) . mockRejectedValue ( e2eError ) ;
110- const mockSetTrustCrossSignedDevices = jest
111- . spyOn ( testPeg . safeGet ( ) , "setCryptoTrustCrossSignedDevices" )
112- . mockImplementation ( ( ) => { } ) ;
113- const mockStartClient = jest . spyOn ( testPeg . safeGet ( ) , "startClient" ) . mockResolvedValue ( undefined ) ;
114- const mockWarning = jest . spyOn ( logger , "warn" ) . mockReturnValue ( undefined ) ;
161+ it ( "should initialise the rust crypto library by default" , async ( ) => {
162+ await SettingsStore . setValue ( Features . RustCrypto , null , SettingLevel . DEVICE , null ) ;
163+
164+ const mockSetValue = jest . spyOn ( SettingsStore , "setValue" ) . mockResolvedValue ( undefined ) ;
165+
166+ const mockInitCrypto = jest . spyOn ( testPeg . safeGet ( ) , "initCrypto" ) . mockResolvedValue ( undefined ) ;
167+ const mockInitRustCrypto = jest . spyOn ( testPeg . safeGet ( ) , "initRustCrypto" ) . mockResolvedValue ( undefined ) ;
115168
116169 await testPeg . start ( ) ;
117- expect ( mockInitCrypto ) . toHaveBeenCalledTimes ( 1 ) ;
118- expect ( mockSetTrustCrossSignedDevices ) . not . toHaveBeenCalled ( ) ;
119- expect ( mockStartClient ) . toHaveBeenCalledTimes ( 1 ) ;
120- expect ( mockWarning ) . toHaveBeenCalledWith ( expect . stringMatching ( "Unable to initialise e2e" ) , e2eError ) ;
170+ expect ( mockInitCrypto ) . not . toHaveBeenCalled ( ) ;
171+ expect ( mockInitRustCrypto ) . toHaveBeenCalledTimes ( 1 ) ;
172+
173+ // we should have stashed the setting in the settings store
174+ expect ( mockSetValue ) . toHaveBeenCalledWith ( "feature_rust_crypto" , null , SettingLevel . DEVICE , true ) ;
121175 } ) ;
122176
123- it ( "should initialise the rust crypto library, if enabled" , async ( ) => {
177+ it ( "should initialise the legacy crypto library if set" , async ( ) => {
178+ await SettingsStore . setValue ( Features . RustCrypto , null , SettingLevel . DEVICE , null ) ;
179+
124180 const originalGetValue = SettingsStore . getValue ;
125181 jest . spyOn ( SettingsStore , "getValue" ) . mockImplementation (
126182 ( settingName : string , roomId : string | null = null , excludeDefault = false ) => {
127183 if ( settingName === "feature_rust_crypto" ) {
128- return true ;
184+ return false ;
129185 }
130186 return originalGetValue ( settingName , roomId , excludeDefault ) ;
131187 } ,
@@ -137,11 +193,11 @@ describe("MatrixClientPeg", () => {
137193 const mockInitRustCrypto = jest . spyOn ( testPeg . safeGet ( ) , "initRustCrypto" ) . mockResolvedValue ( undefined ) ;
138194
139195 await testPeg . start ( ) ;
140- expect ( mockInitCrypto ) . not . toHaveBeenCalled ( ) ;
141- expect ( mockInitRustCrypto ) . toHaveBeenCalledTimes ( 1 ) ;
196+ expect ( mockInitCrypto ) . toHaveBeenCalled ( ) ;
197+ expect ( mockInitRustCrypto ) . not . toHaveBeenCalledTimes ( 1 ) ;
142198
143199 // we should have stashed the setting in the settings store
144- expect ( mockSetValue ) . toHaveBeenCalledWith ( "feature_rust_crypto" , null , SettingLevel . DEVICE , true ) ;
200+ expect ( mockSetValue ) . toHaveBeenCalledWith ( "feature_rust_crypto" , null , SettingLevel . DEVICE , false ) ;
145201 } ) ;
146202
147203 describe ( "Rust staged rollout" , ( ) => {
@@ -178,10 +234,12 @@ describe("MatrixClientPeg", () => {
178234 let mockInitCrypto : jest . SpyInstance ;
179235 let mockInitRustCrypto : jest . SpyInstance ;
180236
181- beforeEach ( ( ) => {
237+ beforeEach ( async ( ) => {
182238 mockSetValue = jest . spyOn ( SettingsStore , "setValue" ) . mockResolvedValue ( undefined ) ;
183239 mockInitCrypto = jest . spyOn ( testPeg . safeGet ( ) , "initCrypto" ) . mockResolvedValue ( undefined ) ;
184240 mockInitRustCrypto = jest . spyOn ( testPeg . safeGet ( ) , "initRustCrypto" ) . mockResolvedValue ( undefined ) ;
241+
242+ await SettingsStore . setValue ( Features . RustCrypto , null , SettingLevel . DEVICE , null ) ;
185243 } ) ;
186244
187245 it ( "Should not migrate existing login if rollout is 0" , async ( ) => {
@@ -254,28 +312,5 @@ describe("MatrixClientPeg", () => {
254312 expect ( mockSetValue ) . toHaveBeenCalledWith ( "feature_rust_crypto" , null , SettingLevel . DEVICE , false ) ;
255313 } ) ;
256314 } ) ;
257-
258- it ( "should reload when store database closes for a guest user" , async ( ) => {
259- testPeg . safeGet ( ) . isGuest = ( ) => true ;
260- const emitter = new EventEmitter ( ) ;
261- testPeg . safeGet ( ) . store . on = emitter . on . bind ( emitter ) ;
262- const platform : any = { reload : jest . fn ( ) } ;
263- PlatformPeg . set ( platform ) ;
264- await testPeg . assign ( ) ;
265- emitter . emit ( "closed" as any ) ;
266- expect ( platform . reload ) . toHaveBeenCalled ( ) ;
267- } ) ;
268-
269- it ( "should show error modal when store database closes" , async ( ) => {
270- testPeg . safeGet ( ) . isGuest = ( ) => false ;
271- const emitter = new EventEmitter ( ) ;
272- const platform : any = { getHumanReadableName : jest . fn ( ) } ;
273- PlatformPeg . set ( platform ) ;
274- testPeg . safeGet ( ) . store . on = emitter . on . bind ( emitter ) ;
275- const spy = jest . spyOn ( Modal , "createDialog" ) ;
276- await testPeg . assign ( ) ;
277- emitter . emit ( "closed" as any ) ;
278- expect ( spy ) . toHaveBeenCalled ( ) ;
279- } ) ;
280315 } ) ;
281316} ) ;
0 commit comments