Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Server tests #446

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 94 additions & 1 deletion test/server.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,96 @@
'use strict';

console.log('No server tests implemented');
const timers = require('node:timers/promises');
const { WebSocket } = require('ws');
const metatests = require('metatests');
const { Server } = require('../lib/server.js');

const { emitWarning } = process;
process.emitWarning = (warning, type, ...args) => {
if (type === 'ExperimentalWarning') return;
emitWarning(warning, type, ...args);
return;
};

class ProcedureMock {
constructor({ access, ...options }) {
this.options = options;
this.access = access;
}

// eslint-disable-next-line class-methods-use-this
async enter() {}
// eslint-disable-next-line class-methods-use-this
leave() {}
invoke(_context, args) {
return this.options.handler(args);
}
}

metatests.test('Server / calls', async (test) => {
const api = {
test: {
hello: {
access: 'public',
handler: async ({ name }) => {
await timers.setTimeout(10);
return `Hello, ${name}`;
},
},
},
};
const noop = () => {};
const options = {
host: 'localhost',
port: 3000,
protocol: 'http',
timeouts: { bind: 100 },
queue: { concurrency: 100, size: 100, timeout: 5_000 },
};
const application = {
console: { log: noop, info: noop, warn: noop, error: noop },
static: { constructor: { name: 'Static' } },
auth: { saveSession: async () => {} },
getMethod: (unit, _version, method) => new ProcedureMock(api[unit][method]),
getHook: noop,
};

/** @type Server */
let server;

test.beforeEach(async () => {
server = new Server(application, options);
});

test.afterEach(async () => {
await server.close();
});

test.testAsync('handles HTTP RPC', async (subtest) => {
const id = 1;
const args = { name: 'Max' };
const packet = { type: 'call', id, method: 'test/hello', args };
const response = await fetch(`http://${options.host}:${options.port}/api`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(packet),
}).then((res) => res.json());
subtest.strictEqual(response.id, id);
subtest.strictEqual(response.type, 'callback');
subtest.strictEqual(response.result, `Hello, ${args.name}`);
});

test.testAsync('handles WS RPC', async (subtest) => {
const id = 1;
const args = { name: 'Max' };
const packet = { type: 'call', id, method: 'test/hello', args };
const socket = new WebSocket(`ws://${options.host}:${options.port}`);
await new Promise((res) => socket.on('open', res));
socket.send(JSON.stringify(packet));
const resPacket = await new Promise((res) => socket.on('message', res));
const response = JSON.parse(resPacket);
subtest.strictEqual(response.id, id);
subtest.strictEqual(response.type, 'callback');
subtest.strictEqual(response.result, `Hello, ${args.name}`);
});
});