|
| 1 | +import fs from 'node:fs'; |
| 2 | +import os from 'node:os'; |
| 3 | +import path from 'node:path'; |
| 4 | + |
| 5 | +import * as rockTools from '@rock-js/tools'; |
| 6 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 7 | + |
| 8 | +import { copyDebugBundleToSimulatorSlice } from '../copyDebugBundleToSimulatorSlice.js'; |
| 9 | + |
| 10 | +vi.mock('@rock-js/tools', async (importOriginal) => { |
| 11 | + const actual = await importOriginal<typeof rockTools>(); |
| 12 | + return { |
| 13 | + ...actual, |
| 14 | + logger: { |
| 15 | + ...actual.logger, |
| 16 | + error: vi.fn(), |
| 17 | + warn: vi.fn(), |
| 18 | + info: vi.fn(), |
| 19 | + success: vi.fn(), |
| 20 | + debug: vi.fn(), |
| 21 | + }, |
| 22 | + }; |
| 23 | +}); |
| 24 | + |
| 25 | +const mockLoggerWarn = rockTools.logger.warn as ReturnType<typeof vi.fn>; |
| 26 | +const mockLoggerSuccess = rockTools.logger.success as ReturnType<typeof vi.fn>; |
| 27 | + |
| 28 | +function createFramework(pathname: string) { |
| 29 | + fs.mkdirSync(pathname, { recursive: true }); |
| 30 | + fs.writeFileSync(path.join(pathname, 'BrownfieldLib'), 'fake binary'); |
| 31 | +} |
| 32 | + |
| 33 | +describe('copyDebugBundleToSimulatorSlice', () => { |
| 34 | + let tempDir: string; |
| 35 | + |
| 36 | + beforeEach(() => { |
| 37 | + tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'copy-debug-bundle-test-')); |
| 38 | + vi.clearAllMocks(); |
| 39 | + }); |
| 40 | + |
| 41 | + afterEach(() => { |
| 42 | + fs.rmSync(tempDir, { recursive: true, force: true }); |
| 43 | + }); |
| 44 | + |
| 45 | + it('copies main.jsbundle into the Debug simulator slice when it is missing', () => { |
| 46 | + const productsPath = path.join(tempDir, 'Build', 'Products'); |
| 47 | + |
| 48 | + const deviceFrameworkPath = path.join( |
| 49 | + productsPath, |
| 50 | + 'Debug-iphoneos', |
| 51 | + 'BrownfieldLib.framework' |
| 52 | + ); |
| 53 | + const simulatorFrameworkPath = path.join( |
| 54 | + productsPath, |
| 55 | + 'Debug-iphonesimulator', |
| 56 | + 'BrownfieldLib.framework' |
| 57 | + ); |
| 58 | + |
| 59 | + createFramework(deviceFrameworkPath); |
| 60 | + createFramework(simulatorFrameworkPath); |
| 61 | + |
| 62 | + fs.writeFileSync( |
| 63 | + path.join(deviceFrameworkPath, 'main.jsbundle'), |
| 64 | + 'debug bundled output' |
| 65 | + ); |
| 66 | + |
| 67 | + copyDebugBundleToSimulatorSlice({ |
| 68 | + productsPath, |
| 69 | + configuration: 'Debug', |
| 70 | + frameworkName: 'BrownfieldLib', |
| 71 | + }); |
| 72 | + |
| 73 | + const simulatorBundlePath = path.join( |
| 74 | + simulatorFrameworkPath, |
| 75 | + 'main.jsbundle' |
| 76 | + ); |
| 77 | + |
| 78 | + expect(fs.readFileSync(simulatorBundlePath, 'utf8')).toBe( |
| 79 | + 'debug bundled output' |
| 80 | + ); |
| 81 | + expect(mockLoggerSuccess).toHaveBeenCalledWith( |
| 82 | + expect.stringContaining('Copied Debug JS bundle to simulator slice') |
| 83 | + ); |
| 84 | + }); |
| 85 | + |
| 86 | + it('does nothing for non-Debug configurations', () => { |
| 87 | + const productsPath = path.join(tempDir, 'Build', 'Products'); |
| 88 | + |
| 89 | + const deviceFrameworkPath = path.join( |
| 90 | + productsPath, |
| 91 | + 'Release-iphoneos', |
| 92 | + 'BrownfieldLib.framework' |
| 93 | + ); |
| 94 | + const simulatorFrameworkPath = path.join( |
| 95 | + productsPath, |
| 96 | + 'Release-iphonesimulator', |
| 97 | + 'BrownfieldLib.framework' |
| 98 | + ); |
| 99 | + |
| 100 | + createFramework(deviceFrameworkPath); |
| 101 | + createFramework(simulatorFrameworkPath); |
| 102 | + fs.writeFileSync( |
| 103 | + path.join(deviceFrameworkPath, 'main.jsbundle'), |
| 104 | + 'release bundle' |
| 105 | + ); |
| 106 | + |
| 107 | + copyDebugBundleToSimulatorSlice({ |
| 108 | + productsPath, |
| 109 | + configuration: 'Release', |
| 110 | + frameworkName: 'BrownfieldLib', |
| 111 | + }); |
| 112 | + |
| 113 | + expect( |
| 114 | + fs.existsSync(path.join(simulatorFrameworkPath, 'main.jsbundle')) |
| 115 | + ).toBe(false); |
| 116 | + expect(mockLoggerSuccess).not.toHaveBeenCalled(); |
| 117 | + }); |
| 118 | + |
| 119 | + it('warns and skips when the device bundle is missing', () => { |
| 120 | + const productsPath = path.join(tempDir, 'Build', 'Products'); |
| 121 | + |
| 122 | + const simulatorFrameworkPath = path.join( |
| 123 | + productsPath, |
| 124 | + 'Debug-iphonesimulator', |
| 125 | + 'BrownfieldLib.framework' |
| 126 | + ); |
| 127 | + |
| 128 | + createFramework(simulatorFrameworkPath); |
| 129 | + |
| 130 | + copyDebugBundleToSimulatorSlice({ |
| 131 | + productsPath, |
| 132 | + configuration: 'Debug', |
| 133 | + frameworkName: 'BrownfieldLib', |
| 134 | + }); |
| 135 | + |
| 136 | + expect(mockLoggerWarn).toHaveBeenCalledWith( |
| 137 | + expect.stringContaining('Skipping simulator JS bundle copy') |
| 138 | + ); |
| 139 | + }); |
| 140 | + |
| 141 | + it('overwrites an existing simulator bundle with the Debug device bundle', () => { |
| 142 | + const productsPath = path.join(tempDir, 'Build', 'Products'); |
| 143 | + |
| 144 | + const deviceFrameworkPath = path.join( |
| 145 | + productsPath, |
| 146 | + 'Debug-iphoneos', |
| 147 | + 'BrownfieldLib.framework' |
| 148 | + ); |
| 149 | + const simulatorFrameworkPath = path.join( |
| 150 | + productsPath, |
| 151 | + 'Debug-iphonesimulator', |
| 152 | + 'BrownfieldLib.framework' |
| 153 | + ); |
| 154 | + |
| 155 | + createFramework(deviceFrameworkPath); |
| 156 | + createFramework(simulatorFrameworkPath); |
| 157 | + |
| 158 | + fs.writeFileSync( |
| 159 | + path.join(deviceFrameworkPath, 'main.jsbundle'), |
| 160 | + 'fresh debug bundle' |
| 161 | + ); |
| 162 | + fs.writeFileSync( |
| 163 | + path.join(simulatorFrameworkPath, 'main.jsbundle'), |
| 164 | + 'stale simulator bundle' |
| 165 | + ); |
| 166 | + |
| 167 | + copyDebugBundleToSimulatorSlice({ |
| 168 | + productsPath, |
| 169 | + configuration: 'Debug', |
| 170 | + frameworkName: 'BrownfieldLib', |
| 171 | + }); |
| 172 | + |
| 173 | + expect( |
| 174 | + fs.readFileSync(path.join(simulatorFrameworkPath, 'main.jsbundle'), 'utf8') |
| 175 | + ).toBe('fresh debug bundle'); |
| 176 | + }); |
| 177 | +}); |
0 commit comments