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 8b40c18
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 70 deletions.
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

0 comments on commit 8b40c18

Please sign in to comment.