|
| 1 | +jest.mock('@/lib/bot', () => ({ |
| 2 | + bot: { |
| 3 | + getAdapter: jest.fn(), |
| 4 | + initialize: jest.fn(), |
| 5 | + registerSingleton: jest.fn(), |
| 6 | + }, |
| 7 | +})); |
| 8 | + |
| 9 | +import { PLATFORM } from '@/lib/integrations/core/constants'; |
| 10 | +import type { PlatformIntegration } from '@kilocode/db'; |
| 11 | +import { withBotPlatformAuthContext } from './platform-auth-context'; |
| 12 | + |
| 13 | +type MockBotModule = { |
| 14 | + bot: { |
| 15 | + getAdapter: jest.Mock; |
| 16 | + initialize: jest.Mock; |
| 17 | + registerSingleton: jest.Mock; |
| 18 | + }; |
| 19 | +}; |
| 20 | + |
| 21 | +const mockBotModule: MockBotModule = jest.requireMock('@/lib/bot'); |
| 22 | +let mockWithBotToken: jest.Mock; |
| 23 | +let mockGetInstallation: jest.Mock; |
| 24 | + |
| 25 | +function createPlatformIntegration( |
| 26 | + overrides: Partial<PlatformIntegration> = {} |
| 27 | +): PlatformIntegration { |
| 28 | + return { |
| 29 | + id: 'pi_slack', |
| 30 | + owned_by_user_id: 'user_1', |
| 31 | + owned_by_organization_id: null, |
| 32 | + created_by_user_id: null, |
| 33 | + platform: PLATFORM.SLACK, |
| 34 | + integration_type: 'oauth', |
| 35 | + platform_installation_id: 'T123', |
| 36 | + platform_account_id: 'T123', |
| 37 | + platform_account_login: 'Test Workspace', |
| 38 | + permissions: null, |
| 39 | + integration_status: 'active', |
| 40 | + scopes: null, |
| 41 | + repository_access: null, |
| 42 | + repositories: null, |
| 43 | + repositories_synced_at: null, |
| 44 | + metadata: { access_token: 'xoxb-current' }, |
| 45 | + kilo_requester_user_id: null, |
| 46 | + platform_requester_account_id: null, |
| 47 | + suspended_at: null, |
| 48 | + suspended_by: null, |
| 49 | + github_app_type: 'standard', |
| 50 | + installed_at: '2026-01-01T00:00:00.000Z', |
| 51 | + created_at: '2026-01-01T00:00:00.000Z', |
| 52 | + updated_at: '2026-01-01T00:00:00.000Z', |
| 53 | + ...overrides, |
| 54 | + }; |
| 55 | +} |
| 56 | + |
| 57 | +describe('withBotPlatformAuthContext', () => { |
| 58 | + beforeEach(() => { |
| 59 | + mockWithBotToken = jest.fn(async (_token, fn) => await fn()); |
| 60 | + mockGetInstallation = jest.fn(); |
| 61 | + |
| 62 | + mockBotModule.bot.getAdapter.mockReset(); |
| 63 | + mockBotModule.bot.initialize.mockReset(); |
| 64 | + mockBotModule.bot.registerSingleton.mockReset(); |
| 65 | + |
| 66 | + mockBotModule.bot.getAdapter.mockReturnValue({ |
| 67 | + getInstallation: mockGetInstallation, |
| 68 | + withBotToken: mockWithBotToken, |
| 69 | + }); |
| 70 | + }); |
| 71 | + |
| 72 | + it('wraps Slack callbacks with the installation bot token', async () => { |
| 73 | + const fn = jest.fn(async () => 'result'); |
| 74 | + mockGetInstallation.mockResolvedValue({ botToken: 'xoxb-stored' }); |
| 75 | + |
| 76 | + const result = await withBotPlatformAuthContext(createPlatformIntegration(), fn); |
| 77 | + |
| 78 | + expect(result).toBe('result'); |
| 79 | + expect(mockBotModule.bot.initialize).toHaveBeenCalledTimes(1); |
| 80 | + expect(mockBotModule.bot.registerSingleton).toHaveBeenCalledTimes(1); |
| 81 | + expect(mockBotModule.bot.getAdapter).toHaveBeenCalledWith(PLATFORM.SLACK); |
| 82 | + expect(mockGetInstallation).toHaveBeenCalledWith('T123'); |
| 83 | + expect(mockWithBotToken).toHaveBeenCalledWith('xoxb-stored', expect.any(Function)); |
| 84 | + expect(fn).toHaveBeenCalledTimes(1); |
| 85 | + }); |
| 86 | + |
| 87 | + it('throws when Slack installation is missing', async () => { |
| 88 | + mockGetInstallation.mockResolvedValue(null); |
| 89 | + |
| 90 | + await expect( |
| 91 | + withBotPlatformAuthContext(createPlatformIntegration({ metadata: {} }), async () => 'result') |
| 92 | + ).rejects.toThrow('No Slack installation for platform integration pi_slack'); |
| 93 | + }); |
| 94 | + |
| 95 | + it('runs non-Slack callbacks directly', async () => { |
| 96 | + const fn = jest.fn(async () => 'result'); |
| 97 | + |
| 98 | + const result = await withBotPlatformAuthContext( |
| 99 | + createPlatformIntegration({ |
| 100 | + id: 'pi_discord', |
| 101 | + platform: PLATFORM.DISCORD, |
| 102 | + metadata: {}, |
| 103 | + }), |
| 104 | + fn |
| 105 | + ); |
| 106 | + |
| 107 | + expect(result).toBe('result'); |
| 108 | + expect(fn).toHaveBeenCalledTimes(1); |
| 109 | + expect(mockBotModule.bot.getAdapter).not.toHaveBeenCalled(); |
| 110 | + expect(mockWithBotToken).not.toHaveBeenCalled(); |
| 111 | + }); |
| 112 | +}); |
0 commit comments