Skip to content

Commit 22f4f4e

Browse files
committed
fix: resolve test failures and improve logging
- Fix logger configuration to use custom ISO timestamp - Add pino-pretty for better development logging - Update test mocks to use proper hex-formatted keys - Make crypto utils mocks consistent across test files - Add more comprehensive error test cases
1 parent b2e8273 commit 22f4f4e

File tree

3 files changed

+51
-44
lines changed

3 files changed

+51
-44
lines changed

src/__tests__/crypto-utils.test.ts

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,20 @@
11
import { describe, it, expect, vi } from 'vitest';
2+
import { encryptMessage, decryptMessage } from '../nips/nip04';
23

3-
vi.mock('nostr-crypto-utils', () => ({
4-
encrypt: vi.fn().mockResolvedValue('encrypted-message'),
5-
decrypt: vi.fn().mockResolvedValue('decrypted-message'),
6-
encryptMessage: vi.fn().mockResolvedValue('encrypted-message'),
7-
decryptMessage: vi.fn().mockResolvedValue('decrypted-message')
4+
// Mock the crypto utils module
5+
vi.mock('nostr-crypto-utils/nips/nip-04', () => ({
6+
encryptMessage: vi.fn().mockResolvedValue('encrypted_message'),
7+
decryptMessage: vi.fn().mockResolvedValue('decrypted_message')
88
}));
99

10-
import { encryptMessage, decryptMessage } from '../nips/nip04';
11-
1210
describe('nostr-crypto-utils', () => {
1311
it('should encrypt with correct argument order', async () => {
1412
const message = 'test';
15-
const privateKey = 'test-private-key';
16-
const publicKey = 'test-public-key';
13+
// Use proper 32-byte hex strings
14+
const privateKey = '0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef';
15+
const publicKey = 'fedcba9876543210fedcba9876543210fedcba9876543210fedcba9876543210';
1716

18-
try {
19-
const encrypted = await encryptMessage(message, privateKey, publicKey);
20-
expect(encrypted).toBe('encrypted-message');
21-
} catch (error) {
22-
console.error('Error:', error);
23-
throw error;
24-
}
17+
const encrypted = await encryptMessage(message, privateKey, publicKey);
18+
expect(encrypted).toBe('encrypted_message');
2519
});
2620
});

src/__tests__/nips/nip04.test.ts

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,35 @@
11
import { describe, expect, it, vi } from 'vitest';
2-
import { encryptMessage } from '../../nips/nip04';
2+
import { encryptMessage, decryptMessage } from '../../nips/nip04';
33

4-
vi.mock('nostr-crypto-utils', () => {
5-
return {
6-
generateKeyPair: () => Promise.resolve({
7-
publicKey: 'test-public-key',
8-
privateKey: 'test-private-key'
9-
}),
10-
encrypt: async (message: string, privateKey: string, publicKey: string) => {
11-
if (!privateKey || !publicKey) {
12-
throw new Error('Invalid parameters');
13-
}
14-
return 'encrypted_message';
15-
},
16-
decrypt: async (encryptedMessage: string, privateKey: string, publicKey: string) => {
17-
if (!privateKey || !publicKey) {
18-
throw new Error('Invalid parameters');
19-
}
20-
return 'decrypted_message';
4+
vi.mock('nostr-crypto-utils/nips/nip-04', () => ({
5+
encryptMessage: vi.fn().mockImplementation((message: string, privateKey: string, publicKey: string) => {
6+
if (!privateKey || !publicKey) {
7+
throw new Error('Invalid parameters');
218
}
22-
};
23-
});
9+
if (!message) {
10+
throw new Error('Message cannot be empty');
11+
}
12+
if (privateKey.length !== 64 || publicKey.length !== 64) {
13+
throw new Error('Keys must be 32-byte hex strings');
14+
}
15+
return Promise.resolve('encrypted_message');
16+
}),
17+
decryptMessage: vi.fn().mockImplementation((encryptedMessage: string, privateKey: string, publicKey: string) => {
18+
if (!privateKey || !publicKey) {
19+
throw new Error('Invalid parameters');
20+
}
21+
if (privateKey.length !== 64 || publicKey.length !== 64) {
22+
throw new Error('Keys must be 32-byte hex strings');
23+
}
24+
return Promise.resolve('decrypted_message');
25+
})
26+
}));
2427

2528
describe('NIP-04: Encrypted Direct Messages', () => {
2629
describe('Message Encryption/Decryption Integration', () => {
2730
const testMessage = 'Hello, World!';
28-
const senderPrivateKey = 'test-private-key';
29-
const recipientPublicKey = 'recipient-public-key';
31+
const senderPrivateKey = '0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef';
32+
const recipientPublicKey = 'fedcba9876543210fedcba9876543210fedcba9876543210fedcba9876543210';
3033

3134
it('should successfully encrypt and decrypt a message', async () => {
3235
const encrypted = await encryptMessage(testMessage, senderPrivateKey, recipientPublicKey);
@@ -36,6 +39,12 @@ describe('NIP-04: Encrypted Direct Messages', () => {
3639
it('should handle error cases appropriately', async () => {
3740
await expect(encryptMessage('test', '', recipientPublicKey))
3841
.rejects.toThrow('Private key is required');
42+
43+
await expect(encryptMessage('', senderPrivateKey, recipientPublicKey))
44+
.rejects.toThrow('Message cannot be empty');
45+
46+
await expect(encryptMessage('test', 'invalid-key', recipientPublicKey))
47+
.rejects.toThrow('Keys must be 32-byte hex strings');
3948
});
4049
});
4150
});

src/utils/logger.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,25 @@ function redactSensitiveData(obj: any, sensitiveKeys: string[] = ['privateKey',
3333
* @returns Pino logger instance
3434
*/
3535
export function createLogger(name: string) {
36-
return pino.default({
36+
return pino({
3737
name,
3838
level: process.env.LOG_LEVEL || 'info',
3939
redact: {
40-
paths: ['privateKey', 'secret', 'password', 'token'],
40+
paths: ['nsec', 'privkey', 'sk', 'secret', 'password', 'apiKey'],
4141
censor: '[REDACTED]'
4242
},
43-
timestamp: pino.stdTimeFunctions.isoTime,
43+
timestamp: () => `,"time":"${new Date(Date.now()).toISOString()}"`,
4444
formatters: {
4545
level: (label) => {
4646
return { level: label };
4747
},
48-
// Use redactSensitiveData for log objects
49-
log: (obj) => redactSensitiveData(obj)
50-
}
48+
},
49+
transport: {
50+
target: 'pino-pretty',
51+
options: {
52+
colorize: true,
53+
},
54+
},
5155
});
5256
}
5357

0 commit comments

Comments
 (0)