Skip to content

Commit 8b0e709

Browse files
committed
test:Add more tests related to response parsing
1 parent 7b700b6 commit 8b0e709

File tree

5 files changed

+158
-18
lines changed

5 files changed

+158
-18
lines changed

src/gotham-node.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export default class GothamModule {
88
this.protocol = new Protocol(connection);
99
}
1010

11-
async initialize(moduleId: string, version: string, deps: Dependency) {
11+
async initialize(moduleId: string, version: string, deps: Dependency = {}) {
1212
// Setup Connection only when initialize called?
1313
await this.protocol.connect();
1414
return await this.protocol.sendRequest(

src/protocol.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export class Protocol {
7272
return {
7373
requestId: this.generateRequestId(),
7474
type: 'registerHook',
75-
hook: `${this.moduleId}-${hook}`,
75+
hook: `${hook}`,
7676
}
7777
}
7878

@@ -144,7 +144,7 @@ export class Protocol {
144144

145145
async parseFunctionCall(obj: FunctionCallRequest) {
146146
if (this.functions[obj.function]) {
147-
let res = this.functions[obj.function]();
147+
let res = this.functions[obj.function](obj.arguments || {});
148148
if (res instanceof Promise) {
149149
res = await res;
150150
}

test/gotham-node-test.d.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { GothamConnection } from "../src/GothamConnection";
22
import GothamModule from "../src/gotham-node";
33
import { SinonSpy } from "sinon";
4-
import { DummyGothamConnection } from "./gotham-node.test";
4+
import { DummyGothamConnection } from "./helpers";
55

66
interface GothamTest {
77
connection: GothamConnection;
@@ -11,6 +11,7 @@ declare module 'mocha' {
1111
interface Runnable {
1212
conn: DummyGothamConnection;
1313
module: GothamModule;
14-
sendFunc: SinonSpy
14+
sendFunc: SinonSpy;
15+
getLatestSent: Function
1516
}
1617
}

test/gotham-node.test.ts

Lines changed: 145 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@ import chai, { expect, assert } from 'chai';
33
import chaiExclude from 'chai-exclude';
44
import chaiAsPromised from 'chai-as-promised';
55
import { sleep, makeConnectionTests } from './helpers';
6+
import sinon from 'sinon';
67

78
chai.use(chaiExclude);
89
chai.use(sinonChai);
910
chai.use(chaiAsPromised);
1011

1112
makeConnectionTests('Initalize Tests', function () {
1213
it('initalize request constructed correctly', async function () {
13-
this.test.module.initialize('test-module', '1.0.0', {});
14+
this.test.module.initialize('test-module', '1.0.0');
1415
await sleep(0);
15-
assert(this.test.sendFunc.calledOnce);
16-
const message = this.test.sendFunc.getCall(0).args[0];
16+
const message = this.test.getLatestSent();
1717
expect(message).excluding('requestId').to.deep.equal({
1818
type: 'moduleRegistration',
1919
moduleId: 'test-module',
@@ -22,6 +22,24 @@ makeConnectionTests('Initalize Tests', function () {
2222
});
2323
});
2424

25+
it('initialize with deps constructed correctly', async function () {
26+
this.test.module.initialize('test-module', '1.0.0', {
27+
'test-module2': '1.0.1',
28+
'test-module3': '1.0.2'
29+
});
30+
await sleep(0);
31+
const message = this.test.getLatestSent();
32+
expect(message).excluding('requestId').to.deep.equal({
33+
type: 'moduleRegistration',
34+
moduleId: 'test-module',
35+
version: '1.0.0',
36+
dependencies: {
37+
'test-module2': '1.0.1',
38+
'test-module3': '1.0.2'
39+
}
40+
});
41+
});
42+
2543
it('Does not allow to send other request without initializing', async function () {
2644
await expect(
2745
this.test.module.declareFunction('test', () => { })
@@ -36,35 +54,49 @@ makeConnectionTests('Initalize Tests', function () {
3654
this.test.module.triggerHook('test')
3755
).to.be.rejectedWith(Error);
3856
});
57+
58+
it('Initialize resolves correctly/Cannot initalize twice', async function () {
59+
const p = this.test.module.initialize('test-module1', '1.0.0', {});
60+
await sleep(0);
61+
const requestId = this.test.getLatestSent().requestId;
62+
this.test.conn.sendResponse({
63+
requestId,
64+
type: 'moduleRegistered'
65+
});
66+
expect(p).to.eventually.equal(true);
67+
return await expect(
68+
this.test.module.initialize('test-module2', '1.0.0', {})
69+
).to.be.rejectedWith(Error);
70+
});
3971
}, false);
4072

4173
makeConnectionTests('Test if requests constructed correctly', function () {
4274
it('declareFunction', async function () {
4375
this.test.module.declareFunction('test_fn', () => { });
44-
const message = this.test.sendFunc.getCall(0).args[0];
76+
const message = this.test.getLatestSent();
4577
expect(message).excluding('requestId').to.deep.equal({
46-
function: "test_fn",
47-
type: "declareFunction"
78+
function: "test_fn",
79+
type: "declareFunction"
4880
});
4981
});
5082

5183

52-
it('functionCall with empty args', async function() {
84+
it('functionCall with empty args', async function () {
5385
this.test.module.functionCall('module.test_fn');
54-
const message = this.test.sendFunc.getCall(0).args[0];
86+
const message = this.test.getLatestSent();
5587
expect(message).excluding('requestId').to.deep.equal({
56-
function: "module.test_fn",
57-
type: "functionCall",
58-
arguments: {}
88+
function: "module.test_fn",
89+
type: "functionCall",
90+
arguments: {}
5991
});
6092
});
6193

62-
it('functionCall with args', async function() {
94+
it('functionCall with args', async function () {
6395
this.test.module.functionCall('module.test_fn', {
6496
a: 1,
6597
b: 2
6698
});
67-
const message = this.test.sendFunc.getCall(0).args[0];
99+
const message = this.test.getLatestSent();
68100
expect(message).excluding('requestId').to.deep.equal({
69101
function: "module.test_fn",
70102
type: "functionCall",
@@ -74,4 +106,104 @@ makeConnectionTests('Test if requests constructed correctly', function () {
74106
}
75107
});
76108
});
109+
110+
it('registerHook', async function () {
111+
this.test.module.registerHook('test_hook', () => { });
112+
const message = this.test.getLatestSent();
113+
expect(message).excluding('requestId').to.deep.equal({
114+
hook: "test_hook",
115+
type: "registerHook",
116+
});
117+
});
118+
119+
it('triggerHook', async function () {
120+
this.test.module.triggerHook('test_hook');
121+
const message = this.test.getLatestSent();
122+
expect(message).excluding('requestId').to.deep.equal({
123+
type: "triggerHook",
124+
hook: 'test_hook'
125+
});
126+
});
127+
});
128+
129+
130+
makeConnectionTests('Test if responses from gotham parsed correctly', async function () {
131+
it('declareFunction', async function () {
132+
const p = this.test.module.declareFunction('test_fn', () => {});
133+
const requestId = this.test.getLatestSent().requestId;
134+
await sleep(0);
135+
this.test.conn.sendResponse({
136+
requestId,
137+
type: 'functionDeclared',
138+
function: 'test_fn'
139+
});
140+
return expect(p).to.eventually.equal(true);
141+
});
142+
it('hookRegistered', async function () {
143+
const p = this.test.module.registerHook('test_hook', () => {});
144+
await sleep(0);
145+
const requestId = this.test.getLatestSent().requestId;
146+
this.test.conn.sendResponse({
147+
requestId,
148+
type: 'hookRegistered',
149+
});
150+
151+
return expect(p).to.eventually.equal(true);
152+
});
153+
154+
it('hookTriggered', async function() {
155+
const fn = sinon.fake();
156+
const p = this.test.module.registerHook('test_hook', fn);
157+
await sleep(0);
158+
this.test.conn.sendResponse({
159+
requestId: '12345',
160+
type: 'hookTriggered',
161+
hook: 'test_hook'
162+
});
163+
// await sleep(0);
164+
assert(fn.calledOnce);
165+
});
166+
167+
it('functionCall', async function() {
168+
const fn = sinon.fake();
169+
const p = this.test.module.declareFunction('test_fn', fn);
170+
await sleep(0);
171+
172+
this.test.conn.sendResponse({
173+
requestId: '12345',
174+
type: 'functionCall',
175+
function: 'test_fn'
176+
});
177+
178+
assert(fn.calledOnce);
179+
180+
this.test.conn.sendResponse({
181+
requestId: '12345',
182+
type: 'functionCall',
183+
function: 'test_fn',
184+
arguments: {a : 1, b: 2}
185+
});
186+
187+
expect(fn.getCall(1).args[0]).to.deep.equal({a : 1, b: 2});
188+
});
189+
190+
it('functionResponse', async function() {
191+
const p = this.test.module.functionCall('module.test_fn');
192+
await sleep(0);
193+
const requestId = this.test.getLatestSent().requestId;
194+
195+
this.test.conn.sendResponse({
196+
requestId,
197+
type: 'functionResponse',
198+
data: {
199+
a:1,
200+
b: 2
201+
}
202+
});
203+
204+
return expect(p).to.eventually.deep.equal({
205+
a:1,
206+
b:2
207+
});
208+
});
77209
});

test/helpers.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ export function makeConnectionTests(name: string, tests: Function, initalizeModu
2424
beforeEach(async function () {
2525
this.currentTest.conn = new DummyGothamConnection();
2626
this.currentTest.sendFunc = sinon.fake();
27+
this.currentTest.getLatestSent = () => {
28+
if (this.currentTest) {
29+
return this.currentTest.sendFunc.getCall(0).args[0];
30+
} else {
31+
return this.test.sendFunc.getCall(0).args[0];
32+
}
33+
}
2734
sinon.replace(this.currentTest.conn, 'send', this.currentTest.sendFunc);
2835
this.currentTest.module = new GothamModule(
2936
this.currentTest.conn

0 commit comments

Comments
 (0)