|
| 1 | +import { Test, TestingModule } from '@nestjs/testing'; |
| 2 | +import { SyncController } from './sync.controller'; |
| 3 | +import { HybridAuthGuard } from '../../auth/hybrid-auth.guard'; |
| 4 | +import { PermissionGuard } from '../../auth/permission.guard'; |
| 5 | +import { ConnectionRepository } from '../repositories/connection.repository'; |
| 6 | +import { CredentialVaultService } from '../services/credential-vault.service'; |
| 7 | +import { OAuthCredentialsService } from '../services/oauth-credentials.service'; |
| 8 | +import { IntegrationSyncLoggerService } from '../services/integration-sync-logger.service'; |
| 9 | +import { GenericEmployeeSyncService } from '../services/generic-employee-sync.service'; |
| 10 | +import { GenericDeviceSyncService } from '../services/generic-device-sync.service'; |
| 11 | +import { DynamicIntegrationRepository } from '../repositories/dynamic-integration.repository'; |
| 12 | +import { CheckRunRepository } from '../repositories/check-run.repository'; |
| 13 | + |
| 14 | +const mockConnectionFindMany = jest.fn(); |
| 15 | +const mockGetActiveManifests = jest.fn(); |
| 16 | + |
| 17 | +jest.mock('@db', () => ({ |
| 18 | + db: { |
| 19 | + integrationConnection: { |
| 20 | + findMany: (...args: unknown[]) => mockConnectionFindMany(...args), |
| 21 | + }, |
| 22 | + }, |
| 23 | +})); |
| 24 | + |
| 25 | +jest.mock('../../auth/auth.server', () => ({ |
| 26 | + auth: { api: { getSession: jest.fn() } }, |
| 27 | +})); |
| 28 | + |
| 29 | +jest.mock('@trycompai/auth', () => ({ |
| 30 | + statement: { integration: ['create', 'read', 'update', 'delete'] }, |
| 31 | + BUILT_IN_ROLE_PERMISSIONS: {}, |
| 32 | +})); |
| 33 | + |
| 34 | +jest.mock('@trycompai/integration-platform', () => { |
| 35 | + const actual = jest.requireActual< |
| 36 | + typeof import('@trycompai/integration-platform') |
| 37 | + >('@trycompai/integration-platform'); |
| 38 | + return { |
| 39 | + ...actual, |
| 40 | + registry: { |
| 41 | + getActiveManifests: (...args: unknown[]) => |
| 42 | + mockGetActiveManifests(...args), |
| 43 | + }, |
| 44 | + TASK_TEMPLATE_INFO: {}, |
| 45 | + }; |
| 46 | +}); |
| 47 | + |
| 48 | +describe('SyncController - getAvailableSyncProviders connection status', () => { |
| 49 | + let controller: SyncController; |
| 50 | + |
| 51 | + const orgId = 'org_test123'; |
| 52 | + const intuneManifest = { |
| 53 | + id: 'intune', |
| 54 | + name: 'Intune', |
| 55 | + logoUrl: 'https://example.com/intune.png', |
| 56 | + capabilities: ['checks', 'device_sync'], |
| 57 | + }; |
| 58 | + |
| 59 | + beforeEach(async () => { |
| 60 | + jest.clearAllMocks(); |
| 61 | + mockGetActiveManifests.mockReturnValue([intuneManifest]); |
| 62 | + |
| 63 | + const module: TestingModule = await Test.createTestingModule({ |
| 64 | + controllers: [SyncController], |
| 65 | + providers: [ |
| 66 | + { |
| 67 | + provide: ConnectionRepository, |
| 68 | + useValue: { findById: jest.fn(), update: jest.fn() }, |
| 69 | + }, |
| 70 | + { |
| 71 | + provide: CredentialVaultService, |
| 72 | + useValue: { |
| 73 | + getDecryptedCredentials: jest.fn(), |
| 74 | + refreshOAuthTokens: jest.fn(), |
| 75 | + }, |
| 76 | + }, |
| 77 | + { |
| 78 | + provide: OAuthCredentialsService, |
| 79 | + useValue: { getCredentials: jest.fn() }, |
| 80 | + }, |
| 81 | + { |
| 82 | + provide: IntegrationSyncLoggerService, |
| 83 | + useValue: { logSync: jest.fn() }, |
| 84 | + }, |
| 85 | + { provide: GenericEmployeeSyncService, useValue: {} }, |
| 86 | + { provide: GenericDeviceSyncService, useValue: {} }, |
| 87 | + { provide: DynamicIntegrationRepository, useValue: {} }, |
| 88 | + { |
| 89 | + provide: CheckRunRepository, |
| 90 | + useValue: { create: jest.fn(), complete: jest.fn() }, |
| 91 | + }, |
| 92 | + ], |
| 93 | + }) |
| 94 | + .overrideGuard(HybridAuthGuard) |
| 95 | + .useValue({ canActivate: () => true }) |
| 96 | + .overrideGuard(PermissionGuard) |
| 97 | + .useValue({ canActivate: () => true }) |
| 98 | + .compile(); |
| 99 | + |
| 100 | + controller = module.get<SyncController>(SyncController); |
| 101 | + }); |
| 102 | + |
| 103 | + it('reports connected + connectionStatus active for an active connection', async () => { |
| 104 | + mockConnectionFindMany.mockResolvedValue([ |
| 105 | + { |
| 106 | + id: 'icn_active', |
| 107 | + status: 'active', |
| 108 | + lastSyncAt: null, |
| 109 | + nextSyncAt: null, |
| 110 | + provider: { slug: 'intune' }, |
| 111 | + }, |
| 112 | + ]); |
| 113 | + |
| 114 | + const result = await controller.getAvailableSyncProviders(orgId, 'device'); |
| 115 | + |
| 116 | + expect(result.providers).toEqual([ |
| 117 | + expect.objectContaining({ |
| 118 | + slug: 'intune', |
| 119 | + connected: true, |
| 120 | + connectionStatus: 'active', |
| 121 | + connectionId: 'icn_active', |
| 122 | + }), |
| 123 | + ]); |
| 124 | + // ONE batched query for all providers — no per-provider point lookups. |
| 125 | + expect(mockConnectionFindMany).toHaveBeenCalledTimes(1); |
| 126 | + const args = mockConnectionFindMany.mock.calls[0][0] as { |
| 127 | + where: { provider: { slug: { in: string[] } } }; |
| 128 | + orderBy: { updatedAt: string }; |
| 129 | + }; |
| 130 | + expect(args.where.provider.slug.in).toEqual(['intune']); |
| 131 | + expect(args.orderBy).toEqual({ updatedAt: 'desc' }); |
| 132 | + }); |
| 133 | + |
| 134 | + it('reports connectionStatus error (not connected) when the latest connection is broken', async () => { |
| 135 | + mockConnectionFindMany.mockResolvedValue([ |
| 136 | + { id: 'icn_broken', status: 'error', lastSyncAt: null, nextSyncAt: null, provider: { slug: 'intune' } }, |
| 137 | + ]); |
| 138 | + |
| 139 | + const result = await controller.getAvailableSyncProviders(orgId, 'device'); |
| 140 | + |
| 141 | + expect(result.providers).toEqual([ |
| 142 | + expect.objectContaining({ |
| 143 | + slug: 'intune', |
| 144 | + connected: false, |
| 145 | + connectionStatus: 'error', |
| 146 | + connectionId: null, |
| 147 | + }), |
| 148 | + ]); |
| 149 | + }); |
| 150 | + |
| 151 | + it('does not resurface a stale error when the latest connection is disconnected', async () => { |
| 152 | + // Rows arrive newest-first (orderBy updatedAt desc): the user reconnected |
| 153 | + // and later disconnected, leaving an older row stuck in 'error'. |
| 154 | + mockConnectionFindMany.mockResolvedValue([ |
| 155 | + { id: 'icn_new', status: 'disconnected', lastSyncAt: null, nextSyncAt: null, provider: { slug: 'intune' } }, |
| 156 | + { id: 'icn_old', status: 'error', lastSyncAt: null, nextSyncAt: null, provider: { slug: 'intune' } }, |
| 157 | + ]); |
| 158 | + |
| 159 | + const result = await controller.getAvailableSyncProviders(orgId, 'device'); |
| 160 | + |
| 161 | + expect(result.providers).toEqual([ |
| 162 | + expect.objectContaining({ |
| 163 | + slug: 'intune', |
| 164 | + connected: false, |
| 165 | + connectionStatus: null, |
| 166 | + connectionId: null, |
| 167 | + }), |
| 168 | + ]); |
| 169 | + }); |
| 170 | + |
| 171 | + it('prefers an active connection even when a newer non-active row exists', async () => { |
| 172 | + mockConnectionFindMany.mockResolvedValue([ |
| 173 | + { id: 'icn_new_err', status: 'error', lastSyncAt: null, nextSyncAt: null, provider: { slug: 'intune' } }, |
| 174 | + { id: 'icn_active', status: 'active', lastSyncAt: null, nextSyncAt: null, provider: { slug: 'intune' } }, |
| 175 | + ]); |
| 176 | + |
| 177 | + const result = await controller.getAvailableSyncProviders(orgId, 'device'); |
| 178 | + |
| 179 | + expect(result.providers).toEqual([ |
| 180 | + expect.objectContaining({ |
| 181 | + slug: 'intune', |
| 182 | + connected: true, |
| 183 | + connectionStatus: 'active', |
| 184 | + connectionId: 'icn_active', |
| 185 | + }), |
| 186 | + ]); |
| 187 | + }); |
| 188 | + |
| 189 | + it('reports connectionStatus null when the org has no connection for the provider', async () => { |
| 190 | + mockConnectionFindMany.mockResolvedValue([]); |
| 191 | + |
| 192 | + const result = await controller.getAvailableSyncProviders(orgId, 'device'); |
| 193 | + |
| 194 | + expect(result.providers).toEqual([ |
| 195 | + expect.objectContaining({ |
| 196 | + slug: 'intune', |
| 197 | + connected: false, |
| 198 | + connectionStatus: null, |
| 199 | + connectionId: null, |
| 200 | + }), |
| 201 | + ]); |
| 202 | + }); |
| 203 | +}); |
0 commit comments