|
| 1 | +import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"; |
| 2 | +import { iFrameScriptExecutionHandlerCode } from "./scriptExecutor"; |
| 3 | + |
| 4 | +describe("iFrameScriptExecutionHandlerCode", () => { |
| 5 | + let iframe: HTMLIFrameElement; |
| 6 | + let messageEventListener: (event: MessageEvent) => void; |
| 7 | + |
| 8 | + beforeEach(() => { |
| 9 | + // Create a spy on window.addEventListener to capture the message event handler |
| 10 | + vi.spyOn(window, "addEventListener").mockImplementation( |
| 11 | + (event, handler) => { |
| 12 | + if (event === "message") { |
| 13 | + messageEventListener = handler as any; |
| 14 | + } |
| 15 | + } |
| 16 | + ); |
| 17 | + |
| 18 | + // Create a spy on window.postMessage |
| 19 | + vi.spyOn(window, "postMessage").mockImplementation(() => {}); |
| 20 | + |
| 21 | + // Build the iframe |
| 22 | + iframe = document.createElement("iframe"); |
| 23 | + document.body.appendChild(iframe); |
| 24 | + |
| 25 | + if (iframe.contentDocument) { |
| 26 | + iframe.contentDocument.open(); |
| 27 | + iframe.contentDocument.write(iFrameScriptExecutionHandlerCode); |
| 28 | + iframe.contentDocument.close(); |
| 29 | + } |
| 30 | + }); |
| 31 | + |
| 32 | + afterEach(() => { |
| 33 | + if (iframe && iframe.parentNode) { |
| 34 | + iframe.parentNode.removeChild(iframe); |
| 35 | + } |
| 36 | + vi.restoreAllMocks(); |
| 37 | + }); |
| 38 | + |
| 39 | + it("should execute simple dynamic code and return results via postMessage", async () => { |
| 40 | + const postMessageMock = vi.fn(); |
| 41 | + |
| 42 | + expect(iframe.contentWindow).not.toBeNull(); |
| 43 | + if (iframe.contentWindow) { |
| 44 | + // Override the postMessage method on the iframe's parent |
| 45 | + Object.defineProperty(iframe.contentWindow, "parent", { |
| 46 | + value: { |
| 47 | + postMessage: postMessageMock |
| 48 | + }, |
| 49 | + configurable: true |
| 50 | + }); |
| 51 | + |
| 52 | + const messageData = { |
| 53 | + functionCode: "return 40 + 2;", |
| 54 | + widget: {}, |
| 55 | + PVs: [] |
| 56 | + }; |
| 57 | + |
| 58 | + const messageEvent = new MessageEvent("message", { |
| 59 | + data: messageData, |
| 60 | + source: null |
| 61 | + }); |
| 62 | + |
| 63 | + // Send the code to the iframe via a message |
| 64 | + iframe.contentWindow.dispatchEvent(messageEvent); |
| 65 | + |
| 66 | + // Short wait for async execution to complete |
| 67 | + await new Promise(resolve => setTimeout(resolve, 1000)); |
| 68 | + |
| 69 | + // Validate that the dynamic script executed as expected |
| 70 | + expect(postMessageMock).toHaveBeenCalledWith(42, "*"); |
| 71 | + } |
| 72 | + }); |
| 73 | + |
| 74 | + it("should execute code containing some Phoebus built ins and return results via postMessage", async () => { |
| 75 | + const postMessageMock = vi.fn(); |
| 76 | + |
| 77 | + expect(iframe.contentWindow).not.toBeNull(); |
| 78 | + if (iframe.contentWindow) { |
| 79 | + // Override the postMessage method on the iframe's parent |
| 80 | + Object.defineProperty(iframe.contentWindow, "parent", { |
| 81 | + value: { |
| 82 | + postMessage: postMessageMock |
| 83 | + }, |
| 84 | + configurable: true |
| 85 | + }); |
| 86 | + |
| 87 | + const script = ` |
| 88 | + importClass(org.csstudio.display.builder.runtime.script.PVUtil); |
| 89 | + importClass(org.csstudio.display.builder.runtime.script.ScriptUtil); |
| 90 | + importPackage(Packages.org.csstudio.opibuilder.scriptUtil); |
| 91 | +
|
| 92 | + // widget.setPropertyValue("background_color", ColorFontUtil.getColorFromRGB(255, 255, 0)); |
| 93 | +
|
| 94 | + return PVUtil.getDouble(1+2); |
| 95 | + `; |
| 96 | + |
| 97 | + const messageData = { |
| 98 | + functionCode: script, |
| 99 | + widget: {}, |
| 100 | + PVs: [] |
| 101 | + }; |
| 102 | + |
| 103 | + const messageEvent = new MessageEvent("message", { |
| 104 | + data: messageData, |
| 105 | + source: null |
| 106 | + }); |
| 107 | + |
| 108 | + // Send the code to the iframe via a message |
| 109 | + iframe.contentWindow.dispatchEvent(messageEvent); |
| 110 | + |
| 111 | + // Short wait for async execution to complete |
| 112 | + await new Promise(resolve => setTimeout(resolve, 1000)); |
| 113 | + |
| 114 | + // Validate that the dynamic script executed as expected |
| 115 | + expect(postMessageMock).toHaveBeenCalledWith(3, "*"); |
| 116 | + } |
| 117 | + }); |
| 118 | + |
| 119 | + it("should handle exception in the dynamic code and send error message via postMessage", async () => { |
| 120 | + const postMessageMock = vi.fn(); |
| 121 | + |
| 122 | + expect(iframe.contentWindow).not.toBeNull(); |
| 123 | + if (iframe.contentWindow) { |
| 124 | + // Override the postMessage method on the iframe's parent |
| 125 | + Object.defineProperty(iframe.contentWindow, "parent", { |
| 126 | + value: { |
| 127 | + postMessage: postMessageMock |
| 128 | + }, |
| 129 | + configurable: true |
| 130 | + }); |
| 131 | + |
| 132 | + const messageData = { |
| 133 | + functionCode: 'throw new Error("Test error");', |
| 134 | + widget: {}, |
| 135 | + PVs: [] |
| 136 | + }; |
| 137 | + |
| 138 | + // Create a test message event with invalid code |
| 139 | + const messageEvent = new MessageEvent("message", { |
| 140 | + data: messageData, |
| 141 | + source: null |
| 142 | + }); |
| 143 | + |
| 144 | + // Dispatch the event to the iframe's contentWindow |
| 145 | + iframe.contentWindow.dispatchEvent(messageEvent); |
| 146 | + |
| 147 | + // Wait for async operations |
| 148 | + await new Promise(resolve => setTimeout(resolve, 1000)); |
| 149 | + |
| 150 | + // Check if postMessage was called with the expected error message |
| 151 | + expect(postMessageMock).toHaveBeenCalledWith("Error: Test error", "*"); |
| 152 | + } |
| 153 | + }); |
| 154 | +}); |
0 commit comments