|
| 1 | +import { DocumentIdentifier } from '@aws-sdk/client-ssm'; |
| 2 | + |
| 3 | +const getSsmDocuments = jest.fn(); |
| 4 | +jest.mock('../../../src/lib/aws/ssm/get-ssm-documents', () => ({ |
| 5 | + getSsmDocuments: (...args: any[]) => getSsmDocuments(...args), |
| 6 | +})); |
| 7 | + |
| 8 | +import type { PropertyContext } from '@openops/blocks-framework'; |
| 9 | +import { runbookNameProperty } from '../../../src/lib/aws/ssm/runbook-name-property'; |
| 10 | + |
| 11 | +describe('runbookNameProperty.options', () => { |
| 12 | + const ctx = {} as PropertyContext; |
| 13 | + beforeEach(() => { |
| 14 | + jest.clearAllMocks(); |
| 15 | + }); |
| 16 | + |
| 17 | + test('returns disabled with placeholder when auth is missing', async () => { |
| 18 | + const res = await runbookNameProperty.options( |
| 19 | + { auth: undefined, owner: undefined, region: 'us-east-1' }, |
| 20 | + ctx, |
| 21 | + ); |
| 22 | + |
| 23 | + expect(res).toEqual({ |
| 24 | + disabled: true, |
| 25 | + options: [], |
| 26 | + placeholder: 'Please authenticate first', |
| 27 | + }); |
| 28 | + |
| 29 | + expect(getSsmDocuments).not.toHaveBeenCalled(); |
| 30 | + }); |
| 31 | + |
| 32 | + test('returns disabled with placeholder when region is missing and no defaultRegion', async () => { |
| 33 | + const auth = {}; |
| 34 | + |
| 35 | + const res = await runbookNameProperty.options( |
| 36 | + { auth, owner: undefined, region: undefined }, |
| 37 | + ctx, |
| 38 | + ); |
| 39 | + |
| 40 | + expect(res).toEqual({ |
| 41 | + disabled: true, |
| 42 | + options: [], |
| 43 | + placeholder: 'Please provide a region', |
| 44 | + }); |
| 45 | + |
| 46 | + expect(getSsmDocuments).not.toHaveBeenCalled(); |
| 47 | + }); |
| 48 | + |
| 49 | + test('uses provided region over auth.defaultRegion and maps docs to options', async () => { |
| 50 | + const auth = { defaultRegion: 'us-west-2' }; |
| 51 | + const owner = 'Self'; |
| 52 | + const region = 'us-east-1'; |
| 53 | + |
| 54 | + const docs: Partial<DocumentIdentifier>[] = [ |
| 55 | + { Name: 'AWS-RunShellScript' }, |
| 56 | + { Name: 'MyCustomRunbook' }, |
| 57 | + ]; |
| 58 | + getSsmDocuments.mockResolvedValueOnce(docs); |
| 59 | + |
| 60 | + const res = await runbookNameProperty.options({ auth, owner, region }, ctx); |
| 61 | + |
| 62 | + expect(getSsmDocuments).toHaveBeenCalledTimes(1); |
| 63 | + expect(getSsmDocuments).toHaveBeenCalledWith({ |
| 64 | + auth, |
| 65 | + region, |
| 66 | + owner, |
| 67 | + }); |
| 68 | + |
| 69 | + expect(res).toEqual({ |
| 70 | + disabled: false, |
| 71 | + options: [ |
| 72 | + { label: 'AWS-RunShellScript', value: 'AWS-RunShellScript' }, |
| 73 | + { label: 'MyCustomRunbook', value: 'MyCustomRunbook' }, |
| 74 | + ], |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + test('uses auth.defaultRegion when region arg is missing', async () => { |
| 79 | + const auth = { defaultRegion: 'eu-central-1' }; |
| 80 | + const owner = 'Amazon'; |
| 81 | + |
| 82 | + getSsmDocuments.mockResolvedValueOnce([]); |
| 83 | + |
| 84 | + const res = await runbookNameProperty.options( |
| 85 | + { auth, owner, region: undefined }, |
| 86 | + ctx, |
| 87 | + ); |
| 88 | + |
| 89 | + expect(getSsmDocuments).toHaveBeenCalledWith({ |
| 90 | + auth, |
| 91 | + region: 'eu-central-1', |
| 92 | + owner, |
| 93 | + }); |
| 94 | + expect(res).toEqual({ disabled: false, options: [] }); |
| 95 | + }); |
| 96 | + |
| 97 | + test('returns disabled with error and placeholder when getSsmDocuments throws', async () => { |
| 98 | + const auth = { defaultRegion: 'eu-west-1' }; |
| 99 | + const err = new Error('boom'); |
| 100 | + getSsmDocuments.mockRejectedValueOnce(err); |
| 101 | + |
| 102 | + const res = await runbookNameProperty.options( |
| 103 | + { auth, owner: undefined, region: undefined }, |
| 104 | + ctx, |
| 105 | + ); |
| 106 | + |
| 107 | + expect(res).toEqual({ |
| 108 | + disabled: true, |
| 109 | + options: [], |
| 110 | + placeholder: 'Failed to load runbooks', |
| 111 | + error: String(err), |
| 112 | + }); |
| 113 | + }); |
| 114 | +}); |
0 commit comments