import { describe, expect, it } from 'vitest'; import { maskContactPhone } from './phone'; import { clampWecomPluginPageSize, isWecomPluginEnabled, matchWecomPluginByApiKey, parseWecomPluginDateRange, parseWecomPluginMetricsKind, toWecomPluginMetricsView, toWecomPluginUserView, verifyWecomPluginApiKey, wecomPluginMetricsPeriod, } from './wecom-plugin'; describe('verifyWecomPluginApiKey', () => { it('accepts an exact match', () => { expect(verifyWecomPluginApiKey('secret-token', 'secret-token')).toBe(true); }); it('rejects wrong, empty, or missing keys', () => { expect(verifyWecomPluginApiKey('secret-token', 'other-token')).toBe(false); expect(verifyWecomPluginApiKey('secret-token', 'secret-toke')).toBe(false); expect(verifyWecomPluginApiKey('', 'secret-token')).toBe(false); expect(verifyWecomPluginApiKey('secret-token', '')).toBe(false); expect(verifyWecomPluginApiKey('secret-token', null)).toBe(false); }); }); describe('matchWecomPluginByApiKey', () => { it('returns the matching enabled instance', () => { const rows = [ { id: '1', apiKey: 'alpha-key-aaaa' }, { id: '2', apiKey: 'beta-key-bbbbb' }, ]; expect(matchWecomPluginByApiKey('beta-key-bbbbb', rows)?.id).toBe('2'); expect(matchWecomPluginByApiKey('missing', rows)).toBeNull(); }); }); describe('isWecomPluginEnabled', () => { it('requires both the switch and a non-empty key', () => { expect(isWecomPluginEnabled({ WECOM_PLUGIN_ENABLED: 'true', WECOM_PLUGIN_API_KEY: 'k' })).toBe( true, ); expect(isWecomPluginEnabled({ WECOM_PLUGIN_ENABLED: 'true', WECOM_PLUGIN_API_KEY: '' })).toBe( false, ); expect(isWecomPluginEnabled({ WECOM_PLUGIN_ENABLED: 'false', WECOM_PLUGIN_API_KEY: 'k' })).toBe( false, ); }); }); describe('clampWecomPluginPageSize / parseWecomPluginMetricsKind', () => { it('defaults and caps pageSize at 10', () => { expect(clampWecomPluginPageSize(undefined)).toBe(5); expect(clampWecomPluginPageSize('3')).toBe(3); expect(clampWecomPluginPageSize(99)).toBe(10); expect(clampWecomPluginPageSize(0)).toBe(5); }); it('parses metrics kind, defaulting empty to today', () => { expect(parseWecomPluginMetricsKind(undefined)).toBe('today'); expect(parseWecomPluginMetricsKind('weekly')).toBe('weekly'); expect(parseWecomPluginMetricsKind('nope')).toBeNull(); }); }); describe('parseWecomPluginDateRange / toWecomPluginMetricsView', () => { it('parses YYYY-MM-DD range with inclusive end day', () => { const range = parseWecomPluginDateRange('2026-09-01', '2026-09-03'); expect(range?.gte?.toISOString()).toBe(new Date('2026-09-01T00:00:00+08:00').toISOString()); expect(range?.lt?.toISOString()).toBe(new Date('2026-09-04T00:00:00+08:00').toISOString()); }); it('exposes newStores alias on metrics stats', () => { const view = toWecomPluginMetricsView({ usersTotal: 1, usersIncrement: 0, partnersTotal: 1, partnersIncrement: 0, storesTotal: 10, storesIncrement: 2, ordersTotal: 0, ordersIncrement: 0, orderAmountTotal: 0, orderAmountIncrement: 0, redeemsTotal: 0, redeemsIncrement: 0, redeemAmountTotal: 0, redeemAmountIncrement: 0, }); expect(view.newStores).toBe(2); expect(view.storesIncrement).toBe(2); }); }); describe('toWecomPluginUserView', () => { it('masks phone and fills empty nickname', () => { expect(toWecomPluginUserView({ userNo: 'DK1', nickname: null, phone: '13800138000' })).toEqual({ userNo: 'DK1', nickname: '—', phone: '138****8000', }); expect(maskContactPhone('13800138000')).toBe('138****8000'); }); }); describe('wecomPluginMetricsPeriod', () => { it('today starts at Shanghai midnight and ends at now', () => { const now = new Date('2026-09-03T21:15:00+08:00'); const p = wecomPluginMetricsPeriod('today', now); expect(p.kind).toBe('today'); expect(p.periodKey).toBe('2026-09-03'); expect(p.start.toISOString()).toBe(new Date('2026-09-03T00:00:00+08:00').toISOString()); expect(p.endExclusive.getTime()).toBe(now.getTime()); }); });