diff --git a/packages/core/src/runtime/compositionLoader.test.ts b/packages/core/src/runtime/compositionLoader.test.ts index 6bd260258c..4192593274 100644 --- a/packages/core/src/runtime/compositionLoader.test.ts +++ b/packages/core/src/runtime/compositionLoader.test.ts @@ -1,4 +1,4 @@ -import { describe, it, expect, vi, afterEach, beforeAll } from "vitest"; +import { describe, it, expect, vi, afterEach, beforeAll, beforeEach } from "vitest"; import { loadCompositions } from "./compositionLoader"; type LoaderParams = Parameters[0]; @@ -45,6 +45,18 @@ beforeAll(() => { } }); +// Studio pins a preview page's project root with ; these fixtures live under it. +function useProjectBase(href = "https://example.com/") { + beforeEach(() => { + const base = document.createElement("base"); + base.href = href; + document.head.prepend(base); + }); + afterEach(() => document.head.querySelectorAll("base").forEach((base) => base.remove())); +} + +useProjectBase(); + describe("loadCompositions external hosts", () => { afterEach(() => { document.body.innerHTML = ""; @@ -456,6 +468,61 @@ describe("loadCompositions external hosts", () => { }, ); + describe("a project under /api/projects/p1/preview/", () => { + beforeEach(() => { + document.querySelector("base")!.href = "https://example.com/api/projects/p1/preview/"; + }); + + it.each([ + "https://elsewhere.example/x.html", + "//elsewhere.example/x.html", + "/api/projects/p2/preview/x.html", + "../p2/preview/x.html", + ])("never fetches or mounts a data-composition-src outside it: %s", async (src) => { + const host = appendExternalHost(src, "x"); + const fetchSpy = vi + .spyOn(globalThis, "fetch") + .mockResolvedValue( + new Response( + `

off

`, + ), + ); + const onDiagnostic = vi.fn(); + const { injectedScripts } = await loadFixture({ onDiagnostic }); + expect(fetchSpy).not.toHaveBeenCalled(); + expect(host.innerHTML).toBe(""); + expect(injectedScripts).toHaveLength(0); + expect(onDiagnostic).toHaveBeenCalledWith({ + code: "external_composition_load_failed", + details: expect.objectContaining({ + hostCompositionSrc: src, + errorMessage: "data-composition-src must name a file inside the project", + }), + }); + }); + + it("mounts a project-relative sub-composition and its script", async () => { + const host = appendExternalHost("compositions/sub.html", "sub"); + const fetchSpy = vi + .spyOn(globalThis, "fetch") + .mockResolvedValue( + new Response( + `

in

`, + ), + ); + const onDiagnostic = vi.fn(); + const { injectedScripts } = await loadFixture({ onDiagnostic }); + expect(new URL(String(fetchSpy.mock.calls[0]?.[0]), document.baseURI).href).toBe( + "https://example.com/api/projects/p1/preview/compositions/sub.html", + ); + expect(host.textContent).toContain("in"); + expect(injectedScripts.map((script) => script.textContent).join("")).toContain( + "window.__in = 1;", + ); + expect(onDiagnostic).not.toHaveBeenCalled(); + }); + }); + it("calls onDiagnostic when HTTP response is not ok", async () => { const host = document.createElement("div"); host.setAttribute("data-composition-src", "https://example.com/404.html"); @@ -897,6 +964,9 @@ describe("loadCompositions external hosts", () => { */ const FRAME_URL = "http://localhost:5190/api/projects/demo/preview/compositions/frames/scene.html"; + beforeEach(() => { + document.querySelector("base")!.href = "http://localhost:5190/api/projects/demo/preview/"; + }); it("rewrites `../`-traversing src on elements inside