Skip to content

Commit bd8902a

Browse files
committed
chore: added tests for vaults handlers
1 parent 5add70c commit bd8902a

File tree

6 files changed

+323
-288
lines changed

6 files changed

+323
-288
lines changed

src/client/handlers/VaultsRename.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import type { ContextTimed } from '@matrixai/contexts';
21
import type { DB } from '@matrixai/db';
3-
import type { JSONValue } from '@matrixai/rpc';
42
import type {
53
ClientRPCRequestParams,
64
ClientRPCResponseResult,
@@ -22,9 +20,6 @@ class VaultsRename extends UnaryHandler<
2220
> {
2321
public handle = async (
2422
input: ClientRPCRequestParams<VaultsRenameMessage>,
25-
_cancel: (reason?: any) => void,
26-
_meta: Record<string, JSONValue> | undefined,
27-
ctx: ContextTimed,
2823
): Promise<ClientRPCResponseResult<VaultIdMessage>> => {
2924
const { db, vaultManager }: { db: DB; vaultManager: VaultManager } =
3025
this.container;
@@ -40,7 +35,7 @@ class VaultsRename extends UnaryHandler<
4035
`Vault "${input.nameOrId}" does not exist`,
4136
);
4237
}
43-
await vaultManager.renameVault(vaultId, input.newName, tran, ctx);
38+
await vaultManager.renameVault(vaultId, input.newName, tran);
4439
return { vaultIdEncoded: vaultsUtils.encodeVaultId(vaultId) };
4540
});
4641
};

src/client/handlers/VaultsVersion.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import type { ContextTimed } from '@matrixai/contexts';
21
import type { DB } from '@matrixai/db';
3-
import type { JSONValue } from '@matrixai/rpc';
42
import type {
53
ClientRPCRequestParams,
64
ClientRPCResponseResult,
@@ -22,9 +20,6 @@ class VaultsVersion extends UnaryHandler<
2220
> {
2321
public handle = async (
2422
input: ClientRPCRequestParams<VaultsVersionMessage>,
25-
_cancel: (reason?: any) => void,
26-
_meta: Record<string, JSONValue> | undefined,
27-
ctx: ContextTimed,
2823
): Promise<ClientRPCResponseResult<VaultsLatestVersionMessage>> => {
2924
const { db, vaultManager }: { db: DB; vaultManager: VaultManager } =
3025
this.container;
@@ -52,7 +47,6 @@ class VaultsVersion extends UnaryHandler<
5247
return [latestOid, currentVersionId];
5348
},
5449
tran,
55-
ctx,
5650
);
5751
// Checking if latest version ID
5852
return { latestVersion: latestOid === currentVersionId };

src/vaults/VaultManager.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -536,23 +536,15 @@ class VaultManager {
536536
/**
537537
* Changes the vault name metadata of a VaultId.
538538
*/
539-
public async renameVault(
540-
vaultId: VaultId,
541-
newVaultName: VaultName,
542-
tran?: DBTransaction,
543-
ctx?: Partial<ContextTimedInput>,
544-
): Promise<void>;
545539
@ready(new vaultsErrors.ErrorVaultManagerNotRunning())
546-
@timedCancellable(true)
547540
public async renameVault(
548541
vaultId: VaultId,
549542
newVaultName: VaultName,
550543
tran: DBTransaction,
551-
@context ctx: ContextTimed,
552544
): Promise<void> {
553545
if (tran == null) {
554546
return this.db.withTransactionF((tran) =>
555-
this.renameVault(vaultId, newVaultName, tran, ctx),
547+
this.renameVault(vaultId, newVaultName, tran),
556548
);
557549
}
558550

tests/client/handlers/vaults.test.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import type {
1313
SecretsRemoveHeaderMessage,
1414
VaultListMessage,
1515
VaultPermissionMessage,
16+
VaultsLogMessage,
1617
} from '@/client/types';
1718
import fs from 'fs';
1819
import path from 'path';
@@ -421,6 +422,68 @@ describe('vaultsLog', () => {
421422
// Checking commits exist in order.
422423
expect(logMessages[0].commitId).toEqual(commit2Oid);
423424
});
425+
test.prop([testsUtils.vaultNameArb(), testsUtils.fileNameArb()], {
426+
numRuns: 2,
427+
})('cancellation should abort the handler', async (vaultName, fileNames) => {
428+
// Generate a random number of steps to cancel the handler after. Preserving
429+
// an extra value to allow the loop to throw.
430+
const maxLogicalSteps = fc.sample(
431+
fc.integer({ min: 0, max: fileNames.length - 2 }),
432+
1,
433+
)[0];
434+
435+
const cancelMessage = new Error('cancel message');
436+
const vaultId = await vaultManager.createVault(vaultName);
437+
const vaultIdEncoded = vaultsUtils.encodeVaultId(vaultId);
438+
await vaultManager.withVaults([vaultId], async (vault) => {
439+
for (const file of fileNames) {
440+
await vault.writeF(async (efs) => {
441+
await efs.writeFile(file);
442+
});
443+
}
444+
});
445+
446+
const inputVal: VaultsLogMessage = {
447+
nameOrId: vaultIdEncoded,
448+
};
449+
450+
// Instantiate the handler
451+
let logicalStepsCounter = 0;
452+
const handler = new VaultsLog({
453+
db: db,
454+
vaultManager: vaultManager,
455+
});
456+
457+
// Create a dummy context object to be used for cancellation
458+
const abortController = new AbortController();
459+
const ctx = { signal: abortController.signal } as ContextTimed;
460+
461+
// The `cancel` and `meta` aren't being used here, so dummy values can be
462+
// passed.
463+
const result = handler.handle(inputVal, () => {}, {}, ctx);
464+
465+
// Create a promise which consumes data from the handler and advances the
466+
// logical step counter. If the count matches a randomly selected value,
467+
// then abort the handler, which would reject the promise.
468+
const consumeP = async () => {
469+
let aborted = false;
470+
for await (const _ of result) {
471+
// If we have already aborted, then the handler should not be sending
472+
// any further information.
473+
if (aborted) {
474+
fail('The handler should not continue after cancellation');
475+
}
476+
// If we are on a logical step that matches what we have to abort on,
477+
// then send an abort signal. Next loop should throw an error.
478+
if (logicalStepsCounter === maxLogicalSteps) {
479+
abortController.abort(cancelMessage);
480+
aborted = true;
481+
}
482+
logicalStepsCounter++;
483+
}
484+
};
485+
await expect(consumeP()).rejects.toThrow(cancelMessage);
486+
});
424487
});
425488
describe('vaultsPermissionSet and vaultsPermissionUnset and vaultsPermissionGet', () => {
426489
const logger = new Logger('vaultsPermissionSetUnsetGet test', LogLevel.WARN, [

0 commit comments

Comments
 (0)