|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | + |
| 3 | +import { |
| 4 | + ControlPlaneType, |
| 5 | + ControlPlaneStatusCondition, |
| 6 | + ReadyStatus, |
| 7 | +} from '../../lib/api/types/crate/controlPlanes'; |
| 8 | +import { canConnectToMCP } from './controlPlanes'; |
| 9 | + |
| 10 | +const createCondition = ( |
| 11 | + overrides: Partial<ControlPlaneStatusCondition>, |
| 12 | +): ControlPlaneStatusCondition => ({ |
| 13 | + type: 'Unknown', |
| 14 | + status: false, |
| 15 | + reason: 'DefaultReason', |
| 16 | + message: 'Default message', |
| 17 | + lastTransitionTime: new Date().toISOString(), |
| 18 | + ...overrides, |
| 19 | +}); |
| 20 | + |
| 21 | +const createControlPlane = ( |
| 22 | + conditions: ControlPlaneStatusCondition[], |
| 23 | +): ControlPlaneType => ({ |
| 24 | + metadata: { |
| 25 | + name: '', |
| 26 | + namespace: '', |
| 27 | + }, |
| 28 | + spec: { |
| 29 | + components: { |
| 30 | + crossplane: undefined, |
| 31 | + btpServiceOperator: undefined, |
| 32 | + externalSecretsOperator: undefined, |
| 33 | + kyverno: undefined, |
| 34 | + flux: undefined, |
| 35 | + }, |
| 36 | + }, |
| 37 | + status: { |
| 38 | + conditions, |
| 39 | + status: ReadyStatus.Ready, |
| 40 | + access: undefined, |
| 41 | + }, |
| 42 | +}); |
| 43 | + |
| 44 | +const baseConditions = [ |
| 45 | + createCondition({ type: 'APIServerHealthy', status: 'True' }), |
| 46 | + createCondition({ type: 'AuthenticationHealthy', status: 'True' }), |
| 47 | + createCondition({ type: 'AuthorizationHealthy', status: 'True' }), |
| 48 | +]; |
| 49 | + |
| 50 | +describe('canConnectToMCP', () => { |
| 51 | + it('returns true when all required conditions are True', () => { |
| 52 | + const controlPlane = createControlPlane([...baseConditions]); |
| 53 | + expect(canConnectToMCP(controlPlane)).toBe(true); |
| 54 | + }); |
| 55 | + |
| 56 | + it('returns false when one required condition is missing', () => { |
| 57 | + const controlPlane = createControlPlane([ |
| 58 | + createCondition({ type: 'APIServerHealthy', status: 'True' }), |
| 59 | + createCondition({ type: 'AuthorizationHealthy', status: 'True' }), |
| 60 | + ]); |
| 61 | + expect(canConnectToMCP(controlPlane)).toBe(false); |
| 62 | + }); |
| 63 | + |
| 64 | + it('returns false when one required condition has status "False"', () => { |
| 65 | + const controlPlane = createControlPlane([ |
| 66 | + createCondition({ type: 'APIServerHealthy', status: 'True' }), |
| 67 | + createCondition({ type: 'AuthenticationHealthy', status: 'False' }), |
| 68 | + createCondition({ type: 'AuthorizationHealthy', status: 'True' }), |
| 69 | + ]); |
| 70 | + expect(canConnectToMCP(controlPlane)).toBe(false); |
| 71 | + }); |
| 72 | + |
| 73 | + it('returns true even if other conditions exist', () => { |
| 74 | + const controlPlane = createControlPlane([ |
| 75 | + ...baseConditions, |
| 76 | + createCondition({ type: 'SomethingElse', status: 'True' }), |
| 77 | + ]); |
| 78 | + expect(canConnectToMCP(controlPlane)).toBe(true); |
| 79 | + }); |
| 80 | + |
| 81 | + it('returns true when required statuses are lowercase', () => { |
| 82 | + const controlPlane = createControlPlane( |
| 83 | + baseConditions.map((c) => ({ |
| 84 | + ...c, |
| 85 | + status: 'true', |
| 86 | + })), |
| 87 | + ); |
| 88 | + expect(canConnectToMCP(controlPlane)).toBe(true); |
| 89 | + }); |
| 90 | + |
| 91 | + it('returns true when required statuses are boolean', () => { |
| 92 | + const controlPlane = createControlPlane( |
| 93 | + baseConditions.map((c) => ({ |
| 94 | + ...c, |
| 95 | + status: true, |
| 96 | + })), |
| 97 | + ); |
| 98 | + expect(canConnectToMCP(controlPlane)).toBe(true); |
| 99 | + }); |
| 100 | + |
| 101 | + it('returns false when there are no conditions', () => { |
| 102 | + const controlPlane = createControlPlane([]); |
| 103 | + expect(canConnectToMCP(controlPlane)).toBe(false); |
| 104 | + }); |
| 105 | + |
| 106 | + it('returns false when status field is missing', () => { |
| 107 | + const controlPlane = { |
| 108 | + metadata: { |
| 109 | + name: '', |
| 110 | + namespace: '', |
| 111 | + }, |
| 112 | + spec: { |
| 113 | + components: { |
| 114 | + crossplane: undefined, |
| 115 | + btpServiceOperator: undefined, |
| 116 | + externalSecretsOperator: undefined, |
| 117 | + kyverno: undefined, |
| 118 | + flux: undefined, |
| 119 | + }, |
| 120 | + }, |
| 121 | + } as ControlPlaneType; |
| 122 | + expect(canConnectToMCP(controlPlane)).toBe(false); |
| 123 | + }); |
| 124 | +}); |
0 commit comments