webhook-targets-crud.test.ts 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /**
  2. * Webhook Targets Repository 集成测试
  3. *
  4. * 覆盖范围:
  5. * - create / get / list / update / delete
  6. * - updateTestResult 写回
  7. */
  8. import { describe, expect, test } from "vitest";
  9. import {
  10. createWebhookTarget,
  11. deleteWebhookTarget,
  12. getAllWebhookTargets,
  13. getWebhookTargetById,
  14. updateTestResult,
  15. updateWebhookTarget,
  16. } from "@/repository/webhook-targets";
  17. const run = process.env.DSN ? describe : describe.skip;
  18. run("Webhook Targets Repository(集成测试)", () => {
  19. test("should create, update and delete webhook target", async () => {
  20. const created = await createWebhookTarget({
  21. name: `测试目标_${Date.now()}`,
  22. providerType: "wechat",
  23. webhookUrl: "https://example.com/webhook",
  24. isEnabled: true,
  25. });
  26. try {
  27. expect(created.id).toBeTypeOf("number");
  28. expect(created.name).toContain("测试目标_");
  29. expect(created.providerType).toBe("wechat");
  30. const found = await getWebhookTargetById(created.id);
  31. expect(found).not.toBeNull();
  32. expect(found?.id).toBe(created.id);
  33. const updated = await updateWebhookTarget(created.id, {
  34. name: `${created.name}_updated`,
  35. isEnabled: false,
  36. });
  37. expect(updated.name).toContain("_updated");
  38. expect(updated.isEnabled).toBe(false);
  39. await updateTestResult(created.id, { success: true, latencyMs: 12 });
  40. const afterTest = await getWebhookTargetById(created.id);
  41. expect(afterTest?.lastTestResult?.success).toBe(true);
  42. expect(afterTest?.lastTestResult?.latencyMs).toBe(12);
  43. const list = await getAllWebhookTargets();
  44. expect(list.some((t) => t.id === created.id)).toBe(true);
  45. } finally {
  46. await deleteWebhookTarget(created.id);
  47. }
  48. });
  49. });