Skip to content

Commit

Permalink
updated tests. working now
Browse files Browse the repository at this point in the history
  • Loading branch information
andorsk committed Oct 2, 2023
1 parent f3c0b62 commit 6e02d41
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/json-rpc-handlers/dwn/process-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export const handleDwnProcessMessage: JsonRpcHandler = async (
messageType === DwnInterfaceName.Records + DwnMethodName.Write &&
!dataStream
) {
console.log('sending');
reply = await dwn.synchronizePrunedInitialRecordsWrite(target, message);
} else if (
messageType ===
Expand Down
2 changes: 2 additions & 0 deletions src/subscription-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,10 @@ export class SubscriptionManager {
const subscription = await this.createSubscription(req.from, req);
this.registerSubscription(subscription);
// set up forwarding.
// console.log('---------', subscriptionReply.subscription.emitter);
subscriptionReply.subscription.emitter.on(
async (e: EventMessage): Promise<void> => {
// console.log('got a record', e);
const jsonRpcResponse = this.createJSONRPCEvent(e);
const str = JSON.stringify(jsonRpcResponse);
return req.socket.send(Buffer.from(str));
Expand Down
48 changes: 45 additions & 3 deletions tests/subscription-manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,23 @@ import type { AddressInfo } from 'ws';
import { WebSocket, type WebSocketServer } from 'ws';
import { v4 as uuidv4 } from 'uuid';

import { DidKeyResolver, SubscriptionRequest } from '@tbd54566975/dwn-sdk-js';
import {
DataStream,
DidKeyResolver,
SubscriptionRequest,

Check failure on line 9 in tests/subscription-manager.spec.ts

View workflow job for this annotation

GitHub Actions / test

Module '"@tbd54566975/dwn-sdk-js"' has no exported member 'SubscriptionRequest'.
} from '@tbd54566975/dwn-sdk-js';

import { Jws } from '@tbd54566975/dwn-sdk-js';
import { assert } from 'chai';
import { createProfile } from './utils.js';
import { createProfile, createRecordsWriteMessage } from './utils.js';
import type { Profile } from './utils.js';
import { WsApi } from '../src/ws-api.js';
import { createJsonRpcRequest } from '../src/lib/json-rpc.js';
import { clear as clearDwn, dwn } from './test-dwn.js';
import { base64url } from 'multiformats/bases/base64';
import { EventType } from '@tbd54566975/dwn-sdk-js';

Check failure on line 20 in tests/subscription-manager.spec.ts

View workflow job for this annotation

GitHub Actions / test

Module '"@tbd54566975/dwn-sdk-js"' has no exported member 'EventType'.
import { DwnInterfaceName } from '@tbd54566975/dwn-sdk-js';
import { DwnMethodName } from '@tbd54566975/dwn-sdk-js';

describe('Subscription Manager Test', async () => {
let server: http.Server;
Expand Down Expand Up @@ -57,12 +65,16 @@ describe('Subscription Manager Test', async () => {
const signer = await DidKeyResolver.generate();
const req = await SubscriptionRequest.create({
signer: Jws.createSigner(signer),
filter: {
eventType: EventType.Operation,
},
});

const port = (wsServer.address() as AddressInfo).port;
const ip = (wsServer.address() as AddressInfo).address;
const addr = `ws://${ip}:${port}`;
const socket = new WebSocket(addr);
let receivedCount = 0;

const socketPromise = new Promise<any>((resolve, reject) => {
// set up lisetner...
Expand All @@ -72,7 +84,16 @@ describe('Subscription Manager Test', async () => {
if (resp.error) {
throw new Error(resp.error.message);
}
resolve(event);
receivedCount += 1;
if (
resp.result?.descriptor?.eventDescriptor?.interface ===
DwnInterfaceName.Records &&
resp.result?.descriptor?.eventDescriptor?.method ===
DwnMethodName.Write
) {
resolve(event);
socket.close();
}
return;
} catch (error) {
reject(error);
Expand Down Expand Up @@ -114,10 +135,31 @@ describe('Subscription Manager Test', async () => {
} catch (error) {
reject(error);
}
try {
const { recordsWrite, dataStream } =
await createRecordsWriteMessage(alice);
const dataBytes = await DataStream.toBytes(dataStream);
const encodedData = base64url.baseEncode(dataBytes);

const requestId = uuidv4();
const dwnRequest = createJsonRpcRequest(
requestId,
'dwn.processMessage',
{
message: recordsWrite.toJSON(),
target: alice.did,
encodedData,
},
);
socket.send(JSON.stringify(dwnRequest));
} catch (error) {
reject(error);
}
return;
};
});
await socketPromise;
assert.equal(receivedCount, 2, 'received count');
} catch (error) {
assert.fail(error, undefined, 'failed to register subscription' + error);
}
Expand Down

0 comments on commit 6e02d41

Please sign in to comment.