Skip to content

Commit

Permalink
fix: linting tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ralfaron committed Jan 16, 2024
1 parent b8b38c2 commit 516029c
Show file tree
Hide file tree
Showing 21 changed files with 193 additions and 153 deletions.
2 changes: 1 addition & 1 deletion projects/aas-server/src/app/live/http/http-socket-item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
*****************************************************************************/

import { LiveNode } from 'common';
import { SocketItem } from '../socket-item.js';
import { noop } from 'lodash-es';
import { SocketItem } from '../socket-item.js';

export class HttpSocketItem implements SocketItem {
public constructor(
Expand Down
2 changes: 1 addition & 1 deletion projects/aas-server/src/test/application-info.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ describe('Application Info service', () => {
const expected: PackageInfo = JSON.parse((await readFile(file)).toString());
await expect(applicationInfo.getAsync(file)).resolves.toEqual(expected);
});
});
});
16 changes: 9 additions & 7 deletions projects/aas-server/src/test/configuration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
*
*****************************************************************************/

import { getEndpointName, getEndpointType } from "../app/configuration.js";
import { describe, it, expect } from '@jest/globals'
import { getEndpointName, getEndpointType } from '../app/configuration.js';
import { describe, it, expect } from '@jest/globals';

describe('configuration', function () {
describe('getEndpointName', function () {
Expand All @@ -24,13 +24,15 @@ describe('configuration', function () {
});

it('gets the default name of an AAS registry', function () {
expect(getEndpointName('http://localhost:1234/v1/registry?type=AASRegistry'))
.toEqual('http://localhost:1234/v1/registry');
expect(getEndpointName('http://localhost:1234/v1/registry?type=AASRegistry')).toEqual(
'http://localhost:1234/v1/registry',
);
});

it('gets the default name of an OPCUA server', function () {
expect(getEndpointName(new URL('opc.tcp://172.16.160.178:30001/I4AASServer')))
.toEqual('opc.tcp://172.16.160.178:30001/I4AASServer');
expect(getEndpointName(new URL('opc.tcp://172.16.160.178:30001/I4AASServer'))).toEqual(
'opc.tcp://172.16.160.178:30001/I4AASServer',
);
});

it('gets the default name of an AASX directory', function () {
Expand All @@ -51,4 +53,4 @@ describe('configuration', function () {
expect(getEndpointType('http://localhost:1234/')).toEqual('AasxServer');
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,21 @@ describe('EndpointsController', function () {
beforeEach(function () {
logger = createSpyObj<Logger>(['error', 'warning', 'info', 'debug', 'start', 'stop']);
variable = createSpyObj<Variable>({}, { JWT_SECRET: 'SecretSecretSecretSecretSecretSecret' });
auth = createSpyObj<AuthService>(
[
'hasUserAsync',
'loginAsync',
'getCookieAsync',
'getCookiesAsync',
'setCookieAsync',
'deleteCookieAsync'
]);

aasProvider = createSpyObj<AASProvider>(
[
'getEndpoints',
'addEndpointAsync',
'removeEndpointAsync',
'resetAsync',
]);
auth = createSpyObj<AuthService>([
'hasUserAsync',
'loginAsync',
'getCookieAsync',
'getCookiesAsync',
'setCookieAsync',
'deleteCookieAsync',
]);

aasProvider = createSpyObj<AASProvider>([
'getEndpoints',
'addEndpointAsync',
'removeEndpointAsync',
'resetAsync',
]);

authentication = createSpyObj<Authentication>(['checkAsync']);
authentication.checkAsync.mockResolvedValue(guestPayload);
Expand All @@ -76,13 +74,11 @@ describe('EndpointsController', function () {
const endpoints: AASEndpoint = {
name: 'Test',
url: 'http://localhost:1234',
type: 'AasxServer'
type: 'AasxServer',
};

aasProvider.getEndpoints.mockResolvedValue([endpoints]);
const response = await request(app)
.get('/api/v1/endpoints')
.set('Authorization', `Bearer ${getToken()}`);
const response = await request(app).get('/api/v1/endpoints').set('Authorization', `Bearer ${getToken()}`);

expect(response.statusCode).toBe(200);
expect(response.body).toEqual([endpoints]);
Expand All @@ -93,7 +89,8 @@ describe('EndpointsController', function () {
const endpoint: AASEndpoint = { name: 'Samples', url: 'file:///assets/samples', type: 'AasxDirectory' };
aasProvider.addEndpointAsync.mockResolvedValue();
auth.hasUserAsync.mockResolvedValue(true);
const response = await request(app).post('/api/v1/endpoints/samples')
const response = await request(app)
.post('/api/v1/endpoints/samples')
.set('Authorization', `Bearer ${getToken('John')}`)
.send(endpoint);

Expand Down Expand Up @@ -122,4 +119,4 @@ describe('EndpointsController', function () {
expect(response.statusCode).toBe(204);
expect(aasProvider.resetAsync).toHaveBeenCalled();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,16 @@ describe('TemplateController', function () {
beforeEach(function () {
logger = createSpyObj<Logger>(['error', 'warning', 'info', 'debug', 'start', 'stop']);
variable = createSpyObj<Variable>({}, { JWT_SECRET: 'SecretSecretSecretSecretSecretSecret' });
auth = createSpyObj<AuthService>(
[
'hasUserAsync',
'loginAsync',
'getCookieAsync',
'getCookiesAsync',
'setCookieAsync',
'deleteCookieAsync'
]);
auth = createSpyObj<AuthService>([
'hasUserAsync',
'loginAsync',
'getCookieAsync',
'getCookiesAsync',
'setCookieAsync',
'deleteCookieAsync',
]);

templateStorage = createSpyObj<TemplateStorage>(['readAsync']);

authentication = createSpyObj<Authentication>(['checkAsync']);
authentication.checkAsync.mockResolvedValue(guestPayload);

Expand All @@ -68,13 +66,11 @@ describe('TemplateController', function () {

it('getTemplates: /api/v1/templates', async function () {
const templates: TemplateDescriptor[] = [{ name: 'TestTemplate' }];
templateStorage.readAsync.mockReturnValue(new Promise<TemplateDescriptor[]>(resolve => resolve(templates)));
const response = await request(app)
.get('/api/v1/templates')
.set('Authorization', `Bearer ${getToken()}`);
templateStorage.readAsync.mockResolvedValue(templates);
const response = await request(app).get('/api/v1/templates').set('Authorization', `Bearer ${getToken()}`);

expect(response.statusCode).toBe(200)
expect(response.statusCode).toBe(200);
expect(response.body).toEqual(templates);
expect(templateStorage.readAsync).toHaveBeenCalled();
});
});
});
12 changes: 7 additions & 5 deletions projects/aas-server/src/test/convert.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@ import { describe, it, expect } from '@jest/globals';
describe('convert', () => {
describe('atob', () => {
it('converts ascii to base64', () => {
expect(encodeBase64Url('https://iosb-ina.fraunhofer.de/ids/aas/5174_7001_0122_9237'))
.toEqual('aHR0cHM6Ly9pb3NiLWluYS5mcmF1bmhvZmVyLmRlL2lkcy9hYXMvNTE3NF83MDAxXzAxMjJfOTIzNw');
expect(encodeBase64Url('https://iosb-ina.fraunhofer.de/ids/aas/5174_7001_0122_9237')).toEqual(
'aHR0cHM6Ly9pb3NiLWluYS5mcmF1bmhvZmVyLmRlL2lkcy9hYXMvNTE3NF83MDAxXzAxMjJfOTIzNw',
);
});
});

describe('btoa', () => {
it('converts base64 to ascii', () => {
expect(decodeBase64Url('aHR0cHM6Ly9pb3NiLWluYS5mcmF1bmhvZmVyLmRlL2lkcy9hYXMvNTE3NF83MDAxXzAxMjJfOTIzNw'))
.toEqual('https://iosb-ina.fraunhofer.de/ids/aas/5174_7001_0122_9237');
expect(
decodeBase64Url('aHR0cHM6Ly9pb3NiLWluYS5mcmF1bmhvZmVyLmRlL2lkcy9hYXMvNTE3NF83MDAxXzAxMjJfOTIzNw'),
).toEqual('https://iosb-ina.fraunhofer.de/ids/aas/5174_7001_0122_9237');
});
});
});
});
8 changes: 4 additions & 4 deletions projects/aas-server/src/test/image-processing.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@
*
*****************************************************************************/

import { describe, it, expect } from '@jest/globals';
import fs from 'fs';
import Jimp from 'jimp';
import { ImageProcessing } from '../app/image-processing.js';
import { describe, it, expect } from '@jest/globals';

describe('image processing', function () {
it('resizes an image to 128 x 128 pixels', async function () {
it('resizes an image to 128 x 128 pixels', async () => {
const source = fs.createReadStream('./src/test/assets/thumbnail.jpg');
const stream = await ImageProcessing.resizeAsync(source, 128, 128);
expect(stream).toBeTruthy();
});

it('converts a tiff-image to a png-image', async function() {
it('converts a tiff-image to a png-image', async () => {
const source = fs.createReadStream('./src/test/assets/image.tiff');
const stream = await ImageProcessing.convertAsync(source, Jimp.MIME_PNG);
expect(stream).toBeTruthy();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ describe('HttpSocketItem', function () {
item = new HttpSocketItem({ nodeId: '', valueType: 'xs:integer' }, 'http://localhost:1234');
});

it('should be created', function() {
it('should be created', () => {
expect(item).toBeTruthy();
expect(item.url).toEqual('http://localhost:1234');
expect(item.node).toEqual({ nodeId: '', valueType: 'xs:integer' });
});
});
});
53 changes: 32 additions & 21 deletions projects/aas-server/src/test/live/http/http-subscription.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,37 @@ describe('HttpSubscription', function () {
beforeEach(function () {
logger = createSpyObj<Logger>(['error', 'warning', 'info', 'debug', 'start', 'stop']);
client = createSpyObj<SocketClient>(['has', 'subscribe', 'notify']);
aasxServer = createSpyObj<AasxServer>(
['getShellsAsync', 'commitAsync', 'openFileAsync', 'readValueAsync', 'resolveNodeId']);
aasxServer = createSpyObj<AasxServer>([
'getShellsAsync',
'commitAsync',
'openFileAsync',
'readValueAsync',
'resolveNodeId',
]);

const reference: aas.Reference = {
type: 'ModelReference',
keys: [{
type: 'Submodel',
value: 'http://i40.customer.com/type/1/1/F13E8576F6488342'
},
{
type: 'Property',
value: 'GLN',
}]
keys: [
{
type: 'Submodel',
value: 'http://i40.customer.com/type/1/1/F13E8576F6488342',
},
{
type: 'Property',
value: 'GLN',
},
],
};

const request: LiveRequest = {
endpoint: 'AasxDirectory',
id: 'http://customer.com/aas/9175_7013_7091_9168',
nodes: [{
nodeId: JSON.stringify(reference),
valueType: 'xs:integer'
}],
nodes: [
{
nodeId: JSON.stringify(reference),
valueType: 'xs:integer',
},
],
};

subscription = new HttpSubscription(logger, aasxServer, client, request, env);
Expand All @@ -57,13 +66,15 @@ describe('HttpSubscription', function () {

it('open/close subscription', function (done: DoneFn) {
jest.useFakeTimers();
aasxServer.readValueAsync.mockReturnValue(new Promise<DefaultType>(result => {
expect(true).toBeTruthy();
result(42);
subscription.close();
done();
}));
aasxServer.readValueAsync.mockReturnValue(
new Promise<DefaultType>(result => {
expect(true).toBeTruthy();
result(42);
subscription.close();
done();
}),
);

subscription.open();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,28 @@
*****************************************************************************/

import { beforeEach, describe, expect, it, jest } from '@jest/globals';
import { ClientMonitoredItem } from 'node-opcua';
import { OpcuaSocketItem } from '../../../app/live/opcua/opcua-socket-item.js';
import { Logger } from '../../../app/logging/logger.js';
import { createSpyObj } from '../../utils.js';
import { SocketClient } from '../../../app/live/socket-client.js';
import { ClientMonitoredItem } from 'node-opcua';

describe('OpcuaSocketItem', function () {
describe('OpcuaSocketItem', () => {
let item: OpcuaSocketItem;
let logger: jest.Mocked<Logger>;
let client: jest.Mocked<SocketClient>;

beforeEach(function () {
beforeEach(() => {
logger = createSpyObj<Logger>(['error', 'warning', 'info', 'debug', 'start', 'stop']);
client = createSpyObj<SocketClient>([]);
item = new OpcuaSocketItem(logger, client, { nodeId: '', valueType: 'xs:integer' });
});

it('should be created', function() {
it('should be created', () => {
expect(item).toBeTruthy();
});

it('can subscribe/unsubscribe', function() {
it('can subscribe/unsubscribe', () => {
const monitoredItem = createSpyObj<ClientMonitoredItem>(['on', 'off', 'terminate']);
item.subscribe(monitoredItem);
expect(monitoredItem.on).toHaveBeenCalled();
Expand All @@ -37,4 +37,4 @@ describe('OpcuaSocketItem', function () {
expect(monitoredItem.off).toHaveBeenCalled();
expect(monitoredItem.terminate).toHaveBeenCalled();
});
});
});
4 changes: 2 additions & 2 deletions projects/aas-server/src/test/live/socket-client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ describe('SocketClient', function () {
expect(client.has('test')).toBeTruthy();

client.notify({ type: 'test', data: 42 });
expect(ws.send).toHaveBeenCalled();
expect(ws.send).toHaveBeenCalled();
});
});
});
8 changes: 3 additions & 5 deletions projects/aas-server/src/test/memory-logger.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,17 @@
*****************************************************************************/

import 'reflect-metadata';
import { describe, beforeEach, it, expect, jest } from '@jest/globals';
import { MemoryLogger, MemoryLoggerLevel } from '../app/logging/memory-logger.js';
import { DebugConsole } from '../app/logging/logger.js';
import { createSpyObj } from './utils.js';
import { describe, beforeEach, it, expect, jest } from '@jest/globals';

describe('MemoryLogger', function () {
describe('log level Info', function () {
let logger: MemoryLogger;

beforeEach(function () {
logger = new MemoryLogger(
MemoryLoggerLevel.Info,
createSpyObj<DebugConsole>(['debug']));
logger = new MemoryLogger(MemoryLoggerLevel.Info, createSpyObj<DebugConsole>(['debug']));
});

it('should create', function () {
Expand Down Expand Up @@ -125,4 +123,4 @@ describe('MemoryLogger', function () {
expect(console.debug).toHaveBeenCalled();
});
});
});
});
Loading

0 comments on commit 516029c

Please sign in to comment.