|
| 1 | +import { Test, TestingModule } from '@nestjs/testing'; |
| 2 | +import { HttpException } from '@nestjs/common'; |
| 3 | +import { DynamicIntegrationsController } from './dynamic-integrations.controller'; |
| 4 | +import { InternalTokenGuard } from '../../auth/internal-token.guard'; |
| 5 | +import { DynamicIntegrationRepository } from '../repositories/dynamic-integration.repository'; |
| 6 | +import { DynamicCheckRepository } from '../repositories/dynamic-check.repository'; |
| 7 | +import { ProviderRepository } from '../repositories/provider.repository'; |
| 8 | +import { CheckRunRepository } from '../repositories/check-run.repository'; |
| 9 | +import { DynamicManifestLoaderService } from '../services/dynamic-manifest-loader.service'; |
| 10 | + |
| 11 | +jest.mock('@db', () => ({ db: {} })); |
| 12 | +jest.mock('@trycompai/integration-platform', () => ({ |
| 13 | + validateIntegrationDefinition: jest.fn(), |
| 14 | + SyncDefinitionSchema: { parse: jest.fn() }, |
| 15 | +})); |
| 16 | + |
| 17 | +describe('DynamicIntegrationsController — check versioning', () => { |
| 18 | + let controller: DynamicIntegrationsController; |
| 19 | + |
| 20 | + const INTEGRATION_ID = 'din_abc'; |
| 21 | + const CHECK_ID = 'dck_abc'; |
| 22 | + |
| 23 | + const oldCheck = { |
| 24 | + id: CHECK_ID, |
| 25 | + integrationId: INTEGRATION_ID, |
| 26 | + definition: { steps: [{ type: 'code', code: 'OLD' }] }, |
| 27 | + variables: [{ key: 'region' }], |
| 28 | + }; |
| 29 | + |
| 30 | + const dynamicCheckRepo = { |
| 31 | + findById: jest.fn(), |
| 32 | + update: jest.fn(), |
| 33 | + recordVersion: jest.fn(), |
| 34 | + listVersions: jest.fn(), |
| 35 | + findVersionById: jest.fn(), |
| 36 | + }; |
| 37 | + const loaderService = { invalidateCache: jest.fn() }; |
| 38 | + |
| 39 | + beforeEach(async () => { |
| 40 | + jest.clearAllMocks(); |
| 41 | + dynamicCheckRepo.findById.mockResolvedValue(oldCheck); |
| 42 | + dynamicCheckRepo.update.mockResolvedValue(oldCheck); |
| 43 | + dynamicCheckRepo.recordVersion.mockResolvedValue({ id: 'dckv_1' }); |
| 44 | + loaderService.invalidateCache.mockResolvedValue(undefined); |
| 45 | + |
| 46 | + const module: TestingModule = await Test.createTestingModule({ |
| 47 | + controllers: [DynamicIntegrationsController], |
| 48 | + providers: [ |
| 49 | + { provide: DynamicIntegrationRepository, useValue: {} }, |
| 50 | + { provide: DynamicCheckRepository, useValue: dynamicCheckRepo }, |
| 51 | + { provide: ProviderRepository, useValue: {} }, |
| 52 | + { provide: CheckRunRepository, useValue: {} }, |
| 53 | + { provide: DynamicManifestLoaderService, useValue: loaderService }, |
| 54 | + ], |
| 55 | + }) |
| 56 | + .overrideGuard(InternalTokenGuard) |
| 57 | + .useValue({ canActivate: () => true }) |
| 58 | + .compile(); |
| 59 | + |
| 60 | + controller = module.get(DynamicIntegrationsController); |
| 61 | + }); |
| 62 | + |
| 63 | + describe('updateCheck', () => { |
| 64 | + it('snapshots the OLD logic before applying the update', async () => { |
| 65 | + await controller.updateCheck(INTEGRATION_ID, CHECK_ID, { |
| 66 | + definition: { steps: [{ type: 'code', code: 'NEW' }] }, |
| 67 | + source: 'agent', |
| 68 | + note: 'fix project-scoped keys', |
| 69 | + }); |
| 70 | + |
| 71 | + // version captured the pre-change definition/variables |
| 72 | + expect(dynamicCheckRepo.recordVersion).toHaveBeenCalledWith({ |
| 73 | + checkId: CHECK_ID, |
| 74 | + definition: oldCheck.definition, |
| 75 | + variables: oldCheck.variables, |
| 76 | + source: 'agent', |
| 77 | + note: 'fix project-scoped keys', |
| 78 | + }); |
| 79 | + // version recorded BEFORE the update |
| 80 | + const verOrder = |
| 81 | + dynamicCheckRepo.recordVersion.mock.invocationCallOrder[0]; |
| 82 | + const updOrder = dynamicCheckRepo.update.mock.invocationCallOrder[0]; |
| 83 | + expect(verOrder).toBeLessThan(updOrder); |
| 84 | + }); |
| 85 | + |
| 86 | + it('strips source/note from the data forwarded to the check update', async () => { |
| 87 | + await controller.updateCheck(INTEGRATION_ID, CHECK_ID, { |
| 88 | + definition: { steps: [] }, |
| 89 | + source: 'agent', |
| 90 | + note: 'x', |
| 91 | + }); |
| 92 | + expect(dynamicCheckRepo.update).toHaveBeenCalledWith(CHECK_ID, { |
| 93 | + definition: { steps: [] }, |
| 94 | + }); |
| 95 | + }); |
| 96 | + |
| 97 | + it('still applies the update if versioning fails (best-effort)', async () => { |
| 98 | + dynamicCheckRepo.recordVersion.mockRejectedValueOnce(new Error('boom')); |
| 99 | + const res = await controller.updateCheck(INTEGRATION_ID, CHECK_ID, { |
| 100 | + definition: { steps: [] }, |
| 101 | + }); |
| 102 | + expect(res).toEqual({ success: true }); |
| 103 | + expect(dynamicCheckRepo.update).toHaveBeenCalled(); |
| 104 | + }); |
| 105 | + |
| 106 | + it('404s when the check does not belong to the integration', async () => { |
| 107 | + dynamicCheckRepo.findById.mockResolvedValueOnce({ |
| 108 | + ...oldCheck, |
| 109 | + integrationId: 'din_other', |
| 110 | + }); |
| 111 | + await expect( |
| 112 | + controller.updateCheck(INTEGRATION_ID, CHECK_ID, {}), |
| 113 | + ).rejects.toBeInstanceOf(HttpException); |
| 114 | + expect(dynamicCheckRepo.update).not.toHaveBeenCalled(); |
| 115 | + }); |
| 116 | + }); |
| 117 | + |
| 118 | + describe('restoreCheckVersion', () => { |
| 119 | + const version = { |
| 120 | + id: 'dckv_1', |
| 121 | + checkId: CHECK_ID, |
| 122 | + definition: { steps: [{ type: 'code', code: 'RESTORED' }] }, |
| 123 | + variables: [], |
| 124 | + }; |
| 125 | + |
| 126 | + it('snapshots current state, then restores the version logic', async () => { |
| 127 | + dynamicCheckRepo.findVersionById.mockResolvedValue(version); |
| 128 | + |
| 129 | + const res = await controller.restoreCheckVersion( |
| 130 | + INTEGRATION_ID, |
| 131 | + CHECK_ID, |
| 132 | + 'dckv_1', |
| 133 | + ); |
| 134 | + |
| 135 | + expect(dynamicCheckRepo.recordVersion).toHaveBeenCalledWith( |
| 136 | + expect.objectContaining({ checkId: CHECK_ID, source: 'restore' }), |
| 137 | + ); |
| 138 | + expect(dynamicCheckRepo.update).toHaveBeenCalledWith(CHECK_ID, { |
| 139 | + definition: version.definition, |
| 140 | + variables: version.variables, |
| 141 | + }); |
| 142 | + expect(res).toEqual({ success: true, restoredFrom: 'dckv_1' }); |
| 143 | + }); |
| 144 | + |
| 145 | + it('404s when the version belongs to a different check', async () => { |
| 146 | + dynamicCheckRepo.findVersionById.mockResolvedValue({ |
| 147 | + ...version, |
| 148 | + checkId: 'dck_other', |
| 149 | + }); |
| 150 | + await expect( |
| 151 | + controller.restoreCheckVersion(INTEGRATION_ID, CHECK_ID, 'dckv_1'), |
| 152 | + ).rejects.toBeInstanceOf(HttpException); |
| 153 | + expect(dynamicCheckRepo.update).not.toHaveBeenCalled(); |
| 154 | + }); |
| 155 | + }); |
| 156 | + |
| 157 | + describe('listCheckVersions', () => { |
| 158 | + it('returns the version history for the check', async () => { |
| 159 | + dynamicCheckRepo.listVersions.mockResolvedValue([{ id: 'dckv_1' }]); |
| 160 | + const res = await controller.listCheckVersions(INTEGRATION_ID, CHECK_ID); |
| 161 | + expect(res).toEqual({ versions: [{ id: 'dckv_1' }] }); |
| 162 | + }); |
| 163 | + }); |
| 164 | +}); |
0 commit comments