rwadurian/backend/services/mpc-service/tests/setup.ts

123 lines
3.8 KiB
TypeScript

/**
* Jest Test Setup
*
* Global test configuration and utilities
*/
// Make this file a module
export {};
// Set test environment variables
process.env.NODE_ENV = 'test';
process.env.APP_PORT = '3006';
process.env.DATABASE_URL = 'mysql://test:test@localhost:3306/test_db';
process.env.REDIS_HOST = 'localhost';
process.env.REDIS_PORT = '6379';
process.env.REDIS_DB = '15';
process.env.JWT_SECRET = 'test-jwt-secret-key-for-testing-only';
process.env.SHARE_MASTER_KEY = '0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef';
process.env.MPC_PARTY_ID = 'party-test-1';
process.env.MPC_COORDINATOR_URL = 'http://localhost:50051';
process.env.MPC_MESSAGE_ROUTER_WS_URL = 'ws://localhost:50052';
process.env.KAFKA_BROKERS = 'localhost:9092';
process.env.KAFKA_ENABLED = 'false';
// Increase timeout for async operations
jest.setTimeout(30000);
// Global beforeAll hook
beforeAll(async () => {
// Any global setup
});
// Global afterAll hook
afterAll(async () => {
// Any global cleanup
});
// Console error suppression for expected errors
const originalError = console.error;
console.error = (...args: any[]) => {
// Suppress expected validation errors in tests
if (
args[0]?.includes?.('Validation failed') ||
args[0]?.includes?.('Expected test error')
) {
return;
}
originalError.call(console, ...args);
};
// Add custom matchers
expect.extend({
toBeValidUUID(received: string) {
const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
const pass = uuidRegex.test(received);
return {
message: () =>
pass
? `expected ${received} not to be a valid UUID`
: `expected ${received} to be a valid UUID`,
pass,
};
},
toBeValidHex(received: string, expectedLength?: number) {
const hexRegex = /^[0-9a-f]+$/i;
const isHex = hexRegex.test(received);
const lengthMatch = expectedLength === undefined || received.length === expectedLength;
const pass = isHex && lengthMatch;
return {
message: () =>
pass
? `expected ${received} not to be valid hex${expectedLength ? ` of length ${expectedLength}` : ''}`
: `expected ${received} to be valid hex${expectedLength ? ` of length ${expectedLength}` : ''}`,
pass,
};
},
toBeValidPublicKey(received: string) {
// Compressed public key is 33 bytes = 66 hex chars
// Uncompressed public key is 65 bytes = 130 hex chars
const hexRegex = /^[0-9a-f]+$/i;
const isHex = hexRegex.test(received);
const isValidLength = received.length === 66 || received.length === 130;
const pass = isHex && isValidLength;
return {
message: () =>
pass
? `expected ${received} not to be a valid public key`
: `expected ${received} to be a valid public key (66 or 130 hex chars)`,
pass,
};
},
toBeValidSignature(received: string) {
// ECDSA signature components r, s are 32 bytes each = 64 bytes total = 128 hex chars
// With recovery id (v) it's 65 bytes = 130 hex chars
const hexRegex = /^[0-9a-f]+$/i;
const isHex = hexRegex.test(received);
const isValidLength = received.length === 128 || received.length === 130;
const pass = isHex && isValidLength;
return {
message: () =>
pass
? `expected ${received} not to be a valid signature`
: `expected ${received} to be a valid signature (128 or 130 hex chars)`,
pass,
};
},
});
// Type declarations for custom matchers
declare global {
namespace jest {
interface Matchers<R> {
toBeValidUUID(): R;
toBeValidHex(expectedLength?: number): R;
toBeValidPublicKey(): R;
toBeValidSignature(): R;
}
}
}