cloudContentService.test.ts 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import { mkdtemp, rm } from "node:fs/promises";
  2. import { tmpdir } from "node:os";
  3. import { join } from "node:path";
  4. import { describe, expect, it } from "vitest";
  5. import { createCloudContentService } from "#src/cloud-content/cloudContentService.js";
  6. import { createMarketingAssetRegistry } from "#src/marketing-touch/marketingAssetRegistry.js";
  7. import { createCloudContentMockServer } from "../../desktop/scripts/cloud-content-mock.mjs";
  8. describe("cloud content host service", () => {
  9. it("runs HTTP JSON -> ZIP -> cache -> sandbox resource URL and expires released leases", async () => {
  10. const mock = await createCloudContentMockServer();
  11. const cacheRoot = await mkdtemp(join(tmpdir(), "cloud-content-service-"));
  12. const assets = createMarketingAssetRegistry({ allowLoopback: true });
  13. const service = createCloudContentService({ cacheRoot, publishedAssets: assets });
  14. try {
  15. const payload = await (
  16. await fetch(`${mock.url}/dialog?type=interactive_bundle&locale=en-US`)
  17. ).json();
  18. assets.allow({
  19. src: payload.dialog.hero.bundle.url,
  20. sha256: payload.dialog.hero.bundle.sha256,
  21. });
  22. expect(payload.dialog.hero.type).toBe("interactive_bundle");
  23. if (payload.dialog.hero.type !== "interactive_bundle") throw new Error("fixture type");
  24. const first = await service.prepare({ bundle: payload.dialog.hero.bundle });
  25. const second = await service.prepare({ bundle: payload.dialog.hero.bundle });
  26. expect(first.cacheHit).toBe(false);
  27. expect(second.cacheHit).toBe(true);
  28. const response = await fetch(first.url);
  29. expect(response.headers.get("content-security-policy")).toContain("sandbox allow-scripts");
  30. expect(response.headers.get("content-type")).toContain("text/html");
  31. expect(await response.text()).toContain("zcode-cloud-hero-v1");
  32. expect((await fetch(second.url)).status).toBe(200);
  33. await service.release({ leaseId: first.leaseId });
  34. expect((await fetch(first.url)).status).toBe(404);
  35. expect(mock.requests.filter((path: string) => path.startsWith("/bundles/")).length).toBe(1);
  36. const changed = { ...payload.dialog.hero.bundle, url: "https://untrusted.example/hero.zip" };
  37. await expect(service.prepare({ bundle: changed })).rejects.toThrow(/source/);
  38. } finally {
  39. await service.disposeAllAndWait();
  40. await mock.close();
  41. await rm(cacheRoot, { recursive: true, force: true });
  42. }
  43. });
  44. it("does not expose temporary mock or debug RPC methods", async () => {
  45. const service = createCloudContentService({
  46. cacheRoot: join(tmpdir(), "unused-cloud-content"),
  47. });
  48. expect(service).not.toHaveProperty("status");
  49. expect(service).not.toHaveProperty("load");
  50. expect(service).not.toHaveProperty("clear");
  51. await service.disposeAllAndWait();
  52. });
  53. it("permits only delivery-authorized bundles", async () => {
  54. const mock = await createCloudContentMockServer();
  55. const cacheRoot = await mkdtemp(join(tmpdir(), "marketing-published-service-"));
  56. const assets = createMarketingAssetRegistry({ allowLoopback: true });
  57. const service = createCloudContentService({
  58. cacheRoot,
  59. publishedAssets: assets,
  60. });
  61. try {
  62. const payload = await (
  63. await fetch(`${mock.url}/dialog?type=interactive_bundle&locale=en-US`)
  64. ).json();
  65. const bundle = payload.dialog.hero.bundle;
  66. await expect(service.prepare({ bundle })).rejects.toThrow(/source/);
  67. assets.allow({ src: bundle.url, sha256: bundle.sha256 });
  68. const { sizeBytes: _size, ...published } = bundle;
  69. const prepared = await service.prepare({ bundle: published });
  70. expect((await fetch(prepared.url)).status).toBe(200);
  71. await service.release({ leaseId: prepared.leaseId });
  72. } finally {
  73. await service.disposeAllAndWait();
  74. await mock.close();
  75. await rm(cacheRoot, { recursive: true, force: true });
  76. }
  77. });
  78. });