From ce23508783cbb47620733d2308ddd334cb0f8a4d Mon Sep 17 00:00:00 2001 From: Brian Botha Date: Thu, 10 Feb 2022 18:16:16 +1100 Subject: [PATCH] WIP - general rebase fixes --- src/nodes/NodeConnection.ts | 5 +- src/validation/utils.ts | 13 +- src/vaults/types.ts | 4 +- tests/bin/notifications/notifications.test.ts | 4 +- tests/client/rpcVaults.test.ts | 8 +- tests/notifications/utils.test.ts | 13 +- tests/vaults/VaultInternal.test.ts | 183 +++++++++--------- tests/vaults/utils.test.ts | 1 - 8 files changed, 120 insertions(+), 111 deletions(-) diff --git a/src/nodes/NodeConnection.ts b/src/nodes/NodeConnection.ts index e169a4a2d6..814b89b016 100644 --- a/src/nodes/NodeConnection.ts +++ b/src/nodes/NodeConnection.ts @@ -28,6 +28,7 @@ import * as utilsPB from '../proto/js/polykey/v1/utils/utils_pb'; import * as nodesPB from '../proto/js/polykey/v1/nodes/nodes_pb'; import * as notificationsPB from '../proto/js/polykey/v1/notifications/notifications_pb'; import { utils as networkUtils } from '../network'; +import * as validationUtils from '../validation/utils'; /** * Encapsulates the unidirectional client-side connection of one node to another. @@ -454,13 +455,13 @@ class NodeConnection { nodeId: NodeId, ): Promise> { const nodeIdMessage = new nodesPB.Node(); - nodeIdMessage.setNodeId(nodeId); + nodeIdMessage.setNodeId(nodesUtils.encodeNodeId(nodeId)); const vaults: Array<[VaultName, VaultId]> = []; const genReadable = this.client.vaultsScan(nodeIdMessage); for await (const vault of genReadable) { vaults.push([ vault.getVaultName() as VaultName, - vaultsUtils.makeVaultId(vault.getVaultId()), + validationUtils.parseVaultId(vault.getVaultId()), ]); } return vaults; diff --git a/src/validation/utils.ts b/src/validation/utils.ts index 5659fb8786..0e0e587ef3 100644 --- a/src/validation/utils.ts +++ b/src/validation/utils.ts @@ -9,7 +9,7 @@ import type { NodeId } from '../nodes/types'; import type { ProviderId, IdentityId } from '../identities/types'; import type { GestaltAction, GestaltId } from '../gestalts/types'; -import type { VaultAction } from '../vaults/types'; +import type { VaultAction, VaultId } from '../vaults/types'; import type { Host, Hostname, Port } from '../network/types'; import type { ClaimId } from '../claims/types'; import * as validationErrors from './errors'; @@ -81,6 +81,16 @@ function parseClaimId(data: any): ClaimId { return data; } +function parseVaultId(data: any): VaultId { + data = vaultsUtils.decodeVaultId(data); + if (data == null) { + throw new validationErrors.ErrorParse( + 'Vault ID must be multibase base58btc encoded strings', + ) + } + return data; +} + function parseGestaltAction(data: any): GestaltAction { if (!gestaltsUtils.isGestaltAction(data)) { throw new validationErrors.ErrorParse( @@ -179,6 +189,7 @@ export { parseNodeId, parseGestaltId, parseClaimId, + parseVaultId, parseGestaltAction, parseVaultAction, parseProviderId, diff --git a/src/vaults/types.ts b/src/vaults/types.ts index 950225886a..b6e679a310 100644 --- a/src/vaults/types.ts +++ b/src/vaults/types.ts @@ -145,7 +145,7 @@ interface FileSystemWritable extends FileSystemReadable { // } // >; -// type VaultName = Opaque<'VaultName', string>; +type VaultName = string; // FIXME, placeholder, remove? // type VaultKey = Opaque<'VaultKey', Buffer>; @@ -195,7 +195,7 @@ export type { FileSystemWritable, // VaultIdPretty, // VaultKey, - // VaultName, + VaultName, // VaultList, // VaultMap, // VaultMetadata, diff --git a/tests/bin/notifications/notifications.test.ts b/tests/bin/notifications/notifications.test.ts index b8fa99b9ba..cdf59ebf29 100644 --- a/tests/bin/notifications/notifications.test.ts +++ b/tests/bin/notifications/notifications.test.ts @@ -7,7 +7,7 @@ import fs from 'fs'; import Logger, { LogLevel, StreamHandler } from '@matrixai/logger'; import { utils as idUtils } from '@matrixai/id'; import PolykeyAgent from '@/PolykeyAgent'; -import { makeVaultId } from '@/vaults/utils'; +import * as vaultsUtils from '@/vaults/utils'; import { utils as nodesUtils } from '@/nodes'; import * as testBinUtils from '../utils'; @@ -353,7 +353,7 @@ describe('CLI Notifications', () => { }; const notificationData3: NotificationData = { type: 'VaultShare', - vaultId: makeVaultId(idUtils.fromString('vaultIdxxxxxxxxx')).toString(), + vaultId: vaultsUtils.encodeVaultId(vaultsUtils.generateVaultId()), vaultName: 'vaultName' as VaultName, actions: { clone: null, diff --git a/tests/client/rpcVaults.test.ts b/tests/client/rpcVaults.test.ts index 4586a5bc34..b5d0136a91 100644 --- a/tests/client/rpcVaults.test.ts +++ b/tests/client/rpcVaults.test.ts @@ -138,9 +138,7 @@ describe('Vaults client service', () => { const vaultId = await createVault(vaultMessage, callCredentials); const vaultNames = await vaultManager.listVaults(); expect(vaultNames.get(vaultList[0])).toBeTruthy(); - expect(vaultNames.get(vaultList[0])).toStrictEqual( - vaultsUtils.makeVaultId(vaultId.getNameOrId()), - ); + expect(vaultNames.get(vaultList[0])).toStrictEqual(vaultId.getNameOrId()); }); test('should delete vaults', async () => { const deleteVault = grpcUtils.promisifyUnaryCall( @@ -174,7 +172,7 @@ describe('Vaults client service', () => { vaultRenameMessage.setNewName(vaultList[1]); const vaultId2 = await renameVault(vaultRenameMessage, callCredentials); - expect(vaultsUtils.makeVaultId(vaultId2.getNameOrId())).toStrictEqual( + expect(vaultsUtils.decodeVaultId(vaultId2.getNameOrId())).toStrictEqual( vaultId1, ); @@ -249,7 +247,7 @@ describe('Vaults client service', () => { vaultVersionMessage.setVersionId('invalidOid'); const version = vaultsVersion(vaultVersionMessage, callCredentials); await expect(version).rejects.toThrow( - vaultErrors.ErrorVaultsCommitUndefined, + vaultErrors.ErrorVaultReferenceMissing, ); }); }); diff --git a/tests/notifications/utils.test.ts b/tests/notifications/utils.test.ts index 5e2ba28085..de9a4ad963 100644 --- a/tests/notifications/utils.test.ts +++ b/tests/notifications/utils.test.ts @@ -9,16 +9,15 @@ import * as notificationsUtils from '@/notifications/utils'; import * as notificationsErrors from '@/notifications/errors'; import { createNotificationIdGenerator } from '@/notifications/utils'; import { sleep } from '@/utils'; -import { makeVaultId } from '@/vaults/utils'; import { utils as nodesUtils } from '@/nodes'; import * as testUtils from '../utils'; +import * as vaultUtils from '@/vaults/utils'; describe('Notifications utils', () => { const nodeId = testUtils.generateRandomNodeId(); const nodeIdEncoded = nodesUtils.encodeNodeId(nodeId); - const vaultId = makeVaultId( - idUtils.fromString('vaultIdxxxxxxxxx'), - ).toString(); + const vaultId = vaultUtils.generateVaultId(); + const vaultIdEncoded = vaultUtils.encodeVaultId(vaultId); test('generates notification ids', async () => { const generator = createNotificationIdGenerator(); @@ -64,7 +63,7 @@ describe('Notifications utils', () => { const vaultShareNotification: Notification = { data: { type: 'VaultShare', - vaultId: vaultId, + vaultId: vaultIdEncoded, vaultName: 'vaultName' as VaultName, actions: { clone: null, @@ -145,7 +144,7 @@ describe('Notifications utils', () => { const vaultShareNotification: Notification = { data: { type: 'VaultShare', - vaultId: vaultId, + vaultId: vaultIdEncoded, vaultName: 'vaultName' as VaultName, actions: { clone: null, @@ -239,7 +238,7 @@ describe('Notifications utils', () => { const vaultShareNotification: Notification = { data: { type: 'VaultShare', - vaultId: vaultId, + vaultId: vaultIdEncoded, vaultName: 'vaultName' as VaultName, actions: { clone: null, diff --git a/tests/vaults/VaultInternal.test.ts b/tests/vaults/VaultInternal.test.ts index e8878164b1..d52e3b04a3 100644 --- a/tests/vaults/VaultInternal.test.ts +++ b/tests/vaults/VaultInternal.test.ts @@ -10,6 +10,7 @@ import { generateVaultId, generateVaultKey } from '@/vaults/utils'; import * as vaultsErrors from '@/vaults/errors'; import { sleep } from '@/utils'; import { KeyManager, utils as keysUtils } from '@/keys'; +import * as testsUtils from '../utils'; jest.mock('@/keys/utils', () => ({ ...jest.requireActual('@/keys/utils'), @@ -29,7 +30,7 @@ describe('VaultInternal', () => { const fakeKeyManager = { getNodeId: () => { - return 'DummyNodeId' as NodeId; + return testsUtils.generateRandomNodeId(); }, } as KeyManager; const secret1 = { name: 'secret-1', content: 'secret-content-1' }; @@ -74,7 +75,7 @@ describe('VaultInternal', () => { await vault.destroy(); await expect(async () => { await vault.log(); - }).rejects.toThrow(vaultsErrors.ErrorVaultsVaultDestroyed); + }).rejects.toThrow(vaultsErrors.ErrorVaultDestroyed); }); test('is type correct', async () => { expect(vault).toBeInstanceOf(VaultInternal); @@ -83,10 +84,10 @@ describe('VaultInternal', () => { expect(await fs.promises.readdir(dataDir)).toContain('db'); }); test('accessing a change', async () => { - await vault.commit(async (efs) => { + await vault.writeF(async (efs) => { await efs.writeFile('secret-1', 'secret-content'); }); - await vault.access(async (efs) => { + await vault.readF(async (efs) => { expect(await efs.readdir('.')).toContain('secret-1'); expect((await efs.readFile('secret-1')).toString()).toStrictEqual( 'secret-content', @@ -94,7 +95,7 @@ describe('VaultInternal', () => { }); }); test('maintains data across VaultInternal instances', async () => { - await vault.commit(async (efs) => { + await vault.writeF(async (efs) => { await efs.writeFile('secret-1', 'secret-content'); }); await vault.destroy(); @@ -105,48 +106,48 @@ describe('VaultInternal', () => { logger, fresh: false, }); - await vault.access(async (efs) => { + await vault.readF(async (efs) => { expect((await efs.readFile('secret-1')).toString()).toStrictEqual( 'secret-content', ); }); }); test('can change to the current commit', async () => { - let commit = (await vault.log(1))[0]; - await vault.version(commit.oid); - const files = await vault.access(async (efs) => { + let commit = (await vault.log(undefined, 1))[0]; + await vault.version(commit.commitId); + const files = await vault.readF(async (efs) => { return await efs.readdir('.'); }); expect(files).toEqual([]); - await vault.commit(async (efs) => { + await vault.writeF(async (efs) => { await efs.writeFile('test', 'testdata'); }); - commit = (await vault.log(1))[0]; - await vault.version(commit.oid); - const file = await vault.access(async (efs) => { + commit = (await vault.log(undefined, 1))[0]; + await vault.version(commit.commitId); + const file = await vault.readF(async (efs) => { return await efs.readFile('test', { encoding: 'utf8' }); }); expect(file).toBe('testdata'); }); test('can change commits and preserve the log with no intermediate vault mutation', async () => { - const initCommit = (await vault.log(1))[0].oid; - await vault.commit(async (efs) => { + const initCommit = (await vault.log(undefined, 1))[0].commitId; + await vault.writeF(async (efs) => { await efs.writeFile('test1', 'testdata1'); }); - await vault.commit(async (efs) => { + await vault.writeF(async (efs) => { await efs.writeFile('test2', 'testdata2'); }); - await vault.commit(async (efs) => { + await vault.writeF(async (efs) => { await efs.writeFile('test3', 'testdata3'); }); await vault.version(initCommit); - const endCommit = (await vault.log(1))[0].oid; - let files = await vault.access(async (efs) => { + const endCommit = (await vault.log(undefined, 1))[0].commitId; + let files = await vault.readF(async (efs) => { return await efs.readdir('.'); }); expect(files).toEqual([]); await vault.version(endCommit); - files = await vault.access(async (efs) => { + files = await vault.readF(async (efs) => { return await efs.readdir('.'); }); expect(files).toEqual(['test1', 'test2', 'test3']); @@ -155,97 +156,97 @@ describe('VaultInternal', () => { await expect(() => vault.version('unrecognisedcommit')).rejects.toThrow( vaultsErrors.ErrorVaultsCommitUndefined, ); - await vault.commit(async (efs) => { + await vault.writeF(async (efs) => { await efs.writeFile('test1', 'testdata1'); }); - const secondCommit = (await vault.log(1))[0].oid; - await vault.commit(async (efs) => { + const secondCommit = (await vault.log(undefined, 1))[0].commitId; + await vault.writeF(async (efs) => { await efs.writeFile('test2', 'testdata2'); }); - await vault.commit(async (efs) => { + await vault.writeF(async (efs) => { await efs.writeFile('test3', 'testdata3'); }); - const fourthCommit = (await vault.log(1))[0].oid; + const fourthCommit = (await vault.log(undefined, 1))[0].commitId; await vault.version(secondCommit); - await vault.commit(async (efs) => { + await vault.writeF(async (efs) => { const fd = await efs.open('test3', 'w'); await efs.write(fd, 'testdata6', 3, 6); await efs.close(fd); }); await vault.version(fourthCommit); - await vault.commit(async (efs) => { + await vault.writeF(async (efs) => { await efs.writeFile('test4', 'testdata4'); }); }); test('can change to the HEAD commit', async () => { - const initCommit = (await vault.log(1))[0].oid; - await vault.commit(async (efs) => { + const initCommit = (await vault.log(undefined, 1))[0].commitId; + await vault.writeF(async (efs) => { await efs.writeFile('test1', 'testdata1'); }); - await vault.commit(async (efs) => { + await vault.writeF(async (efs) => { await efs.writeFile('test2', 'testdata2'); }); - await vault.commit(async (efs) => { + await vault.writeF(async (efs) => { await efs.writeFile('test3', 'testdata3'); }); await vault.version(initCommit); await vault.version('HEAD'); - let files = await vault.access(async (efs) => { + let files = await vault.readF(async (efs) => { return await efs.readdir('.'); }); expect(files).toEqual(['test1', 'test2', 'test3']); await vault.version(initCommit); await vault.version('last'); - files = await vault.access(async (efs) => { + files = await vault.readF(async (efs) => { return await efs.readdir('.'); }); expect(files).toEqual(['test1', 'test2', 'test3']); }); test('adjusts HEAD after vault mutation, discarding forward and preserving backwards history', async () => { - const initCommit = (await vault.log(1))[0].oid; - await vault.commit(async (efs) => { + const initCommit = (await vault.log(undefined, 1))[0].commitId; + await vault.writeF(async (efs) => { await efs.writeFile('test1', 'testdata1'); }); - const secondCommit = (await vault.log(1))[0].oid; - await vault.commit(async (efs) => { + const secondCommit = (await vault.log(undefined, 1))[0].commitId; + await vault.writeF(async (efs) => { await efs.writeFile('test2', 'testdata2'); }); - await vault.commit(async (efs) => { + await vault.writeF(async (efs) => { await efs.writeFile('test3', 'testdata3'); }); await vault.version(secondCommit); - await vault.commit(async (efs) => { + await vault.writeF(async (efs) => { await efs.writeFile('test4', 'testdata4'); }); - let files = await vault.access(async (efs) => { + let files = await vault.readF(async (efs) => { return await efs.readdir('.'); }); expect(files).toEqual(['test1', 'test4']); await vault.version(initCommit); - files = await vault.access(async (efs) => { + files = await vault.readF(async (efs) => { return await efs.readdir('.'); }); expect(files).toEqual([]); }); test('write operation allowed', async () => { - await vault.commit(async (efs) => { + await vault.writeF(async (efs) => { await efs.writeFile('secret-1', 'secret-content'); }); }); test('concurrent write operations prevented', async () => { await Promise.all([ - vault.commit(async (efs) => { + vault.writeF(async (efs) => { await efs.writeFile('secret-1', 'secret-content-1'); }), - vault.commit(async (efs) => { + vault.writeF(async (efs) => { await efs.writeFile('secret-2', 'secret-content-2'); }), - vault.commit(async (efs) => { + vault.writeF(async (efs) => { await efs.writeFile('secret-3', 'secret-content-3'); }), ]); - await vault.access(async (efs) => { + await vault.readF(async (efs) => { const directory = await efs.readdir('.'); expect(directory).toContain('secret-1'); expect(directory).toContain('secret-2'); @@ -255,15 +256,15 @@ describe('VaultInternal', () => { expect(log.length).toEqual(4); }); test('write locks read', async () => { - await vault.commit(async (efs) => { + await vault.writeF(async (efs) => { await efs.writeFile('secret-1', 'secret-content'); }); await Promise.all([ - vault.commit(async (efs) => { + vault.writeF(async (efs) => { await efs.writeFile('secret-1', 'SUPER-DUPER-SECRET-CONTENT'); }), - vault.access(async (efs) => { + vault.readF(async (efs) => { expect((await efs.readFile('secret-1')).toString()).toEqual( 'SUPER-DUPER-SECRET-CONTENT', ); @@ -271,26 +272,26 @@ describe('VaultInternal', () => { ]); }); test('commit added if mutation in write', async () => { - const commit = (await vault.log())[0].oid; - await vault.commit(async (efs) => { + const commit = (await vault.log())[0].commitId; + await vault.writeF(async (efs) => { await efs.writeFile('secret-1', 'secret-content'); }); const log = await vault.log(); expect(log).toHaveLength(2); expect(log[0].message).toContain('secret-1'); - expect(log[0].oid).not.toStrictEqual(commit); + expect(log[0].commitId).not.toStrictEqual(commit); }); test('no commit added if no mutation in write', async () => { - const commit = (await vault.log())[0].oid; - await vault.commit(async (_efs) => {}); + const commit = (await vault.log())[0].commitId; + await vault.writeF(async (_efs) => {}); const log = await vault.log(); expect(log).toHaveLength(1); expect(log[0].message).not.toContain('secret-1'); - expect(log[0].oid).toStrictEqual(commit); + expect(log[0].commitId).toStrictEqual(commit); }); test('commit message contains all actions made in the commit', async () => { // Adding - await vault.commit(async (efs) => { + await vault.writeF(async (efs) => { await efs.writeFile(secret1.name, secret1.content); await efs.writeFile(secret2.name, secret2.content); }); @@ -298,7 +299,7 @@ describe('VaultInternal', () => { expect(log[0].message).toContain(`${secret1.name} added`); expect(log[0].message).toContain(`${secret2.name} added`); // Checking contents - await vault.access(async (efs) => { + await vault.readF(async (efs) => { expect((await efs.readFile(secret1.name)).toString()).toEqual( secret1.content, ); @@ -308,25 +309,25 @@ describe('VaultInternal', () => { }); // Modifying - await vault.commit(async (efs) => { + await vault.writeF(async (efs) => { await efs.writeFile(secret2.name, `${secret2.content} new content`); }); log = await vault.log(); expect(log[0].message).toContain(`${secret2.name} modified`); // Checking changes - await vault.access(async (efs) => { + await vault.readF(async (efs) => { expect((await efs.readFile(secret2.name)).toString()).toEqual( `${secret2.content} new content`, ); }); // Moving and removing - await vault.commit(async (efs) => { + await vault.writeF(async (efs) => { await efs.rename(secret1.name, `${secret1.name}-new`); await efs.unlink(secret2.name); }); // Checking changes. - await vault.access(async (efs) => { + await vault.readF(async (efs) => { expect(await efs.exists(secret1.name)).toBeFalsy(); expect(await efs.exists(`${secret1.name}-new`)).toBeTruthy(); expect(await efs.exists(secret2.name)).toBeFalsy(); @@ -340,14 +341,14 @@ describe('VaultInternal', () => { test('no mutation to vault when part of a commit operation fails', async () => { // Failing commit operation await expect(() => - vault.commit(async (efs) => { + vault.writeF(async (efs) => { await efs.writeFile(secret1.name, secret1.content); await efs.rename('notValid', 'randomName'); // Throws }), ).rejects.toThrow(); // Make sure secret1 wasn't written when the above commit failed. - await vault.access(async (efs) => { + await vault.readF(async (efs) => { expect(await efs.readdir('.')).not.toContain(secret1.name); }); @@ -355,12 +356,12 @@ describe('VaultInternal', () => { expect(await vault.log()).toHaveLength(1); // Succeeding commit operation. - await vault.commit(async (efs) => { + await vault.writeF(async (efs) => { await efs.writeFile(secret2.name, secret2.content); }); // Secret 1 shouldn't exist while secret2 exists. - await vault.access(async (efs) => { + await vault.readF(async (efs) => { const directory = await efs.readdir('.'); expect(directory).not.toContain(secret1.name); // expect(directory).toContain(secret2.name); @@ -382,7 +383,7 @@ describe('VaultInternal', () => { // @ts-ignore expect(vault.lock.isLocked()).toBeFalsy(); - const commit1 = vault.commit(async (efs) => { + const commit1 = vault.writeF(async (efs) => { await efs.writeFile(secret1.name, secret1.content); await delayPromise; // Hold the lock hostage. firstCommitResolved = true; @@ -396,7 +397,7 @@ describe('VaultInternal', () => { let secondCommitResolved = false; let secondCommitResolveTime; - const commit2 = vault.commit(async (efs) => { + const commit2 = vault.writeF(async (efs) => { await efs.writeFile(secret2.name, secret2.content); secondCommitResolved = true; await sleep(2); @@ -429,22 +430,22 @@ describe('VaultInternal', () => { expect(log[1].message).toContain(secret1.name); }); test('read operation allowed', async () => { - await vault.commit(async (efs) => { + await vault.writeF(async (efs) => { await efs.writeFile(secret1.name, secret1.content); await efs.writeFile(secret2.name, secret2.content); }); - await vault.access(async (efs) => { + await vault.readF(async (efs) => { expect((await efs.readFile(secret1.name)).toString()).toEqual( secret1.content, ); }); }); test('concurrent read operations allowed', async () => { - await vault.commit(async (efs) => { + await vault.writeF(async (efs) => { await efs.writeFile(secret1.name, secret1.content); await efs.writeFile(secret2.name, secret2.content); }); - await vault.access(async (efs) => { + await vault.readF(async (efs) => { expect((await efs.readFile(secret1.name)).toString()).toEqual( secret1.content, ); @@ -457,17 +458,17 @@ describe('VaultInternal', () => { }); await Promise.all([ - vault.access(async (efs) => { + vault.readF(async (efs) => { expect((await efs.readFile(secret1.name)).toString()).toEqual( secret1.content, ); }), - vault.access(async (efs) => { + vault.readF(async (efs) => { expect((await efs.readFile(secret2.name)).toString()).toEqual( secret2.content, ); }), - vault.access(async (efs) => { + vault.readF(async (efs) => { expect((await efs.readFile(secret1.name)).toString()).toEqual( secret1.content, ); @@ -475,20 +476,20 @@ describe('VaultInternal', () => { ]); }); test('read locks write', async () => { - await vault.commit(async (efs) => { + await vault.writeF(async (efs) => { await efs.writeFile(secret1.name, secret1.content); await efs.writeFile(secret2.name, secret2.content); }); await Promise.all([ - vault.access(async (efs) => { + vault.readF(async (efs) => { expect((await efs.readFile(secret1.name)).toString()).toEqual( secret1.content, ); }), - vault.commit(async (efs) => { + vault.writeF(async (efs) => { await efs.writeFile(secret1.name, 'NEW-CONTENT'); }), - vault.access(async (efs) => { + vault.readF(async (efs) => { expect((await efs.readFile(secret1.name)).toString()).toEqual( 'NEW-CONTENT', ); @@ -496,22 +497,22 @@ describe('VaultInternal', () => { ]); }); test('no commit after read', async () => { - await vault.commit(async (efs) => { + await vault.writeF(async (efs) => { await efs.writeFile(secret1.name, secret1.content); await efs.writeFile(secret2.name, secret2.content); }); - const commit = (await vault.log())[0].oid; - await vault.access(async (efs) => { + const commit = (await vault.log())[0].commitId; + await vault.readF(async (efs) => { expect((await efs.readFile(secret1.name)).toString()).toEqual( secret1.content, ); }); const log = await vault.log(); expect(log).toHaveLength(2); - expect(log[0].oid).toStrictEqual(commit); + expect(log[0].commitId).toStrictEqual(commit); }); test('locking occurs when making an access.', async () => { - await vault.commit(async (efs) => { + await vault.writeF(async (efs) => { await efs.writeFile(secret1.name, secret1.content); await efs.writeFile(secret2.name, secret2.content); }); @@ -526,7 +527,7 @@ describe('VaultInternal', () => { // @ts-ignore expect(vault.lock.isLocked()).toBeFalsy(); - const commit1 = vault.access(async (efs) => { + const commit1 = vault.readF(async (efs) => { await efs.readFile(secret1.name); await delayPromise; // Hold the lock hostage. firstCommitResolved = true; @@ -540,7 +541,7 @@ describe('VaultInternal', () => { let secondCommitResolved = false; let secondCommitResolveTime; - const commit2 = vault.access(async (efs) => { + const commit2 = vault.readF(async (efs) => { await efs.readFile(secret2.name); secondCommitResolved = true; await sleep(10); @@ -572,11 +573,11 @@ describe('VaultInternal', () => { const vaultInterface = vault as Vault; // Using the avaliable functions. - await vaultInterface.commit(async (efs) => { + await vaultInterface.writeF(async (efs) => { await efs.writeFile('test', 'testContent'); }); - await vaultInterface.access(async (efs) => { + await vaultInterface.readF(async (efs) => { const content = (await efs.readFile('test')).toString(); expect(content).toStrictEqual('testContent'); }); @@ -603,14 +604,14 @@ describe('VaultInternal', () => { remote: true, fresh: true, }); - const commit = (await vault.log(1))[0]; - await vault.version(commit.oid); - const files = await vault.access(async (efs) => { + const commit = (await vault.log(undefined, 1))[0]; + await vault.version(commit.commitId); + const files = await vault.readF(async (efs) => { return await efs.readdir('.'); }); expect(files).toEqual([]); await expect( - vault.commit(async (efs) => { + vault.writeF(async (efs) => { await efs.writeFile('test', 'testdata'); }), ).rejects.toThrow(vaultsErrors.ErrorVaultsVaultImmutable); diff --git a/tests/vaults/utils.test.ts b/tests/vaults/utils.test.ts index 7d0faec0c2..67d2f53886 100644 --- a/tests/vaults/utils.test.ts +++ b/tests/vaults/utils.test.ts @@ -6,7 +6,6 @@ import { EncryptedFS } from 'encryptedfs'; import Logger, { LogLevel, StreamHandler } from '@matrixai/logger'; import { IdRandom } from '@matrixai/id'; import * as vaultsUtils from '@/vaults/utils'; -import { isVaultId } from '@/vaults/utils'; describe('Vaults utils', () => { const logger = new Logger('Vaults utils tests', LogLevel.WARN, [