Skip to content

Commit d13ca98

Browse files
committed
feat: Setup test libraries
1 parent e4507a5 commit d13ca98

File tree

11 files changed

+2962
-4007
lines changed

11 files changed

+2962
-4007
lines changed

.nycrc.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"extension": [".ts"],
3+
"include": ["src/**"]
4+
}

package-lock.json

Lines changed: 2675 additions & 3862 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 19 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
"prebuild": "rimraf dist",
2424
"build": "tsc --module commonjs && rollup -c rollup.config.ts && typedoc --out docs --target es6 --theme minimal --mode file src",
2525
"start": "rollup -c rollup.config.ts -w",
26-
"test": "jest --coverage",
27-
"test:watch": "jest --coverage --watch",
28-
"test:prod": "npm run lint && npm run test -- --no-cache",
26+
"test": "ts-mocha -p ./tsconfig.json test/**/*.test.ts",
27+
"test:coverage": "nyc --reporter=html --reporter=text --reporter=text-summary npm test",
28+
"test:prod": "npm run lint && npm run test",
2929
"deploy-docs": "ts-node tools/gh-pages-publish",
3030
"report-coverage": "cat ./coverage/lcov.info | coveralls",
3131
"commit": "git-cz",
@@ -45,33 +45,6 @@
4545
"path": "node_modules/cz-conventional-changelog"
4646
}
4747
},
48-
"jest": {
49-
"transform": {
50-
".(ts|tsx)": "ts-jest"
51-
},
52-
"testEnvironment": "node",
53-
"testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$",
54-
"moduleFileExtensions": [
55-
"ts",
56-
"tsx",
57-
"js"
58-
],
59-
"coveragePathIgnorePatterns": [
60-
"/node_modules/",
61-
"/test/"
62-
],
63-
"coverageThreshold": {
64-
"global": {
65-
"branches": 90,
66-
"functions": 95,
67-
"lines": 95,
68-
"statements": 95
69-
}
70-
},
71-
"collectCoverageFrom": [
72-
"src/*.{js,ts}"
73-
]
74-
},
7548
"prettier": {
7649
"semi": false,
7750
"singleQuote": true
@@ -84,18 +57,26 @@
8457
"devDependencies": {
8558
"@commitlint/cli": "^7.1.2",
8659
"@commitlint/config-conventional": "^7.1.2",
87-
"@types/jest": "^23.3.2",
60+
"@types/chai": "^4.2.7",
61+
"@types/chai-as-promised": "^7.1.2",
62+
"@types/expect": "^24.3.0",
63+
"@types/mocha": "^5.2.7",
8864
"@types/node": "^10.11.0",
65+
"@types/sinon": "^7.5.1",
66+
"@types/sinon-chai": "^3.2.3",
67+
"chai": "^4.2.0",
68+
"chai-as-promised": "^7.1.1",
69+
"chai-exclude": "^2.0.2",
8970
"colors": "^1.3.2",
9071
"commitizen": "^3.0.0",
9172
"coveralls": "^3.0.2",
9273
"cross-env": "^5.2.0",
9374
"cz-conventional-changelog": "^2.1.0",
9475
"husky": "^1.0.1",
95-
"jest": "^23.6.0",
96-
"jest-config": "^23.6.0",
9776
"lint-staged": "^8.0.0",
9877
"lodash.camelcase": "^4.3.0",
78+
"mocha": "^7.0.0",
79+
"nyc": "^15.0.0",
9980
"prettier": "^1.14.3",
10081
"prompt": "^1.0.0",
10182
"replace-in-file": "^3.4.2",
@@ -108,13 +89,16 @@
10889
"rollup-plugin-typescript2": "^0.18.0",
10990
"semantic-release": "^15.9.16",
11091
"shelljs": "^0.8.3",
92+
"sinon": "^8.0.2",
93+
"sinon-chai": "^3.4.0",
11194
"travis-deploy-once": "^5.0.9",
112-
"ts-jest": "^23.10.2",
95+
"ts-mocha": "^6.0.0",
11396
"ts-node": "^7.0.1",
11497
"tslint": "^5.11.0",
11598
"tslint-config-prettier": "^1.15.0",
11699
"tslint-config-standard": "^8.0.1",
117100
"typedoc": "^0.12.0",
118101
"typescript": "^3.0.3"
119-
}
102+
},
103+
"dependencies": {}
120104
}

src/protocol/GothamConnection.ts renamed to src/GothamConnection.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
import * as net from 'net';
22

33
type dataListenerFn = (data: any) => any;
4-
export abstract class GothamConnection {
5-
dataListener: dataListenerFn = (data) => { };
6-
abstract async setupConnection(): Promise<any>;
7-
abstract async closeConnection(): Promise<any>;
8-
abstract setupDataListener(dataListener: dataListenerFn): any;
9-
abstract async send(message: object): Promise<void>;
4+
export class GothamConnection {
5+
async setupConnection(): Promise<any> {}
6+
async closeConnection(): Promise<any> {}
7+
setupDataListener(dataListener: dataListenerFn): any {};
8+
async send(message: object): Promise<void> {};
109
}
1110

12-
13-
1411
export class SocketConnection extends GothamConnection {
1512
client!: net.Socket;
1613
sockPath: string;
@@ -34,13 +31,13 @@ export class SocketConnection extends GothamConnection {
3431
}
3532

3633
setupDataListener(dataListener: dataListenerFn) {
37-
this.dataListener = (data: Buffer) => {
34+
const parsedDataListener = (data: Buffer) => {
3835
const parsed = JSON.parse(data.toString('utf-8'));
3936
// console.log("Socket Received Data:");
4037
// console.log(parsed);
4138
dataListener(parsed);
4239
};
43-
this.client.on('data', this.dataListener);
40+
this.client.on('data', parsedDataListener);
4441
}
4542

4643
async send(message: object) {

src/gotham-node.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { Protocol, Dependency, FnArgs } from "./protocol/protocol";
2-
import { GothamConnection, SocketConnection } from './protocol/GothamConnection';
1+
import { Protocol } from "./protocol";
2+
import { GothamConnection, SocketConnection } from './GothamConnection';
3+
import { Dependency, FnArgs } from "./types/protocol";
34

45
export default class GothamModule {
5-
protocol: Protocol
6+
private protocol: Protocol;
67
constructor(connection: GothamConnection) {
78
this.protocol = new Protocol(connection);
89
}
@@ -21,7 +22,7 @@ export default class GothamModule {
2122
);
2223
}
2324

24-
async functionCall(fnNmame: string, args: FnArgs) {
25+
async functionCall(fnNmame: string, args: FnArgs = {}) {
2526
return await this.protocol.sendRequest(
2627
this.protocol.callFunction(fnNmame, args)
2728
);

src/protocol/protocol.ts renamed to src/protocol.ts

Lines changed: 15 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,9 @@
1-
import { constants } from "../constants";
1+
import { constants } from "./constants";
22
import { GothamConnection } from './GothamConnection';
3-
4-
export interface Dependency {
5-
[type: string]: string;
6-
}
7-
8-
export interface FnArgs {
9-
[type: string]: any;
10-
}
11-
12-
interface FnMappings {
13-
[type: string]: Function;
14-
}
15-
16-
interface HookMappings {
17-
[type: string]: Function[]
18-
}
19-
20-
interface ProtocolMessage {
21-
requestId: string;
22-
type: string;
23-
}
24-
25-
interface RegisterModuleRequest extends ProtocolMessage {
26-
type: 'moduleRegistration';
27-
moduleId: string;
28-
version: string;
29-
dependencies: Dependency;
30-
}
31-
32-
interface RegisterModuleResponse extends ProtocolMessage {
33-
type: 'moduleRegistered';
34-
}
35-
36-
interface FunctionCallRequest extends ProtocolMessage {
37-
type: 'functionCall';
38-
function: string;
39-
arguments: FnArgs;
40-
}
41-
42-
interface FunctionCallResponse extends ProtocolMessage {
43-
type: 'functionResponse';
44-
data: FnArgs;
45-
}
46-
47-
interface RegisterHookRequest extends ProtocolMessage {
48-
type: 'registerHook';
49-
hook: string;
50-
}
51-
52-
interface ListenHookResponse extends ProtocolMessage {
53-
type: 'hookRegistered';
54-
}
55-
56-
interface TriggerHookRequest extends ProtocolMessage {
57-
type: 'triggerHook';
58-
hook: string;
59-
}
60-
61-
interface TriggerHookResponse extends ProtocolMessage {
62-
type: 'hookTriggered';
63-
hook: string;
64-
}
65-
66-
interface CallHookResponse extends ProtocolMessage {
67-
type: 'hookTriggered';
68-
hook: string;
69-
}
70-
71-
interface DeclareFunctionRequest extends ProtocolMessage {
72-
type: 'declareFunction';
73-
function: string;
74-
}
75-
76-
interface DeclareFunctionResponse extends ProtocolMessage {
77-
type: 'functionDeclared';
78-
function: string;
79-
}
3+
import { FnMappings, HookMappings, GothamRequest, Dependency, RegisterModuleRequest, RegisterHookRequest, TriggerHookRequest, DeclareFunctionRequest, FnArgs, FunctionCallRequest, GothamResponse, FunctionCallResponse } from "./types/protocol";
804

815
export class Protocol {
6+
private registered: boolean;
827
private moduleId!: string;
838
private functions: FnMappings = {};
849
private hookListeners: HookMappings = {};
@@ -88,6 +13,7 @@ export class Protocol {
8813

8914
constructor(connection: GothamConnection) {
9015
this.connection = connection;
16+
this.registered = false;
9117
}
9218

9319
async connect() {
@@ -101,7 +27,15 @@ export class Protocol {
10127
await this.connection.closeConnection();
10228
}
10329

104-
async sendRequest(obj: any) {
30+
async sendRequest(obj: GothamRequest) {
31+
if (obj.type !== 'moduleRegistration' && !this.registered) {
32+
throw new Error("Can't send request before module has been registered");
33+
}
34+
35+
if (obj.type === 'moduleRegistration' && this.registered) {
36+
throw new Error("Module already registered");
37+
}
38+
10539
await this.connection.send(obj);
10640
return new Promise((resolve, reject) => {
10741
this.requests[obj.requestId] = (res: Object) => {
@@ -182,9 +116,10 @@ export class Protocol {
182116
}
183117
}
184118

185-
async parseResponse(obj: ProtocolMessage) {
119+
async parseResponse(obj: GothamResponse) {
186120
let res;
187121
if (obj.type === constants.responseType.moduleRegistered) {
122+
this.registered = true;
188123
res = true;
189124
} else if (obj.type === constants.requestType.functionCall) {
190125
res = await this.parseFunctionCall(obj as FunctionCallRequest);

src/types/protocol.ts

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
export type dataListenerFn = (data: any) => any;
2+
3+
export interface Dependency {
4+
[type: string]: string;
5+
}
6+
7+
export interface FnArgs {
8+
[type: string]: any;
9+
}
10+
11+
export interface FnMappings {
12+
[type: string]: Function;
13+
}
14+
15+
export interface HookMappings {
16+
[type: string]: Function[]
17+
}
18+
19+
export interface ProtocolMessage {
20+
requestId: string;
21+
type: string;
22+
}
23+
24+
export interface RegisterModuleRequest extends ProtocolMessage {
25+
type: 'moduleRegistration';
26+
moduleId: string;
27+
version: string;
28+
dependencies: Dependency;
29+
}
30+
31+
export interface RegisterModuleResponse extends ProtocolMessage {
32+
type: 'moduleRegistered';
33+
}
34+
35+
export interface FunctionCallRequest extends ProtocolMessage {
36+
type: 'functionCall';
37+
function: string;
38+
arguments: FnArgs;
39+
}
40+
41+
export interface FunctionCallResponse extends ProtocolMessage {
42+
type: 'functionResponse';
43+
data: FnArgs;
44+
}
45+
46+
export interface RegisterHookRequest extends ProtocolMessage {
47+
type: 'registerHook';
48+
hook: string;
49+
}
50+
51+
export interface ListenHookResponse extends ProtocolMessage {
52+
type: 'hookRegistered';
53+
}
54+
55+
export interface TriggerHookRequest extends ProtocolMessage {
56+
type: 'triggerHook';
57+
hook: string;
58+
}
59+
60+
export interface TriggerHookResponse extends ProtocolMessage {
61+
type: 'hookTriggered';
62+
hook: string;
63+
}
64+
65+
export interface CallHookResponse extends ProtocolMessage {
66+
type: 'hookTriggered';
67+
hook: string;
68+
}
69+
70+
export interface DeclareFunctionRequest extends ProtocolMessage {
71+
type: 'declareFunction';
72+
function: string;
73+
}
74+
75+
export interface FunctionResponseRequest extends ProtocolMessage {
76+
type: 'functionResponse';
77+
data: any;
78+
}
79+
80+
export interface DeclareFunctionResponse extends ProtocolMessage {
81+
type: 'functionDeclared';
82+
function: string;
83+
}
84+
85+
export type GothamRequest = RegisterModuleRequest | FunctionCallRequest | RegisterHookRequest | DeclareFunctionRequest | TriggerHookRequest | FunctionResponseRequest;
86+
export type GothamResponse = RegisterModuleResponse | ListenHookResponse | TriggerHookResponse | CallHookResponse | DeclareFunctionResponse | FunctionCallResponse | FunctionCallRequest | TriggerHookRequest;

test/gotham-node-test.d.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { GothamConnection } from "../src/GothamConnection";
2+
import GothamModule from "../src/gotham-node";
3+
import { SinonSpy } from "sinon";
4+
import { DummyGothamConnection } from "./gotham-node.test";
5+
6+
interface GothamTest {
7+
connection: GothamConnection;
8+
}
9+
10+
declare module 'mocha' {
11+
interface Runnable {
12+
conn: DummyGothamConnection;
13+
module: GothamModule;
14+
sendFunc: SinonSpy
15+
}
16+
}

0 commit comments

Comments
 (0)