|
| 1 | +/** |
| 2 | + * @fileoverview Distribution integration test runner |
| 3 | + * Runs pre-test build check, then executes vitest with appropriate distribution flags. |
| 4 | + * |
| 5 | + * Usage: |
| 6 | + * node scripts/integration.mjs --js # Test JS distribution only |
| 7 | + * node scripts/integration.mjs --sea # Test SEA binary only |
| 8 | + * node scripts/integration.mjs --smol # Test smol binary only |
| 9 | + * node scripts/integration.mjs --all # Test all distributions |
| 10 | + */ |
| 11 | + |
| 12 | +import { existsSync } from 'node:fs' |
| 13 | +import path from 'node:path' |
| 14 | +import { fileURLToPath } from 'node:url' |
| 15 | + |
| 16 | +import colors from 'yoctocolors-cjs' |
| 17 | + |
| 18 | +import { getDefaultLogger } from '@socketsecurity/lib/logger' |
| 19 | +import { spawn } from '@socketsecurity/lib/spawn' |
| 20 | + |
| 21 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)) |
| 22 | +const ROOT_DIR = path.resolve(__dirname, '..') |
| 23 | +const MONOREPO_ROOT = path.resolve(ROOT_DIR, '../..') |
| 24 | + |
| 25 | +const BINARY_PATHS = { |
| 26 | + __proto__: null, |
| 27 | + js: path.join(ROOT_DIR, 'dist/index.js'), |
| 28 | + sea: path.join(MONOREPO_ROOT, 'packages/node-sea-builder/dist/socket-sea'), |
| 29 | + smol: path.join(MONOREPO_ROOT, 'packages/node-smol-builder/dist/socket-smol'), |
| 30 | +} |
| 31 | + |
| 32 | +const BINARY_FLAGS = { |
| 33 | + __proto__: null, |
| 34 | + all: { |
| 35 | + TEST_JS_BINARY: '1', |
| 36 | + TEST_SEA_BINARY: '1', |
| 37 | + TEST_SMOL_BINARY: '1', |
| 38 | + }, |
| 39 | + js: { |
| 40 | + TEST_JS_BINARY: '1', |
| 41 | + }, |
| 42 | + sea: { |
| 43 | + TEST_SEA_BINARY: '1', |
| 44 | + }, |
| 45 | + smol: { |
| 46 | + TEST_SMOL_BINARY: '1', |
| 47 | + }, |
| 48 | +} |
| 49 | + |
| 50 | +async function checkBinaryExists(binaryType) { |
| 51 | + // For explicit binary requests (js, sea, smol), require binary to exist. |
| 52 | + if (binaryType === 'js' || binaryType === 'sea' || binaryType === 'smol') { |
| 53 | + const binaryPath = BINARY_PATHS[binaryType] |
| 54 | + if (!existsSync(binaryPath)) { |
| 55 | + getDefaultLogger().error( |
| 56 | + `${colors.red('✗')} Binary not found: ${binaryPath}`, |
| 57 | + ) |
| 58 | + getDefaultLogger().log('') |
| 59 | + getDefaultLogger().log( |
| 60 | + 'The binary must be built before running e2e tests.', |
| 61 | + ) |
| 62 | + getDefaultLogger().log('Build commands:') |
| 63 | + if (binaryType === 'js') { |
| 64 | + getDefaultLogger().log(' pnpm run build') |
| 65 | + } else if (binaryType === 'sea') { |
| 66 | + getDefaultLogger().log( |
| 67 | + ' pnpm --filter @socketbin/node-sea-builder run build', |
| 68 | + ) |
| 69 | + } else if (binaryType === 'smol') { |
| 70 | + getDefaultLogger().log( |
| 71 | + ' pnpm --filter @socketbin/node-smol-builder run build', |
| 72 | + ) |
| 73 | + } |
| 74 | + getDefaultLogger().log('') |
| 75 | + return false |
| 76 | + } |
| 77 | + getDefaultLogger().log(`${colors.green('✓')} Binary found: ${binaryPath}`) |
| 78 | + getDefaultLogger().log('') |
| 79 | + } |
| 80 | + |
| 81 | + // For 'all', we'll skip missing binaries (handled by test suite). |
| 82 | + return true |
| 83 | +} |
| 84 | + |
| 85 | +async function runVitest(binaryType) { |
| 86 | + const envVars = BINARY_FLAGS[binaryType] |
| 87 | + getDefaultLogger().log( |
| 88 | + `${colors.blue('ℹ')} Running distribution integration tests for ${binaryType}...`, |
| 89 | + ) |
| 90 | + getDefaultLogger().log('') |
| 91 | + |
| 92 | + // Check if binary exists when explicitly requested. |
| 93 | + const binaryExists = await checkBinaryExists(binaryType) |
| 94 | + if (!binaryExists) { |
| 95 | + process.exit(1) |
| 96 | + } |
| 97 | + |
| 98 | + const result = await spawn( |
| 99 | + 'dotenvx', |
| 100 | + [ |
| 101 | + '-q', |
| 102 | + 'run', |
| 103 | + '-f', |
| 104 | + '.env.test', |
| 105 | + '--', |
| 106 | + 'vitest', |
| 107 | + 'run', |
| 108 | + 'test/integration/binary/binary-test-suite.test.mts', |
| 109 | + '--config', |
| 110 | + 'vitest.config.mts', |
| 111 | + ], |
| 112 | + { |
| 113 | + env: { |
| 114 | + ...process.env, |
| 115 | + RUN_INTEGRATION_TESTS: '1', // Automatically enable tests when explicitly running integration.mjs. |
| 116 | + ...envVars, |
| 117 | + }, |
| 118 | + stdio: 'inherit', |
| 119 | + }, |
| 120 | + ) |
| 121 | + |
| 122 | + process.exit(result.code ?? 0) |
| 123 | +} |
| 124 | + |
| 125 | +async function main() { |
| 126 | + const args = process.argv.slice(2) |
| 127 | + const flag = args.find(arg => arg.startsWith('--'))?.slice(2) |
| 128 | + |
| 129 | + if (!flag || !BINARY_FLAGS[flag]) { |
| 130 | + getDefaultLogger().error('Invalid or missing flag') |
| 131 | + getDefaultLogger().log('') |
| 132 | + getDefaultLogger().log('Usage:') |
| 133 | + getDefaultLogger().log( |
| 134 | + ' node scripts/integration.mjs --js # Test JS distribution', |
| 135 | + ) |
| 136 | + getDefaultLogger().log( |
| 137 | + ' node scripts/integration.mjs --sea # Test SEA binary', |
| 138 | + ) |
| 139 | + getDefaultLogger().log( |
| 140 | + ' node scripts/integration.mjs --smol # Test smol binary', |
| 141 | + ) |
| 142 | + getDefaultLogger().log( |
| 143 | + ' node scripts/integration.mjs --all # Test all distributions', |
| 144 | + ) |
| 145 | + getDefaultLogger().log('') |
| 146 | + process.exit(1) |
| 147 | + } |
| 148 | + |
| 149 | + await runVitest(flag) |
| 150 | +} |
| 151 | + |
| 152 | +main().catch(e => { |
| 153 | + getDefaultLogger().error('Integration test runner failed:', e) |
| 154 | + process.exit(1) |
| 155 | +}) |
0 commit comments