|
| 1 | +// Copyright (c) 2026 Music Blocks contributors |
| 2 | +// |
| 3 | +// This program is free software; you can redistribute it and/or |
| 4 | +// modify it under the terms of the The GNU Affero General Public |
| 5 | +// License as published by the Free Software Foundation; either |
| 6 | +// version 3 of the License, or (at your option) any later version. |
| 7 | +// |
| 8 | +// You should have received a copy of the GNU Affero General Public |
| 9 | +// License along with this library; if not, write to the Free Software |
| 10 | +// Foundation, 51 Franklin Street, Suite 500 Boston, MA 02110-1335 USA |
| 11 | + |
| 12 | +const createActivity = () => ({ |
| 13 | + blocks: { |
| 14 | + protoBlockDict: {} |
| 15 | + }, |
| 16 | + logo: { |
| 17 | + evalFlowDict: {}, |
| 18 | + evalArgDict: {}, |
| 19 | + evalSetterDict: {}, |
| 20 | + evalParameterDict: {}, |
| 21 | + evalOnStartList: {}, |
| 22 | + evalOnStopList: {} |
| 23 | + }, |
| 24 | + palettes: { |
| 25 | + buttons: {}, |
| 26 | + add: jest.fn(), |
| 27 | + makePalettes: jest.fn(), |
| 28 | + updatePalettes: jest.fn(), |
| 29 | + show: jest.fn(), |
| 30 | + pluginMacros: {} |
| 31 | + }, |
| 32 | + pluginsImages: {} |
| 33 | +}); |
| 34 | + |
| 35 | +describe("processPluginData script cleanup", () => { |
| 36 | + let processPluginData; |
| 37 | + let originalCreateObjectURL; |
| 38 | + let originalRevokeObjectURL; |
| 39 | + let appendChildSpy; |
| 40 | + |
| 41 | + beforeEach(() => { |
| 42 | + jest.useFakeTimers(); |
| 43 | + jest.resetModules(); |
| 44 | + document.head.innerHTML = ""; |
| 45 | + |
| 46 | + global._ = msg => msg; |
| 47 | + global.PALETTEICONS = {}; |
| 48 | + global.PALETTEFILLCOLORS = {}; |
| 49 | + global.PALETTESTROKECOLORS = {}; |
| 50 | + global.PALETTEHIGHLIGHTCOLORS = {}; |
| 51 | + global.HIGHLIGHTSTROKECOLORS = {}; |
| 52 | + global.MULTIPALETTES = [[], [], []]; |
| 53 | + global.platformColor = { paletteColors: {} }; |
| 54 | + window.__mb_plugin_registry = {}; |
| 55 | + |
| 56 | + originalCreateObjectURL = URL.createObjectURL; |
| 57 | + originalRevokeObjectURL = URL.revokeObjectURL; |
| 58 | + URL.createObjectURL = jest.fn(() => "blob:plugin-setup"); |
| 59 | + URL.revokeObjectURL = jest.fn(); |
| 60 | + |
| 61 | + ({ processPluginData } = require("../utils.js")); |
| 62 | + }); |
| 63 | + |
| 64 | + afterEach(() => { |
| 65 | + if (appendChildSpy) { |
| 66 | + appendChildSpy.mockRestore(); |
| 67 | + appendChildSpy = undefined; |
| 68 | + } |
| 69 | + URL.createObjectURL = originalCreateObjectURL; |
| 70 | + URL.revokeObjectURL = originalRevokeObjectURL; |
| 71 | + document.head.innerHTML = ""; |
| 72 | + delete window.__mb_plugin_registry; |
| 73 | + jest.useRealTimers(); |
| 74 | + }); |
| 75 | + |
| 76 | + it("removes setup script elements after they load", async () => { |
| 77 | + const originalAppendChild = document.head.appendChild.bind(document.head); |
| 78 | + appendChildSpy = jest.spyOn(document.head, "appendChild").mockImplementation(script => { |
| 79 | + originalAppendChild(script); |
| 80 | + script.onload(); |
| 81 | + return script; |
| 82 | + }); |
| 83 | + |
| 84 | + await processPluginData( |
| 85 | + createActivity(), |
| 86 | + JSON.stringify({ |
| 87 | + BLOCKPLUGINS: { |
| 88 | + testBlock: "globalThis.pluginSetupLoaded = true;" |
| 89 | + } |
| 90 | + }), |
| 91 | + "plugins/test.json" |
| 92 | + ); |
| 93 | + |
| 94 | + expect(document.head.querySelectorAll("script[src^='blob:plugin-setup']")).toHaveLength(0); |
| 95 | + expect(URL.revokeObjectURL).toHaveBeenCalledWith("blob:plugin-setup"); |
| 96 | + }); |
| 97 | + |
| 98 | + it("removes setup script elements after load errors", async () => { |
| 99 | + const originalAppendChild = document.head.appendChild.bind(document.head); |
| 100 | + appendChildSpy = jest.spyOn(document.head, "appendChild").mockImplementation(script => { |
| 101 | + originalAppendChild(script); |
| 102 | + script.onerror(new Error("load failed")); |
| 103 | + return script; |
| 104 | + }); |
| 105 | + |
| 106 | + await processPluginData( |
| 107 | + createActivity(), |
| 108 | + JSON.stringify({ |
| 109 | + BLOCKPLUGINS: { |
| 110 | + testBlock: "globalThis.pluginSetupLoaded = true;" |
| 111 | + } |
| 112 | + }), |
| 113 | + "plugins/test.json" |
| 114 | + ); |
| 115 | + |
| 116 | + expect(document.head.querySelectorAll("script[src^='blob:plugin-setup']")).toHaveLength(0); |
| 117 | + expect(URL.revokeObjectURL).toHaveBeenCalledWith("blob:plugin-setup"); |
| 118 | + }); |
| 119 | +}); |
0 commit comments