Simplify and speed up DeviceListener (#33543)

* Fix incoherent DeviceListener tests

* Re-order DeviceListenerCurrentDevice.recheck to reduce unnecessary work

* Introduce scopes in recheck to demonstrate which variables are shared

* Break recheck into separate functions
This commit is contained in:
Andy Balaam
2026-06-03 14:33:28 +00:00
committed by GitHub
parent 2c8fafdec4
commit cb37bfcf36
2 changed files with 177 additions and 53 deletions
+39 -12
View File
@@ -591,31 +591,58 @@ describe("DeviceListener", () => {
describe("key backup status", () => {
it("checks keybackup status when cross signing and secret storage are ready", async () => {
// default mocks set cross signing and secret storage to ready
// Given our device is verified
mockCrypto.getDeviceVerificationStatus.mockResolvedValue({
crossSigningVerified: true,
} as any as DeviceVerificationStatus);
// When we check the current device
await createAndStart();
// Then we get to the code that checks the active session backup
expect(mockCrypto.getActiveSessionBackupVersion).toHaveBeenCalled();
});
it("checks keybackup status when setup encryption toast has been dismissed", async () => {
mockCrypto!.isCrossSigningReady.mockResolvedValue(false);
const instance = await createAndStart();
it("does not check keybackup status when setup encryption toast has been dismissed", async () => {
// Given our device is not verified (this is the default in the mock)
// And we have run the checks once (and we were told to verify)
const instance = await createAndStart();
expect(SetupEncryptionToast.showToast).toHaveBeenCalledWith("verify_this_session");
mocked(SetupEncryptionToast.showToast).mockClear();
mockCrypto.getDeviceVerificationStatus.mockClear();
// When we dismiss the dialog telling us to set up encryption
instance.dismissEncryptionSetup();
await flushPromises();
expect(mockCrypto.getActiveSessionBackupVersion).toHaveBeenCalled();
// Then we have rechecked, but stopped when we found the device was not verified
expect(mockCrypto.getDeviceVerificationStatus).toHaveBeenCalled();
expect(SetupEncryptionToast.showToast).not.toHaveBeenCalled();
});
it("does not check key backup status again after check is complete", async () => {
mockCrypto.getActiveSessionBackupVersion.mockResolvedValue("1");
const instance = await createAndStart();
expect(mockCrypto.getActiveSessionBackupVersion).toHaveBeenCalled();
// Given our device is verified
mockCrypto.getDeviceVerificationStatus.mockResolvedValue({
crossSigningVerified: true,
} as any as DeviceVerificationStatus);
// trigger a recheck
instance.dismissEncryptionSetup();
// And our active backup version is 1
mockCrypto.getActiveSessionBackupVersion.mockResolvedValue("1");
// And we have run the checks once: no toast was shown and we
// checked the backup version
const instance = await createAndStart();
expect(SetupEncryptionToast.showToast).not.toHaveBeenCalled();
expect(mockCrypto.getActiveSessionBackupVersion).toHaveBeenCalled();
mockCrypto.getActiveSessionBackupVersion.mockClear();
// When we check again
instance.recheck();
await flushPromises();
// not called again, check was complete last time
expect(mockCrypto.getActiveSessionBackupVersion).toHaveBeenCalledTimes(1);
// Then we didn't re-call getActiveSessionBackupVersion
expect(mockCrypto.getActiveSessionBackupVersion).not.toHaveBeenCalled();
});
});