|
| 1 | +import { writeFileSync } from 'node:fs'; |
| 2 | +import { join } from 'node:path'; |
| 3 | +import type { FrameworkConfig } from '@spotify-confidence/core'; |
| 4 | +import { createProjectDir } from '@spotify-confidence/testing/scaffold'; |
| 5 | +import { runSdkInstall } from '@features/sdk/install.js'; |
| 6 | + |
| 7 | +const mockExecFile = vi.fn<() => Promise<void>>(); |
| 8 | +const mockDetectFramework = vi.fn<() => Promise<FrameworkConfig | null>>(); |
| 9 | +const mockMessage = vi.fn(); |
| 10 | + |
| 11 | +vi.mock('ora', () => ({ |
| 12 | + default: () => ({ start: () => ({ succeed: vi.fn(), fail: vi.fn() }) }), |
| 13 | +})); |
| 14 | + |
| 15 | +vi.mock('@spotify-confidence/core', async (importOriginal) => { |
| 16 | + const actual = await importOriginal<typeof import('@spotify-confidence/core')>(); |
| 17 | + return { |
| 18 | + ...actual, |
| 19 | + detectFramework: (...args: unknown[]) => mockDetectFramework(...(args as [])), |
| 20 | + execFile: (...args: unknown[]) => mockExecFile(...(args as [])), |
| 21 | + }; |
| 22 | +}); |
| 23 | + |
| 24 | +vi.mock('@output/print.js', () => ({ |
| 25 | + message: (...args: unknown[]) => mockMessage(...args), |
| 26 | + fail: vi.fn(), |
| 27 | +})); |
| 28 | + |
| 29 | +const REACT_FRAMEWORK: FrameworkConfig = { |
| 30 | + id: 'react', |
| 31 | + name: 'React', |
| 32 | + docsUrl: 'https://confidence.spotify.com/docs/sdk/react', |
| 33 | + sdkPackage: '@spotify-confidence/sdk', |
| 34 | + detect: async () => true, |
| 35 | +}; |
| 36 | + |
| 37 | +const PYTHON_FRAMEWORK: FrameworkConfig = { |
| 38 | + id: 'python', |
| 39 | + name: 'Python', |
| 40 | + docsUrl: 'https://confidence.spotify.com/docs/sdk/python', |
| 41 | + sdkPackage: 'spotify-confidence-sdk', |
| 42 | + detect: async () => true, |
| 43 | +}; |
| 44 | + |
| 45 | +beforeEach(() => { |
| 46 | + mockExecFile.mockResolvedValue(undefined); |
| 47 | + mockDetectFramework.mockResolvedValue(REACT_FRAMEWORK); |
| 48 | +}); |
| 49 | + |
| 50 | +describe('runSdkInstall workspace root handling', () => { |
| 51 | + it('passes -w when pnpm-workspace.yaml is present', async () => { |
| 52 | + using project = createProjectDir('empty'); |
| 53 | + writeFileSync(join(project.path, 'pnpm-lock.yaml'), ''); |
| 54 | + writeFileSync(join(project.path, 'pnpm-workspace.yaml'), 'packages:\n - packages/*'); |
| 55 | + |
| 56 | + await runSdkInstall({ dir: project.path }); |
| 57 | + |
| 58 | + expect(mockExecFile).toHaveBeenCalledWith( |
| 59 | + 'pnpm', |
| 60 | + ['add', '-w', '@spotify-confidence/sdk'], |
| 61 | + expect.objectContaining({ cwd: project.path }), |
| 62 | + ); |
| 63 | + }); |
| 64 | + |
| 65 | + it('omits -w for pnpm without workspace config', async () => { |
| 66 | + using project = createProjectDir('empty'); |
| 67 | + writeFileSync(join(project.path, 'pnpm-lock.yaml'), ''); |
| 68 | + |
| 69 | + await runSdkInstall({ dir: project.path }); |
| 70 | + |
| 71 | + expect(mockExecFile).toHaveBeenCalledWith( |
| 72 | + 'pnpm', |
| 73 | + ['add', '@spotify-confidence/sdk'], |
| 74 | + expect.objectContaining({ cwd: project.path }), |
| 75 | + ); |
| 76 | + }); |
| 77 | + |
| 78 | + it('passes -W for yarn classic with workspaces', async () => { |
| 79 | + using project = createProjectDir('empty'); |
| 80 | + writeFileSync(join(project.path, 'yarn.lock'), ''); |
| 81 | + writeFileSync( |
| 82 | + join(project.path, 'package.json'), |
| 83 | + JSON.stringify({ workspaces: ['packages/*'] }), |
| 84 | + ); |
| 85 | + |
| 86 | + await runSdkInstall({ dir: project.path }); |
| 87 | + |
| 88 | + expect(mockExecFile).toHaveBeenCalledWith( |
| 89 | + 'yarn', |
| 90 | + ['add', '-W', '@spotify-confidence/sdk'], |
| 91 | + expect.objectContaining({ cwd: project.path }), |
| 92 | + ); |
| 93 | + }); |
| 94 | + |
| 95 | + it('omits -W for yarn berry with workspaces', async () => { |
| 96 | + using project = createProjectDir('empty'); |
| 97 | + writeFileSync(join(project.path, 'yarn.lock'), ''); |
| 98 | + writeFileSync( |
| 99 | + join(project.path, 'package.json'), |
| 100 | + JSON.stringify({ workspaces: ['packages/*'] }), |
| 101 | + ); |
| 102 | + writeFileSync(join(project.path, '.yarnrc.yml'), 'nodeLinker: node-modules'); |
| 103 | + |
| 104 | + await runSdkInstall({ dir: project.path }); |
| 105 | + |
| 106 | + expect(mockExecFile).toHaveBeenCalledWith( |
| 107 | + 'yarn', |
| 108 | + ['add', '@spotify-confidence/sdk'], |
| 109 | + expect.objectContaining({ cwd: project.path }), |
| 110 | + ); |
| 111 | + }); |
| 112 | + |
| 113 | + it('omits -W for yarn without workspaces', async () => { |
| 114 | + using project = createProjectDir('empty'); |
| 115 | + writeFileSync(join(project.path, 'yarn.lock'), ''); |
| 116 | + |
| 117 | + await runSdkInstall({ dir: project.path }); |
| 118 | + |
| 119 | + expect(mockExecFile).toHaveBeenCalledWith( |
| 120 | + 'yarn', |
| 121 | + ['add', '@spotify-confidence/sdk'], |
| 122 | + expect.objectContaining({ cwd: project.path }), |
| 123 | + ); |
| 124 | + }); |
| 125 | + |
| 126 | + it('does not add flags for npm at a workspace root', async () => { |
| 127 | + using project = createProjectDir('empty'); |
| 128 | + writeFileSync( |
| 129 | + join(project.path, 'package.json'), |
| 130 | + JSON.stringify({ workspaces: ['packages/*'] }), |
| 131 | + ); |
| 132 | + |
| 133 | + await runSdkInstall({ dir: project.path }); |
| 134 | + |
| 135 | + expect(mockExecFile).toHaveBeenCalledWith( |
| 136 | + 'npm', |
| 137 | + ['add', '@spotify-confidence/sdk'], |
| 138 | + expect.objectContaining({ cwd: project.path }), |
| 139 | + ); |
| 140 | + }); |
| 141 | + |
| 142 | + it('does not add flags for bun at a workspace root', async () => { |
| 143 | + using project = createProjectDir('empty'); |
| 144 | + writeFileSync(join(project.path, 'bun.lockb'), ''); |
| 145 | + writeFileSync( |
| 146 | + join(project.path, 'package.json'), |
| 147 | + JSON.stringify({ workspaces: ['packages/*'] }), |
| 148 | + ); |
| 149 | + |
| 150 | + await runSdkInstall({ dir: project.path }); |
| 151 | + |
| 152 | + expect(mockExecFile).toHaveBeenCalledWith( |
| 153 | + 'bun', |
| 154 | + ['add', '@spotify-confidence/sdk'], |
| 155 | + expect.objectContaining({ cwd: project.path }), |
| 156 | + ); |
| 157 | + }); |
| 158 | +}); |
| 159 | + |
| 160 | +describe('runSdkInstall Python package manager detection', () => { |
| 161 | + beforeEach(() => { |
| 162 | + mockDetectFramework.mockResolvedValue(PYTHON_FRAMEWORK); |
| 163 | + }); |
| 164 | + |
| 165 | + it('uses poetry add when poetry.lock is present', async () => { |
| 166 | + using project = createProjectDir('empty'); |
| 167 | + writeFileSync(join(project.path, 'poetry.lock'), ''); |
| 168 | + |
| 169 | + await runSdkInstall({ dir: project.path }); |
| 170 | + |
| 171 | + expect(mockExecFile).toHaveBeenCalledWith( |
| 172 | + 'poetry', |
| 173 | + ['add', 'spotify-confidence-sdk'], |
| 174 | + expect.objectContaining({ cwd: project.path }), |
| 175 | + ); |
| 176 | + }); |
| 177 | + |
| 178 | + it('uses uv add when uv.lock is present', async () => { |
| 179 | + using project = createProjectDir('empty'); |
| 180 | + writeFileSync(join(project.path, 'uv.lock'), ''); |
| 181 | + |
| 182 | + await runSdkInstall({ dir: project.path }); |
| 183 | + |
| 184 | + expect(mockExecFile).toHaveBeenCalledWith( |
| 185 | + 'uv', |
| 186 | + ['add', 'spotify-confidence-sdk'], |
| 187 | + expect.objectContaining({ cwd: project.path }), |
| 188 | + ); |
| 189 | + }); |
| 190 | + |
| 191 | + it('uses pipenv install when Pipfile is present', async () => { |
| 192 | + using project = createProjectDir('empty'); |
| 193 | + writeFileSync(join(project.path, 'Pipfile'), ''); |
| 194 | + |
| 195 | + await runSdkInstall({ dir: project.path }); |
| 196 | + |
| 197 | + expect(mockExecFile).toHaveBeenCalledWith( |
| 198 | + 'pipenv', |
| 199 | + ['install', 'spotify-confidence-sdk'], |
| 200 | + expect.objectContaining({ cwd: project.path }), |
| 201 | + ); |
| 202 | + }); |
| 203 | + |
| 204 | + it('falls back to manual install when no Python PM is detected', async () => { |
| 205 | + using project = createProjectDir('empty'); |
| 206 | + |
| 207 | + await runSdkInstall({ dir: project.path }); |
| 208 | + |
| 209 | + expect(mockExecFile).not.toHaveBeenCalled(); |
| 210 | + expect(mockMessage).toHaveBeenCalledWith(expect.stringContaining('pip install')); |
| 211 | + }); |
| 212 | +}); |
0 commit comments