marketingResources.test.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // @vitest-environment jsdom
  2. import { afterEach, describe, expect, it, vi } from "vitest";
  3. import { prepareMarketingHero } from "@/components/marketing-touch/marketingResources.js";
  4. import type { ICloudContentService } from "@zcode/services";
  5. afterEach(() => vi.unstubAllGlobals());
  6. describe("marketing resource preparation", () => {
  7. it("uses the verified image fallback when video download fails", async () => {
  8. vi.stubGlobal(
  9. "Image",
  10. class {
  11. onload?: () => void;
  12. set src(_value: string) {
  13. queueMicrotask(() => this.onload?.());
  14. }
  15. },
  16. );
  17. const readPublishedMedia = vi.fn(async ({ kind }: { kind: string }) => {
  18. if (kind === "video") throw new Error("download failed");
  19. return "data:image/png;base64,a";
  20. });
  21. const result = await prepareMarketingHero(
  22. {
  23. type: "video",
  24. video: {
  25. src: { src: "https://cdn.example.com/banner.webm", sha256: "a".repeat(64) },
  26. fallback: { src: "https://cdn.example.com/poster.png", sha256: "b".repeat(64) },
  27. },
  28. },
  29. { readPublishedMedia } as unknown as ICloudContentService,
  30. "en-US",
  31. false,
  32. );
  33. expect(result.hero).toEqual({ type: "image", src: "data:image/png;base64,a", alt: "" });
  34. expect(readPublishedMedia.mock.calls.map(([arg]) => arg.kind)).toEqual(["image", "video"]);
  35. });
  36. it("prepares a verified fallback on Web without acquiring a bundle", async () => {
  37. vi.stubGlobal(
  38. "Image",
  39. class {
  40. onload?: () => void;
  41. set src(_value: string) {
  42. queueMicrotask(() => this.onload?.());
  43. }
  44. },
  45. );
  46. const prepare = vi.fn();
  47. const result = await prepareMarketingHero(
  48. {
  49. type: "bundle",
  50. bundle: {
  51. bundle: { src: "https://cdn.example.com/banner.zip", sha256: "a".repeat(64) },
  52. entry: "index.html",
  53. fallback: { src: "https://cdn.example.com/fallback.png", sha256: "b".repeat(64) },
  54. },
  55. },
  56. {
  57. prepare,
  58. readPublishedMedia: async () => "data:image/png;base64,a",
  59. } as unknown as ICloudContentService,
  60. "en-US",
  61. false,
  62. );
  63. expect(result.hero).toEqual({ type: "image", src: "data:image/png;base64,a", alt: "" });
  64. expect(prepare).not.toHaveBeenCalled();
  65. });
  66. it("degrades a bundle on Web without asking for a desktop loopback lease", async () => {
  67. const prepare = vi.fn();
  68. const result = await prepareMarketingHero(
  69. {
  70. type: "bundle",
  71. bundle: {
  72. bundle: { src: "https://cdn.example.com/a.zip", sha256: "a".repeat(64) },
  73. entry: "index.html",
  74. },
  75. },
  76. { prepare } as unknown as ICloudContentService,
  77. "en-US",
  78. false,
  79. );
  80. expect(result.hero).toBeNull();
  81. expect(prepare).not.toHaveBeenCalled();
  82. });
  83. it("releases a prepared bundle exactly once", async () => {
  84. const release = vi.fn(async () => {});
  85. const service = {
  86. prepare: async () => ({
  87. url: "http://127.0.0.1:123/lease/index.html",
  88. leaseId: "lease",
  89. cacheHit: false,
  90. }),
  91. release,
  92. } as unknown as ICloudContentService;
  93. const result = await prepareMarketingHero(
  94. {
  95. type: "bundle",
  96. args: {
  97. heading: "Banner",
  98. zcode_plan: {
  99. name: "Weekend",
  100. entitlements: [
  101. {
  102. meter: "model_usage",
  103. show_name: "GLM",
  104. grant_units: 300000000,
  105. unit_type: "token",
  106. },
  107. ],
  108. },
  109. },
  110. bundle: {
  111. bundle: { src: "https://cdn.example.com/a.zip", sha256: "a".repeat(64) },
  112. entry: "index.html",
  113. },
  114. },
  115. service,
  116. "en-US",
  117. true,
  118. );
  119. expect(result.hero?.type).toBe("interactive_bundle");
  120. expect(result.hero).toMatchObject({
  121. data: {
  122. heading: "Banner",
  123. planName: "Weekend",
  124. amountValue: "300,000,000",
  125. zcode_plan: { name: "Weekend" },
  126. },
  127. });
  128. await result.release();
  129. await result.release();
  130. expect(release).toHaveBeenCalledTimes(1);
  131. });
  132. });