mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-20 16:42:56 +00:00
feat: tests
This commit is contained in:
@@ -13,36 +13,35 @@
|
||||
* this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import {Converter} from '@openstapps/core-tools/lib/schema';
|
||||
import {Converter} from '@openstapps/core-tools';
|
||||
import chai from 'chai';
|
||||
import {expect} from 'chai';
|
||||
import chaiSpies from 'chai-spies';
|
||||
import {readFileSync} from 'fs';
|
||||
import {suite, test, timeout} from '@testdeck/mocha';
|
||||
import {resolve} from 'path';
|
||||
import {HttpClient} from '../src/http-client.js';
|
||||
import {HttpClient} from '../src/index.js';
|
||||
import {TestPlugin} from './plugin-resources/test-plugin.js';
|
||||
import path from "path";
|
||||
import {readFile} from "fs/promises";
|
||||
import {fileURLToPath} from "url";
|
||||
|
||||
chai.use(chaiSpies);
|
||||
|
||||
process.on('unhandledRejection', err => {
|
||||
throw err;
|
||||
process.on('unhandledRejection', error => {
|
||||
throw error;
|
||||
});
|
||||
|
||||
const sandbox = chai.spy.sandbox();
|
||||
|
||||
const httpClient = new HttpClient();
|
||||
|
||||
@suite(timeout(20000))
|
||||
export class PluginSpec {
|
||||
static testPlugin: TestPlugin;
|
||||
const dirname = path.dirname(fileURLToPath(import.meta.url));
|
||||
|
||||
static async after() {
|
||||
PluginSpec.testPlugin.close();
|
||||
}
|
||||
describe('Plugin', function () {
|
||||
this.timeout(20_000);
|
||||
|
||||
static async before() {
|
||||
PluginSpec.testPlugin = new TestPlugin(
|
||||
let testPlugin: TestPlugin;
|
||||
|
||||
beforeEach(function () {
|
||||
testPlugin = new TestPlugin(
|
||||
4000,
|
||||
'',
|
||||
'',
|
||||
@@ -52,22 +51,22 @@ export class PluginSpec {
|
||||
getSchema: () => {
|
||||
/***/
|
||||
},
|
||||
} as any,
|
||||
} as never,
|
||||
'',
|
||||
'',
|
||||
'',
|
||||
);
|
||||
}
|
||||
})
|
||||
|
||||
async after() {
|
||||
afterEach(async function () {
|
||||
await testPlugin.close();
|
||||
sandbox.restore();
|
||||
}
|
||||
})
|
||||
|
||||
@test
|
||||
async construct() {
|
||||
it('should construct', async function () {
|
||||
const converter = new Converter(
|
||||
__dirname,
|
||||
resolve(__dirname, 'plugin-resources', 'test-plugin-response.ts'),
|
||||
dirname,
|
||||
path.resolve(dirname, 'plugin-resources', 'test-plugin-response.ts'),
|
||||
);
|
||||
|
||||
sandbox.on(converter, 'getSchema', schemaName => {
|
||||
@@ -80,20 +79,19 @@ export class PluginSpec {
|
||||
'http://B',
|
||||
'/C', // this doesn't matter for our tests, it's only something that affects the backend
|
||||
'http://D',
|
||||
// @ts-ignore fake converter is not a converter
|
||||
converter,
|
||||
'PluginTestRequest',
|
||||
'PluginTestResponse',
|
||||
JSON.parse(readFileSync(resolve(__dirname, '..', 'package.json')).toString()).version,
|
||||
JSON.parse(await readFile(path.resolve(dirname, '..', 'package.json'), 'utf8')).version,
|
||||
);
|
||||
expect(constructTestPlugin.port).to.be.equal(4001);
|
||||
expect(constructTestPlugin.name).to.be.equal('A');
|
||||
expect(constructTestPlugin.url).to.be.equal('http://B');
|
||||
expect(constructTestPlugin.route).to.be.equal('/C');
|
||||
// @ts-ignore backendUrl is protected
|
||||
// @ts-expect-error private property
|
||||
expect(constructTestPlugin.backendUrl).to.be.equal('http://D');
|
||||
// schemas are already covered, together with the directory and version
|
||||
// @ts-ignore active is private
|
||||
// @ts-expect-error private property
|
||||
expect(constructTestPlugin.active).to.be.equal(false);
|
||||
expect(constructTestPlugin.requestSchema.$id).to.be.equal('PluginTestRequest');
|
||||
expect(constructTestPlugin.responseSchema.$id).to.be.equal('PluginTestResponse');
|
||||
@@ -103,15 +101,14 @@ export class PluginSpec {
|
||||
url: new URL('http://localhost:4001'),
|
||||
});
|
||||
// onRouteInvoke is a protected method, but we need to access it from the outside to test it
|
||||
// @ts-ignore
|
||||
// @ts-expect-error protected method
|
||||
expect(constructTestPlugin.onRouteInvoke).not.to.have.been.called();
|
||||
|
||||
await constructTestPlugin.close();
|
||||
sandbox.restore(constructTestPlugin, 'onRouteInvoke');
|
||||
}
|
||||
});
|
||||
|
||||
@test
|
||||
async fullUrl() {
|
||||
it('should have full url', async function () {
|
||||
const constructTestPlugin = new TestPlugin(
|
||||
4001,
|
||||
'',
|
||||
@@ -122,37 +119,35 @@ export class PluginSpec {
|
||||
getSchema: () => {
|
||||
/***/
|
||||
},
|
||||
} as any,
|
||||
} as never,
|
||||
'',
|
||||
'',
|
||||
'',
|
||||
);
|
||||
expect(constructTestPlugin.fullUrl).to.be.equal('http://B:4001');
|
||||
await constructTestPlugin.close();
|
||||
}
|
||||
});
|
||||
|
||||
@test
|
||||
async start() {
|
||||
PluginSpec.testPlugin.start();
|
||||
it('should start', async function () {
|
||||
testPlugin.start();
|
||||
|
||||
sandbox.on(PluginSpec.testPlugin, 'onRouteInvoke');
|
||||
sandbox.on(testPlugin, 'onRouteInvoke');
|
||||
|
||||
await httpClient.request({
|
||||
url: new URL('http://localhost:4000'),
|
||||
});
|
||||
|
||||
// onRouteInvoke is a protected method, but we need to access it from the outside to test it
|
||||
// @ts-ignore
|
||||
expect(PluginSpec.testPlugin.onRouteInvoke).to.have.been.called();
|
||||
}
|
||||
// @ts-expect-error protected method
|
||||
expect(testPlugin.onRouteInvoke).to.have.been.called();
|
||||
});
|
||||
|
||||
@test
|
||||
async stop() {
|
||||
it('should stop', async function () {
|
||||
// simulate a normal use case by first starting the plugin and then stopping it
|
||||
PluginSpec.testPlugin.start();
|
||||
PluginSpec.testPlugin.stop();
|
||||
testPlugin.start();
|
||||
testPlugin.stop();
|
||||
|
||||
sandbox.on(PluginSpec.testPlugin, 'onRouteInvoke');
|
||||
sandbox.on(testPlugin, 'onRouteInvoke');
|
||||
|
||||
const response = await httpClient.request({
|
||||
url: new URL('http://localhost:4000'),
|
||||
@@ -160,7 +155,7 @@ export class PluginSpec {
|
||||
|
||||
await expect(response.statusCode).to.be.equal(404);
|
||||
// onRouteInvoke is a protected method, but we need to access it from the outside to test it
|
||||
// @ts-ignore
|
||||
expect(PluginSpec.testPlugin.onRouteInvoke).not.to.have.been.called();
|
||||
}
|
||||
}
|
||||
// @ts-expect-error protected method
|
||||
expect(testPlugin.onRouteInvoke).not.to.have.been.called();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user