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 08f5584
Showing 1 changed file with 38 additions and 1 deletion.
39 changes: 38 additions & 1 deletion packages/agent/tests/prototyping/clients/json-rpc-socket.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ async function sleepWhileWaitingForEvents(override?: number):Promise<void> {
await new Promise((resolve) => setTimeout(resolve, override || 10));
}

describe('JsonRpcSocket', () => {
describe.only('JsonRpcSocket', () => {
let alice: Persona;
// we set the client to a websocket url
const dwnUrl = new URL(testDwnUrl);
Expand Down 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 08f5584

Please sign in to comment.