This commit is contained in:
2022-04-29 16:02:11 +08:00
parent da8024fc30
commit 4e027e2e98
96 changed files with 1687 additions and 14493 deletions

View File

@@ -1,10 +0,0 @@
import { ApiCall } from "../../src/server/base/ApiCall";
import { ReqObjId, ResObjId } from "../proto/PtlObjId";
export async function ApiObjId(call: ApiCall<ReqObjId, ResObjId>) {
call.succ({
id2: call.req.id1,
buf: call.req.buf,
date: call.req.date
})
}

40
test/api/ApiSend.test.ts Normal file
View File

@@ -0,0 +1,40 @@
import assert from 'assert';
import { TsrpcError, WsClient } from 'tsrpc';
import { serviceProto } from '../../src/shared/protocols/serviceProto';
// 1. EXECUTE `npm run dev` TO START A LOCAL DEV SERVER
// 2. EXECUTE `npm test` TO START UNIT TEST
describe('ApiSend', function () {
let client = new WsClient(serviceProto, {
server: 'ws://127.0.0.1:3000',
json: true,
logger: console
});
before(async function () {
let res = await client.connect();
assert.strictEqual(res.isSucc, true, 'Failed to connect to server, have you executed `npm run dev` already?');
})
it('Success', async function () {
let ret = await client.callApi('Send', {
content: 'Test'
});
assert.ok(ret.isSucc)
});
it('Check content is empty', async function () {
let ret = await client.callApi('Send', {
content: ''
});
assert.deepStrictEqual(ret, {
isSucc: false,
err: new TsrpcError('Content is empty')
});
})
after(async function () {
await client.disconnect();
})
})

View File

@@ -1,27 +0,0 @@
import { TsrpcError } from "tsrpc-proto";
import { ApiCall } from "../../src/server/base/ApiCall";
import { ReqTest, ResTest } from "../proto/PtlTest";
export async function ApiTest(call: ApiCall<ReqTest, ResTest>) {
if (call.req.name === 'InnerError') {
await new Promise(rs => { setTimeout(rs, 50) })
throw new Error('Test InnerError')
}
else if (call.req.name === 'TsrpcError') {
await new Promise(rs => { setTimeout(rs, 50) })
throw new TsrpcError('Test TsrpcError', {
code: 'CODE_TEST',
info: 'ErrInfo Test'
});
}
else if (call.req.name === 'error') {
await new Promise(rs => { setTimeout(rs, 50) })
call.error('Got an error')
}
else {
await new Promise(rs => { setTimeout(rs, 50) })
call.succ({
reply: 'Test reply: ' + call.req.name
})
}
}

View File

@@ -1,21 +0,0 @@
import { TsrpcError } from "tsrpc-proto";
export async function ApiTest(call: any) {
if (call.req.name === 'InnerError') {
throw new Error('a/b/c/Test InnerError')
}
else if (call.req.name === 'TsrpcError') {
throw new TsrpcError('a/b/c/Test TsrpcError', {
code: 'CODE_TEST',
info: 'ErrInfo a/b/c/Test'
});
}
else if (call.req.name === 'error') {
call.error('Got an error')
}
else {
call.succ({
reply: 'a/b/c/Test reply: ' + call.req.name
})
}
}