text: 'use strict';
  
  const uuidv4 = require('uuid/v4');
  
  const { ecs, s3 } = require('@cumulus/aws-client/services');
  const { randomString } = require('@cumulus/common/test-utils');
  const { getClusterArn, waitForAsyncOperationStatus } = require('@cumulus/integration-tests');
  const { AsyncOperation } = require('@cumulus/api/models');
  const { findAsyncOperationTaskDefinitionForDeployment } = require('../helpers/ecsHelpers');
  const { loadConfig } = require('../helpers/testUtils');
  
  describe('The AsyncOperation task runner running a non-existent lambda function', () => {
    let asyncOperation;
    let asyncOperationId;
    let asyncOperationModel;
    let asyncOperationsTableName;
    let asyncOperationTaskDefinition;
    let beforeAllFailed = false;
    let cluster;
    let config;
    let payloadKey;
  
    beforeAll(async () => {
      try {
        config = await loadConfig();
  
        asyncOperationsTableName = `${config.stackName}-AsyncOperationsTable`;
  
        asyncOperationModel = new AsyncOperation({
          stackName: config.stackName,
          systemBucket: config.bucket,
          tableName: asyncOperationsTableName,
        });
  
        // Find the ARN of the cluster
        cluster = await getClusterArn(config.stackName);
  
        // Find the ARN of the AsyncOperationTaskDefinition
        asyncOperationTaskDefinition = await findAsyncOperationTaskDefinitionForDeployment(config.stackName);
        asyncOperationId = uuidv4();
  
        payloadKey = `${config.stackName}/integration-tests/payloads/${asyncOperationId}.json`;
        await s3().putObject({
          Bucket: config.bucket,
          Key: payloadKey,
          Body: JSON.stringify([1, 2, 3]),
        }).promise();
  
        await asyncOperationModel.create({
          id: asyncOperationId,
          taskArn: randomString(),
          description: 'Some description',
          operationType: 'ES Index',
          status: 'RUNNING',
        });
  
        const runTaskResponse = await ecs().runTask({
          cluster,
          taskDefinition: asyncOperationTaskDefinition,
          launchType: 'EC2',
          overrides: {
            containerOverrides: [
              {
                name: 'AsyncOperation',
                environment: [
                  { name: 'asyncOperationId', value: asyncOperationId },
                  { name: 'asyncOperationsTable', value: asyncOperationsTableName },
                  { name: 'lambdaName', value: 'notARealFunction' },
                  { name: 'payloadUrl', value: `s3://${config.bucket}/${payloadKey}` },
                ],
              },
            ],
          },
        }).promise();
  
        const taskArn = runTaskResponse.tasks[0].taskArn;
  
        await ecs().waitFor(
          'tasksStopped',
          {
            cluster,
            tasks: [taskArn],
          }
        ).promise();
  
        asyncOperation = await waitForAsyncOperationStatus({
          id: asyncOperationId,
          status: 'RUNNER_FAILED',
          stackName: config.stackName,
        });
      } catch (error) {
        beforeAllFailed = true;
        throw error;
      }
    });
  
    it('updates the status field in DynamoDB to "RUNNER_FAILED"', () => {
      if (beforeAllFailed) fail('beforeAll() failed');
      else expect(asyncOperation.status).toEqual('RUNNER_FAILED');
    });
  
    it('updates the output field in DynamoDB', () => {
      if (beforeAllFailed) fail('beforeAll() failed');
      else {
        const parsedOutput = JSON.parse(asyncOperation.output);
  
        expect(parsedOutput.message).toContain('Function not found');
      }
    });
  });
  