|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import { Plugin } from './plugin.js'; |
| 3 | +import { ParameterOperation, ResourceOperation, StringIndexedObject } from 'codify-schemas'; |
| 4 | +import { Resource } from './resource.js'; |
| 5 | +import { Plan } from './plan.js'; |
| 6 | +import { ValidationResult } from './resource-types.js'; |
| 7 | +import { ApplyValidationError } from './errors.js'; |
| 8 | + |
| 9 | +interface TestConfig extends StringIndexedObject { |
| 10 | + propA: string; |
| 11 | + propB: number; |
| 12 | + propC?: string; |
| 13 | +} |
| 14 | + |
| 15 | +class TestResource extends Resource<TestConfig> { |
| 16 | + constructor() { |
| 17 | + super({ |
| 18 | + type: 'testResource' |
| 19 | + }); |
| 20 | + } |
| 21 | + |
| 22 | + applyCreate(plan: Plan<TestConfig>): Promise<void> { |
| 23 | + return Promise.resolve(undefined); |
| 24 | + } |
| 25 | + |
| 26 | + applyDestroy(plan: Plan<TestConfig>): Promise<void> { |
| 27 | + return Promise.resolve(undefined); |
| 28 | + } |
| 29 | + |
| 30 | + async refresh(keys: Map<string, unknown>): Promise<Partial<TestConfig> | null> { |
| 31 | + return { |
| 32 | + propA: 'a', |
| 33 | + propB: 10, |
| 34 | + propC: 'c', |
| 35 | + }; |
| 36 | + } |
| 37 | + |
| 38 | + async validateResource(config: unknown): Promise<ValidationResult> { |
| 39 | + return { |
| 40 | + isValid: true |
| 41 | + } |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +describe('Plugin tests', () => { |
| 46 | + it('Validates that applies were successfully applied', async () => { |
| 47 | + const resource = new class extends TestResource { |
| 48 | + async applyCreate(plan: Plan<TestConfig>): Promise<void> { |
| 49 | + } |
| 50 | + |
| 51 | + // Refresh has to line up with desired for the apply to go through |
| 52 | + async refresh(keys: Map<string, unknown>): Promise<Partial<TestConfig> | null> { |
| 53 | + return { |
| 54 | + propA: 'abc' |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + const testPlugin = Plugin.create('testPlugin', [resource]) |
| 60 | + |
| 61 | + const desiredPlan = { |
| 62 | + operation: ResourceOperation.CREATE, |
| 63 | + resourceType: 'testResource', |
| 64 | + parameters: [ |
| 65 | + { name: 'propA', operation: ParameterOperation.ADD, newValue: 'abc', previousValue: null }, |
| 66 | + ] |
| 67 | + }; |
| 68 | + |
| 69 | + // If this doesn't throw then it passes the test |
| 70 | + await testPlugin.apply({ plan: desiredPlan }); |
| 71 | + }); |
| 72 | + |
| 73 | + it('Validates that applies were successfully applied (error)', async () => { |
| 74 | + const resource = new class extends TestResource { |
| 75 | + async applyCreate(plan: Plan<TestConfig>): Promise<void> { |
| 76 | + } |
| 77 | + |
| 78 | + // Return null to indicate that the resource was not created |
| 79 | + async refresh(keys: Map<string, unknown>): Promise<Partial<TestConfig> | null> { |
| 80 | + return null; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + const testPlugin = Plugin.create('testPlugin', [resource]) |
| 85 | + |
| 86 | + const desiredPlan = { |
| 87 | + operation: ResourceOperation.CREATE, |
| 88 | + resourceType: 'testResource', |
| 89 | + parameters: [ |
| 90 | + { name: 'propA', operation: ParameterOperation.ADD, newValue: 'abc', previousValue: null }, |
| 91 | + ] |
| 92 | + }; |
| 93 | + |
| 94 | + await expect(async () => testPlugin.apply({ plan: desiredPlan })).rejects.toThrowError(expect.any(ApplyValidationError)); |
| 95 | + }); |
| 96 | +}); |
0 commit comments