1
0

telegram.test.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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({
  33. ids: [-1001234567, 42],
  34. invalid: ['abc'],
  35. });
  36. });
  37. it('returns empty for blank input', () => {
  38. expect(parseAdminIds(' ')).toEqual({ ids: [], invalid: [] });
  39. });
  40. });
  41. describe('validateRunTime', () => {
  42. it('accepts the @daily macro', () => {
  43. expect(validateRunTime('@daily')).toMatchObject({ valid: true, kind: 'macro' });
  44. });
  45. it('accepts an @every duration', () => {
  46. expect(validateRunTime('@every 8h')).toMatchObject({ valid: true, kind: 'every' });
  47. });
  48. it('accepts a 5-field cron', () => {
  49. expect(validateRunTime('0 0 * * *')).toMatchObject({ valid: true, kind: 'cron' });
  50. });
  51. it('accepts a 6-field cron', () => {
  52. expect(validateRunTime('*/30 * * * * *')).toMatchObject({ valid: true, kind: 'cron' });
  53. });
  54. it('rejects an unknown macro', () => {
  55. expect(validateRunTime('@bogus').valid).toBe(false);
  56. });
  57. it('rejects a malformed @every duration', () => {
  58. expect(validateRunTime('@every 8x').valid).toBe(false);
  59. });
  60. });
  61. describe('telegramApiBase', () => {
  62. it('builds the bot API base URL', () => {
  63. expect(telegramApiBase(VALID_TOKEN)).toBe(`https://api.telegram.org/bot${VALID_TOKEN}`);
  64. });
  65. });
  66. describe('renderMessageTemplate', () => {
  67. it('substitutes known variables', () => {
  68. expect(
  69. renderMessageTemplate('Host {{host}} up {{uptime}}', { host: 'srv', uptime: '3d' }),
  70. ).toBe('Host srv up 3d');
  71. });
  72. it('leaves unknown variables literal', () => {
  73. expect(renderMessageTemplate('{{a}} {{b}}', { a: 'x' })).toBe('x {{b}}');
  74. });
  75. });
  76. describe('buildBotConfigSummary', () => {
  77. it('emits the panel settings keys with admin ids joined', () => {
  78. const s = buildBotConfigSummary({
  79. token: VALID_TOKEN,
  80. adminIds: '111, 222',
  81. runTime: '@daily',
  82. });
  83. expect(s.tgBotEnable).toBe(true);
  84. expect(s.tgBotToken).toBe(VALID_TOKEN);
  85. expect(s.tgBotChatId).toBe('111,222');
  86. expect(s.tgRunTime).toBe('@daily');
  87. });
  88. });