import test from 'node:test' import assert from 'node:assert/strict' import { countBy, dateKey, percent, telegramAttachment } from '../src/business.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 }, ) })