Skip to content

The open alternative to Salesforce, designed for AI.

11.5 KB
Raw Blame
import request from 'supertest';
import {
  coreWorkflowGraphqlRequest,
  findCoreWorkflowVersion,
  runCoreWorkflowVersion,
} from 'test/integration/graphql/suites/workflow/utils/core-workflow.util';
import { submitFormStep } from 'test/integration/graphql/suites/workflow/utils/submit-form-step.util';
import {
  destroyWorkflowRun,
  getWorkflowRun,
  waitForWorkflowCompletion,
  waitForWorkflowRunStatus,
  waitForWorkflowRunStepStatus,
} from 'test/integration/graphql/suites/workflow/utils/workflow-run-test.util';
import { v4 as uuidv4 } from 'uuid';

import { SEED_APPLE_WORKSPACE_ID } from 'src/engine/workspace-manager/dev-seeder/core/constants/seeder-workspaces.constant';
import { getWorkflowPrefillIds } from 'src/engine/workspace-manager/standard-objects-prefill-data/utils/prefill-workflows.util';

const client = request(`http://localhost:${APP_PORT}`);

const {
  coreQuickLeadWorkflowId: QUICK_LEAD_CORE_WORKFLOW_ID,
  coreQuickLeadWorkflowVersionId: QUICK_LEAD_CORE_WORKFLOW_VERSION_ID,
} = getWorkflowPrefillIds(SEED_APPLE_WORKSPACE_ID);
const FORM_STEP_ID = '6e089bc9-aabd-435f-865f-f31c01c8f4a7';

describe('Quick Lead Workflow (e2e)', () => {
  let createdWorkflowRunId: string | null = null;

  afterAll(async () => {
    if (createdWorkflowRunId) {
      await destroyWorkflowRun(createdWorkflowRunId);
    }
  });

  describe('Workflow triggering', () => {
    it('should verify Quick Lead workflow exists and is active', async () => {
      const { coreWorkflowById } = await coreWorkflowGraphqlRequest<{
        coreWorkflowById: {
          id: string;
          name: string;
          statuses: string[];
          lastPublishedCoreWorkflowVersionId: string | null;
        };
      }>({
        query: `
          query FindCoreWorkflow($coreWorkflowId: UUID!) {
            coreWorkflowById(coreWorkflowId: $coreWorkflowId) {
              id
              name
              statuses
              lastPublishedCoreWorkflowVersionId
            }
          }
        `,
        variables: { coreWorkflowId: QUICK_LEAD_CORE_WORKFLOW_ID },
      });

      expect(coreWorkflowById.id).toBe(QUICK_LEAD_CORE_WORKFLOW_ID);
      expect(coreWorkflowById.name).toBe('Quick Lead');
      expect(coreWorkflowById.lastPublishedCoreWorkflowVersionId).toBe(
        QUICK_LEAD_CORE_WORKFLOW_VERSION_ID,
      );
      expect(coreWorkflowById.statuses).toContain('ACTIVE');
    });

    it('should verify Quick Lead workflow version has correct structure', async () => {
      const workflowVersion = await findCoreWorkflowVersion({
        coreWorkflowVersionId: QUICK_LEAD_CORE_WORKFLOW_VERSION_ID,
      });

      expect(workflowVersion.status).toBe('ACTIVE');

      expect(workflowVersion.trigger?.type).toBe('MANUAL');
      expect(workflowVersion.trigger?.nextStepIds).toContain(FORM_STEP_ID);

      const steps = workflowVersion.steps ?? [];

      expect(steps).toHaveLength(3);
      expect(steps).toEqual(
        expect.arrayContaining([
          expect.objectContaining({
            id: FORM_STEP_ID,
            type: 'FORM',
            name: 'Quick Lead Form',
          }),
          expect.objectContaining({
            id: '0715b6cd-7cc1-4b98-971b-00f54dfe643b',
            type: 'CREATE_RECORD',
            name: 'Create Company',
          }),
          expect.objectContaining({
            id: '6f553ea7-b00e-4371-9d88-d8298568a246',
            type: 'CREATE_RECORD',
            name: 'Create Person',
          }),
        ]),
      );
    });

    it('should trigger Quick Lead workflow and create workflow run', async () => {
      const workflowRunId = await runCoreWorkflowVersion({
        coreWorkflowVersionId: QUICK_LEAD_CORE_WORKFLOW_VERSION_ID,
      });

      createdWorkflowRunId = workflowRunId;

      await waitForWorkflowRunStatus(workflowRunId, 'RUNNING');

      const workflowRun = await waitForWorkflowRunStepStatus(
        workflowRunId,
        FORM_STEP_ID,
        'PENDING',
      );

      expect(workflowRun).toBeDefined();
      expect(workflowRun?.coreWorkflowVersionId).toBe(
        QUICK_LEAD_CORE_WORKFLOW_VERSION_ID,
      );
      expect(workflowRun?.status).toBe('RUNNING');
      expect(workflowRun?.state).toBeDefined();
      expect(workflowRun?.state?.stepInfos).toBeDefined();
      expect(workflowRun?.state?.stepInfos?.trigger).toBeDefined();
      expect(workflowRun?.state?.stepInfos?.[FORM_STEP_ID]).toBeDefined();
      expect(
        workflowRun?.state?.stepInfos?.['0715b6cd-7cc1-4b98-971b-00f54dfe643b'],
      ).toBeDefined();
      expect(
        workflowRun?.state?.stepInfos?.['6f553ea7-b00e-4371-9d88-d8298568a246'],
      ).toBeDefined();

      expect(workflowRun?.state?.stepInfos?.trigger?.status).toBe('SUCCESS');
      expect(workflowRun?.state?.stepInfos?.[FORM_STEP_ID]?.status).toBe(
        'PENDING',
      );
      expect(
        workflowRun?.state?.stepInfos?.['0715b6cd-7cc1-4b98-971b-00f54dfe643b']
          ?.status,
      ).toBe('NOT_STARTED');
      expect(
        workflowRun?.state?.stepInfos?.['6f553ea7-b00e-4371-9d88-d8298568a246']
          ?.status,
      ).toBe('NOT_STARTED');
    });

    it('should be able to stop a running workflow run', async () => {
      const workflowRunId = await runCoreWorkflowVersion({
        coreWorkflowVersionId: QUICK_LEAD_CORE_WORKFLOW_VERSION_ID,
      });

      const stopResponse = await client
        .post('/graphql')
        .set('Authorization', `Bearer ${APPLE_JANE_ADMIN_ACCESS_TOKEN}`)
        .send({
          query: `
            mutation StopWorkflowRun($workflowRunId: UUID!) {
              stopWorkflowRun(workflowRunId: $workflowRunId) {
                id
                status
              }
            }
          `,
          variables: { workflowRunId },
        });

      expect(stopResponse.body.errors).toBeUndefined();
      expect(stopResponse.body.data.stopWorkflowRun.status).toBe('STOPPED');

      const workflowRun = await getWorkflowRun(workflowRunId);

      expect(workflowRun?.status).toBe('STOPPED');
      await destroyWorkflowRun(workflowRunId);
    });
  });

  describe('Full workflow execution with form submission', () => {
    let testWorkflowRunId: string | null = null;
    let createdCompanyId: string | null = null;
    let createdPersonId: string | null = null;

    afterAll(async () => {
      if (createdPersonId) {
        await client
          .post('/graphql')
          .set('Authorization', `Bearer ${APPLE_JANE_ADMIN_ACCESS_TOKEN}`)
          .send({
            query: `
              mutation DestroyPerson($id: ID!) {
                destroyPerson(id: $id) {
                  id
                }
              }
            `,
            variables: { id: createdPersonId },
          });
      }

      if (createdCompanyId) {
        await client
          .post('/graphql')
          .set('Authorization', `Bearer ${APPLE_JANE_ADMIN_ACCESS_TOKEN}`)
          .send({
            query: `
              mutation DestroyCompany($id: ID!) {
                destroyCompany(id: $id) {
                  id
                }
              }
            `,
            variables: { id: createdCompanyId },
          });
      }

      if (testWorkflowRunId) {
        await destroyWorkflowRun(testWorkflowRunId);
      }
    });

    it('should complete full workflow: trigger → submit form → create Company and Person', async () => {
      testWorkflowRunId = await runCoreWorkflowVersion({
        coreWorkflowVersionId: QUICK_LEAD_CORE_WORKFLOW_VERSION_ID,
      });

      expect(testWorkflowRunId).toBeDefined();

      await waitForWorkflowRunStatus(testWorkflowRunId as string, 'RUNNING');

      let workflowRun = await waitForWorkflowRunStepStatus(
        testWorkflowRunId as string,
        FORM_STEP_ID,
        'PENDING',
      );

      expect(workflowRun?.status).toBe('RUNNING');
      expect(workflowRun?.state?.stepInfos?.[FORM_STEP_ID]?.status).toBe(
        'PENDING',
      );

      const testId = uuidv4().slice(0, 8);
      const testFormData = {
        firstName: 'Integration',
        lastName: `TestUser-${testId}`,
        email: `test-${testId}@example.com`,
        jobTitle: 'Test Engineer',
        companyName: `Test Company ${testId}`,
        companyDomain: `https://test-${testId}.example.com`,
      };

      const submitFormResponse = await submitFormStep({
        workflowRunId: testWorkflowRunId as string,
        stepId: FORM_STEP_ID,
        response: testFormData,
      });

      expect(submitFormResponse.body.errors).toBeUndefined();
      expect(submitFormResponse.body.data.submitFormStep).toBe(true);

      workflowRun = await waitForWorkflowCompletion(
        testWorkflowRunId as string,
      );
      expect(workflowRun?.status).toBe('COMPLETED');
      expect(workflowRun?.state?.stepInfos?.trigger?.status).toBe('SUCCESS');
      expect(workflowRun?.state?.stepInfos?.[FORM_STEP_ID]?.status).toBe(
        'SUCCESS',
      );
      expect(
        workflowRun?.state?.stepInfos?.['0715b6cd-7cc1-4b98-971b-00f54dfe643b']
          ?.status,
      ).toBe('SUCCESS');
      expect(
        workflowRun?.state?.stepInfos?.['6f553ea7-b00e-4371-9d88-d8298568a246']
          ?.status,
      ).toBe('SUCCESS');

      const companyStepResult = workflowRun?.state?.stepInfos?.[
        '0715b6cd-7cc1-4b98-971b-00f54dfe643b'
      ]?.result as { id?: string } | undefined;

      createdCompanyId = companyStepResult?.id ?? null;
      expect(createdCompanyId).toBeDefined();

      const companyResponse = await client
        .post('/graphql')
        .set('Authorization', `Bearer ${APPLE_JANE_ADMIN_ACCESS_TOKEN}`)
        .send({
          query: `
            query FindCompany($id: UUID!) {
              company(filter: { id: { eq: $id } }) {
                id
                name
                domainName {
                  primaryLinkUrl
                }
              }
            }
          `,
          variables: { id: createdCompanyId },
        });

      expect(companyResponse.body.errors).toBeUndefined();
      expect(companyResponse.body.data.company).toBeDefined();
      expect(companyResponse.body.data.company.name).toBe(
        testFormData.companyName,
      );
      expect(
        companyResponse.body.data.company.domainName.primaryLinkUrl,
      ).toContain(`test-${testId}.example.com`);

      const personStepResult = workflowRun?.state?.stepInfos?.[
        '6f553ea7-b00e-4371-9d88-d8298568a246'
      ]?.result as { id?: string } | undefined;

      createdPersonId = personStepResult?.id ?? null;
      expect(createdPersonId).toBeDefined();

      const personResponse = await client
        .post('/graphql')
        .set('Authorization', `Bearer ${APPLE_JANE_ADMIN_ACCESS_TOKEN}`)
        .send({
          query: `
            query FindPerson($id: UUID!) {
              person(filter: { id: { eq: $id } }) {
                id
                name {
                  firstName
                  lastName
                }
                emails {
                  primaryEmail
                }
                jobTitle
              }
            }
          `,
          variables: { id: createdPersonId },
        });

      expect(personResponse.body.errors).toBeUndefined();
      expect(personResponse.body.data.person).toBeDefined();
      expect(personResponse.body.data.person.name.firstName).toBe(
        testFormData.firstName,
      );
      expect(personResponse.body.data.person.name.lastName).toBe(
        testFormData.lastName,
      );
      expect(personResponse.body.data.person.emails.primaryEmail).toBe(
        testFormData.email,
      );
      expect(personResponse.body.data.person.jobTitle).toBe(
        testFormData.jobTitle,
      );
    });
  });
});