Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | // The generated pluginImports.js // is codegen and gitignored, so test the GENERATOR against a fixture config in // a temp dir and assert on the emitted source. All runtime-loading // logic (descriptor detection/loading, the runtimeModules cache, the origin // allowlist gate) lives in platform/app/src/runtimeExtensionLoader.ts (tested // directly in its own suite); the codegen emits the loader import, the // descriptor branches, the cache lookup, and the gated fallthrough. const fs = require('fs'); const os = require('os'); const path = require('path'); const writePluginImportsFile = require('../../.rspack/writePluginImportsFile.js'); const fixture = { extensions: [ { packageName: '@fixture/ext-a' }, { packageName: '@fixture/ext-url', importPath: '/plugins/ext-url/index.umd.js', globalName: 'fixtureExtUrl', }, ], modes: [{ packageName: '@fixture/mode-a' }], public: [], }; describe('writePluginImportsFile codegen', () => { let src; beforeAll(() => { const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'ohif-codegen-')); writePluginImportsFile(tmp, path.join(tmp, 'dist'), fixture); src = fs.readFileSync(path.join(tmp, 'pluginImports.js'), 'utf8'); }); test('emits the runtimeExtensionLoader import (all six names) before loadModule', () => { expect(src).toContain("} from './runtimeExtensionLoader';"); for (const name of [ 'isRuntimeDescriptor', 'loadRuntimeDescriptor', 'resolveRuntimeModule', 'loadExternalModule', 'sharePlugin', 'loadInPhases', ]) { expect(src).toContain(name); expect(src.indexOf(name)).toBeLessThan(src.indexOf('async function loadModule')); } }); test('branch order: descriptor < string passthrough < static branches < cache < gated fallthrough', () => { const order = [ "} from './runtimeExtensionLoader';", 'if (isRuntimeDescriptor(module)) return loadRuntimeDescriptor(module, builtInPlugins, kind);', "if (typeof module !== 'string') return module;", 'if( module==="@fixture/ext-a")', 'const runtimeModule = resolveRuntimeModule(module);', 'return loadExternalModule(module);', ]; const positions = order.map(s => src.indexOf(s)); positions.forEach((pos, i) => { expect(pos).toBeGreaterThan(-1); if (i > 0) { expect(pos).toBeGreaterThan(positions[i - 1]); } }); }); test('emits the tuple-descriptor branch ahead of the string passthrough', () => { expect(src).toContain('if (Array.isArray(module) && isRuntimeDescriptor(module[0])) {'); expect(src).toContain('return [await loadRuntimeDescriptor(module[0], builtInPlugins, kind), module[1]];'); expect(src.indexOf('Array.isArray(module)')).toBeLessThan( src.indexOf("if (typeof module !== 'string') return module;") ); }); test('declares every pluginConfig extension and mode as built in, before loadModule', () => { const declaration = 'const builtInPlugins = new Set(["@fixture/ext-a","@fixture/ext-url","@fixture/mode-a"]);'; expect(src).toContain(declaration); expect(src.indexOf(declaration)).toBeLessThan(src.indexOf('async function loadModule')); }); test('the cache lookup returns before the fallthrough', () => { expect(src).toContain('if (runtimeModule !== undefined) return runtimeModule;'); expect(src.indexOf('if (runtimeModule !== undefined) return runtimeModule;')).toBeLessThan( src.indexOf('return loadExternalModule(module);') ); }); test('the fallthrough delegates to loadExternalModule and never calls browserImportFunction(module) directly', () => { expect(src).toContain('return loadExternalModule(module);'); expect(src.match(/window\.browserImportFunction\(module\)/g)).toBe(null); expect(src).not.toContain('(await window.browserImportFunction(module)).default'); }); test('the fallthrough comes after every declared-plugin branch', () => { expect(src.indexOf('if( module==="@fixture/ext-a")')).toBeLessThan( src.indexOf('return loadExternalModule(module);') ); expect(src.indexOf('if( module==="@fixture/mode-a")')).toBeLessThan( src.indexOf('return loadExternalModule(module);') ); }); test('preserves existing branches', () => { expect(src).toContain(" if (typeof module !== 'string') return module;"); expect(src).toContain('if( module==="@fixture/ext-a") {'); expect(src).toContain('const imported = await import("@fixture/ext-a");'); expect(src).toContain("window.browserImportFunction('/plugins/ext-url/index.umd.js')"); expect(src).toContain('window["fixtureExtUrl"]'); expect(src).toContain('export { loadModule, modes, extensions, importItems };'); }); test('every compiled-in extension and mode shares itself', () => { expect(src).toContain('return sharePlugin("@fixture/ext-a", imported).default;'); expect(src).toContain('return sharePlugin("@fixture/mode-a", imported).default;'); }); test('an importPath entry does not share itself (its own script sets its global)', () => { expect(src).not.toContain('sharePlugin("@fixture/ext-url"'); }); test('importItems loads in phases instead of all at once', () => { expect(src).toContain(' return loadInPhases(modules, module => loadModule(module, kind));'); expect(src).not.toContain('Promise.all(modules.map(loadModule))'); }); test('importItems and loadModule pass the kind through to the descriptor loader', () => { expect(src).toContain('export default function importItems(modules, kind) {'); expect(src).toContain('async function loadModule(module, kind) {'); }); }); // The same generator run against the REAL pluginConfig.json (the // default third argument), still writing to a temp dir so the checked-out tree // is untouched. Proves the shipped codegen shape: branch order, the absent // ungated fallthrough, the dicom-microscopy-viewer importPath branch, exports. describe('writePluginImportsFile codegen against the real pluginConfig.json', () => { let src; beforeAll(() => { const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'ohif-codegen-real-')); writePluginImportsFile(tmp, path.join(tmp, 'dist')); src = fs.readFileSync(path.join(tmp, 'pluginImports.js'), 'utf8'); }); test('branch ORDER: loader import < descriptor branch < string passthrough < first static branch < cache < gated fallthrough', () => { const order = [ "} from './runtimeExtensionLoader';", 'isRuntimeDescriptor(module)', "typeof module !== 'string'", 'if( module==="@ohif/extension-default")', 'resolveRuntimeModule(module)', 'return loadExternalModule(module);', ]; const positions = order.map(s => src.indexOf(s)); positions.forEach((pos, i) => { expect(pos).toBeGreaterThan(-1); if (i > 0) { expect(pos).toBeGreaterThan(positions[i - 1]); } }); }); test('the old ungated fallthrough is gone', () => { expect(src).not.toContain('(await window.browserImportFunction(module)).default'); }); test('the dicom-microscopy-viewer importPath branch is still emitted verbatim', () => { expect(src).toContain( [ ' if( module==="dicom-microscopy-viewer") {', " const imported = await window.browserImportFunction('/dicom-microscopy-viewer/dicomMicroscopyViewer.min.js');", ' return window["dicomMicroscopyViewer"];', ' }', ].join('\n') ); }); test('the exports line is present', () => { expect(src).toContain('export { loadModule, modes, extensions, importItems };'); }); }); describe('copying plugin public/ and dist/ assets', () => { test('empty folders are skipped; folders with files are copied', () => { const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'ohif-copy-')); const plugin = name => { const dir = path.join(tmp, name); fs.mkdirSync(dir, { recursive: true }); return dir; }; // Empty dist/ (what migrate used to leave), and an empty public/ holding only an empty folder. const emptyExt = plugin('empty-ext'); fs.mkdirSync(path.join(emptyExt, 'dist')); fs.mkdirSync(path.join(emptyExt, 'public', 'nested'), { recursive: true }); // A plugin with real assets in both. const fullExt = plugin('full-ext'); fs.mkdirSync(path.join(fullExt, 'dist')); fs.writeFileSync(path.join(fullExt, 'dist', 'worker.js'), ''); fs.mkdirSync(path.join(fullExt, 'public', 'img'), { recursive: true }); fs.writeFileSync(path.join(fullExt, 'public', 'img', 'logo.png'), ''); const forward = dir => dir.split(path.sep).join('/'); const copies = writePluginImportsFile(tmp, path.join(tmp, 'out'), { extensions: [ { packageName: '@fixture/empty-ext', directory: forward(emptyExt) }, { packageName: '@fixture/full-ext', directory: forward(fullExt) }, ], modes: [], public: [], }); const sources = copies.map(copy => path.resolve(copy.from)); expect(sources).toContain(path.join(fullExt, 'dist')); expect(sources).toContain(path.join(fullExt, 'public')); expect(sources).not.toContain(path.join(emptyExt, 'dist')); expect(sources).not.toContain(path.join(emptyExt, 'public')); }); }); |