|
| 1 | +import { buildTestJwt, prepareAuthTokens } from '@spotify-confidence/testing/auth'; |
| 2 | +import { warehouseCommand } from '@commands/warehouse.js'; |
| 3 | +import { captureOutput } from '../helpers/capture.js'; |
| 4 | +import { createMockIntegration } from '../helpers/mock-integration.js'; |
| 5 | +import { createRunner } from '../helpers/run-command.js'; |
| 6 | + |
| 7 | +const mockIntegration = createMockIntegration({ |
| 8 | + detectPlugin: vi.fn().mockResolvedValue('cli'), |
| 9 | + detectMcpStatuses: vi.fn().mockResolvedValue({ |
| 10 | + 'confidence-flags': 'connected', |
| 11 | + 'confidence-docs': 'connected', |
| 12 | + }), |
| 13 | +}); |
| 14 | + |
| 15 | +const mockAuthenticate = vi.fn().mockResolvedValue({ |
| 16 | + accessToken: buildTestJwt({ email: 'test@example.com' }), |
| 17 | + region: 'EU', |
| 18 | +}); |
| 19 | + |
| 20 | +vi.mock('@spotify-confidence/core', async (importOriginal) => { |
| 21 | + const actual = await importOriginal<typeof import('@spotify-confidence/core')>(); |
| 22 | + return { |
| 23 | + ...actual, |
| 24 | + getIntegration: () => mockIntegration, |
| 25 | + getIntegrations: () => [mockIntegration], |
| 26 | + authenticate: (...args: unknown[]) => mockAuthenticate(...args), |
| 27 | + }; |
| 28 | +}); |
| 29 | + |
| 30 | +const run = createRunner(warehouseCommand); |
| 31 | + |
| 32 | +describe('warehouse setup', () => { |
| 33 | + it('launches chat session with correct skill for bigquery', async () => { |
| 34 | + using _auth = prepareAuthTokens('valid'); |
| 35 | + using _output = captureOutput(); |
| 36 | + |
| 37 | + await run(['warehouse', 'setup', '--warehouse-type', 'bigquery', '--ide', 'claude']); |
| 38 | + |
| 39 | + expect(mockIntegration.launchChat).toHaveBeenCalledWith( |
| 40 | + expect.objectContaining({ |
| 41 | + userPrompt: expect.stringContaining('setup-warehouse-bigquery'), |
| 42 | + cwd: process.cwd(), |
| 43 | + }), |
| 44 | + ); |
| 45 | + }); |
| 46 | + |
| 47 | + it('launches chat session with correct skill for snowflake', async () => { |
| 48 | + using _auth = prepareAuthTokens('valid'); |
| 49 | + using _output = captureOutput(); |
| 50 | + |
| 51 | + await run(['warehouse', 'setup', '--warehouse-type', 'snowflake', '--ide', 'claude']); |
| 52 | + |
| 53 | + expect(mockIntegration.launchChat).toHaveBeenCalledWith( |
| 54 | + expect.objectContaining({ |
| 55 | + userPrompt: expect.stringContaining('setup-warehouse-snowflake'), |
| 56 | + }), |
| 57 | + ); |
| 58 | + }); |
| 59 | + |
| 60 | + it('passes auth token to chat session', async () => { |
| 61 | + using _auth = prepareAuthTokens('valid'); |
| 62 | + using _output = captureOutput(); |
| 63 | + |
| 64 | + await run(['warehouse', 'setup', '--warehouse-type', 'bigquery', '--ide', 'claude']); |
| 65 | + |
| 66 | + expect(mockIntegration.launchChat).toHaveBeenCalledWith( |
| 67 | + expect.objectContaining({ token: expect.any(String) }), |
| 68 | + ); |
| 69 | + }); |
| 70 | + |
| 71 | + it('fails when plugin is not installed', async () => { |
| 72 | + using _auth = prepareAuthTokens('valid'); |
| 73 | + using output = captureOutput(); |
| 74 | + vi.mocked(mockIntegration.detectPlugin).mockResolvedValueOnce(null); |
| 75 | + |
| 76 | + await run(['warehouse', 'setup', '--warehouse-type', 'bigquery', '--ide', 'claude']); |
| 77 | + |
| 78 | + expect(output.stderr).toContain('Confidence AI plugin not installed'); |
| 79 | + expect(mockIntegration.launchChat).not.toHaveBeenCalled(); |
| 80 | + }); |
| 81 | + |
| 82 | + it('fails when MCP servers are not installed', async () => { |
| 83 | + using _auth = prepareAuthTokens('valid'); |
| 84 | + using output = captureOutput(); |
| 85 | + vi.mocked(mockIntegration.detectMcpStatuses).mockResolvedValueOnce({ |
| 86 | + 'confidence-flags': 'not-installed', |
| 87 | + 'confidence-docs': 'not-installed', |
| 88 | + }); |
| 89 | + |
| 90 | + await run(['warehouse', 'setup', '--warehouse-type', 'bigquery', '--ide', 'claude']); |
| 91 | + |
| 92 | + expect(output.stderr).toContain('MCP servers not installed'); |
| 93 | + expect(mockIntegration.launchChat).not.toHaveBeenCalled(); |
| 94 | + }); |
| 95 | + |
| 96 | + it('fails when MCP auth is expired', async () => { |
| 97 | + using _auth = prepareAuthTokens('valid'); |
| 98 | + using output = captureOutput(); |
| 99 | + vi.mocked(mockIntegration.detectMcpStatuses).mockResolvedValueOnce({ |
| 100 | + 'confidence-flags': 'auth-expired', |
| 101 | + 'confidence-docs': 'connected', |
| 102 | + }); |
| 103 | + |
| 104 | + await run(['warehouse', 'setup', '--warehouse-type', 'bigquery', '--ide', 'claude']); |
| 105 | + |
| 106 | + expect(output.stderr).toContain('MCP server auth expired'); |
| 107 | + expect(mockIntegration.launchChat).not.toHaveBeenCalled(); |
| 108 | + }); |
| 109 | + |
| 110 | + it('fails when authentication is unavailable', async () => { |
| 111 | + using _auth = prepareAuthTokens('none'); |
| 112 | + using output = captureOutput(); |
| 113 | + mockAuthenticate.mockRejectedValueOnce(new Error('Authentication timed out')); |
| 114 | + |
| 115 | + await run(['warehouse', 'setup', '--warehouse-type', 'bigquery', '--ide', 'claude']); |
| 116 | + |
| 117 | + expect(output.stderr).toContain('Authentication failed'); |
| 118 | + expect(mockIntegration.launchChat).not.toHaveBeenCalled(); |
| 119 | + }); |
| 120 | +}); |
0 commit comments