



























@@ -3,6 +3,36 @@ import { MatrixCryptoBootstrapper, type MatrixCryptoBootstrapperDeps } from "./c
33import type { MatrixCryptoBootstrapApi, MatrixRawEvent } from "./types.js";
4455type BootstrapCrossSigningMock = Mock<MatrixCryptoBootstrapApi["bootstrapCrossSigning"]>;
6+type MockCallSource = { mock: { calls: Array<Array<unknown>> } };
7+8+function mockObjectArg(
9+source: MockCallSource,
10+label: string,
11+callIndex = 0,
12+argIndex = 0,
13+): Record<string, unknown> {
14+const call = source.mock.calls[callIndex];
15+if (!call) {
16+throw new Error(`Expected ${label} call ${callIndex} to exist`);
17+}
18+const value = call[argIndex];
19+if (!value || typeof value !== "object") {
20+throw new Error(`Expected ${label} call ${callIndex} argument ${argIndex} to be an object`);
21+}
22+return value as Record<string, unknown>;
23+}
24+25+function expectBootstrapCrossSigningCall(
26+source: MockCallSource,
27+callNumber: number,
28+expected?: { setupNewCrossSigning?: boolean },
29+) {
30+const options = mockObjectArg(source, "bootstrapCrossSigning", callNumber - 1);
31+expect(options.authUploadDeviceSigningKeys).toBeTypeOf("function");
32+if (expected && "setupNewCrossSigning" in expected) {
33+expect(options.setupNewCrossSigning).toBe(expected.setupNewCrossSigning);
34+}
35+}
636737function createBootstrapperDeps() {
838return {
@@ -106,19 +136,10 @@ function expectForcedResetCrossSigningCalls(
106136params: { setupNewCall: number; totalCalls: number },
107137) {
108138expect(bootstrapCrossSigning).toHaveBeenCalledTimes(params.totalCalls);
109-expect(bootstrapCrossSigning).toHaveBeenNthCalledWith(
110-params.setupNewCall,
111-expect.objectContaining({
112-setupNewCrossSigning: true,
113-authUploadDeviceSigningKeys: expect.any(Function),
114-}),
115-);
116-expect(bootstrapCrossSigning).toHaveBeenNthCalledWith(
117-params.totalCalls,
118-expect.objectContaining({
119-authUploadDeviceSigningKeys: expect.any(Function),
120-}),
121-);
139+expectBootstrapCrossSigningCall(bootstrapCrossSigning, params.setupNewCall, {
140+setupNewCrossSigning: true,
141+});
142+expectBootstrapCrossSigningCall(bootstrapCrossSigning, params.totalCalls);
122143}
123144124145async function bootstrapWithVerificationRequestListener(overrides?: {
@@ -169,11 +190,8 @@ describe("MatrixCryptoBootstrapper", () => {
169190170191await bootstrapper.bootstrap(crypto);
171192172-expect(crypto.bootstrapCrossSigning).toHaveBeenCalledWith(
173-expect.objectContaining({
174-authUploadDeviceSigningKeys: expect.any(Function),
175-}),
176-);
193+expect(crypto.bootstrapCrossSigning).toHaveBeenCalledOnce();
194+expectBootstrapCrossSigningCall(crypto.bootstrapCrossSigning as unknown as MockCallSource, 1);
177195expect(deps.recoveryKeyStore.bootstrapSecretStorageWithRecoveryKey).toHaveBeenCalledWith(
178196crypto,
179197{
@@ -209,19 +227,8 @@ describe("MatrixCryptoBootstrapper", () => {
209227await bootstrapper.bootstrap(crypto);
210228211229expect(bootstrapCrossSigning).toHaveBeenCalledTimes(2);
212-expect(bootstrapCrossSigning).toHaveBeenNthCalledWith(
213-1,
214-expect.objectContaining({
215-authUploadDeviceSigningKeys: expect.any(Function),
216-}),
217-);
218-expect(bootstrapCrossSigning).toHaveBeenNthCalledWith(
219-2,
220-expect.objectContaining({
221-setupNewCrossSigning: true,
222-authUploadDeviceSigningKeys: expect.any(Function),
223-}),
224-);
230+expectBootstrapCrossSigningCall(bootstrapCrossSigning, 1);
231+expectBootstrapCrossSigningCall(bootstrapCrossSigning, 2, { setupNewCrossSigning: true });
225232});
226233227234it("does not auto-reset cross-signing when automatic reset is disabled", async () => {
@@ -247,11 +254,7 @@ describe("MatrixCryptoBootstrapper", () => {
247254});
248255249256expect(bootstrapCrossSigning).toHaveBeenCalledTimes(1);
250-expect(bootstrapCrossSigning).toHaveBeenCalledWith(
251-expect.objectContaining({
252-authUploadDeviceSigningKeys: expect.any(Function),
253-}),
254-);
257+expectBootstrapCrossSigningCall(bootstrapCrossSigning, 1);
255258});
256259257260it("does not mark the own Matrix identity verified before cross-signing the current device", async () => {
@@ -349,18 +352,8 @@ describe("MatrixCryptoBootstrapper", () => {
349352);
350353351354expectSecretStorageRepairRetry(deps, crypto, bootstrapCrossSigning);
352-expect(bootstrapCrossSigning).toHaveBeenNthCalledWith(
353-1,
354-expect.objectContaining({
355-authUploadDeviceSigningKeys: expect.any(Function),
356-}),
357-);
358-expect(bootstrapCrossSigning).toHaveBeenNthCalledWith(
359-2,
360-expect.objectContaining({
361-authUploadDeviceSigningKeys: expect.any(Function),
362-}),
363-);
355+expectBootstrapCrossSigningCall(bootstrapCrossSigning, 1);
356+expectBootstrapCrossSigningCall(bootstrapCrossSigning, 2);
364357});
365358366359it("recreates secret storage and retries cross-signing when explicit bootstrap hits bad MAC", async () => {
@@ -592,13 +585,7 @@ describe("MatrixCryptoBootstrapper", () => {
592585await bootstrapper.bootstrap(crypto);
593586594587expect(bootstrapCrossSigning).toHaveBeenCalledTimes(2);
595-expect(bootstrapCrossSigning).toHaveBeenNthCalledWith(
596-2,
597-expect.objectContaining({
598-setupNewCrossSigning: true,
599-authUploadDeviceSigningKeys: expect.any(Function),
600-}),
601-);
588+expectBootstrapCrossSigningCall(bootstrapCrossSigning, 2, { setupNewCrossSigning: true });
602589});
603590604591it("marks own device verified and cross-signs it when needed", async () => {
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。