dingtalk.test.ts 1.9 KB

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