test: tighten matrix recovery key assertions

This commit is contained in:
Peter Steinberger
2026-05-11 02:45:09 +01:00
parent c2ac272287
commit ccbf3132de

View File

@@ -57,6 +57,31 @@ function createRecoveryKeyCrypto(params: {
} as unknown as MatrixCryptoBootstrapApi;
}
function bootstrapSecretStorageCallArg(
bootstrapSecretStorage: ReturnType<typeof vi.fn>,
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<typeof createGeneratedRecoveryKey>;
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,
);
});