import {render, screen} from '@testing-library/react' import userEvent from '@testing-library/user-event' import {expect, test, vi} from 'vitest' import {LambdaCreatePanel} from './LambdaCreatePanel' import type {ServiceSchema} from '@/types/schema' const schema: ServiceSchema = { cloud: 'aws', service: 'serverless', displayName: 'AWS Lambda', actions: ['create'], columns: [], filters: [], capabilities: {resourceActions: []}, fields: [ {name: 'functionName', label: 'Function Name', type: 'text', required: true}, {name: 'runtime', label: 'Runtime', type: 'select', required: true, options: [{label: 'Node.js 22.x', value: 'nodejs22.x'}]}, {name: 'handler', label: 'Handler', type: 'text', required: true}, {name: 'role', label: 'Execution Role ARN', type: 'text', required: true}, {name: 'description', label: 'Description', type: 'text', required: false}, {name: 'code', label: 'Hello World Source', type: 'textarea', required: false}, ], } function renderPanel(onSubmit = vi.fn()) { render() return onSubmit } test('offers scratch and blueprint Lambda creation paths', () => { renderPanel() expect(screen.getByRole('button', {name: /start from scratch/i})).toBeVisible() expect(screen.getByRole('button', {name: /^blueprints/i})).toBeVisible() }) test('prefills the reviewed S3 upload processor blueprint', async () => { const user = userEvent.setup() renderPanel() await user.click(screen.getByRole('button', {name: /^blueprints/i})) expect(screen.getByText('Lambda Blueprints')).toBeVisible() await user.click(screen.getByRole('button', {name: /s3 upload processor/i})) expect(screen.getByText('Blueprint: S3 upload processor')).toBeVisible() expect(screen.getByDisplayValue('Processes S3 object-created events.')).toBeVisible() expect(screen.getByDisplayValue(/record\.s3\.bucket\.name/)).toBeVisible() expect(screen.getByText(/create lambda, create bucket/i)).toBeVisible() expect(screen.getByLabelText(/s3 bucket name/i)).toBeRequired() }) test('keeps the blank creation form available', async () => { const user = userEvent.setup() renderPanel() await user.click(screen.getByRole('button', {name: /start from scratch/i})) expect(screen.getByText('Start from scratch')).toBeVisible() expect(screen.getByLabelText(/function name/i)).toHaveValue('') }) test('prefills the REST Lambda API blueprint and its deployment plan', async () => { const user = userEvent.setup() renderPanel() await user.click(screen.getByRole('button', {name: /^blueprints/i})) await user.click(screen.getByRole('button', {name: /rest lambda api/i})) expect(screen.getByText('Blueprint: REST Lambda API')).toBeVisible() expect(screen.getByDisplayValue(/event\.httpMethod/)).toBeVisible() expect(screen.getByLabelText(/api name/i)).toBeVisible() expect(screen.getByText(/grant api gateway permission/i)).toBeVisible() }) test('prefills the scheduled Lambda blueprint and its EventBridge schedule', async () => { const user = userEvent.setup() renderPanel() await user.click(screen.getByRole('button', {name: /^blueprints/i})) await user.click(screen.getByRole('button', {name: /scheduled lambda/i})) expect(screen.getByText('Blueprint: Scheduled Lambda')).toBeVisible() expect(screen.getByDisplayValue(/scheduled event received/i)).toBeVisible() expect(screen.getByLabelText(/rule name/i)).toBeVisible() expect(screen.getByLabelText(/schedule expression/i)).toHaveValue('rate(5 minutes)') expect(screen.getByText(/create eventbridge rule/i)).toBeVisible() }) test('prefills the SQS worker blueprint and submits queue mapping settings', async () => { const user = userEvent.setup() const onSubmit = renderPanel() await user.click(screen.getByRole('button', {name: /^blueprints/i})) await user.click(screen.getByRole('button', {name: /sqs worker/i})) expect(screen.getByText('Blueprint: SQS Worker')).toBeVisible() expect(screen.getByDisplayValue(/event\.Records/)).toBeVisible() await user.type(screen.getByLabelText(/sqs queue name/i), 'orders-queue') await user.clear(screen.getByLabelText(/batch size/i)) await user.type(screen.getByLabelText(/batch size/i), '5') await user.type(screen.getByLabelText(/function name/i), 'orders-worker') await user.type(screen.getByLabelText(/execution role arn/i), 'arn:aws:iam::000000000000:role/lambda') await user.click(screen.getByRole('button', {name: 'Create Lambda'})) expect(onSubmit).toHaveBeenCalledWith(expect.objectContaining({ blueprintQueueName: 'orders-queue', blueprintSqsBatchSize: 5, code: expect.stringContaining('event.Records'), })) })