import test from 'node:test' import assert from 'node:assert/strict' import { countBy, dateKey, percent, telegramAttachment } from '../src/business.js' import { decryptEmailPassword, encryptEmailPassword } from '../src/email.js' test('Telegram 图片、文档、语音和贴纸映射为可下载文件', () => { assert.deepEqual( telegramAttachment({ '@type': 'messagePhoto', photo: { sizes: [{ photo: { id: 11, size: 2048 } }] }, }), { telegramFileId: 11, attachmentName: 'telegram-photo-11.jpg', attachmentMimeType: 'image/jpeg', attachmentSize: 2048, }, ) assert.equal(telegramAttachment({ '@type': 'messageDocument', document: { file_name: 'quote.pdf', mime_type: 'application/pdf', document: { id: 12, size: 4096 } }, }).attachmentMimeType, 'application/pdf') assert.equal(telegramAttachment({ '@type': 'messageVoiceNote', voice_note: { duration: 8, voice: { id: 13, expected_size: 1024 } }, }).duration, 8) assert.equal(telegramAttachment({ '@type': 'messageSticker', sticker: { emoji: '👍', format: { '@type': 'stickerFormatWebp' }, sticker: { id: 14, size: 512 } }, }).attachmentMimeType, 'image/webp') }) test('业务指标工具处理百分比、分组和无效日期', () => { assert.equal(percent(3, 8), 37.5) assert.equal(percent(1, 0), 0) assert.equal(dateKey('invalid'), '') assert.equal(dateKey('2026-07-27T12:30:00Z'), '2026-07-27') assert.deepEqual( Object.fromEntries(countBy([{ stage: 'new' }, { stage: 'new' }, { stage: 'won' }], 'stage')), { new: 2, won: 1 }, ) }) test('Email 渠道凭据使用认证加密且可正确解密', () => { process.env.EMAIL_CREDENTIAL_KEY = 'lingxi-email-test-key-with-more-than-32-bytes' const encrypted = encryptEmailPassword('app-password-123') assert.notEqual(encrypted, 'app-password-123') assert.match(encrypted, /^v1:/) assert.equal(decryptEmailPassword(encrypted), 'app-password-123') const tampered = encrypted.split(':') tampered[2] = `${tampered[2][0] === 'A' ? 'B' : 'A'}${tampered[2].slice(1)}` assert.throws(() => decryptEmailPassword(tampered.join(':'))) })