import { expect, tap } from '@git.zone/tstest/tapbundle'; import { DcRouter } from '../ts/index.js'; import { TypedRequest } from '@api.global/typedrequest'; import * as interfaces from '../ts_interfaces/index.js'; let testDcRouter: DcRouter; let identity: interfaces.data.IIdentity; tap.test('should start DCRouter with OpsServer', async () => { testDcRouter = new DcRouter({ // Minimal config for testing opsServerPort: 3102, dbConfig: { enabled: false }, }); await testDcRouter.start(); expect(testDcRouter.opsServer).toBeInstanceOf(Object); }); tap.test('should login with admin credentials and receive JWT', async () => { const loginRequest = new TypedRequest( 'http://localhost:3102/typedrequest', 'adminLoginWithUsernameAndPassword' ); const response = await loginRequest.fire({ username: 'admin', password: 'admin' }); expect(response).toHaveProperty('identity'); const responseIdentity = response.identity; if (!responseIdentity) { throw new Error('Expected admin login response to include identity'); } expect(responseIdentity).toHaveProperty('jwt'); expect(responseIdentity).toHaveProperty('userId'); expect(responseIdentity).toHaveProperty('name'); expect(responseIdentity).toHaveProperty('expiresAt'); expect(responseIdentity).toHaveProperty('role'); expect(responseIdentity.role).toEqual('admin'); identity = responseIdentity; console.log('JWT:', identity.jwt); }); tap.test('should verify valid JWT identity', async () => { const verifyRequest = new TypedRequest( 'http://localhost:3102/typedrequest', 'verifyIdentity' ); const response = await verifyRequest.fire({ identity }); expect(response).toHaveProperty('valid'); expect(response.valid).toBeTrue(); expect(response).toHaveProperty('identity'); const responseIdentity = response.identity; if (!responseIdentity) { throw new Error('Expected verify response to include identity'); } expect(responseIdentity.userId).toEqual(identity.userId); }); tap.test('should reject invalid JWT', async () => { const verifyRequest = new TypedRequest( 'http://localhost:3102/typedrequest', 'verifyIdentity' ); const response = await verifyRequest.fire({ identity: { ...identity, jwt: 'invalid.jwt.token' } }); expect(response).toHaveProperty('valid'); expect(response.valid).toBeFalse(); }); tap.test('should verify JWT matches identity data', async () => { const verifyRequest = new TypedRequest( 'http://localhost:3102/typedrequest', 'verifyIdentity' ); // The response should contain the same identity data as the JWT const response = await verifyRequest.fire({ identity }); expect(response).toHaveProperty('valid'); expect(response.valid).toBeTrue(); const responseIdentity = response.identity; if (!responseIdentity) { throw new Error('Expected verify response to include identity'); } expect(responseIdentity.expiresAt).toEqual(identity.expiresAt); expect(responseIdentity.userId).toEqual(identity.userId); }); tap.test('should handle logout', async () => { const logoutRequest = new TypedRequest( 'http://localhost:3102/typedrequest', 'adminLogout' ); const response = await logoutRequest.fire({ identity }); expect(response).toHaveProperty('success'); expect(response.success).toBeTrue(); }); tap.test('should reject wrong credentials', async () => { const loginRequest = new TypedRequest( 'http://localhost:3102/typedrequest', 'adminLoginWithUsernameAndPassword' ); let errorOccurred = false; try { await loginRequest.fire({ username: 'admin', password: 'wrongpassword' }); } catch (error) { errorOccurred = true; // TypedResponseError is thrown expect(error).toBeTruthy(); } expect(errorOccurred).toBeTrue(); }); tap.test('should stop DCRouter', async () => { await testDcRouter.stop(); }); export default tap.start();