Skip to content

Commit

Permalink
chore: fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aryanjassal committed Aug 23, 2024
1 parent f1eac5e commit 8e881e7
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 71 deletions.
61 changes: 21 additions & 40 deletions src/client/handlers/VaultsSecretsList.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
import type { DB } from '@matrixai/db';
import type {
ClientRPCRequestParams,
ClientRPCResponseResult,
SecretFilesMessage,
SecretFiles,
} from '../types';
import type VaultManager from '../../vaults/VaultManager';
import type { ClientRPCRequestParams, ClientRPCResponseResult } from '../types';
import type { SecretFilesMessage, SecretFile } from '../types';
import type { StatEncoded } from '../../vaults/types';
import path from 'path';
import { ServerHandler } from '@matrixai/rpc';
import { generateStats } from '../../vaults/fileTree';
import * as vaultsUtils from '../../vaults/utils';
import * as vaultsErrors from '../../vaults/errors';
import * as clientErrors from '../errors';

class VaultsSecretsGetFileTree extends ServerHandler<
class VaultsSecretsList extends ServerHandler<
{
vaultManager: VaultManager;
db: DB;
},
ClientRPCRequestParams<SecretFilesMessage>,
ClientRPCResponseResult<SecretFile>
ClientRPCResponseResult<SecretFiles>
> {
public async *handle(
input: ClientRPCRequestParams<SecretFilesMessage>,
_cancel: any,
): AsyncGenerator<ClientRPCResponseResult<SecretFile>, void, void> {
): AsyncGenerator<ClientRPCResponseResult<SecretFiles>, void, void> {
const { vaultManager, db } = this.container;
const vaultId = await db.withTransactionF(async (tran) => {
const vaultIdFromName = await vaultManager.getVaultId(
Expand All @@ -36,51 +38,30 @@ class VaultsSecretsGetFileTree extends ServerHandler<

yield* vaultManager.withVaultsG([vaultId], (vault) => {
return vault.readG(async function* (fs): AsyncGenerator<
SecretFile,
SecretFiles,
void,
void
> {
let files: Array<string>;
try {
// @ts-ignore: While the types don't fully match, it matches enough for our usage.
let files: Array<string> = await fs.promises.readdir(input.path);
files = files.map((file) => path.join(input.path, file));

for await (const file of files) {
try {
const stat = await fs.promises.lstat(file);
const type = stat.isFile() ? 'FILE' : 'DIRECTORY';
let stats: StatEncoded | undefined;

if (input.yieldStats) {
stats = generateStats(stat);
if (stats.isSymbolicLink) {
// @ts-ignore: Again, the types don't fully match, but it works.
stats.symbolicLinkTarget = await fs.promises.readlink(file);
}
} else {
stats = undefined;
}
yield {
path: file,
type: type,
stat: stats,
};
} catch (e) {
throw new clientErrors.ErrorClientFSReadFailed(
`Failed to read file: ${file}`,
{ cause: e },
);
}
}
files = await fs.promises.readdir(input.path);
} catch (e) {
throw new clientErrors.ErrorClientFSReadFailed(
`Failed to read directory: ${input.path}`,
`No matches exist for path: ${input.path}`,
{ cause: e },
);
}
files = files.map((file) => path.join(input.path, file));

for await (const file of files) {
const stat = await fs.promises.stat(file);
const type = stat.isFile() ? 'FILE' : 'DIRECTORY';
yield { path: file, type: type };
}
});
});
}
}

export default VaultsSecretsGetFileTree;
export default VaultsSecretsList;
13 changes: 3 additions & 10 deletions src/client/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,7 @@ import type {
VaultIdEncoded,
} from '../ids';
import type { GestaltAction } from '../gestalts/types';
import type {
CommitId,
StatEncoded,
VaultAction,
VaultName,
} from '../vaults/types';
import type { CommitId, VaultAction, VaultName } from '../vaults/types';
import type { CertificatePEM, JWKEncrypted, PublicKeyJWK } from '../keys/types';
import type { Notification } from '../notifications/types';
import type { ProviderToken } from '../identities/types';
Expand Down Expand Up @@ -307,7 +302,6 @@ type VaultsLatestVersionMessage = {
// Secrets
type SecretFilesMessage = VaultIdentifierMessage & {
path: string;
yieldStats: boolean;
};

type SecretNameMessage = {
Expand Down Expand Up @@ -336,10 +330,9 @@ type SecretRenameMessage = SecretIdentifierMessage & {
newSecretName: string;
};

type SecretFile = {
type SecretFiles = {
path: string;
type: 'FILE' | 'DIRECTORY';
stat?: StatEncoded;
};

// Stat is the 'JSON.stringify version of the file stat
Expand Down Expand Up @@ -433,7 +426,7 @@ export type {
SecretMkdirMessage,
SecretDirMessage,
SecretRenameMessage,
SecretFile,
SecretFiles,
SecretStatMessage,
SignatureMessage,
OverrideRPClientType,
Expand Down
6 changes: 1 addition & 5 deletions src/vaults/fileTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,11 +370,7 @@ function serializerStreamFactory(
}
return new ReadableStream<Uint8Array>({
start: (controller) => {
controller.enqueue(
generateGenericHeader({
type: HeaderType.TREE,
}),
);
controller.enqueue(generateGenericHeader({ type: HeaderType.TREE }));
},
pull: async (controller) => {
try {
Expand Down
2 changes: 0 additions & 2 deletions tests/client/handlers/vaults.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1593,7 +1593,6 @@ describe('vaultsSecretsNewDir and vaultsSecretsList', () => {
const files = await rpcClient.methods.vaultsSecretsList({
nameOrId: vaultsIdEncoded,
path: 'doesntExist',
yieldStats: false,
});
try {
for await (const _ of files); // Consume values
Expand All @@ -1605,7 +1604,6 @@ describe('vaultsSecretsNewDir and vaultsSecretsList', () => {
const secrets = await rpcClient.methods.vaultsSecretsList({
nameOrId: vaultsIdEncoded,
path: 'secretDir',
yieldStats: false,
});

// Extract secret file paths
Expand Down
5 changes: 1 addition & 4 deletions tests/vaults/VaultOps.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import * as vaultOps from '@/vaults/VaultOps';
import * as vaultsErrors from '@/vaults/errors';
import * as vaultsUtils from '@/vaults/utils';
import * as keysUtils from '@/keys/utils';
import * as utils from '@/utils';
import * as testNodesUtils from '../nodes/utils';

describe('VaultOps', () => {
Expand Down Expand Up @@ -590,13 +589,11 @@ describe('VaultOps', () => {
});
const data: Array<TreeNode | ContentNode | Uint8Array> = [];
const parserTransform = fileTree.parserTransformStreamFactory();
const serializedGen = fileTree.serializerStreamFactory(
const serializedStream = fileTree.serializerStreamFactory(
fs,
fileTreeGen,
true,
);
const serializedStream =
utils.asyncGeneratorToReadableStream(serializedGen);
const outputStream = serializedStream.pipeThrough(parserTransform);
for await (const output of outputStream) {
data.push(output);
Expand Down
13 changes: 3 additions & 10 deletions tests/vaults/fileTree.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { ReadableStream } from 'stream/web';
import { test } from '@fast-check/jest';
import fc from 'fast-check';
import * as fileTree from '@/vaults/fileTree';
import * as utils from '@/utils';
import * as vaultsTestUtils from './utils';

describe('fileTree', () => {
Expand Down Expand Up @@ -497,13 +496,11 @@ describe('fileTree', () => {
});
const data: Array<TreeNode | ContentNode | Uint8Array> = [];
const parserTransform = fileTree.parserTransformStreamFactory();
const serializedGen = fileTree.serializerStreamFactory(
const serializedStream = fileTree.serializerStreamFactory(
fs,
fileTreeGen,
false,
);
const serializedStream =
utils.asyncGeneratorToReadableStream(serializedGen);
const outputStream = serializedStream.pipeThrough(parserTransform);
for await (const output of outputStream) {
data.push(output);
Expand Down Expand Up @@ -545,13 +542,11 @@ describe('fileTree', () => {
5, 7, 11, 13,
]);
const parserTransform = fileTree.parserTransformStreamFactory();
const serializedGen = fileTree.serializerStreamFactory(
const serializedStream = fileTree.serializerStreamFactory(
fs,
fileTreeGen,
false,
);
const serializedStream =
utils.asyncGeneratorToReadableStream(serializedGen);
const outputStream = serializedStream
.pipeThrough(snipperTransform)
.pipeThrough(parserTransform);
Expand Down Expand Up @@ -664,13 +659,11 @@ describe('fileTree', () => {
});
const data: Array<TreeNode | ContentNode | Uint8Array> = [];
const parserTransform = fileTree.parserTransformStreamFactory();
const serializedGen = fileTree.serializerStreamFactory(
const serializedStream = fileTree.serializerStreamFactory(
fs,
fileTreeGen,
true,
);
const serializedStream =
utils.asyncGeneratorToReadableStream(serializedGen);
const outputStream = serializedStream.pipeThrough(parserTransform);
for await (const output of outputStream) {
data.push(output);
Expand Down

0 comments on commit 8e881e7

Please sign in to comment.