|
| 1 | +import path from 'path'; |
| 2 | +import { DestroyOrchestrator } from '../../../src/orchestrators/destroy.js'; |
| 3 | + |
| 4 | +import { describe, it, vi, afterEach, expect } from 'vitest'; |
| 5 | +import { MockOs } from '../mocks/system.js'; |
| 6 | +import { Plan } from '../../../src/entities/plan.js'; |
| 7 | +import { ResourceOperation } from 'codify-schemas'; |
| 8 | +import { MockReporter } from '../mocks/reporter.js'; |
| 9 | +import { ApplyOrchestrator } from '../../../src/orchestrators/apply'; |
| 10 | + |
| 11 | +vi.mock('../../../src/plugins/plugin.js', async () => { |
| 12 | + const { MockPlugin } = await import('../mocks/plugin.js'); |
| 13 | + return { Plugin: MockPlugin }; |
| 14 | +}) |
| 15 | + |
| 16 | +// The apply orchestrator directly calls plan so this will test both |
| 17 | +describe('Apply and plan orchestrator tests', () => { |
| 18 | + it('Can apply a resource (create)', async () => { |
| 19 | + const reporter = new MockReporter({ |
| 20 | + validatePlan(plan: Plan) { |
| 21 | + // Xcode-tools will always show up in the plan |
| 22 | + expect(plan.getResourcePlan('xcode-tools')).toMatchObject({ |
| 23 | + operation: ResourceOperation.NOOP |
| 24 | + }) |
| 25 | + expect(plan.getResourcePlan('mock')).toMatchObject({ |
| 26 | + operation: ResourceOperation.CREATE, |
| 27 | + }); |
| 28 | + }, |
| 29 | + promptApplyConfirmation(): boolean { |
| 30 | + return true; |
| 31 | + } |
| 32 | + }); |
| 33 | + |
| 34 | + const applyConfirmationSpy = vi.spyOn(reporter, 'promptConfirmation'); |
| 35 | + const applyCompleteSpy = vi.spyOn(reporter, 'displayApplyComplete'); |
| 36 | + |
| 37 | + console.log(MockOs.get('xcode-tools')) |
| 38 | + expect(MockOs.get('mock')).to.be.undefined; |
| 39 | + |
| 40 | + await ApplyOrchestrator.run({ |
| 41 | + path: path.join(__dirname, 'create.codify.json') |
| 42 | + }, reporter) |
| 43 | + |
| 44 | + expect(applyConfirmationSpy).toHaveBeenCalledOnce(); |
| 45 | + expect(applyCompleteSpy).toHaveBeenCalledOnce(); |
| 46 | + |
| 47 | + // This is two because the system by default has xcode-tools installed |
| 48 | + // Codify is designed to always install xcode-tools regardless since a lot of the commands depends on it. |
| 49 | + expect(MockOs.getAll().size).to.eq(2); |
| 50 | + |
| 51 | + // These values are form the create.codify.json file. Check that they were applied to the system |
| 52 | + expect(MockOs.get('mock')).toMatchObject({ |
| 53 | + propA: 'abc', |
| 54 | + propB: 123, |
| 55 | + directory: '/home' |
| 56 | + }) |
| 57 | + }); |
| 58 | + |
| 59 | + it('Installs xcode-tools if it doesnt exist', async () => { |
| 60 | + const reporter = new MockReporter({ |
| 61 | + validatePlan(plan: Plan) { |
| 62 | + expect(plan.getResourcePlan('xcode-tools')).toMatchObject({ |
| 63 | + operation: ResourceOperation.CREATE, |
| 64 | + }); |
| 65 | + }, |
| 66 | + promptApplyConfirmation(): boolean { |
| 67 | + return true; |
| 68 | + } |
| 69 | + }); |
| 70 | + |
| 71 | + const applyConfirmationSpy = vi.spyOn(reporter, 'promptConfirmation'); |
| 72 | + const applyCompleteSpy = vi.spyOn(reporter, 'displayApplyComplete'); |
| 73 | + |
| 74 | + MockOs.destroy('xcode-tools'); |
| 75 | + expect(MockOs.get('xcode-tools')).to.be.undefined; |
| 76 | + |
| 77 | + await ApplyOrchestrator.run({ |
| 78 | + path: path.join(__dirname, 'xcode-tools.codify.json') |
| 79 | + }, reporter) |
| 80 | + |
| 81 | + expect(applyConfirmationSpy).toHaveBeenCalledOnce(); |
| 82 | + expect(applyCompleteSpy).toHaveBeenCalledOnce(); |
| 83 | + |
| 84 | + // This is two because the system by default has xcode-tools installed |
| 85 | + // Codify is designed to always install xcode-tools regardless since a lot of the commands depends on it. |
| 86 | + expect(MockOs.getAll().size).to.eq(1); |
| 87 | + |
| 88 | + // These values are form the codify.json file. Check that they were applied to the system |
| 89 | + expect(MockOs.get('xcode-tools')).toMatchObject({}) |
| 90 | + }); |
| 91 | + |
| 92 | + it('Can apply a resource (recreate)', async () => { |
| 93 | + const reporter = new MockReporter({ |
| 94 | + validatePlan(plan: Plan) { |
| 95 | + // As always these values are from recreate.codify.json |
| 96 | + expect(plan.getResourcePlan('mock')).toMatchObject({ |
| 97 | + operation: ResourceOperation.RECREATE, |
| 98 | + parameters: expect.arrayContaining([ |
| 99 | + expect.objectContaining({ |
| 100 | + name: 'propA', |
| 101 | + previousValue: 'current', |
| 102 | + newValue: 'newPropA', |
| 103 | + operation: 'modify' |
| 104 | + }), |
| 105 | + expect.objectContaining({ |
| 106 | + name: 'propB', |
| 107 | + previousValue: 1, |
| 108 | + newValue: 0, |
| 109 | + operation: 'modify' |
| 110 | + }), |
| 111 | + // Special array filtering logic is happening here. Even though we requested ['a', 'b'] and the system has |
| 112 | + // ['a', 'b', 'c'], we won't try to delete the additional elements. |
| 113 | + expect.objectContaining({ |
| 114 | + name: 'array', |
| 115 | + previousValue: expect.arrayContaining(['a', 'b']), |
| 116 | + newValue: expect.arrayContaining(['a', 'b']), |
| 117 | + operation: 'noop' |
| 118 | + }), |
| 119 | + expect.objectContaining({ |
| 120 | + name: 'directory', |
| 121 | + previousValue: '~/home', |
| 122 | + newValue: '/home', |
| 123 | + operation: 'modify' |
| 124 | + }) |
| 125 | + ]) |
| 126 | + }); |
| 127 | + } |
| 128 | + }); |
| 129 | + |
| 130 | + MockOs.create('mock', { |
| 131 | + propA: 'current', |
| 132 | + propB: 1, |
| 133 | + array: ['a', 'b', 'c'], |
| 134 | + directory: '~/home' |
| 135 | + }) |
| 136 | + |
| 137 | + await ApplyOrchestrator.run({ |
| 138 | + path: path.join(__dirname, 'recreate.codify.json') |
| 139 | + }, reporter) |
| 140 | + |
| 141 | + expect(MockOs.get('mock')).to.toMatchObject({ |
| 142 | + propA: 'newPropA', |
| 143 | + propB: 0, |
| 144 | + array: ['a', 'b'], |
| 145 | + directory: '/home' |
| 146 | + }) |
| 147 | + }); |
| 148 | + |
| 149 | + // Very similar to the re-create test except that this time only the array is changed (which is modifiable) |
| 150 | + it('Can apply a resource (modify)', async () => { |
| 151 | + const reporter = new MockReporter({ |
| 152 | + validatePlan(plan: Plan) { |
| 153 | + // As always these values are from modfiy.codify.json |
| 154 | + expect(plan.getResourcePlan('mock')).toMatchObject({ |
| 155 | + operation: ResourceOperation.MODIFY, |
| 156 | + parameters: expect.arrayContaining([ |
| 157 | + expect.objectContaining({ |
| 158 | + name: 'propA', |
| 159 | + previousValue: 'current', |
| 160 | + newValue: 'current', |
| 161 | + operation: 'noop' |
| 162 | + }), |
| 163 | + expect.objectContaining({ |
| 164 | + name: 'propB', |
| 165 | + previousValue: 1, |
| 166 | + newValue: 1, |
| 167 | + operation: 'noop' |
| 168 | + }), |
| 169 | + // Special array filtering logic is happening here. Even though we requested ['a', 'b'] and the system has |
| 170 | + // ['a', 'b', 'c'], we won't try to delete the additional elements. |
| 171 | + expect.objectContaining({ |
| 172 | + name: 'array', |
| 173 | + previousValue: expect.arrayContaining(['a', 'b', 'c']), |
| 174 | + newValue: expect.arrayContaining(['a', 'b', 'c', 'd']), |
| 175 | + operation: 'modify' |
| 176 | + }), |
| 177 | + expect.objectContaining({ |
| 178 | + name: 'directory', |
| 179 | + previousValue: '/home', |
| 180 | + newValue: '/home', |
| 181 | + operation: 'noop' |
| 182 | + }) |
| 183 | + ]) |
| 184 | + }); |
| 185 | + } |
| 186 | + }); |
| 187 | + |
| 188 | + MockOs.create('mock', { |
| 189 | + propA: 'current', |
| 190 | + propB: 1, |
| 191 | + array: ['a', 'b', 'c'], |
| 192 | + directory: '/home' |
| 193 | + }) |
| 194 | + |
| 195 | + await ApplyOrchestrator.run({ |
| 196 | + path: path.join(__dirname, 'modify.codify.json') |
| 197 | + }, reporter) |
| 198 | + |
| 199 | + expect(MockOs.get('mock')).to.toMatchObject({ |
| 200 | + propA: 'current', |
| 201 | + propB: 1, |
| 202 | + array: ['a', 'b', 'c', 'd'], |
| 203 | + directory: '/home' |
| 204 | + }) |
| 205 | + }); |
| 206 | + |
| 207 | + afterEach(() => { |
| 208 | + vi.resetAllMocks(); |
| 209 | + MockOs.reset(); |
| 210 | + }) |
| 211 | + |
| 212 | +}) |
0 commit comments