|
| 1 | +/** |
| 2 | + * Unit tests for the bespoke `spawnSocketPatchDlx` flow. |
| 3 | + * |
| 4 | + * The Vfs / auto-dispatch code is tested via define-tool-spawn.test.mts. |
| 5 | + * This file targets the three-way Dlx dispatch (local override / GitHub |
| 6 | + * release / legacy npm fallback). |
| 7 | + */ |
| 8 | + |
| 9 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 10 | + |
| 11 | +const mockSpawn = vi.hoisted(() => vi.fn()) |
| 12 | +const mockSpawnDlx = vi.hoisted(() => vi.fn()) |
| 13 | +const mockDownloadGitHubReleaseBinary = vi.hoisted(() => vi.fn()) |
| 14 | +const mockResolveSocketPatch = vi.hoisted(() => vi.fn()) |
| 15 | +const mockDetectExecutableType = vi.hoisted(() => vi.fn()) |
| 16 | + |
| 17 | +vi.mock('@socketsecurity/lib/spawn', () => ({ |
| 18 | + spawn: mockSpawn, |
| 19 | +})) |
| 20 | + |
| 21 | +vi.mock('@socketsecurity/lib/dlx/detect', () => ({ |
| 22 | + detectExecutableType: mockDetectExecutableType, |
| 23 | +})) |
| 24 | + |
| 25 | +vi.mock('../../../../src/utils/dlx/spawn.mts', () => ({ |
| 26 | + downloadGitHubReleaseBinary: mockDownloadGitHubReleaseBinary, |
| 27 | + spawnDlx: mockSpawnDlx, |
| 28 | +})) |
| 29 | + |
| 30 | +vi.mock('../../../../src/utils/dlx/resolve-binary.mts', () => ({ |
| 31 | + resolveSocketPatch: mockResolveSocketPatch, |
| 32 | +})) |
| 33 | + |
| 34 | +import { spawnSocketPatchDlx } from '../../../../src/utils/dlx/spawn-socket-patch.mts' |
| 35 | + |
| 36 | +describe('spawnSocketPatchDlx', () => { |
| 37 | + beforeEach(() => { |
| 38 | + vi.clearAllMocks() |
| 39 | + }) |
| 40 | + |
| 41 | + it('runs a local socket-patch binary when SOCKET_CLI_SOCKET_PATCH_LOCAL_PATH is set', async () => { |
| 42 | + mockResolveSocketPatch.mockReturnValue({ |
| 43 | + type: 'local', |
| 44 | + path: '/local/socket-patch', |
| 45 | + }) |
| 46 | + mockDetectExecutableType.mockReturnValue({ type: 'binary' }) |
| 47 | + mockSpawn.mockReturnValue('p') |
| 48 | + |
| 49 | + const result = await spawnSocketPatchDlx( |
| 50 | + ['apply'], |
| 51 | + undefined, |
| 52 | + undefined, |
| 53 | + ) |
| 54 | + |
| 55 | + expect(mockSpawn).toHaveBeenCalledWith( |
| 56 | + '/local/socket-patch', |
| 57 | + ['apply'], |
| 58 | + expect.objectContaining({ stdio: 'inherit' }), |
| 59 | + ) |
| 60 | + expect(result).toEqual({ spawnPromise: 'p' }) |
| 61 | + }) |
| 62 | + |
| 63 | + it('runs the local script via node when not a binary', async () => { |
| 64 | + mockResolveSocketPatch.mockReturnValue({ |
| 65 | + type: 'local', |
| 66 | + path: '/local/socket-patch.js', |
| 67 | + }) |
| 68 | + mockDetectExecutableType.mockReturnValue({ type: 'script' }) |
| 69 | + mockSpawn.mockReturnValue('p') |
| 70 | + |
| 71 | + await spawnSocketPatchDlx([], undefined, undefined) |
| 72 | + |
| 73 | + expect(mockSpawn).toHaveBeenCalledWith( |
| 74 | + 'node', |
| 75 | + ['/local/socket-patch.js'], |
| 76 | + expect.any(Object), |
| 77 | + ) |
| 78 | + }) |
| 79 | + |
| 80 | + it('downloads from GitHub releases when resolution.type is "github-release"', async () => { |
| 81 | + mockResolveSocketPatch.mockReturnValue({ |
| 82 | + type: 'github-release', |
| 83 | + details: { name: 'socket-patch', version: '2.0.0' }, |
| 84 | + }) |
| 85 | + mockDownloadGitHubReleaseBinary.mockResolvedValue('/cache/socket-patch') |
| 86 | + mockSpawn.mockReturnValue('p') |
| 87 | + |
| 88 | + const result = await spawnSocketPatchDlx(['apply'], undefined, undefined) |
| 89 | + |
| 90 | + expect(mockDownloadGitHubReleaseBinary).toHaveBeenCalled() |
| 91 | + expect(mockSpawn).toHaveBeenCalledWith( |
| 92 | + '/cache/socket-patch', |
| 93 | + ['apply'], |
| 94 | + expect.objectContaining({ stdio: 'inherit' }), |
| 95 | + ) |
| 96 | + expect(result).toEqual({ spawnPromise: 'p' }) |
| 97 | + }) |
| 98 | + |
| 99 | + it('falls back to spawnDlx for legacy npm-package resolutions', async () => { |
| 100 | + mockResolveSocketPatch.mockReturnValue({ |
| 101 | + type: 'dlx', |
| 102 | + details: { name: 'socket-patch', version: '1.0.0' }, |
| 103 | + }) |
| 104 | + mockSpawnDlx.mockResolvedValue({ spawnPromise: 'p' }) |
| 105 | + |
| 106 | + const result = await spawnSocketPatchDlx([], undefined, undefined) |
| 107 | + |
| 108 | + expect(mockSpawnDlx).toHaveBeenCalled() |
| 109 | + expect(result).toEqual({ spawnPromise: 'p' }) |
| 110 | + }) |
| 111 | + |
| 112 | + it('honors a custom stdio passed via spawnExtra', async () => { |
| 113 | + mockResolveSocketPatch.mockReturnValue({ |
| 114 | + type: 'github-release', |
| 115 | + details: { name: 'socket-patch', version: '2.0.0' }, |
| 116 | + }) |
| 117 | + mockDownloadGitHubReleaseBinary.mockResolvedValue('/cache/socket-patch') |
| 118 | + mockSpawn.mockReturnValue('p') |
| 119 | + |
| 120 | + await spawnSocketPatchDlx([], undefined, { stdio: 'pipe' } as any) |
| 121 | + |
| 122 | + expect(mockSpawn).toHaveBeenCalledWith( |
| 123 | + '/cache/socket-patch', |
| 124 | + [], |
| 125 | + expect.objectContaining({ stdio: 'pipe' }), |
| 126 | + ) |
| 127 | + }) |
| 128 | + |
| 129 | + it('merges options.env into the child env (local path)', async () => { |
| 130 | + mockResolveSocketPatch.mockReturnValue({ |
| 131 | + type: 'local', |
| 132 | + path: '/local/socket-patch', |
| 133 | + }) |
| 134 | + mockDetectExecutableType.mockReturnValue({ type: 'binary' }) |
| 135 | + mockSpawn.mockReturnValue('p') |
| 136 | + |
| 137 | + await spawnSocketPatchDlx([], { env: { FOO: 'bar' } } as any, undefined) |
| 138 | + |
| 139 | + const callEnv = mockSpawn.mock.calls[0][2].env |
| 140 | + expect(callEnv.FOO).toBe('bar') |
| 141 | + }) |
| 142 | +}) |
0 commit comments