|
| 1 | +// @vitest-environment jsdom |
| 2 | +import pageHtml from "../index.html?raw"; |
| 3 | +import { expect, it, vi } from "vitest"; |
| 4 | + |
| 5 | +vi.mock("@hyperframes/sdk", () => ({ openComposition: vi.fn() })); |
| 6 | +vi.mock("@hyperframes/core/gsap-parser-acorn", () => ({ parseGsapScriptAcorn: vi.fn() })); |
| 7 | +vi.mock("gsap/dist/gsap.min.js?raw", () => ({ default: "" })); |
| 8 | +vi.mock("./fileAdapter.js", () => ({ |
| 9 | + createFileAdapter: () => Promise.reject(new Error('<img src=x onerror="alert(1)"> & failed')), |
| 10 | +})); |
| 11 | + |
| 12 | +it("shows initialization errors as literal text without creating attacker markup", async () => { |
| 13 | + document.documentElement.innerHTML = pageHtml; |
| 14 | + vi.stubGlobal( |
| 15 | + "ResizeObserver", |
| 16 | + class { |
| 17 | + observe() {} |
| 18 | + }, |
| 19 | + ); |
| 20 | + const listeners = vi.spyOn(window, "addEventListener"); |
| 21 | + try { |
| 22 | + await import("./main.js"); |
| 23 | + await vi.waitFor(() => expect(document.querySelector("pre")).not.toBeNull()); |
| 24 | + expect(document.body.textContent).toBe('Error: <img src=x onerror="alert(1)"> & failed'); |
| 25 | + expect(document.body.querySelector("img")).toBeNull(); |
| 26 | + expect(document.body.children).toHaveLength(1); |
| 27 | + expect(document.querySelector("pre")?.style.padding).toBe("20px"); |
| 28 | + } finally { |
| 29 | + for (const [type, listener] of listeners.mock.calls) window.removeEventListener(type, listener); |
| 30 | + listeners.mockRestore(); |
| 31 | + vi.unstubAllGlobals(); |
| 32 | + document.documentElement.innerHTML = ""; |
| 33 | + } |
| 34 | +}); |
0 commit comments