cloudContent.test.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import { describe, expect, it } from "vitest";
  2. import { cloudContentPayloadSchema } from "../src/cloudContent.js";
  3. const payload = {
  4. schemaVersion: 1,
  5. id: "notice",
  6. revision: 1,
  7. kind: "feature",
  8. locale: "en-US",
  9. dialog: {
  10. title: "Title",
  11. description: { format: "html", text: "<b>Hello</b>" },
  12. hero: {
  13. type: "interactive_bundle",
  14. runtime: "zcode-hero-sandbox-v1",
  15. bundle: {
  16. format: "zip",
  17. url: "https://example.com/hero.zip",
  18. sha256: "a".repeat(64),
  19. sizeBytes: 100,
  20. entry: "index.html",
  21. },
  22. viewport: { aspectRatio: "4:3" },
  23. data: {},
  24. events: {},
  25. },
  26. buttons: [{ id: "close", label: "Close", variant: "primary", actionId: "close" }],
  27. },
  28. actions: { close: { type: "close" } },
  29. };
  30. describe("cloud content wire schema", () => {
  31. it("rejects malformed URLs without throwing outside safeParse", () => {
  32. const bad = structuredClone(payload);
  33. bad.dialog.hero.bundle.url = "https://";
  34. expect(cloudContentPayloadSchema.safeParse(bad).success).toBe(false);
  35. });
  36. it("accepts the complete ZIP/HTML payload", () => {
  37. expect(cloudContentPayloadSchema.parse(payload).dialog.title).toBe("Title");
  38. });
  39. it("rejects unsupported schema, malformed ZIP and dangling button references", () => {
  40. expect(cloudContentPayloadSchema.safeParse({ ...payload, schemaVersion: 2 }).success).toBe(
  41. false,
  42. );
  43. expect(cloudContentPayloadSchema.safeParse({ ...payload, actions: {} }).success).toBe(false);
  44. const bad = structuredClone(payload);
  45. bad.dialog.hero.bundle.sha256 = "bad";
  46. expect(cloudContentPayloadSchema.safeParse(bad).success).toBe(false);
  47. });
  48. it("rejects oversized content, duplicate buttons and unknown action types", () => {
  49. const bad = structuredClone(payload);
  50. bad.dialog.description.text = "x".repeat(20001);
  51. expect(cloudContentPayloadSchema.safeParse(bad).success).toBe(false);
  52. bad.dialog.description.text = "okay";
  53. bad.dialog.buttons.push(bad.dialog.buttons[0]!);
  54. expect(cloudContentPayloadSchema.safeParse(bad).success).toBe(false);
  55. expect(
  56. cloudContentPayloadSchema.safeParse({
  57. ...payload,
  58. actions: { close: { type: "execute_js" } },
  59. }).success,
  60. ).toBe(false);
  61. });
  62. });