diff --git a/extensions/matrix/src/matrix/actions/verification.test.ts b/extensions/matrix/src/matrix/actions/verification.test.ts index fbf5f6a2291..a7aac1991f7 100644 --- a/extensions/matrix/src/matrix/actions/verification.test.ts +++ b/extensions/matrix/src/matrix/actions/verification.test.ts @@ -40,6 +40,44 @@ let runMatrixSelfVerification: typeof import("./verification.js").runMatrixSelfV let startMatrixVerification: typeof import("./verification.js").startMatrixVerification; let confirmMatrixVerificationSas: typeof import("./verification.js").confirmMatrixVerificationSas; +type MockCallSource = { mock: { calls: Array> } }; + +function mockCallArg(source: MockCallSource, label: string, callIndex = 0, argIndex = 0): unknown { + const call = source.mock.calls[callIndex]; + if (!call) { + throw new Error(`Expected ${label} call ${callIndex} to exist`); + } + if (!(argIndex in call)) { + throw new Error(`Expected ${label} call ${callIndex} argument ${argIndex} to exist`); + } + return call[argIndex]; +} + +function mockObjectArg( + source: MockCallSource, + label: string, + callIndex = 0, + argIndex = 0, +): Record { + const value = mockCallArg(source, label, callIndex, argIndex); + if (!value || typeof value !== "object") { + throw new Error(`Expected ${label} call ${callIndex} argument ${argIndex} to be an object`); + } + return value as Record; +} + +function expectResolvedActionClientReadinessNone(): void { + expect(mockObjectArg(withResolvedActionClientMock, "withResolvedActionClient").readiness).toBe( + "none", + ); + expect(mockCallArg(withResolvedActionClientMock, "withResolvedActionClient", 0, 1)).toBeTypeOf( + "function", + ); + expect(mockCallArg(withResolvedActionClientMock, "withResolvedActionClient", 0, 2)).toBe( + "discard", + ); +} + describe("matrix verification actions", () => { beforeAll(async () => { ({ @@ -219,17 +257,11 @@ describe("matrix verification actions", () => { const status = await getMatrixVerificationStatus({ includeRecoveryKey: true }); - expect(status).toMatchObject({ - verified: true, - pendingVerifications: 0, - recoveryKey: "rec-key", - }); + expect(status.verified).toBe(true); + expect(status.pendingVerifications).toBe(0); + expect("recoveryKey" in status ? status.recoveryKey : undefined).toBe("rec-key"); expect(withResolvedActionClientMock).toHaveBeenCalledTimes(1); - expect(withResolvedActionClientMock).toHaveBeenCalledWith( - expect.objectContaining({ readiness: "none" }), - expect.any(Function), - "discard", - ); + expectResolvedActionClientReadinessNone(); expect(prepareForOneOff).toHaveBeenCalledTimes(1); expect(start).not.toHaveBeenCalled(); expect(getOwnDeviceVerificationStatus).toHaveBeenCalledTimes(2); @@ -273,16 +305,10 @@ describe("matrix verification actions", () => { const status = await getMatrixVerificationStatus(); - expect(status).toMatchObject({ - deviceId: "DEVICE123", - serverDeviceKnown: false, - pendingVerifications: 0, - }); - expect(withResolvedActionClientMock).toHaveBeenCalledWith( - expect.objectContaining({ readiness: "none" }), - expect.any(Function), - "discard", - ); + expect(status.deviceId).toBe("DEVICE123"); + expect(status.serverDeviceKnown).toBe(false); + expect(status.pendingVerifications).toBe(0); + expectResolvedActionClientReadinessNone(); expect(prepareForOneOff).not.toHaveBeenCalled(); expect(getOwnDeviceVerificationStatus).toHaveBeenCalledTimes(1); }); @@ -317,16 +343,12 @@ describe("matrix verification actions", () => { const encryption = await getMatrixEncryptionStatus({ includeRecoveryKey: true }); const backup = await getMatrixRoomKeyBackupStatus(); - expect(encryption).toMatchObject({ - encryptionEnabled: true, - recoveryKeyStored: true, - recoveryKey: "rec-key", - pendingVerifications: 1, - }); - expect(backup).toMatchObject({ - serverVersion: "11", - trusted: true, - }); + expect(encryption.encryptionEnabled).toBe(true); + expect(encryption.recoveryKeyStored).toBe(true); + expect(encryption.recoveryKey).toBe("rec-key"); + expect(encryption.pendingVerifications).toBe(1); + expect(backup.serverVersion).toBe("11"); + expect(backup.trusted).toBe(true); expect(withResolvedActionClientMock).toHaveBeenCalledTimes(2); expect(withStartedActionClientMock).not.toHaveBeenCalled(); }); @@ -343,7 +365,7 @@ describe("matrix verification actions", () => { const restored = await restoreMatrixRoomKeyBackup({ recoveryKey: " key " }); - expect(restored).toMatchObject({ success: true }); + expect(restored.success).toBe(true); expect(restoreRoomKeyBackup).toHaveBeenCalledWith({ recoveryKey: "key" }); expect(withResolvedActionClientMock).toHaveBeenCalledTimes(1); expect(withStartedActionClientMock).not.toHaveBeenCalled(); @@ -370,16 +392,14 @@ describe("matrix verification actions", () => { return await run({ crypto }); }); - await expect( - startMatrixVerification("txn-dm", { - verificationDmRoomId: "!dm:example.org", - verificationDmUserId: "@alice:example.org", - }), - ).resolves.toMatchObject({ - id: "verification-1", - phaseName: "started", + const result = await startMatrixVerification("txn-dm", { + verificationDmRoomId: "!dm:example.org", + verificationDmUserId: "@alice:example.org", }); + expect(result.id).toBe("verification-1"); + expect(result.phaseName).toBe("started"); + expect(crypto.ensureVerificationDmTracked).toHaveBeenCalledWith({ roomId: "!dm:example.org", userId: "@alice:example.org", @@ -460,14 +480,12 @@ describe("matrix verification actions", () => { }); }); - await expect(runMatrixSelfVerification({ confirmSas, timeoutMs: 500 })).resolves.toMatchObject({ - completed: true, - deviceOwnerVerified: true, - id: "verification-1", - ownerVerification: { - verified: true, - }, - }); + const result = await runMatrixSelfVerification({ confirmSas, timeoutMs: 500 }); + + expect(result.completed).toBe(true); + expect(result.deviceOwnerVerified).toBe(true); + expect(result.id).toBe("verification-1"); + expect((result.ownerVerification as { verified?: unknown } | undefined)?.verified).toBe(true); expect(withStartedActionClientMock).toHaveBeenCalledTimes(1); expect(crypto.requestVerification).toHaveBeenCalledWith({ ownUser: true }); @@ -529,16 +547,15 @@ describe("matrix verification actions", () => { }); }); - await expect( - runMatrixSelfVerification({ confirmSas: vi.fn(async () => true), timeoutMs: 500 }), - ).resolves.toMatchObject({ - completed: true, - deviceOwnerVerified: true, - ownerVerification: { - verified: true, - }, + const result = await runMatrixSelfVerification({ + confirmSas: vi.fn(async () => true), + timeoutMs: 500, }); + expect(result.completed).toBe(true); + expect(result.deviceOwnerVerified).toBe(true); + expect((result.ownerVerification as { verified?: unknown } | undefined)?.verified).toBe(true); + expect(getOwnDeviceVerificationStatus).toHaveBeenCalledTimes(2); expect(getOwnCrossSigningPublicationStatus).toHaveBeenCalledTimes(2); expect(trustOwnIdentityAfterSelfVerification).toHaveBeenCalledTimes(1); @@ -595,13 +612,14 @@ describe("matrix verification actions", () => { }); }); - await expect( - runMatrixSelfVerification({ confirmSas: vi.fn(async () => true), timeoutMs: 500 }), - ).resolves.toMatchObject({ - completed: true, - deviceOwnerVerified: true, + const result = await runMatrixSelfVerification({ + confirmSas: vi.fn(async () => true), + timeoutMs: 500, }); + expect(result.completed).toBe(true); + expect(result.deviceOwnerVerified).toBe(true); + expect(getOwnDeviceIdentityVerificationStatus).not.toHaveBeenCalled(); expect(getOwnDeviceVerificationStatus).toHaveBeenCalledTimes(1); }); @@ -654,16 +672,15 @@ describe("matrix verification actions", () => { }); }); - await expect( - runMatrixSelfVerification({ confirmSas: vi.fn(async () => true), timeoutMs: 500 }), - ).resolves.toMatchObject({ - completed: true, - deviceOwnerVerified: true, - ownerVerification: { - verified: true, - }, + const result = await runMatrixSelfVerification({ + confirmSas: vi.fn(async () => true), + timeoutMs: 500, }); + expect(result.completed).toBe(true); + expect(result.deviceOwnerVerified).toBe(true); + expect((result.ownerVerification as { verified?: unknown } | undefined)?.verified).toBe(true); + expect(getOwnDeviceVerificationStatus).toHaveBeenCalledTimes(2); expect(getOwnCrossSigningPublicationStatus).toHaveBeenCalledTimes(2); expect(trustOwnIdentityAfterSelfVerification).not.toHaveBeenCalled(); @@ -713,13 +730,14 @@ describe("matrix verification actions", () => { }); }); - await expect( - runMatrixSelfVerification({ confirmSas: vi.fn(async () => true), timeoutMs: 500 }), - ).resolves.toMatchObject({ - completed: true, - deviceOwnerVerified: true, + const result = await runMatrixSelfVerification({ + confirmSas: vi.fn(async () => true), + timeoutMs: 500, }); + expect(result.completed).toBe(true); + expect(result.deviceOwnerVerified).toBe(true); + expect(crypto.startVerification).not.toHaveBeenCalled(); }); @@ -793,11 +811,11 @@ describe("matrix verification actions", () => { }); }); - await expect(runMatrixSelfVerification({ confirmSas, timeoutMs: 500 })).resolves.toMatchObject({ - completed: true, - deviceOwnerVerified: true, - id: "verification-1", - }); + const result = await runMatrixSelfVerification({ confirmSas, timeoutMs: 500 }); + + expect(result.completed).toBe(true); + expect(result.deviceOwnerVerified).toBe(true); + expect(result.id).toBe("verification-1"); expect(crypto.listVerifications).not.toHaveBeenCalled(); expect(crypto.startVerification).not.toHaveBeenCalled(); @@ -847,12 +865,13 @@ describe("matrix verification actions", () => { }); }); - await expect( - runMatrixSelfVerification({ confirmSas: vi.fn(async () => true), timeoutMs: 500 }), - ).resolves.toMatchObject({ - completed: true, - deviceOwnerVerified: true, + const result = await runMatrixSelfVerification({ + confirmSas: vi.fn(async () => true), + timeoutMs: 500, }); + + expect(result.completed).toBe(true); + expect(result.deviceOwnerVerified).toBe(true); }); it("fails self-verification if SAS completes but full identity trust cannot be established", async () => {