cloud-content-mock.test.mjs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import assert from "node:assert/strict";
  2. import { createHash } from "node:crypto";
  3. import { test } from "node:test";
  4. import { createCloudContentMockServer } from "./cloud-content-mock.mjs";
  5. test("HTTP mock returns a real ZIP with matching integrity and tracks requests", async () => {
  6. const mock = await createCloudContentMockServer();
  7. try {
  8. const response = await fetch(`${mock.url}/dialog?locale=en-US`);
  9. assert.equal(response.status, 200);
  10. const payload = await response.json();
  11. assert.equal(payload.locale, "en-US");
  12. assert.equal(payload.dialog.description.format, "html");
  13. const bundle = payload.dialog.hero.bundle;
  14. assert.equal(bundle.format, "zip");
  15. assert.equal(bundle.entry, "index.html");
  16. const bytes = Buffer.from(await (await fetch(bundle.url)).arrayBuffer());
  17. assert.equal(bytes.subarray(0, 2).toString(), "PK");
  18. assert.equal(bytes.length, bundle.sizeBytes);
  19. assert.equal(createHash("sha256").update(bytes).digest("hex"), bundle.sha256);
  20. assert.equal((await fetch(`${mock.url}/missing`)).status, 404);
  21. assert.equal(mock.requests.filter((url) => url.startsWith("/bundles/")).length, 1);
  22. } finally {
  23. await mock.close();
  24. }
  25. });
  26. test("provides real image, video and Lottie resources plus a feature scenario", async () => {
  27. const mock = await createCloudContentMockServer();
  28. try {
  29. for (const type of ["image", "video", "lottie"]) {
  30. const payload = await (
  31. await fetch(`${mock.url}/dialog?type=${type}&scenario=feature&locale=en-US`)
  32. ).json();
  33. assert.equal(payload.dialog.hero.type, type);
  34. assert.equal(payload.kind, "feature");
  35. const response = await fetch(payload.dialog.hero.src);
  36. assert.equal(response.status, 200);
  37. if (type === "video")
  38. assert.equal(
  39. Buffer.from(await response.arrayBuffer())
  40. .subarray(0, 4)
  41. .toString("hex"),
  42. "1a45dfa3",
  43. );
  44. else if (type === "lottie") assert.equal((await response.json()).layers.length, 1);
  45. else assert.match(await response.text(), /<svg/);
  46. }
  47. } finally {
  48. await mock.close();
  49. }
  50. });