|
| 1 | +/*! |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +import assert from 'assert' |
| 7 | +import * as vscode from 'vscode' |
| 8 | +import path from 'path' |
| 9 | +import { PrompterTester } from '../../../test/shared/wizards/prompterTester' |
| 10 | +import { describe } from 'mocha' |
| 11 | +import { ProjectMetadata } from '../../../awsService/appBuilder/serverlessLand/metadataManager' |
| 12 | +import fs from '../../../shared/fs/fs' |
| 13 | +import { AppBuilderRootNode } from '../../../awsService/appBuilder/explorer/nodes/rootNode' |
| 14 | +import * as sinon from 'sinon' |
| 15 | +import { AppNode } from '../../../awsService/appBuilder/explorer/nodes/appNode' |
| 16 | +import { ResourceNode } from '../../../awsService/appBuilder/explorer/nodes/resourceNode' |
| 17 | +import { getTestWindow } from '../../../test/shared/vscode/window' |
| 18 | + |
| 19 | +describe('Serverless Land Integration', async () => { |
| 20 | + const metadataPath = path.resolve( |
| 21 | + __dirname, |
| 22 | + '../../../../../src/awsService/appBuilder/serverlessLand/metadata.json' |
| 23 | + ) |
| 24 | + const metadataContent = await fs.readFileText(metadataPath) |
| 25 | + const parseMetadata = JSON.parse(metadataContent) as ProjectMetadata |
| 26 | + const workspaceFolder = vscode.workspace.workspaceFolders![0] |
| 27 | + const projectFolder = 'my-project-from-Serverless-Land' |
| 28 | + let rootNode: sinon.SinonSpiedInstance<AppBuilderRootNode> |
| 29 | + let sandbox: sinon.SinonSandbox |
| 30 | + |
| 31 | + beforeEach(async () => { |
| 32 | + sandbox = sinon.createSandbox() |
| 33 | + await fs.delete(path.join(workspaceFolder.uri.fsPath, projectFolder), { recursive: true }) |
| 34 | + rootNode = sandbox.spy(AppBuilderRootNode.instance) |
| 35 | + }) |
| 36 | + |
| 37 | + afterEach(async () => { |
| 38 | + await fs.delete(path.join(workspaceFolder.uri.fsPath, projectFolder), { recursive: true }) |
| 39 | + sandbox.restore() |
| 40 | + }) |
| 41 | + |
| 42 | + it('creates project from Serverless Land integration', async () => { |
| 43 | + /** |
| 44 | + * Selection: |
| 45 | + * - pattern : [Select] 2 apigw-rest-api-lambda-sam |
| 46 | + * - runtime : [Select] 3 dotnet |
| 47 | + * - iac : [Select] 1 sam |
| 48 | + * - location : [Input] From TestFolder.uri |
| 49 | + * - name : [Input] "my-project-from-Serverless-Land" |
| 50 | + */ |
| 51 | + |
| 52 | + const testWindow = getTestWindow() |
| 53 | + const prompterTester = PrompterTester.init({ testWindow }) |
| 54 | + .handleQuickPick('Select a Pattern for your application', async (quickPick) => { |
| 55 | + await quickPick.untilReady() |
| 56 | + const options = quickPick.items |
| 57 | + Object.entries(parseMetadata.patterns).map(([key, pattern]) => { |
| 58 | + options.find((option) => option.label === key && option.detail === pattern.description) |
| 59 | + }) |
| 60 | + quickPick.acceptItem(quickPick.items[1]) |
| 61 | + }) |
| 62 | + .handleQuickPick('Select Runtime', async (quickPick) => { |
| 63 | + await quickPick.untilReady() |
| 64 | + const options = quickPick.items |
| 65 | + assert.strictEqual(options[0].label, 'python') |
| 66 | + assert.strictEqual(options[1].label, 'javascript') |
| 67 | + assert.strictEqual(options[2].label, 'java') |
| 68 | + assert.strictEqual(options[3].label, 'dotnet') |
| 69 | + quickPick.acceptItem(options[3]) |
| 70 | + }) |
| 71 | + .handleQuickPick('Select IaC', async (quickPick) => { |
| 72 | + await quickPick.untilReady() |
| 73 | + const options = quickPick.items |
| 74 | + assert.strictEqual(options[0].label, 'sam') |
| 75 | + quickPick.acceptItem(options[0]) |
| 76 | + }) |
| 77 | + .handleQuickPick('Select Project Location', async (quickPick) => { |
| 78 | + await quickPick.untilReady() |
| 79 | + const options = quickPick.items |
| 80 | + assert.strictEqual(options[0].label, '$(folder) workspaceFolder') |
| 81 | + assert.strictEqual(options[1].label, '$(folder-opened) Select a folder...') |
| 82 | + quickPick.acceptItem(options[0]) |
| 83 | + }) |
| 84 | + .handleInputBox('Enter Project Name', (inputBox) => { |
| 85 | + inputBox.acceptValue('my-project-from-Serverless-Land') |
| 86 | + }) |
| 87 | + .build() |
| 88 | + |
| 89 | + // Validate that the README.md is shown. |
| 90 | + testWindow.onDidChangeActiveTextEditor((editors) => { |
| 91 | + assert(editors) |
| 92 | + const readMe = path.join(workspaceFolder.uri.fsPath, projectFolder, 'README.md') |
| 93 | + assert.strictEqual(editors?.document.fileName, readMe) |
| 94 | + }) |
| 95 | + |
| 96 | + await vscode.commands.executeCommand('aws.toolkit.lambda.createServerlessLandProject') |
| 97 | + |
| 98 | + // projectNodes set from previous step |
| 99 | + |
| 100 | + const projectNode = await rootNode |
| 101 | + .getChildren() |
| 102 | + .then( |
| 103 | + (children) => |
| 104 | + children.find( |
| 105 | + (node) => |
| 106 | + node instanceof AppNode && node.label === 'workspaceFolder/my-project-from-Serverless-Land' |
| 107 | + ) as AppNode | undefined |
| 108 | + ) |
| 109 | + |
| 110 | + assert.ok(projectNode, 'Expect Serverless Land project node in Application Builder') |
| 111 | + |
| 112 | + // Check App Builder resources |
| 113 | + const resourceNodes = await projectNode.getChildren() |
| 114 | + assert.strictEqual(resourceNodes.length, 1) |
| 115 | + assert.ok(resourceNodes[0] instanceof ResourceNode) |
| 116 | + |
| 117 | + // Validate Lambda resource configuration |
| 118 | + const lambdaResource = resourceNodes[0] as ResourceNode |
| 119 | + assert.strictEqual(lambdaResource.resource.resource.Type, 'AWS::Serverless::Function') |
| 120 | + assert.strictEqual(lambdaResource.resource.resource.Runtime, 'dotnet8') |
| 121 | + assert.strictEqual(lambdaResource.resource.resource.Id, 'HelloWorldFunction') |
| 122 | + assert.deepStrictEqual(lambdaResource.resource.resource.Events, [ |
| 123 | + { |
| 124 | + Id: 'HelloWorld', |
| 125 | + Type: 'Api', |
| 126 | + Path: '/hello', |
| 127 | + Method: 'get', |
| 128 | + }, |
| 129 | + ]) |
| 130 | + assert.deepStrictEqual(lambdaResource.resource.resource.Environment, { |
| 131 | + Variables: { |
| 132 | + PARAM1: 'VALUE', |
| 133 | + }, |
| 134 | + }) |
| 135 | + |
| 136 | + prompterTester.assertCallAll() |
| 137 | + }) |
| 138 | +}) |
0 commit comments