import { describe, test, expect, beforeEach, afterEach } from 'bun:test';
import { config, getPrivateKeyAsHex } from '../../core/config.js';
describe('Config Module', () => {
// Store original environment
const originalEnv = { ...process.env };
// Reset environment before each test
beforeEach(() => {
// Clear any environment variables that might affect tests
delete process.env.PRIVATE_KEY;
});
// Restore original environment after each test
afterEach(() => {
process.env = { ...originalEnv };
});
describe('formatPrivateKey', () => {
test('should add 0x prefix if missing', () => {
// Set environment variable without 0x prefix
process.env.PRIVATE_KEY = 'abcdef1234567890';
// Re-import the module to trigger environment variable processing
// Note: In a real test, you'd use a mechanism to reload the module
// For this example, we'll check the behavior indirectly
// Set the private key directly to simulate reloading
// @ts-ignore - Accessing private implementation
config.privateKey = process.env.PRIVATE_KEY.startsWith('0x')
? process.env.PRIVATE_KEY
: `0x${process.env.PRIVATE_KEY}`;
// Check that the private key has 0x prefix
expect(config.privateKey).toBe('0xabcdef1234567890');
});
test('should not modify key if 0x prefix exists', () => {
// Set environment variable with 0x prefix
process.env.PRIVATE_KEY = '0xabcdef1234567890';
// Set the private key directly to simulate reloading
// @ts-ignore - Accessing private implementation
config.privateKey = process.env.PRIVATE_KEY.startsWith('0x')
? process.env.PRIVATE_KEY
: `0x${process.env.PRIVATE_KEY}`;
// Check that the private key still has just one 0x prefix
expect(config.privateKey).toBe('0xabcdef1234567890');
});
});
describe('getPrivateKeyAsHex', () => {
test('should return undefined if private key is not set', () => {
// Ensure private key is not set
// @ts-ignore - Accessing private implementation
config.privateKey = undefined;
// Check that getPrivateKeyAsHex returns undefined
expect(getPrivateKeyAsHex()).toBeUndefined();
});
test('should return private key as Hex if set', () => {
// Set private key
// @ts-ignore - Accessing private implementation
config.privateKey = '0xabcdef1234567890';
// Check that getPrivateKeyAsHex returns the key
expect(getPrivateKeyAsHex()).toBe('0xabcdef1234567890');
});
});
});