|
| 1 | +import { stubInterface } from 'ts-sinon'; |
1 | 2 | import { describe, it, expect, beforeEach, vi } from 'vitest'; |
2 | | -import { CancellationToken } from 'vscode-languageserver-protocol'; |
| 3 | +import { CancellationToken, RequestHandler } from 'vscode-languageserver-protocol'; |
3 | 4 | import { getEntityMap } from '../../../src/context/SectionContextBuilder'; |
| 5 | +import { CloudFormationFileType, Document } from '../../../src/document/Document'; |
4 | 6 | import { |
5 | 7 | getManagedResourceStackTemplateHandler, |
| 8 | + importResourceStateHandler, |
6 | 9 | removeResourceTypeHandler, |
7 | 10 | } from '../../../src/handlers/ResourceHandler'; |
| 11 | +import { |
| 12 | + ResourceStateParams, |
| 13 | + ResourceStatePurpose, |
| 14 | + ResourceStateResult, |
| 15 | +} from '../../../src/resourceState/ResourceStateTypes'; |
8 | 16 | import { GetStackTemplateParams } from '../../../src/stacks/StackRequestType'; |
9 | 17 | import { createMockComponents } from '../../utils/MockServerComponents'; |
10 | 18 |
|
@@ -156,3 +164,43 @@ describe('ResourceHandler - removeResourceTypeHandler', () => { |
156 | 164 | expect(() => handler(undefined as any)).toThrow(); |
157 | 165 | }); |
158 | 166 | }); |
| 167 | + |
| 168 | +describe('ResourceHandler - importResourceStateHandler', () => { |
| 169 | + let mockComponents: ReturnType<typeof createMockComponents>; |
| 170 | + let handler: RequestHandler<ResourceStateParams, ResourceStateResult, void>; |
| 171 | + const params = { |
| 172 | + textDocument: { uri: 'docUri' }, |
| 173 | + resourceSelections: [ |
| 174 | + { |
| 175 | + resourceType: 'AWS::S3::Bucket', |
| 176 | + resourceIdentifiers: ['bucket1234'], |
| 177 | + }, |
| 178 | + ], |
| 179 | + purpose: ResourceStatePurpose.IMPORT, |
| 180 | + }; |
| 181 | + |
| 182 | + beforeEach(() => { |
| 183 | + vi.clearAllMocks(); |
| 184 | + mockComponents = createMockComponents(); |
| 185 | + handler = importResourceStateHandler(mockComponents); |
| 186 | + }); |
| 187 | + |
| 188 | + it('should throw error if document not found', async () => { |
| 189 | + mockComponents.documentManager.get.returns(undefined); |
| 190 | + |
| 191 | + await expect(async () => { |
| 192 | + await handler(params, CancellationToken.None); |
| 193 | + }).rejects.toThrow('Import failed: docUri not found'); |
| 194 | + }); |
| 195 | + |
| 196 | + it('should throw error if document is not a valid CloudFormation template', async () => { |
| 197 | + const mockDoc = stubInterface<Document>(); |
| 198 | + mockDoc.isTemplate.returns(false); |
| 199 | + Object.defineProperty(mockDoc, 'cfnFileType', { value: CloudFormationFileType.Other }); |
| 200 | + mockComponents.documentManager.get.returns(mockDoc); |
| 201 | + |
| 202 | + await expect(async () => { |
| 203 | + await handler(params, CancellationToken.None); |
| 204 | + }).rejects.toThrow('Import failed: docUri is not a valid CloudFormation template'); |
| 205 | + }); |
| 206 | +}); |
0 commit comments