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 <miroslav.kovar@absa.africa>
  • Loading branch information
mirgee committed Nov 23, 2022
1 parent 5575993 commit 427c99c
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 75 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
19 changes: 5 additions & 14 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 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;
}
9 changes: 0 additions & 9 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 @@ -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

0 comments on commit 427c99c

Please sign in to comment.