import { ConnectionTestService } from './connection-test.service'; import { RequestUser } from 'src/decorators/user.decorator'; describe('ConnectionTestService catalog cache', () => { const user: RequestUser = { user_id: 'user-id', username: 'user@example.com', permissions: [], customer_id: 'customer-id', customer_name: 'customer-name', customer_tier: 'standard', access_token: 'token', customer_modules: [], roles: [], }; const grpcClient = { getService: jest.fn().mockReturnValue({}) }; const connectionsService = {}; const connectionsApiService = { proxy: jest.fn() }; const platformApiService = { proxy: jest.fn() }; let service: ConnectionTestService; beforeEach(() => { jest.clearAllMocks(); service = new ConnectionTestService( grpcClient as any, connectionsService as any, connectionsApiService as any, platformApiService as any, ); }); it('keeps the existing schemas response contract', async () => { connectionsApiService.proxy.mockResolvedValue({ schemas: [{ schema_name: 'analytics' }, { schema_name: 'public' }], }); await expect( service.connectionTestListSchemas( { connection_id: 'config-id', plugin: 'postgresql' }, user, ), ).resolves.toEqual({ operation_result: true, schema_list: ['analytics', 'public'], }); }); it('keeps the existing tables response contract', async () => { connectionsApiService.proxy.mockResolvedValue({ tables: [{ table_name: 'customers' }, { table_name: 'orders' }], }); await expect( service.connectionTestListTables( { connection_id: 'config-id', plugin: 'postgresql', schema: 'public', }, user, ), ).resolves.toEqual({ operation_result: true, table_list: ['customers', 'orders'], }); }); it('maps cached columns to the existing table metadata contract', async () => { connectionsApiService.proxy.mockResolvedValue({ columns: [ { column_name: 'id', data_type: 'bigint', is_primary_key: true, }, ], }); await expect( service.getTableMetadata( { connection_id: 'config-id', plugin: 'postgresql', schema: 'public', table_list: ['customers'], }, user, ), ).resolves.toEqual({ operation_result: true, tables_metadata: [ { table_name: 'customers', columns: [ { name: 'id', type: 'bigint', is_primary_key: true, }, ], references: [], }, ], }); expect(connectionsApiService.proxy).toHaveBeenCalledWith( 'GET', '/connection_catalog/config-id/schemas/public/tables/customers/columns', user, ); }); it('submits a catalog refresh without holding the request open', async () => { platformApiService.proxy.mockResolvedValue({ session_id: 'session-id', date: '20260731', }); await expect( service.refreshCatalog( { connection_id: 'config-id', plugin: 'postgresql' }, user, ), ).resolves.toEqual({ operation_result: true, status: 'PENDING', session_id: 'session-id', date: '20260731', }); expect(platformApiService.proxy).toHaveBeenCalledWith( 'POST', '/connection_test', user, { customer_id: user.customer_name, plugin: 'postgresql', task: { task_type: 'refresh_catalog', connection: { provider: 'connection_manager', config_id: 'config-id', }, }, }, ); }); it('keeps polling without changing the catalog pointer while pending', async () => { platformApiService.proxy.mockResolvedValue({ status: 'PENDING' }); await expect( service.refreshCatalogStatus( { connection_id: 'config-id', plugin: 'postgresql', session_id: 'session-id', date: '20260731', }, user, ), ).resolves.toEqual({ operation_result: false, status: 'PENDING', session_id: 'session-id', date: '20260731', }); expect(connectionsApiService.proxy).not.toHaveBeenCalled(); }); it('publishes the catalog pointer after the refresh finishes', async () => { platformApiService.proxy.mockResolvedValue({ status: 'DONE' }); connectionsApiService.proxy.mockResolvedValue({ last_catalog_refresh_status: 'SUCCESS', }); await expect( service.refreshCatalogStatus( { connection_id: 'config/id', plugin: 'postgresql', session_id: 'session-id', date: '20260731', }, user, ), ).resolves.toEqual({ operation_result: true, status: 'DONE', session_id: 'session-id', date: '20260731', }); expect(connectionsApiService.proxy).toHaveBeenCalledWith( 'PUT', '/connection_config/config%2Fid/catalog_metadata', user, { last_catalog_refresh_status: 'SUCCESS', last_catalog_connection_test_date: '20260731', last_catalog_connection_test_session_id: 'session-id', }, ); }); });