Skip to content

Commit

Permalink
chore: added tests for vaults handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
aryanjassal committed Jan 24, 2025
1 parent 5add70c commit bd8902a
Show file tree
Hide file tree
Showing 6 changed files with 323 additions and 288 deletions.
7 changes: 1 addition & 6 deletions src/client/handlers/VaultsRename.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import type { ContextTimed } from '@matrixai/contexts';
import type { DB } from '@matrixai/db';
import type { JSONValue } from '@matrixai/rpc';
import type {
ClientRPCRequestParams,
ClientRPCResponseResult,
Expand All @@ -22,9 +20,6 @@ class VaultsRename extends UnaryHandler<
> {
public handle = async (
input: ClientRPCRequestParams<VaultsRenameMessage>,
_cancel: (reason?: any) => void,
_meta: Record<string, JSONValue> | undefined,
ctx: ContextTimed,
): Promise<ClientRPCResponseResult<VaultIdMessage>> => {
const { db, vaultManager }: { db: DB; vaultManager: VaultManager } =
this.container;
Expand All @@ -40,7 +35,7 @@ class VaultsRename extends UnaryHandler<
`Vault "${input.nameOrId}" does not exist`,
);
}
await vaultManager.renameVault(vaultId, input.newName, tran, ctx);
await vaultManager.renameVault(vaultId, input.newName, tran);
return { vaultIdEncoded: vaultsUtils.encodeVaultId(vaultId) };
});
};
Expand Down
6 changes: 0 additions & 6 deletions src/client/handlers/VaultsVersion.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import type { ContextTimed } from '@matrixai/contexts';
import type { DB } from '@matrixai/db';
import type { JSONValue } from '@matrixai/rpc';
import type {
ClientRPCRequestParams,
ClientRPCResponseResult,
Expand All @@ -22,9 +20,6 @@ class VaultsVersion extends UnaryHandler<
> {
public handle = async (
input: ClientRPCRequestParams<VaultsVersionMessage>,
_cancel: (reason?: any) => void,
_meta: Record<string, JSONValue> | undefined,
ctx: ContextTimed,
): Promise<ClientRPCResponseResult<VaultsLatestVersionMessage>> => {
const { db, vaultManager }: { db: DB; vaultManager: VaultManager } =
this.container;
Expand Down Expand Up @@ -52,7 +47,6 @@ class VaultsVersion extends UnaryHandler<
return [latestOid, currentVersionId];
},
tran,
ctx,
);
// Checking if latest version ID
return { latestVersion: latestOid === currentVersionId };
Expand Down
10 changes: 1 addition & 9 deletions src/vaults/VaultManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -536,23 +536,15 @@ class VaultManager {
/**
* Changes the vault name metadata of a VaultId.
*/
public async renameVault(
vaultId: VaultId,
newVaultName: VaultName,
tran?: DBTransaction,
ctx?: Partial<ContextTimedInput>,
): Promise<void>;
@ready(new vaultsErrors.ErrorVaultManagerNotRunning())
@timedCancellable(true)
public async renameVault(
vaultId: VaultId,
newVaultName: VaultName,
tran: DBTransaction,
@context ctx: ContextTimed,
): Promise<void> {
if (tran == null) {
return this.db.withTransactionF((tran) =>
this.renameVault(vaultId, newVaultName, tran, ctx),
this.renameVault(vaultId, newVaultName, tran),
);
}

Expand Down
63 changes: 63 additions & 0 deletions tests/client/handlers/vaults.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import type {
SecretsRemoveHeaderMessage,
VaultListMessage,
VaultPermissionMessage,
VaultsLogMessage,
} from '@/client/types';
import fs from 'fs';
import path from 'path';
Expand Down Expand Up @@ -421,6 +422,68 @@ describe('vaultsLog', () => {
// Checking commits exist in order.
expect(logMessages[0].commitId).toEqual(commit2Oid);
});
test.prop([testsUtils.vaultNameArb(), testsUtils.fileNameArb()], {
numRuns: 2,
})('cancellation should abort the handler', async (vaultName, fileNames) => {
// Generate a random number of steps to cancel the handler after. Preserving
// an extra value to allow the loop to throw.
const maxLogicalSteps = fc.sample(
fc.integer({ min: 0, max: fileNames.length - 2 }),
1,
)[0];

const cancelMessage = new Error('cancel message');
const vaultId = await vaultManager.createVault(vaultName);
const vaultIdEncoded = vaultsUtils.encodeVaultId(vaultId);
await vaultManager.withVaults([vaultId], async (vault) => {
for (const file of fileNames) {
await vault.writeF(async (efs) => {
await efs.writeFile(file);
});
}
});

const inputVal: VaultsLogMessage = {
nameOrId: vaultIdEncoded,
};

// Instantiate the handler
let logicalStepsCounter = 0;
const handler = new VaultsLog({
db: db,
vaultManager: vaultManager,
});

// Create a dummy context object to be used for cancellation
const abortController = new AbortController();
const ctx = { signal: abortController.signal } as ContextTimed;

// The `cancel` and `meta` aren't being used here, so dummy values can be
// passed.
const result = handler.handle(inputVal, () => {}, {}, ctx);

// Create a promise which consumes data from the handler and advances the
// logical step counter. If the count matches a randomly selected value,
// then abort the handler, which would reject the promise.
const consumeP = async () => {
let aborted = false;
for await (const _ of result) {
// If we have already aborted, then the handler should not be sending
// any further information.
if (aborted) {
fail('The handler should not continue after cancellation');
}
// If we are on a logical step that matches what we have to abort on,
// then send an abort signal. Next loop should throw an error.
if (logicalStepsCounter === maxLogicalSteps) {
abortController.abort(cancelMessage);
aborted = true;
}
logicalStepsCounter++;
}
};
await expect(consumeP()).rejects.toThrow(cancelMessage);
});
});
describe('vaultsPermissionSet and vaultsPermissionUnset and vaultsPermissionGet', () => {
const logger = new Logger('vaultsPermissionSetUnsetGet test', LogLevel.WARN, [
Expand Down
Loading

0 comments on commit bd8902a

Please sign in to comment.