diff --git a/src/web/api/__tests__/ServiceContext.test.ts b/src/web/api/__tests__/ServiceContext.test.ts index 5a02d076b..20440898d 100644 --- a/src/web/api/__tests__/ServiceContext.test.ts +++ b/src/web/api/__tests__/ServiceContext.test.ts @@ -1,13 +1,17 @@ import {root} from 'proto' import { connect, ServiceContext } from '../src/ServiceContext' -import WebSocket from 'ws' +import { HelloService } from '../src/HelloService' +// Fix websocket for running in js +import WebSocket from 'ws' global["WebSocket"] = WebSocket -test('promise works', async () => { - let result = await connect("bts", "b"); - expect(result instanceof ServiceContext).toBe(true) +test('log in and say hi', async () => { + let ctx = await connect("bts", "password"); + expect(ctx instanceof ServiceContext).toBe(true) + const hello = new HelloService(ctx) + const reply = await hello.say_hello({ name: "name" }) }); @@ -18,7 +22,7 @@ test('api_errors_updated', () => { code: 3 }).finish() const round = ResponseHeader.decode(rh) - console.log(round.code) + expect(round).toBeDefined() }); diff --git a/src/web/api/src/HelloService.ts b/src/web/api/src/HelloService.ts index be0d88d16..ed5757655 100644 --- a/src/web/api/src/HelloService.ts +++ b/src/web/api/src/HelloService.ts @@ -1,6 +1,7 @@ // Future generated code import root from 'proto' import { ServiceContext } from 'ServiceContext' +// import { assertValid } from 'bts_assert'; const HelloRequestProto = root.lookupType("bts.api.test.HelloRequest") const HelloResponseProto = root.lookupType("bts.api.test.HelloResponse") @@ -16,17 +17,21 @@ export class HelloService { private context : ServiceContext private service_id : string - constructor(sc : ServiceContext, qualifier: string) { + constructor(sc : ServiceContext, qualifier?: string) { this.context = sc - this.qualifier = qualifier.replace(/[+_]/g,""); + this.service_id = (qualifier||"").replace(/[+_]/g,""); } async say_hello(request : HelloRequest) : Promise { const r = HelloRequestProto.create(request) - console.log(JSON.stringify(r)) + // assertValid(HelloRequestProto, r) const buffer = HelloRequestProto.encode(r).finish() // result = Uint8Array - const result = await this.context.call("HelloService", this.qualifier, 0, buffer) + const result = await this.context.call(this.service_id, 0, buffer) + + // decode HelloResponse type + const v = HelloResponseProto.decode(result); + return v } diff --git a/src/web/api/src/ServiceContext.ts b/src/web/api/src/ServiceContext.ts index 64241fba5..3456d65d1 100644 --- a/src/web/api/src/ServiceContext.ts +++ b/src/web/api/src/ServiceContext.ts @@ -1,5 +1,5 @@ import {root} from 'proto' -import { assertValid } from 'bts_assert' +// import { assertValid } from 'bts_assert' const debug=true @@ -15,6 +15,18 @@ function default_ws_uri() { return ws_uri; } +function assert(condition:boolean, message?:string) : void { + if (!condition) { + message = message || "Assertion failed" + throw new Error(message); + } +} + +function assertValid(mtype, message) : void { + const r = mtype.verify(message) + assert(r != null, r) +} + // Message types diff --git a/src/web/api/src/bts_assert.ts b/src/web/api/src/bts_assert.ts index 012484f86..b7b06c2d5 100644 --- a/src/web/api/src/bts_assert.ts +++ b/src/web/api/src/bts_assert.ts @@ -2,10 +2,11 @@ export function assert(condition:boolean, message?:string) { if (!condition) { message = message || "Assertion failed" - throw new Error(message) + throw new Error(message); } } export function assertValid(mtype, message) { const r = mtype.verify(message) assert(r != null, r) -} \ No newline at end of file +} +