probably a working typecheck for callbacks

This commit is contained in:
nitowa
2022-04-05 05:41:08 +02:00
parent 7e7ec0deb8
commit a1f4055194
9 changed files with 254 additions and 60 deletions
+83 -5
View File
@@ -1,7 +1,7 @@
import { describe, it } from "mocha";
import { RPCServer, RPCSocket } from '../Index'
import { RPCServer, RPCSocket, Serializable } from '../Index'
import { RPCExporter, Socket } from "../src/Interfaces";
import { ConnectedSocket } from "../src/Types";
import { ConnectedSocket, Callback } from "../src/Types";
import * as log from 'why-is-node-running';
import * as http from 'http';
import * as express from 'express';
@@ -614,7 +614,7 @@ type topicDTO = { topic: string; }
type SesameTestIfc = {
test: {
checkCandy: () => Promise<string>
subscribe: (callback: Function) => Promise<topicDTO>
subscribe: (callback: Callback<string>) => Promise<topicDTO>
manyParams: <A = string, B = number, C = boolean, D = Object>(a: A, b: B, c: C, d: D) => Promise<[A, B, C, D]>
}
}
@@ -688,7 +688,7 @@ describe('Sesame should unlock the socket', () => {
})
it('callback should work with sesame', (done) => {
client.test.subscribe((c) => {
client.test.subscribe(function(c){
if (c === candy) {
done()
}
@@ -1009,5 +1009,83 @@ describe("attaching handlers before connecting", () => {
done(e)
})
})
})
describe("class (de-)serialization", () => {
@Serializable()
class SubClass{
fString = "F"
}
@Serializable()
class TestClass{
aString = "A"
aNumber = 46
aObject = {
x: "x",
y: undefined,
sub: new SubClass()
}
aClassObject = new SubClass()
public returnOK(){
return "OK"
}
}
let myServer: RPCServer;
let mySocket: RPCSocket;
before(function(done){
myServer = new RPCServer([{
name: "Test",
RPCs: [
function returnClass(){
return new TestClass()
}
]
}])
myServer.listen(8084)
mySocket = new RPCSocket(8084, 'localhost')
mySocket.connect().then(() => done())
})
after(function(done){
mySocket.close()
myServer.close()
done()
})
it("receives class in call response", async () => {
const obj: TestClass = await mySocket['Test'].returnClass()
expect(obj).to.be.an.instanceOf(TestClass)
expect(obj.aString).to.be.a('string')
expect(obj.aNumber).to.be.a('number')
expect(obj.aObject).to.be.a('object')
expect(obj.aObject.x).to.be.a('string')
expect(obj.aObject.y).to.be.undefined
expect(obj.aObject.sub).to.be.an.instanceOf(SubClass)
expect(obj.aClassObject).to.be.an.instanceOf(SubClass)
expect(obj.returnOK()).to.be.equal('OK')
})
it("receives class in hook response", async () => {
const obj: TestClass = await mySocket['Test'].returnClass()
expect(obj).to.be.an.instanceOf(TestClass)
expect(obj.aString).to.be.a('string')
expect(obj.aNumber).to.be.a('number')
expect(obj.aObject).to.be.a('object')
expect(obj.aObject.x).to.be.a('string')
expect(obj.aObject.y).to.be.undefined
expect(obj.aObject.sub).to.be.an.instanceOf(SubClass)
expect(obj.aClassObject).to.be.an.instanceOf(SubClass)
expect(obj.returnOK()).to.be.equal('OK')
})
})