marketingTouchService.test.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import { beforeEach, describe, expect, it, vi } from "vitest";
  2. import { createMarketingTouchService } from "#src/marketing-touch/marketingTouchService.js";
  3. function fixture(createService = createMarketingTouchService) {
  4. let token: string | null = "first-token";
  5. const request = vi.fn(
  6. async () =>
  7. new Response(
  8. JSON.stringify({ code: 0, data: { server_time: 1, language: "en-US", deliveries: [] } }),
  9. ),
  10. );
  11. const service = createService({
  12. apiClient: { request },
  13. getToken: async () => token,
  14. getDeviceMid: () => "550e8400-e29b-41d4-a716-446655440000",
  15. baseUrl: "https://api.example.com",
  16. appVersion: "3.11.2",
  17. });
  18. return {
  19. service,
  20. request,
  21. setToken: (next: string | null) => {
  22. token = next;
  23. },
  24. };
  25. }
  26. describe("MarketingTouchService", () => {
  27. it("supplies common headers, original campaign ID and no unsupported event fields", async () => {
  28. const f = fixture();
  29. const snapshot = await f.service.query({ locale: "en-US" });
  30. await f.service.report({
  31. locale: "en-US",
  32. scope: snapshot.scope,
  33. campaignId: "193001",
  34. actionType: "confirm",
  35. });
  36. const [url, init] = f.request.mock.calls[1] as unknown as [string, RequestInit];
  37. expect(url).toBe("https://api.example.com/api/v1/marketing/touch/action");
  38. expect(new Headers(init.headers).get("X-Device-Mid")).toBe(
  39. "550e8400-e29b-41d4-a716-446655440000",
  40. );
  41. expect(new Headers(init.headers).get("X-Client-Language")).toBe("en-US");
  42. expect(new Headers(init.headers).get("X-ZCode-App-Version")).toBe("3.11.2");
  43. expect(JSON.parse(String(init.body))).toEqual({
  44. campaign_id: "193001",
  45. action_type: "confirm",
  46. });
  47. expect(JSON.stringify(snapshot)).not.toContain("first-token");
  48. });
  49. it("refuses to report an old operation using a new account", async () => {
  50. const f = fixture();
  51. const snapshot = await f.service.query({ locale: "en-US" });
  52. f.setToken("second-token");
  53. await expect(
  54. f.service.report({
  55. locale: "en-US",
  56. scope: snapshot.scope,
  57. campaignId: "193001",
  58. actionType: "cancel",
  59. }),
  60. ).rejects.toThrow("marketing_identity_changed");
  61. expect(f.request).toHaveBeenCalledTimes(1);
  62. });
  63. it("allows anonymous delivery and does not retry uncertain reports", async () => {
  64. const f = fixture();
  65. f.setToken(null);
  66. const snapshot = await f.service.query({ locale: "en-US" });
  67. f.request.mockRejectedValueOnce(new Error("network"));
  68. await expect(
  69. f.service.report({
  70. locale: "en-US",
  71. scope: snapshot.scope,
  72. campaignId: "193001",
  73. actionType: "cancel",
  74. }),
  75. ).rejects.toThrow("network");
  76. expect(f.request).toHaveBeenCalledTimes(2);
  77. const [, init] = f.request.mock.calls[0] as unknown as [string, RequestInit];
  78. expect(new Headers(init.headers).has("Authorization")).toBe(false);
  79. });
  80. });
  81. describe("MarketingTouchService boot request sequence", () => {
  82. let createService: typeof createMarketingTouchService;
  83. beforeEach(async () => {
  84. vi.resetModules();
  85. ({ createMarketingTouchService: createService } =
  86. await import("#src/marketing-touch/marketingTouchService.js"));
  87. });
  88. function sequences(f: ReturnType<typeof fixture>) {
  89. return f.request.mock.calls.map((call) =>
  90. new URL((call as unknown as [string])[0]).searchParams.get("seq"),
  91. );
  92. }
  93. it("starts at zero, continues across accounts, locales and service instances", async () => {
  94. const f = fixture(createService);
  95. await f.service.query({ locale: "en-US" });
  96. f.setToken(null);
  97. await f.service.query({ locale: "zh-CN" });
  98. f.setToken("second-token");
  99. await f.service.query({ locale: "en-US" });
  100. expect(sequences(f)).toEqual(["0", "1", "2"]);
  101. const next = fixture(createService);
  102. await next.service.query({ locale: "en-US" });
  103. expect(sequences(next)).toEqual(["3"]);
  104. });
  105. it("counts failed and concurrent requests without reusing a sequence", async () => {
  106. const f = fixture(createService);
  107. f.request.mockRejectedValueOnce(new Error("network"));
  108. await expect(f.service.query({ locale: "en-US" })).rejects.toThrow("network");
  109. f.request.mockResolvedValueOnce(new Response("invalid JSON"));
  110. await expect(f.service.query({ locale: "en-US" })).rejects.toThrow();
  111. await Promise.all([f.service.query({ locale: "en-US" }), f.service.query({ locale: "en-US" })]);
  112. expect(sequences(f)).toEqual(["0", "1", "2", "3"]);
  113. });
  114. it("does not count validation failures or action reports", async () => {
  115. const f = fixture(createService);
  116. await expect(f.service.query({ locale: "invalid" as "en-US" })).rejects.toThrow();
  117. expect(f.request).not.toHaveBeenCalled();
  118. const snapshot = await f.service.query({ locale: "en-US" });
  119. await f.service.report({
  120. locale: "en-US",
  121. scope: snapshot.scope,
  122. campaignId: "193001",
  123. actionType: "cancel",
  124. });
  125. await f.service.query({ locale: "en-US" });
  126. expect(sequences(f)).toEqual(["0", null, "1"]);
  127. });
  128. it("starts at zero in a freshly initialized process module", async () => {
  129. const f = fixture(createService);
  130. await f.service.query({ locale: "en-US" });
  131. expect(sequences(f)).toEqual(["0"]);
  132. });
  133. });