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

Client (on server) tests for example interface #204

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"main": "server.js",
"types": "types/global.d.ts",
"scripts": {
"test": "npm run lint && npm run types && node test/system.js",
"test": "npm run lint && npm run types && node test/system.js && node test/client.js",
"types": "tsc",
"lint": "eslint . && prettier -c \"**/*.js\" \"**/*.json\" \"**/*.md\" \"**/*.yml\" \"**/*.ts\"",
"fmt": "prettier --write \"**/*.js\" \"**/*.json\" \"**/*.md\" \"**/*.yml\" \"**/*.ts\"",
Expand Down
158 changes: 158 additions & 0 deletions test/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
'use strict';

const metatests = require('metatests');
const { Metacom } = require('metacom/lib/client');

let token = '';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this to the runTests argument.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, explain

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redefine runTests as

onst runTests = (client, token) => {

To get rid of global parameters.


const runTests = (client) => {
metatests.testAsync('system/introspect', async (test) => {
const introspect = await client.socketCall('system')('introspect')([
'auth',
'console',
'example',
'files',
'test',
]);
test.strictSame(introspect?.auth?.restore?.[0], 'token');
test.end();
});

metatests.testAsync('example/add', async (test) => {
const add = await client.socketCall('example')('add')({ a: 1, b: 2 });
test.strictSame(add, 3);
test.end();
});

metatests.testAsync('example/citiesByCountry', async (test) => {
const cities = await client.socketCall('example')('citiesByCountry')({
countryId: 1,
});
test.strictEqual(cities?.result, 'success');
test.strictEqual(Array.isArray(cities?.data), true);
test.end();
});

metatests.testAsync('example/customError', async (test) => {
try {
await client.socketCall('example')('customError')();
leonpolak marked this conversation as resolved.
Show resolved Hide resolved
} catch (customError) {
test.errorCompare(customError, new Error('Return custom error', 12345));
test.strictEqual(customError?.code, 12345);
} finally {
test.end();
}
});

metatests.testAsync('example/customException', async (test) => {
try {
await client.socketCall('example')('customException')();
leonpolak marked this conversation as resolved.
Show resolved Hide resolved
} catch (customError) {
test.errorCompare(customError, new Error('Custom ecxeption', 12345));
test.strictEqual(customError?.code, 12345);
} finally {
test.end();
}
});

metatests.testAsync('example/error', async (test) => {
try {
await client.socketCall('example')('error')();
leonpolak marked this conversation as resolved.
Show resolved Hide resolved
} catch (Error) {
test.errorCompare(Error, new Error('Return error'));
} finally {
test.end();
}
});

metatests.testAsync('example/exception', async (test) => {
try {
await client.socketCall('example')('exception')();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to catch an exception to check the message and custom data in custom exceptions and errors

} catch (Error) {
test.errorCompare(Error, new Error('Example exception'));
} finally {
test.end();
}
});

metatests.testAsync('example/getClientInfo', async (test) => {
try {
const info = await client.socketCall('example')('getClientInfo')();
test.strictEqual(info?.result?.ip, '127.0.0.1');
test.strictEqual(info?.result?.token, token);
test.strictEqual(info?.result?.accountId, '2');
} catch (Error) {
console.log(Error);
leonpolak marked this conversation as resolved.
Show resolved Hide resolved
} finally {
test.end();
}
});

tshemsedinov marked this conversation as resolved.
Show resolved Hide resolved
metatests.testAsync('example/redisSet + redisGet', async (test) => {
try {
const setting = await client.socketCall('example')('redisSet')({
key: 'MetarhiaExampleTest',
value: 1,
});
const getting = await client.socketCall('example')('redisGet')({
key: 'MetarhiaExampleTest',
});
console.log('setting', setting);
console.log('getting', getting);
} catch (Error) {
test.errorCompare(Error, new Error('Example exception'));
} finally {
test.end();
}
});

metatests.testAsync('example/resources', async (test) => {
try {
const resources = await client.socketCall('example')('resources')();
console.log(resources);
} catch (Error) {
console.log(Error);
} finally {
test.end();
}
});

metatests.testAsync('example/wait', async (test) => {
try {
const wait = await client.socketCall('example')('wait')({ delay: 1000 });
test.strictEqual(wait, 'done');
} catch (Error) {
console.log(Error);
} finally {
test.end();
}
});
};

const connect = async () => {
const url = 'ws://127.0.0.1:8000/api';
let client;
try {
client = Metacom.create(url);
} catch (e) {
console.log(e);
}

if (!client) {
console.log('No client');
return;
}
leonpolak marked this conversation as resolved.
Show resolved Hide resolved
await client.ready();
const signin = await client.socketCall('auth')('signin')({
login: 'marcus',
password: 'marcus',
});
if (typeof signin !== 'object' || signin?.status !== 'logged') {
console.log('Not logged');
leonpolak marked this conversation as resolved.
Show resolved Hide resolved
return;
}
token = signin?.token;
runTests(client);
};

connect();