76 lines
2.7 KiB
TypeScript
76 lines
2.7 KiB
TypeScript
import { describe, expect, it } from 'vitest';
|
|
import { maskContactPhone } from './phone';
|
|
import {
|
|
clampWecomPluginPageSize,
|
|
isWecomPluginEnabled,
|
|
parseWecomPluginMetricsKind,
|
|
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('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('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());
|
|
});
|
|
});
|