|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 2 | + |
| 3 | +import { |
| 4 | + getBuildTargets, |
| 5 | + getDefaultNodeVersion, |
| 6 | + getLatestCurrentRelease, |
| 7 | +} from '../../registry/dist/lib/sea-build.js' |
| 8 | + |
| 9 | +describe('sea-build module', () => { |
| 10 | + let originalEnv: NodeJS.ProcessEnv |
| 11 | + |
| 12 | + beforeEach(() => { |
| 13 | + originalEnv = { ...process.env } |
| 14 | + }) |
| 15 | + |
| 16 | + afterEach(() => { |
| 17 | + process.env = originalEnv |
| 18 | + vi.unstubAllGlobals() |
| 19 | + }) |
| 20 | + |
| 21 | + describe('getDefaultNodeVersion', () => { |
| 22 | + it('should return env var when set', async () => { |
| 23 | + process.env['SOCKET_SEA_NODE_VERSION'] = '22.0.0' |
| 24 | + const version = await getDefaultNodeVersion() |
| 25 | + expect(version).toBe('22.0.0') |
| 26 | + }) |
| 27 | + |
| 28 | + it('should fetch latest when env var not set', async () => { |
| 29 | + delete process.env['SOCKET_SEA_NODE_VERSION'] |
| 30 | + |
| 31 | + vi.stubGlobal('fetch', async () => ({ |
| 32 | + ok: true, |
| 33 | + json: async () => [ |
| 34 | + { version: 'v24.8.0' }, |
| 35 | + { version: 'v23.0.0' }, |
| 36 | + { version: 'v22.0.0' }, |
| 37 | + ], |
| 38 | + })) |
| 39 | + |
| 40 | + const version = await getDefaultNodeVersion() |
| 41 | + expect(version).toBe('24.8.0') |
| 42 | + }) |
| 43 | + }) |
| 44 | + |
| 45 | + describe('getLatestCurrentRelease', () => { |
| 46 | + it('should fetch and parse latest even-numbered version', async () => { |
| 47 | + vi.stubGlobal('fetch', async () => ({ |
| 48 | + ok: true, |
| 49 | + json: async () => [ |
| 50 | + { version: 'v26.1.0' }, |
| 51 | + { version: 'v25.0.0' }, |
| 52 | + { version: 'v24.8.0' }, |
| 53 | + { version: 'v23.0.0' }, |
| 54 | + { version: 'v22.0.0' }, |
| 55 | + ], |
| 56 | + })) |
| 57 | + |
| 58 | + const version = await getLatestCurrentRelease() |
| 59 | + expect(version).toBe('26.1.0') |
| 60 | + }) |
| 61 | + |
| 62 | + it('should filter out odd-numbered versions', async () => { |
| 63 | + vi.stubGlobal('fetch', async () => ({ |
| 64 | + ok: true, |
| 65 | + json: async () => [ |
| 66 | + { version: 'v25.0.0' }, |
| 67 | + { version: 'v24.8.0' }, |
| 68 | + { version: 'v23.0.0' }, |
| 69 | + ], |
| 70 | + })) |
| 71 | + |
| 72 | + const version = await getLatestCurrentRelease() |
| 73 | + expect(version).toBe('24.8.0') |
| 74 | + }) |
| 75 | + |
| 76 | + it('should filter out versions below v24', async () => { |
| 77 | + vi.stubGlobal('fetch', async () => ({ |
| 78 | + ok: true, |
| 79 | + json: async () => [ |
| 80 | + { version: 'v22.0.0' }, |
| 81 | + { version: 'v20.0.0' }, |
| 82 | + { version: 'v18.0.0' }, |
| 83 | + ], |
| 84 | + })) |
| 85 | + |
| 86 | + const version = await getLatestCurrentRelease() |
| 87 | + expect(version).toBe('24.8.0') |
| 88 | + }) |
| 89 | + |
| 90 | + it('should fallback to 24.8.0 when no suitable version found', async () => { |
| 91 | + vi.stubGlobal('fetch', async () => ({ |
| 92 | + ok: true, |
| 93 | + json: async () => [{ version: 'v21.0.0' }, { version: 'v19.0.0' }], |
| 94 | + })) |
| 95 | + |
| 96 | + const version = await getLatestCurrentRelease() |
| 97 | + expect(version).toBe('24.8.0') |
| 98 | + }) |
| 99 | + |
| 100 | + it('should throw error on fetch failure', async () => { |
| 101 | + vi.stubGlobal('fetch', async () => ({ |
| 102 | + ok: false, |
| 103 | + statusText: 'Not Found', |
| 104 | + })) |
| 105 | + |
| 106 | + await expect(getLatestCurrentRelease()).rejects.toThrow('Failed to fetch') |
| 107 | + }) |
| 108 | + |
| 109 | + it('should throw error on network error', async () => { |
| 110 | + vi.stubGlobal('fetch', async () => { |
| 111 | + throw new Error('Network error') |
| 112 | + }) |
| 113 | + |
| 114 | + await expect(getLatestCurrentRelease()).rejects.toThrow( |
| 115 | + 'Failed to fetch latest Node.js Current release', |
| 116 | + ) |
| 117 | + }) |
| 118 | + |
| 119 | + it('should handle invalid version format', async () => { |
| 120 | + vi.stubGlobal('fetch', async () => ({ |
| 121 | + ok: true, |
| 122 | + json: async () => [{ version: 'invalid' }, { version: 'v24.8.0' }], |
| 123 | + })) |
| 124 | + |
| 125 | + const version = await getLatestCurrentRelease() |
| 126 | + expect(version).toBe('24.8.0') |
| 127 | + }) |
| 128 | + }) |
| 129 | + |
| 130 | + describe('getBuildTargets', () => { |
| 131 | + it('should return array of build targets', async () => { |
| 132 | + process.env['SOCKET_SEA_NODE_VERSION'] = '24.0.0' |
| 133 | + |
| 134 | + const targets = await getBuildTargets() |
| 135 | + |
| 136 | + expect(Array.isArray(targets)).toBe(true) |
| 137 | + expect(targets.length).toBeGreaterThan(0) |
| 138 | + }) |
| 139 | + |
| 140 | + it('should include all platforms', async () => { |
| 141 | + process.env['SOCKET_SEA_NODE_VERSION'] = '24.0.0' |
| 142 | + |
| 143 | + const targets = await getBuildTargets() |
| 144 | + const platforms = targets.map(t => t.platform) |
| 145 | + |
| 146 | + expect(platforms).toContain('win32') |
| 147 | + expect(platforms).toContain('darwin') |
| 148 | + expect(platforms).toContain('linux') |
| 149 | + }) |
| 150 | + |
| 151 | + it('should include arm64 and x64 architectures', async () => { |
| 152 | + process.env['SOCKET_SEA_NODE_VERSION'] = '24.0.0' |
| 153 | + |
| 154 | + const targets = await getBuildTargets() |
| 155 | + const arches = targets.map(t => t.arch) |
| 156 | + |
| 157 | + expect(arches).toContain('arm64') |
| 158 | + expect(arches).toContain('x64') |
| 159 | + }) |
| 160 | + |
| 161 | + it('should use default node version', async () => { |
| 162 | + process.env['SOCKET_SEA_NODE_VERSION'] = '25.5.5' |
| 163 | + |
| 164 | + const targets = await getBuildTargets() |
| 165 | + |
| 166 | + expect(targets.every(t => t.nodeVersion === '25.5.5')).toBe(true) |
| 167 | + }) |
| 168 | + |
| 169 | + it('should have correct output names', async () => { |
| 170 | + process.env['SOCKET_SEA_NODE_VERSION'] = '24.0.0' |
| 171 | + |
| 172 | + const targets = await getBuildTargets() |
| 173 | + const outputNames = targets.map(t => t.outputName) |
| 174 | + |
| 175 | + expect(outputNames).toContain('socket-win-arm64.exe') |
| 176 | + expect(outputNames).toContain('socket-win-x64.exe') |
| 177 | + expect(outputNames).toContain('socket-macos-arm64') |
| 178 | + expect(outputNames).toContain('socket-macos-x64') |
| 179 | + expect(outputNames).toContain('socket-linux-arm64') |
| 180 | + expect(outputNames).toContain('socket-linux-x64') |
| 181 | + }) |
| 182 | + }) |
| 183 | +}) |
0 commit comments