Skip to content

Commit

Permalink
add test cases for messageHandlers map
Browse files Browse the repository at this point in the history
  • Loading branch information
LiranCohen committed Apr 24, 2024
1 parent 58c028a commit 35f19db
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions packages/agent/tests/prototyping/clients/json-rpc-socket.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,42 @@ describe('JsonRpcSocket', () => {
}
});

it('adds a handler to the messageHandlers map when listening for a response to a request', async () => {
const client = await JsonRpcSocket.connect(socketDwnUrl);
const { message } = await TestDataGenerator.generateRecordsSubscribe({ author: alice });
const requestId = cryptoUtils.randomUuid();
const request = createJsonRpcRequest(requestId, 'dwn.processMessage', { target: alice.did, message });
const response = client.request(request);
expect(client['messageHandlers'].has(requestId)).to.be.true;

await response;

// removes the handler after the response is received
expect(client['messageHandlers'].has(requestId)).to.be.false;
});

it('adds a handler to the messageHandlers map when listening for a response to a subscription', async () => {
const client = await JsonRpcSocket.connect(socketDwnUrl);
const { message } = await TestDataGenerator.generateRecordsSubscribe({ author: alice });

const requestId = cryptoUtils.randomUuid();
const subscriptionId = cryptoUtils.randomUuid();
const request = createJsonRpcSubscriptionRequest(
requestId,
'dwn.processMessage',
subscriptionId,
{ target: alice.did, message }
);

const responseListener = (_response: JsonRpcResponse): void => {};
const subscription = await client.subscribe(request, responseListener);
expect(client['messageHandlers'].has(subscriptionId)).to.be.true;

// removes the handler after the subscription is closed
await subscription.close!();
expect(client['messageHandlers'].has(subscriptionId)).to.be.false;
});

it('removes listener if subscription json rpc is rejected ', async () => {
const client = await JsonRpcSocket.connect(socketDwnUrl);
const requestId = cryptoUtils.randomUuid();
Expand All @@ -82,6 +118,7 @@ describe('JsonRpcSocket', () => {

const subscription = await client.subscribe(request, responseListener);
expect(subscription.response.error).to.not.be.undefined;
expect(client['messageHandlers'].has(subscribeId)).to.be.false;
});

it('opens a subscription', async () => {
Expand Down

0 comments on commit 35f19db

Please sign in to comment.