Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions packages/core/src/runtime/bridge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,54 @@ function createMockDeps() {

function makeControlMessage(action: string, extra?: Record<string, unknown>) {
return new MessageEvent("message", {
source: window.parent,
data: { source: "hf-parent", type: "control", action, ...extra },
});
}

describe("installRuntimeControlBridge", () => {
it("rejects foreign and null senders before dispatching controls", () => {
const foreign = document.createElement("iframe");
document.body.append(foreign);
const deps = createMockDeps();
const handler = installRuntimeControlBridge(deps);
try {
for (const source of [foreign.contentWindow, null]) {
handler(
new MessageEvent("message", {
source,
data: { source: "hf-parent", type: "control", action: "play" },
}),
);
}
expect(deps.onPlay).not.toHaveBeenCalled();
handler(
new MessageEvent("message", {
source: window,
data: { source: "hf-parent", type: "control", action: "play" },
}),
);
expect(deps.onPlay).toHaveBeenCalledOnce();
} finally {
window.removeEventListener("message", handler);
foreign.remove();
}
});
it("accepts an embedding parent distinct from the runtime window", () => {
const frame = document.createElement("iframe");
document.body.append(frame);
vi.stubGlobal("parent", frame.contentWindow);
const deps = createMockDeps();
const handler = installRuntimeControlBridge(deps);
try {
handler(makeControlMessage("play"));
expect(deps.onPlay).toHaveBeenCalledOnce();
} finally {
window.removeEventListener("message", handler);
vi.unstubAllGlobals();
frame.remove();
}
});
it("dispatches play command", () => {
const deps = createMockDeps();
const handler = installRuntimeControlBridge(deps);
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/runtime/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ function handleFlashElements(data: BridgeControlData): void {

export function installRuntimeControlBridge(deps: BridgeDeps): (event: MessageEvent) => void {
const handler = (event: MessageEvent) => {
if (event.source !== window.parent && event.source !== window) return;
const data = event.data as BridgeControlData | null;
if (!data || data.source !== "hf-parent" || data.type !== "control") return;
if (rejectUnsupportedProtocol(data)) return;
Expand Down
9 changes: 9 additions & 0 deletions packages/core/src/runtime/init.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ describe("initSandboxRuntimeModular", () => {

window.dispatchEvent(
new MessageEvent("message", {
source: window.parent,
data: { source: "hf-parent", type: "control", action: "set-volume", volume: 1 },
}),
);
Expand Down Expand Up @@ -239,6 +240,7 @@ describe("initSandboxRuntimeModular", () => {
window.addEventListener("error", onError);
window.dispatchEvent(
new MessageEvent("message", {
source: window.parent,
data: { source: "hf-parent", type: "control", action: "set-volume", volume: 1 },
}),
);
Expand Down Expand Up @@ -2846,6 +2848,7 @@ describe("initSandboxRuntimeModular", () => {
if (payload.source !== "hf-preview" || payload.type !== "ready") return;
window.dispatchEvent(
new MessageEvent("message", {
source: window.parent,
data: {
source: "hf-parent",
type: "control",
Expand All @@ -2856,6 +2859,7 @@ describe("initSandboxRuntimeModular", () => {
);
window.dispatchEvent(
new MessageEvent("message", {
source: window.parent,
data: {
source: "hf-parent",
type: "control",
Expand Down Expand Up @@ -3049,6 +3053,7 @@ describe("initSandboxRuntimeModular", () => {

window.dispatchEvent(
new MessageEvent("message", {
source: window.parent,
data: { source: "hf-parent", type: "control", action: "set-muted", muted: false },
}),
);
Expand All @@ -3058,6 +3063,7 @@ describe("initSandboxRuntimeModular", () => {

window.dispatchEvent(
new MessageEvent("message", {
source: window.parent,
data: { source: "hf-parent", type: "control", action: "set-muted", muted: true },
}),
);
Expand All @@ -3067,6 +3073,7 @@ describe("initSandboxRuntimeModular", () => {

window.dispatchEvent(
new MessageEvent("message", {
source: window.parent,
data: { source: "hf-parent", type: "control", action: "set-muted", muted: false },
}),
);
Expand Down Expand Up @@ -3100,6 +3107,7 @@ describe("initSandboxRuntimeModular", () => {

window.dispatchEvent(
new MessageEvent("message", {
source: window.parent,
data: {
source: "hf-parent",
type: "control",
Expand Down Expand Up @@ -3148,6 +3156,7 @@ describe("initSandboxRuntimeModular", () => {

window.dispatchEvent(
new MessageEvent("message", {
source: window.parent,
data: {
source: "hf-parent",
type: "control",
Expand Down
Loading