Files
remoteingress/test/test.ts

62 lines
2.0 KiB
TypeScript
Raw Permalink Normal View History

import { expect, tap } from '@push.rocks/tapbundle';
import * as remoteingress from '../ts/index.js';
2024-03-24 14:44:44 +01:00
tap.test('should export RemoteIngressHub', async () => {
expect(remoteingress.RemoteIngressHub).toBeTypeOf('function');
});
2024-03-24 14:44:44 +01:00
tap.test('should export RemoteIngressEdge', async () => {
expect(remoteingress.RemoteIngressEdge).toBeTypeOf('function');
});
tap.test('should export encodeConnectionToken and decodeConnectionToken', async () => {
expect(remoteingress.encodeConnectionToken).toBeTypeOf('function');
expect(remoteingress.decodeConnectionToken).toBeTypeOf('function');
});
tap.test('should roundtrip encode → decode a connection token', async () => {
const data: remoteingress.IConnectionTokenData = {
hubHost: 'hub.example.com',
hubPort: 8443,
edgeId: 'edge-001',
secret: 'super-secret-key',
};
const token = remoteingress.encodeConnectionToken(data);
const decoded = remoteingress.decodeConnectionToken(token);
expect(decoded.hubHost).toEqual(data.hubHost);
expect(decoded.hubPort).toEqual(data.hubPort);
expect(decoded.edgeId).toEqual(data.edgeId);
expect(decoded.secret).toEqual(data.secret);
});
tap.test('should throw on malformed token', async () => {
let error: Error | undefined;
try {
remoteingress.decodeConnectionToken('not-valid-json!!!');
} catch (e) {
error = e as Error;
}
expect(error).toBeInstanceOf(Error);
expect(error!.message).toInclude('Invalid connection token');
});
tap.test('should throw on token with missing fields', async () => {
// Encode a partial object (missing 'p' and 's')
const partial = Buffer.from(JSON.stringify({ h: 'host', e: 'edge' }), 'utf-8')
.toString('base64')
.replace(/\+/g, '-')
.replace(/\//g, '_')
.replace(/=+$/, '');
let error: Error | undefined;
try {
remoteingress.decodeConnectionToken(partial);
} catch (e) {
error = e as Error;
}
expect(error).toBeInstanceOf(Error);
expect(error!.message).toInclude('missing required fields');
});
export default tap.start();