Skip to content

Commit

Permalink
Fix oob node wrapper unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Miroslav Kovar <[email protected]>
  • Loading branch information
mirgee committed Nov 23, 2022
1 parent 5575993 commit 637177a
Show file tree
Hide file tree
Showing 11 changed files with 28 additions and 86 deletions.
6 changes: 3 additions & 3 deletions libvcx/src/api_lib/api_c/out_of_band.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ pub extern "C" fn vcx_out_of_band_sender_create(
config
);

execute_async::<BoxFuture<'static, Result<(), ()>>>(Box::pin(async move {
match out_of_band::create_out_of_band(config).await {
execute(move || {
match out_of_band::create_out_of_band(config) {
Ok(handle) => {
trace!(
"vcx_out_of_band_sender_create_cb(command_handle: {}, rc: {}, handle: {})",
Expand All @@ -51,7 +51,7 @@ pub extern "C" fn vcx_out_of_band_sender_create(
}
}
Ok(())
}));
});

error::SUCCESS.code_num
}
Expand Down
2 changes: 1 addition & 1 deletion libvcx/src/api_lib/api_handle/out_of_band.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ fn store_out_of_band_sender(oob: OutOfBandSender) -> VcxResult<u32> {
}

#[napi]
pub async fn create_out_of_band(config: String) -> ::napi::Result<u32> {
pub fn create_out_of_band(config: String) -> ::napi::Result<u32> {
trace!("create_out_of_band >>> config: {}", config);
let config: OOBConfig = serde_json::from_str(&config).map_err(|err| {
VcxError::from_msg(
Expand Down
4 changes: 2 additions & 2 deletions wrappers/node/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion wrappers/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
"test:proof": " TS_NODE_PROJECT=./test/tsconfig.json NODE_ENV=test RUST_BACKTRACE=full ./node_modules/.bin/mocha --timeout 60000 --v8-expose-gc --v8-use-strict --exit --recursive --require ts-node/register ./test/suite1/ariesvcx-proof.test.ts",
"test:schema": " TS_NODE_PROJECT=./test/tsconfig.json NODE_ENV=test RUST_BACKTRACE=full ./node_modules/.bin/mocha --timeout 60000 --v8-expose-gc --v8-use-strict --exit --recursive --require ts-node/register ./test/suite1/ariesvcx-schema.test.ts",
"test:utils": " TS_NODE_PROJECT=./test/tsconfig.json NODE_ENV=test RUST_BACKTRACE=full ./node_modules/.bin/mocha --timeout 60000 --v8-expose-gc --v8-use-strict --exit --recursive --require ts-node/register ./test/suite1/ariesvcx-utils.test.ts",
"test:oob": " TS_NODE_PROJECT=./test/tsconfig.json NODE_ENV=test RUST_BACKTRACE=full ./node_modules/.bin/mocha --timeout 60000 --v8-expose-gc --v8-use-strict --exit --recursive --require ts-node/register ./test/suite1/ariesvcx-oob.test.ts",
"test:oob": " TS_NODE_PROJECT=./test/tsconfig.json NODE_ENV=test RUST_BACKTRACE=full ./node_modules/.bin/mocha -s 0 --timeout 60000 --v8-expose-gc --v8-use-strict --exit --recursive --require ts-node/register ./test/suite1/ariesvcx-oob.test.ts",
"test:wallet": " TS_NODE_PROJECT=./test/tsconfig.json NODE_ENV=test RUST_BACKTRACE=full ./node_modules/.bin/mocha --timeout 60000 --v8-expose-gc --v8-use-strict --exit --recursive --require ts-node/register ./test/suite1/ariesvcx-wallet.test.ts",
"test:ffi": " TS_NODE_PROJECT=./test/tsconfig.json NODE_ENV=test RUST_BACKTRACE=full ./node_modules/.bin/mocha --timeout 60000 --v8-expose-gc --v8-use-strict --exit --recursive --require ts-node/register ./test/suite2/ffi.test.ts"
},
Expand Down
23 changes: 7 additions & 16 deletions wrappers/node/src/api/out-of-band-receiver.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import * as ffi from 'node-napi-rs';
import { VCXInternalError } from '../errors';
import { rustAPI } from '../rustlib';
import { IOOBSerializedData } from './out-of-band-sender';
import { Connection } from './mediated-connection';
import { VCXBase } from './vcx-base';
import { VCXBase1 } from './vcx-base-1';
import { ISerializedData } from './common';

export class OutOfBandReceiver extends VCXBase<IOOBSerializedData> {
export class OutOfBandReceiver extends VCXBase1<IOOBSerializedData> {
public static createWithMessage(msg: string): OutOfBandReceiver {
const oob = new OutOfBandReceiver("");
try {
Expand All @@ -17,9 +16,9 @@ export class OutOfBandReceiver extends VCXBase<IOOBSerializedData> {
}
}

public static async deserialize(
public static deserialize(
data: ISerializedData<IOOBSerializedData>,
): Promise<OutOfBandReceiver> {
): OutOfBandReceiver {
const newObj = { ...data, source_id: 'foo' };
return super._deserialize(OutOfBandReceiver, newObj);
}
Expand Down Expand Up @@ -59,15 +58,7 @@ export class OutOfBandReceiver extends VCXBase<IOOBSerializedData> {
}
}

public serialize_(): ISerializedData<IOOBSerializedData> {
try {
return JSON.parse(ffi.toStringReceiver(this.handle))
} catch (err: any) {
throw new VCXInternalError(err);
}
}

protected _serializeFn = rustAPI().vcx_out_of_band_receiver_serialize;
protected _deserializeFn = rustAPI().vcx_out_of_band_receiver_deserialize;
protected _releaseFn = rustAPI().vcx_out_of_band_receiver_release;
protected _serializeFn = ffi.toStringReceiver;
protected _deserializeFn = ffi.fromStringReceiver;
protected _releaseFn = ffi.releaseReceiver;
}
17 changes: 4 additions & 13 deletions wrappers/node/src/api/out-of-band-sender.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as ffi from 'node-napi-rs';
import { VCXInternalError } from '../errors';
import { rustAPI } from '../rustlib';
import { VCXBase1 } from './vcx-base-1';
import { ISerializedData } from './common';

Expand Down Expand Up @@ -36,19 +35,19 @@ export enum HandshakeProtocol {
}

export class OutOfBandSender extends VCXBase1<IOOBSerializedData> {
public static async create(config: IOOBCreateData): Promise<OutOfBandSender> {
public static create(config: IOOBCreateData): OutOfBandSender {
const oob = new OutOfBandSender(config.source_id);
try {
oob._setHandle(await ffi.createOutOfBand(JSON.stringify(config)));
oob._setHandle(ffi.createOutOfBand(JSON.stringify(config)));
return oob;
} catch (err: any) {
throw new VCXInternalError(err);
}
}

public static async deserialize(
public static deserialize(
data: ISerializedData<IOOBSerializedData>,
): Promise<OutOfBandSender> {
): OutOfBandSender {
const newObj = { ...data, source_id: 'foo' };
return super._deserialize(OutOfBandSender, newObj);
}
Expand Down Expand Up @@ -93,14 +92,6 @@ export class OutOfBandSender extends VCXBase1<IOOBSerializedData> {
}
}

public serialize_(): ISerializedData<IOOBSerializedData> {
try {
return JSON.parse(ffi.toStringSender(this.handle))
} catch (err: any) {
throw new VCXInternalError(err);
}
}

protected _serializeFn = ffi.toStringSender;
protected _deserializeFn = ffi.fromStringSender;
protected _releaseFn = ffi.releaseSender;
Expand Down
50 changes: 5 additions & 45 deletions wrappers/node/src/api/vcx-base-1.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
import * as ffi from 'ffi-napi';
import { VCXInternalError } from '../errors';
import { createFFICallbackPromise, ICbRef } from '../utils/ffi-helpers';
import { ISerializedData } from './common';

export type IVCXBaseCreateFn = (cb: ICbRef) => number;

export abstract class VCXBase1<SerializedData> {
private _handleRef!: number;

protected static async _deserialize<T extends VCXBase1<unknown>, P = unknown>(
protected static _deserialize<T extends VCXBase1<unknown>, P = unknown>(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
VCXClass: new (sourceId: string, args?: any) => T,
objData: ISerializedData<{ source_id: string }>,
constructorParams?: P,
): Promise<T> {
): T {
try {
const obj = new VCXClass(objData.source_id || objData.data.source_id, constructorParams);
await obj._initFromData(objData);
obj._initFromData(objData);
return obj;
} catch (err: any) {
throw new VCXInternalError(err);
Expand All @@ -31,19 +27,7 @@ export abstract class VCXBase1<SerializedData> {
this._sourceId = sourceId;
}

/**
*
* Data returned can be used to recreate an entity by passing it to the deserialize function.
*
* Same json object structure that is passed to the deserialize function.
*
* Example:
*
* ```
* data = await object.serialize()
* ```
*/
public async serialize(): Promise<ISerializedData<SerializedData>> {
public serialize(): ISerializedData<SerializedData> {
try {
return JSON.parse(this._serializeFn(this.handle));
} catch (err: any) {
Expand All @@ -56,31 +40,7 @@ export abstract class VCXBase1<SerializedData> {
return this._sourceId;
}

protected async _create(createFn: IVCXBaseCreateFn): Promise<void> {
const handleRes = await createFFICallbackPromise<number>(
(resolve, reject, cb) => {
const rc = createFn(cb);
if (rc) {
reject(rc);
}
},
(resolve, reject) =>
ffi.Callback(
'void',
['uint32', 'uint32', 'uint32'],
(xHandle: number, err: number, handle: number) => {
if (err) {
reject(err);
return;
}
resolve(handle);
},
),
);
this._setHandle(handleRes);
}

private async _initFromData(objData: ISerializedData<{ source_id: string }>): Promise<void> {
private _initFromData(objData: ISerializedData<{ source_id: string }>): void {
const objHandle = this._deserializeFn(JSON.stringify(objData))
this._setHandle(objHandle);
}
Expand Down
4 changes: 2 additions & 2 deletions wrappers/node/test/suite1/ariesvcx-oob.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ describe('Out of Band:', () => {
it('success', async () => {
const oobSender = await OutOfBandSender.create({ source_id: "abcd" })
oobSender.appendServiceDid("VsKV7grR1BUE29mG2Fm2kX")
const serialized = oobSender.serialize_()
const serialized = oobSender.serialize()
await OutOfBandSender.deserialize(serialized)
})
})
Expand All @@ -65,7 +65,7 @@ describe('Out of Band:', () => {
it('success', async () => {
const oobSender = await OutOfBandSender.create({ source_id: "abcd" })
const oobReceiver = OutOfBandReceiver.createWithMessage(oobSender.toMessage())
const serialized = await oobReceiver.serialize()
const serialized = oobReceiver.serialize()
await OutOfBandReceiver.deserialize(serialized)
})
})
Expand Down
2 changes: 1 addition & 1 deletion wrappers/node_napi_rs/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

/* auto-generated by NAPI-RS */

export function createOutOfBand(config: string): Promise<number>
export function createOutOfBand(config: string): number
export function createOutOfBandMsgFromMsg(msg: string): number
export function appendMessage(handle: number, msg: string): void
export function appendService(handle: number, service: string): void
Expand Down
2 changes: 1 addition & 1 deletion wrappers/node_napi_rs/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion wrappers/node_napi_rs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"name": "index"
},
"devDependencies": {
"@napi-rs/cli": "^2.13.1",
"@napi-rs/cli": "^2.10.1",
"typescript": "^4.8.4"
},
"scripts": {
Expand Down

0 comments on commit 637177a

Please sign in to comment.