diff --git a/src/operations.ts b/src/operations.ts index 1f16e37..4ab2570 100644 --- a/src/operations.ts +++ b/src/operations.ts @@ -1,37 +1,27 @@ -import { SignifyClient, Dict } from 'signify-ts'; +import { SignifyClient } from 'signify-ts'; const DONE = 'done'; const NAME = 'name'; const RESPONSE = 'response'; -export const getAgentOperationResult = async ( - client: SignifyClient, - op: any, - log: boolean = false, - time: number = 1000 -) => { - const ops = client.operations(); - while (!op[DONE]) { - op = await ops.get(op[NAME]); - if (log) { - console.log(op); +export interface OperationsArgs { + client: SignifyClient; + op: any; + time?: number; +} + +namespace operations { + export async function getResult({ + client, + op, + time = 1000, + }: OperationsArgs) { + while (!op[DONE]) { + op = await client.operations().get(op[NAME]); + await new Promise((resolve) => setTimeout(resolve, time)); } - await new Promise((resolve) => setTimeout(resolve, time)); + return op[RESPONSE]; } - return op[RESPONSE]; -}; +} -export const sendAgentMessage = async ( - client: SignifyClient, - name: string, - topic: string, - sender: Dict, - route: string, - payload: Dict, - embeds: Dict, - recipients: string[] -) => { - return await client - .exchanges() - .send(name, topic, sender, route, payload, embeds, recipients); -}; +export { operations }; diff --git a/src/qvi/qvi.ts b/src/qvi/qvi.ts index a6a254f..371c69c 100644 --- a/src/qvi/qvi.ts +++ b/src/qvi/qvi.ts @@ -1,5 +1,4 @@ import { SignifyClient, Siger, messagize, d } from 'signify-ts'; -import { getAgentOperationResult, sendAgentMessage } from '../operations'; import { Schema } from '../schema'; import { Rules } from '../rules'; import { LEvLEICredentialData, LEvLEICredentialEdge } from './credentials/le'; @@ -7,6 +6,7 @@ import { ECRAuthvLEIEdgeData, ECRvLEICredentialData } from './credentials/ecr'; import { OORvLEICredentialData } from './credentials/oor'; import { OORAuthvLEICredentialData } from '../le/credentials/oor-auth'; import { AID } from '..'; +import { operations } from '../operations'; type qb64 = string; @@ -42,7 +42,7 @@ export class QVI { * * @param {AID} issuee * @param {LEvLEICredentialData} data - * @param {LEvLEICredentialEdge} edge} + * @param {LEvLEICredentialEdge} edge * @returns */ public async createLegalEntityCredential( @@ -95,7 +95,7 @@ export class QVI { * Create Official Organizational Role Credential * * @param {AID} issuee - * @param {OORvLEICredentialData}data`` + * @param {OORvLEICredentialData} data * @param {OORAuthvLEICredentialData} edge * @returns */ @@ -125,7 +125,7 @@ export class QVI { credential?: any //result for createLegalEntityCredential ) { let sender = await this.client.identifiers().get(''); - sender = await getAgentOperationResult(this.client, sender); + sender = await operations.getResult({client: this.client, op: sender}); if (getFromAgent) { let msgSaid = ''; @@ -157,16 +157,18 @@ export class QVI { let embeds = { cred: [serder, atc], }; - return await sendAgentMessage( - this.client, - alias, - 'multisig_issuance', - sender, - '/multisig/iss', - {}, //TODO:payload - embeds, - [recipient] - ); + + return await this.client + .exchanges() + .send( + alias, + 'multisig_issuance', + sender, + '/multisig/iss', + {}, + embeds, + [recipient] + ); } } diff --git a/test/operations.test.ts b/test/operations.test.ts new file mode 100644 index 0000000..c2a591d --- /dev/null +++ b/test/operations.test.ts @@ -0,0 +1,37 @@ +import { SignifyClient, Credentials, Operations } from 'signify-ts'; +import { describe, expect, it } from '@jest/globals'; +import { instance, mock, when } from 'ts-mockito'; +import { operations } from '../src/operations'; + +describe('operations', () => { + it('should complete', async () => { + let mockedClient: SignifyClient = mock(SignifyClient); + + let c: Credentials = mock(Credentials); + when(mockedClient.credentials()).thenReturn(instance(c)); + + let client = instance(mockedClient); + + let op = { done: true, name: 'my_op', response: 'yay'}; + + let resp = await operations.getResult({client: client, op: op}); + expect(resp).toEqual('yay'); + }); + + it('should retry', async () => { + let mockedClient: SignifyClient = mock(SignifyClient); + + let c: Credentials = mock(Credentials); + when(mockedClient.credentials()).thenReturn(instance(c)); + + let mockedOps: Operations = mock(Operations); + when(mockedOps.get('my_op')).thenResolve({done: true, name: 'my_op', response: 'yay'}); + when(mockedClient.operations()).thenReturn(instance(mockedOps)); + + let client = instance(mockedClient); + + let op = { done: false, name: 'my_op' }; + let resp = await operations.getResult({ client: client, op: op }); + expect(resp).toEqual('yay'); + }); +}); diff --git a/test/qvi.test.ts b/test/qvi.test.ts index 4ed4a94..e620b4b 100644 --- a/test/qvi.test.ts +++ b/test/qvi.test.ts @@ -17,7 +17,11 @@ describe('a qvi', () => { let client = instance(mockedClient); let qvi = new QVI(client, 'qvi_name', 'qvi_registry_aid'); - let data = new LEvLEICredentialData({LEI: 'an LEI', issuee: 'issuee', timestamp: 'timestamp'}); + let data = new LEvLEICredentialData({ + LEI: 'an LEI', + issuee: 'issuee', + timestamp: 'timestamp', + }); let edge = new LEvLEICredentialEdge('qvi_aid'); qvi.createLegalEntityCredential('issuee aid', data, edge);