@@ -24,6 +24,7 @@ import SettingsStore from "../src/settings/SettingsStore";
24
24
import Modal from "../src/Modal" ;
25
25
import PlatformPeg from "../src/PlatformPeg" ;
26
26
import { SettingLevel } from "../src/settings/SettingLevel" ;
27
+ import { Features } from "../src/settings/Settings" ;
27
28
28
29
jest . useFakeTimers ( ) ;
29
30
@@ -91,41 +92,96 @@ describe("MatrixClientPeg", () => {
91
92
} ) ;
92
93
} ) ;
93
94
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
+ } ) ;
100
107
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
+ } ) ;
105
159
} ) ;
106
160
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 ) ;
115
168
116
169
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 ) ;
121
175
} ) ;
122
176
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
+
124
180
const originalGetValue = SettingsStore . getValue ;
125
181
jest . spyOn ( SettingsStore , "getValue" ) . mockImplementation (
126
182
( settingName : string , roomId : string | null = null , excludeDefault = false ) => {
127
183
if ( settingName === "feature_rust_crypto" ) {
128
- return true ;
184
+ return false ;
129
185
}
130
186
return originalGetValue ( settingName , roomId , excludeDefault ) ;
131
187
} ,
@@ -137,11 +193,11 @@ describe("MatrixClientPeg", () => {
137
193
const mockInitRustCrypto = jest . spyOn ( testPeg . safeGet ( ) , "initRustCrypto" ) . mockResolvedValue ( undefined ) ;
138
194
139
195
await testPeg . start ( ) ;
140
- expect ( mockInitCrypto ) . not . toHaveBeenCalled ( ) ;
141
- expect ( mockInitRustCrypto ) . toHaveBeenCalledTimes ( 1 ) ;
196
+ expect ( mockInitCrypto ) . toHaveBeenCalled ( ) ;
197
+ expect ( mockInitRustCrypto ) . not . toHaveBeenCalledTimes ( 1 ) ;
142
198
143
199
// 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 ) ;
145
201
} ) ;
146
202
147
203
describe ( "Rust staged rollout" , ( ) => {
@@ -178,10 +234,12 @@ describe("MatrixClientPeg", () => {
178
234
let mockInitCrypto : jest . SpyInstance ;
179
235
let mockInitRustCrypto : jest . SpyInstance ;
180
236
181
- beforeEach ( ( ) => {
237
+ beforeEach ( async ( ) => {
182
238
mockSetValue = jest . spyOn ( SettingsStore , "setValue" ) . mockResolvedValue ( undefined ) ;
183
239
mockInitCrypto = jest . spyOn ( testPeg . safeGet ( ) , "initCrypto" ) . mockResolvedValue ( undefined ) ;
184
240
mockInitRustCrypto = jest . spyOn ( testPeg . safeGet ( ) , "initRustCrypto" ) . mockResolvedValue ( undefined ) ;
241
+
242
+ await SettingsStore . setValue ( Features . RustCrypto , null , SettingLevel . DEVICE , null ) ;
185
243
} ) ;
186
244
187
245
it ( "Should not migrate existing login if rollout is 0" , async ( ) => {
@@ -254,28 +312,5 @@ describe("MatrixClientPeg", () => {
254
312
expect ( mockSetValue ) . toHaveBeenCalledWith ( "feature_rust_crypto" , null , SettingLevel . DEVICE , false ) ;
255
313
} ) ;
256
314
} ) ;
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
- } ) ;
280
315
} ) ;
281
316
} ) ;
0 commit comments