telegram.test.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { describe, it, expect } from 'vitest';
  2. import {
  3. validateBotToken,
  4. parseAdminIds,
  5. validateRunTime,
  6. telegramApiBase,
  7. renderMessageTemplate,
  8. buildBotConfigSummary,
  9. } from './telegram';
  10. const VALID_TOKEN = '123456789:AAH-abcdefghijklmnopqrstuvwxyz0123456';
  11. describe('validateBotToken', () => {
  12. it('accepts a BotFather-format token and extracts the bot id', () => {
  13. const r = validateBotToken(VALID_TOKEN);
  14. expect(r.valid).toBe(true);
  15. expect(r.botId).toBe('123456789');
  16. });
  17. it('rejects a token with no colon', () => {
  18. expect(validateBotToken('123456789AAH').valid).toBe(false);
  19. });
  20. it('rejects a too-short secret', () => {
  21. expect(validateBotToken('123:short').valid).toBe(false);
  22. });
  23. it('rejects an empty token', () => {
  24. expect(validateBotToken(' ').valid).toBe(false);
  25. });
  26. });
  27. describe('parseAdminIds', () => {
  28. it('splits a comma list into integer ids', () => {
  29. expect(parseAdminIds('111, 222')).toEqual({ ids: [111, 222], invalid: [] });
  30. });
  31. it('accepts negative group ids and captures invalid entries', () => {
  32. expect(parseAdminIds('-1001234567, abc, 42')).toEqual({ ids: [-1001234567, 42], invalid: ['abc'] });
  33. });
  34. it('returns empty for blank input', () => {
  35. expect(parseAdminIds(' ')).toEqual({ ids: [], invalid: [] });
  36. });
  37. });
  38. describe('validateRunTime', () => {
  39. it('accepts the @daily macro', () => {
  40. expect(validateRunTime('@daily')).toMatchObject({ valid: true, kind: 'macro' });
  41. });
  42. it('accepts an @every duration', () => {
  43. expect(validateRunTime('@every 8h')).toMatchObject({ valid: true, kind: 'every' });
  44. });
  45. it('accepts a 5-field cron', () => {
  46. expect(validateRunTime('0 0 * * *')).toMatchObject({ valid: true, kind: 'cron' });
  47. });
  48. it('accepts a 6-field cron', () => {
  49. expect(validateRunTime('*/30 * * * * *')).toMatchObject({ valid: true, kind: 'cron' });
  50. });
  51. it('rejects an unknown macro', () => {
  52. expect(validateRunTime('@bogus').valid).toBe(false);
  53. });
  54. it('rejects a malformed @every duration', () => {
  55. expect(validateRunTime('@every 8x').valid).toBe(false);
  56. });
  57. });
  58. describe('telegramApiBase', () => {
  59. it('builds the bot API base URL', () => {
  60. expect(telegramApiBase(VALID_TOKEN)).toBe(`https://api.telegram.org/bot${VALID_TOKEN}`);
  61. });
  62. });
  63. describe('renderMessageTemplate', () => {
  64. it('substitutes known variables', () => {
  65. expect(renderMessageTemplate('Host {{host}} up {{uptime}}', { host: 'srv', uptime: '3d' })).toBe(
  66. 'Host srv up 3d',
  67. );
  68. });
  69. it('leaves unknown variables literal', () => {
  70. expect(renderMessageTemplate('{{a}} {{b}}', { a: 'x' })).toBe('x {{b}}');
  71. });
  72. });
  73. describe('buildBotConfigSummary', () => {
  74. it('emits the panel settings keys with admin ids joined', () => {
  75. const s = buildBotConfigSummary({ token: VALID_TOKEN, adminIds: '111, 222', runTime: '@daily' });
  76. expect(s.tgBotEnable).toBe(true);
  77. expect(s.tgBotToken).toBe(VALID_TOKEN);
  78. expect(s.tgBotChatId).toBe('111,222');
  79. expect(s.tgRunTime).toBe('@daily');
  80. });
  81. });