2
0

telegram.test.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import { describe, expect, it } from "vitest";
  2. import { TelegramRenderer } from "@/lib/webhook/renderers/telegram";
  3. import type { StructuredMessage } from "@/lib/webhook/types";
  4. describe("TelegramRenderer", () => {
  5. it("should render HTML payload with chat_id and parse_mode", () => {
  6. const renderer = new TelegramRenderer("123");
  7. const message: StructuredMessage = {
  8. header: { title: '测试 <标题>&"', level: "info" },
  9. sections: [
  10. { content: [{ type: "text", value: 'Hello & <world> "' }] },
  11. { content: [{ type: "quote", value: "引用 <tag>" }] },
  12. {
  13. title: "字段",
  14. content: [
  15. {
  16. type: "fields",
  17. items: [{ label: "状态", value: "<OK>&" }],
  18. },
  19. ],
  20. },
  21. {
  22. content: [
  23. {
  24. type: "list",
  25. style: "bullet",
  26. items: [{ primary: "用户A", secondary: '消费 <10>& "' }],
  27. },
  28. ],
  29. },
  30. ],
  31. timestamp: new Date("2025-01-02T12:00:00Z"),
  32. };
  33. const result = renderer.render(message);
  34. const body = JSON.parse(result.body) as any;
  35. expect(body.chat_id).toBe("123");
  36. expect(body.parse_mode).toBe("HTML");
  37. expect(body.disable_web_page_preview).toBe(true);
  38. expect(body.text).toContain("<b>测试 &lt;标题&gt;&amp;&quot;</b>");
  39. expect(body.text).toContain("Hello &amp; &lt;world&gt; &quot;");
  40. expect(body.text).toContain("&gt; 引用 &lt;tag&gt;");
  41. expect(body.text).toContain("<b>字段</b>");
  42. expect(body.text).toContain("<b>状态</b>: &lt;OK&gt;&amp;");
  43. expect(body.text).toContain("- <b>用户A</b>");
  44. expect(body.text).toContain("消费 &lt;10&gt;&amp; &quot;");
  45. expect(body.text).toContain("2025");
  46. });
  47. });