|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | + |
| 3 | +// Mock the wasm_exec module |
| 4 | +vi.mock('../../lib/wasm_exec', () => ({})); |
| 5 | + |
| 6 | +// Mock @lightninglabs/lnc-core to avoid actual imports |
| 7 | +vi.mock('@lightninglabs/lnc-core'); |
| 8 | + |
| 9 | +describe('Index Module', () => { |
| 10 | + let originalInstantiateStreaming: any; |
| 11 | + let originalGlobal: any; |
| 12 | + |
| 13 | + beforeEach(() => { |
| 14 | + // Store original values |
| 15 | + originalInstantiateStreaming = |
| 16 | + globalThis.WebAssembly?.instantiateStreaming; |
| 17 | + originalGlobal = globalThis; |
| 18 | + |
| 19 | + // Mock WebAssembly for testing |
| 20 | + globalThis.WebAssembly = { |
| 21 | + instantiateStreaming: vi.fn(), |
| 22 | + instantiate: vi.fn(), |
| 23 | + compile: vi.fn() |
| 24 | + } as any; |
| 25 | + }); |
| 26 | + |
| 27 | + afterEach(() => { |
| 28 | + // Restore original values |
| 29 | + if (originalInstantiateStreaming) { |
| 30 | + globalThis.WebAssembly.instantiateStreaming = |
| 31 | + originalInstantiateStreaming; |
| 32 | + } |
| 33 | + vi.restoreAllMocks(); |
| 34 | + }); |
| 35 | + |
| 36 | + describe('WebAssembly Polyfill', () => { |
| 37 | + it('should polyfill WebAssembly.instantiateStreaming when not available', async () => { |
| 38 | + // Remove instantiateStreaming to test polyfill |
| 39 | + delete (globalThis.WebAssembly as any).instantiateStreaming; |
| 40 | + |
| 41 | + // Import the index module to trigger the polyfill |
| 42 | + await import('./index'); |
| 43 | + |
| 44 | + // Now WebAssembly.instantiateStreaming should exist |
| 45 | + expect(typeof globalThis.WebAssembly?.instantiateStreaming).toBe( |
| 46 | + 'function' |
| 47 | + ); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should use existing WebAssembly.instantiateStreaming when available', async () => { |
| 51 | + // Set up existing WebAssembly.instantiateStreaming |
| 52 | + const existingInstantiateStreaming = vi.fn().mockResolvedValue({ |
| 53 | + module: {}, |
| 54 | + instance: {} |
| 55 | + }); |
| 56 | + |
| 57 | + globalThis.WebAssembly = { |
| 58 | + ...globalThis.WebAssembly, |
| 59 | + instantiateStreaming: existingInstantiateStreaming |
| 60 | + }; |
| 61 | + |
| 62 | + // Import the index module |
| 63 | + await import('./index'); |
| 64 | + |
| 65 | + // The existing function should still be there |
| 66 | + expect(globalThis.WebAssembly.instantiateStreaming).toBe( |
| 67 | + existingInstantiateStreaming |
| 68 | + ); |
| 69 | + }); |
| 70 | + }); |
| 71 | +}); |
0 commit comments