|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import { Plan } from './plan.js'; |
| 3 | +import { TestResource } from './resource.test.js'; |
| 4 | +import { ParameterOperation, ResourceOperation } from 'codify-schemas'; |
| 5 | +import { Resource } from './resource.js'; |
| 6 | + |
| 7 | +describe('Plan entity tests', () => { |
| 8 | + it('Adds default values properly when plan is parsed from request (Create)', () => { |
| 9 | + const resource = createResource(); |
| 10 | + |
| 11 | + const plan = Plan.fromResponse({ |
| 12 | + operation: ResourceOperation.CREATE, |
| 13 | + resourceType: 'type', |
| 14 | + parameters: [{ |
| 15 | + name: 'propB', |
| 16 | + operation: ParameterOperation.ADD, |
| 17 | + previousValue: null, |
| 18 | + newValue: 'propBValue' |
| 19 | + }] |
| 20 | + }, resource.defaultValues); |
| 21 | + |
| 22 | + expect(plan.currentConfig).toMatchObject({ |
| 23 | + type: 'type', |
| 24 | + propA: null, |
| 25 | + propB: null, |
| 26 | + }) |
| 27 | + |
| 28 | + expect(plan.desiredConfig).toMatchObject({ |
| 29 | + type: 'type', |
| 30 | + propA: 'defaultA', |
| 31 | + propB: 'propBValue', |
| 32 | + }) |
| 33 | + |
| 34 | + expect(plan.changeSet.parameterChanges |
| 35 | + .every((pc) => pc.operation === ParameterOperation.ADD) |
| 36 | + ).to.be.true; |
| 37 | + }) |
| 38 | + |
| 39 | + it('Adds default values properly when plan is parsed from request (Destroy)', () => { |
| 40 | + const resource = createResource(); |
| 41 | + |
| 42 | + const plan = Plan.fromResponse({ |
| 43 | + operation: ResourceOperation.DESTROY, |
| 44 | + resourceType: 'type', |
| 45 | + parameters: [{ |
| 46 | + name: 'propB', |
| 47 | + operation: ParameterOperation.REMOVE, |
| 48 | + previousValue: 'propBValue', |
| 49 | + newValue: null, |
| 50 | + }] |
| 51 | + }, resource.defaultValues); |
| 52 | + |
| 53 | + expect(plan.currentConfig).toMatchObject({ |
| 54 | + type: 'type', |
| 55 | + propA: 'defaultA', |
| 56 | + propB: 'propBValue', |
| 57 | + }) |
| 58 | + |
| 59 | + expect(plan.desiredConfig).toMatchObject({ |
| 60 | + type: 'type', |
| 61 | + propA: null, |
| 62 | + propB: null, |
| 63 | + }) |
| 64 | + |
| 65 | + expect(plan.changeSet.parameterChanges |
| 66 | + .every((pc) => pc.operation === ParameterOperation.REMOVE) |
| 67 | + ).to.be.true; |
| 68 | + }) |
| 69 | + |
| 70 | + it('Adds default values properly when plan is parsed from request (No-op)', () => { |
| 71 | + const resource = createResource(); |
| 72 | + |
| 73 | + const plan = Plan.fromResponse({ |
| 74 | + operation: ResourceOperation.NOOP, |
| 75 | + resourceType: 'type', |
| 76 | + parameters: [{ |
| 77 | + name: 'propB', |
| 78 | + operation: ParameterOperation.NOOP, |
| 79 | + previousValue: 'propBValue', |
| 80 | + newValue: 'propBValue', |
| 81 | + }] |
| 82 | + }, resource.defaultValues); |
| 83 | + |
| 84 | + expect(plan.currentConfig).toMatchObject({ |
| 85 | + type: 'type', |
| 86 | + propA: 'defaultA', |
| 87 | + propB: 'propBValue', |
| 88 | + }) |
| 89 | + |
| 90 | + expect(plan.desiredConfig).toMatchObject({ |
| 91 | + type: 'type', |
| 92 | + propA: 'defaultA', |
| 93 | + propB: 'propBValue', |
| 94 | + }) |
| 95 | + |
| 96 | + expect(plan.changeSet.parameterChanges |
| 97 | + .every((pc) => pc.operation === ParameterOperation.NOOP) |
| 98 | + ).to.be.true; |
| 99 | + }) |
| 100 | + |
| 101 | + it('Does not add default value if a value has already been specified', () => { |
| 102 | + const resource = createResource(); |
| 103 | + |
| 104 | + const plan = Plan.fromResponse({ |
| 105 | + operation: ResourceOperation.CREATE, |
| 106 | + resourceType: 'type', |
| 107 | + parameters: [{ |
| 108 | + name: 'propB', |
| 109 | + operation: ParameterOperation.ADD, |
| 110 | + previousValue: null, |
| 111 | + newValue: 'propBValue', |
| 112 | + }, { |
| 113 | + name: 'propA', |
| 114 | + operation: ParameterOperation.ADD, |
| 115 | + previousValue: null, |
| 116 | + newValue: 'propAValue', |
| 117 | + }] |
| 118 | + }, resource.defaultValues); |
| 119 | + |
| 120 | + expect(plan.currentConfig).toMatchObject({ |
| 121 | + type: 'type', |
| 122 | + propA: null, |
| 123 | + propB: null, |
| 124 | + }) |
| 125 | + |
| 126 | + expect(plan.desiredConfig).toMatchObject({ |
| 127 | + type: 'type', |
| 128 | + propA: 'propAValue', |
| 129 | + propB: 'propBValue', |
| 130 | + }) |
| 131 | + |
| 132 | + expect(plan.changeSet.parameterChanges |
| 133 | + .every((pc) => pc.operation === ParameterOperation.ADD) |
| 134 | + ).to.be.true; |
| 135 | + }) |
| 136 | +}) |
| 137 | + |
| 138 | +function createResource(): Resource<any> { |
| 139 | + return new class extends TestResource { |
| 140 | + constructor() { |
| 141 | + super({ |
| 142 | + type: 'type', |
| 143 | + parameterConfigurations: { |
| 144 | + propA: { |
| 145 | + defaultValue: 'defaultA' |
| 146 | + } |
| 147 | + } |
| 148 | + }); |
| 149 | + } |
| 150 | + } |
| 151 | +} |
0 commit comments