From ccbf3132de876f84e25cc862a5fcdb35217722da Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Mon, 11 May 2026 02:45:09 +0100 Subject: [PATCH] test: tighten matrix recovery key assertions --- .../src/matrix/sdk/recovery-key-store.test.ts | 57 ++++++++++++------- 1 file changed, 37 insertions(+), 20 deletions(-) diff --git a/extensions/matrix/src/matrix/sdk/recovery-key-store.test.ts b/extensions/matrix/src/matrix/sdk/recovery-key-store.test.ts index caf47513660..48142d0126c 100644 --- a/extensions/matrix/src/matrix/sdk/recovery-key-store.test.ts +++ b/extensions/matrix/src/matrix/sdk/recovery-key-store.test.ts @@ -57,6 +57,31 @@ function createRecoveryKeyCrypto(params: { } as unknown as MatrixCryptoBootstrapApi; } +function bootstrapSecretStorageCallArg( + bootstrapSecretStorage: ReturnType, + index: number, +) { + const call = bootstrapSecretStorage.mock.calls[index]; + if (!call) { + throw new Error(`expected bootstrapSecretStorage call ${index}`); + } + return call[0] as { setupNewSecretStorage?: boolean } | undefined; +} + +function expectRecoveryKeySummary( + store: MatrixRecoveryKeyStore, + expected: { keyId: string; encodedPrivateKey?: string }, +) { + const summary = store.getRecoveryKeySummary(); + if (!summary) { + throw new Error("expected recovery key summary"); + } + expect(summary.keyId).toBe(expected.keyId); + if (expected.encodedPrivateKey !== undefined) { + expect(summary.encodedPrivateKey).toBe(expected.encodedPrivateKey); + } +} + async function runSecretStorageBootstrapScenario(params: { generated: ReturnType; status: MatrixSecretStorageStatus; @@ -153,12 +178,10 @@ describe("MatrixRecoveryKeyStore", () => { }); expect(createRecoveryKeyFromPassphrase).toHaveBeenCalledTimes(1); - expect(bootstrapSecretStorage).toHaveBeenCalledWith( - expect.objectContaining({ - setupNewSecretStorage: true, - }), + expect(bootstrapSecretStorageCallArg(bootstrapSecretStorage, 0)?.setupNewSecretStorage).toBe( + true, ); - expect(store.getRecoveryKeySummary()).toMatchObject({ + expectRecoveryKeySummary(store, { keyId: "GENERATED", encodedPrivateKey: "encoded-generated-key", // pragma: allowlist secret }); @@ -194,7 +217,7 @@ describe("MatrixRecoveryKeyStore", () => { await store.bootstrapSecretStorageWithRecoveryKey(crypto); expect(createRecoveryKeyFromPassphrase).not.toHaveBeenCalled(); - expect(store.getRecoveryKeySummary()).toMatchObject({ + expectRecoveryKeySummary(store, { keyId: "NEW", }); }); @@ -212,12 +235,10 @@ describe("MatrixRecoveryKeyStore", () => { }); expect(createRecoveryKeyFromPassphrase).toHaveBeenCalledTimes(1); - expect(bootstrapSecretStorage).toHaveBeenCalledWith( - expect.objectContaining({ - setupNewSecretStorage: true, - }), + expect(bootstrapSecretStorageCallArg(bootstrapSecretStorage, 0)?.setupNewSecretStorage).toBe( + true, ); - expect(store.getRecoveryKeySummary()).toMatchObject({ + expectRecoveryKeySummary(store, { keyId: "RECOVERED", encodedPrivateKey: "encoded-recovered-key", // pragma: allowlist secret }); @@ -243,12 +264,10 @@ describe("MatrixRecoveryKeyStore", () => { expect(createRecoveryKeyFromPassphrase).toHaveBeenCalledTimes(1); expect(bootstrapSecretStorage).toHaveBeenCalledTimes(2); - expect(bootstrapSecretStorage).toHaveBeenLastCalledWith( - expect.objectContaining({ - setupNewSecretStorage: true, - }), + expect(bootstrapSecretStorageCallArg(bootstrapSecretStorage, 1)?.setupNewSecretStorage).toBe( + true, ); - expect(store.getRecoveryKeySummary()).toMatchObject({ + expectRecoveryKeySummary(store, { keyId: "REPAIRED", encodedPrivateKey: "encoded-repaired-key", // pragma: allowlist secret }); @@ -274,10 +293,8 @@ describe("MatrixRecoveryKeyStore", () => { expect(createRecoveryKeyFromPassphrase).toHaveBeenCalledTimes(1); expect(bootstrapSecretStorage).toHaveBeenCalledTimes(2); - expect(bootstrapSecretStorage).toHaveBeenLastCalledWith( - expect.objectContaining({ - setupNewSecretStorage: true, - }), + expect(bootstrapSecretStorageCallArg(bootstrapSecretStorage, 1)?.setupNewSecretStorage).toBe( + true, ); });