Skip to content

The open alternative to Salesforce, designed for AI.

5.9 KB
Raw Blame
import { createChatParticipationTestContext } from 'test/integration/ai/utils/create-chat-participation-test-context.util';
import { createManyOperationFactory } from 'test/integration/graphql/utils/create-many-operation-factory.util';
import { createOneOperationFactory } from 'test/integration/graphql/utils/create-one-operation-factory.util';
import { makeGraphqlApiRequest } from 'test/integration/graphql/utils/make-graphql-api-request.util';
import { getAppProviderByClassName } from 'test/integration/utils/get-app-provider-by-class-name.util';
import { isDefined, isPlainObject } from 'twenty-shared/utils';

import { DatabaseEventAction } from 'src/engine/api/graphql/graphql-query-runner/enums/database-event-action';
import { type WorkspaceEventEmitter } from 'src/engine/workspace-event-emitter/workspace-event-emitter';
import { WORKSPACE_MEMBER_DATA_SEED_IDS } from 'src/engine/workspace-manager/dev-seeder/data/constants/workspace-member-data-seeds.constant';

const isThreadRecordEvent = ({
  recordEvent: { recordId, properties },
  threadId,
}: {
  recordEvent: { recordId: string; properties: object };
  threadId: string;
}) => {
  if (recordId === threadId) {
    return true;
  }

  return (
    'after' in properties &&
    isPlainObject(properties.after) &&
    properties.after.threadId === threadId
  );
};

describe('Record API chat creation initializes personal inbox state', () => {
  let context: Awaited<ReturnType<typeof createChatParticipationTestContext>>;

  beforeEach(async () => {
    context = await createChatParticipationTestContext();
  });

  afterEach(async () => {
    await context.cleanup();
    jest.restoreAllMocks();
  });

  const observeCreationEvents = () => {
    const emit = jest.spyOn(
      getAppProviderByClassName<WorkspaceEventEmitter>('WorkspaceEventEmitter'),
      'emitDatabaseBatchEvent',
    );

    return (threadId: string) =>
      emit.mock.calls.flatMap(([batchEvent]) => {
        if (!isDefined(batchEvent)) {
          return [];
        }

        return batchEvent.events
          .filter((recordEvent) =>
            isThreadRecordEvent({ recordEvent, threadId }),
          )
          .map(
            () =>
              `${batchEvent.objectMetadataNameSingular} ${batchEvent.action}`,
          );
      });
  };
  const expectParticipantCreatedBefore = ({
    events,
    threadAction,
  }: {
    events: string[];
    threadAction: DatabaseEventAction;
  }) => {
    const participantCreatedIndex = events.indexOf(
      `agentChatThreadParticipant ${DatabaseEventAction.CREATED}`,
    );

    expect(participantCreatedIndex).toBeGreaterThanOrEqual(0);
    expect(participantCreatedIndex).toBeLessThan(
      events.lastIndexOf(`agentChatThread ${threadAction}`),
    );
  };

  const expectCreatorInboxState = async (threadId: string) => {
    const thread = await context.readThread(threadId);
    const participants = await context.readParticipants(threadId);

    expect(thread.workspaceMemberId).toBe(context.workspaceMemberId);
    expect(thread.lastActivityAt).toEqual(expect.anything());
    expect(participants).toEqual([
      expect.objectContaining({
        workspaceMemberId: context.workspaceMemberId,
        lastReadAt: thread.lastActivityAt,
        archivedAt: null,
        isSubscribed: true,
      }),
    ]);
  };

  it('creates a read personal inbox copy with the same activity boundary', async () => {
    const readCreationEvents = observeCreationEvents();
    const response = await makeGraphqlApiRequest(
      createOneOperationFactory({
        objectMetadataSingularName: 'agentChatThread',
        gqlFields: 'id',
        data: { title: 'Created through the record API' },
      }),
    );

    expect(response.body.errors).toBeUndefined();
    const threadId = context.registerThread(
      response.body.data.createAgentChatThread.id,
    );

    await expectCreatorInboxState(threadId);
    expectParticipantCreatedBefore({
      events: readCreationEvents(threadId),
      threadAction: DatabaseEventAction.UPDATED,
    });
  });

  it('initializes each conversation returned by createMany', async () => {
    const response = await makeGraphqlApiRequest(
      createManyOperationFactory({
        objectMetadataSingularName: 'agentChatThread',
        objectMetadataPluralName: 'agentChatThreads',
        gqlFields: 'id',
        data: [
          { title: 'Created in a batch' },
          { title: 'Created in a batch' },
        ],
      }),
    );

    expect(response.body.errors).toBeUndefined();

    const createdThreads: { id: string }[] =
      response.body.data.createAgentChatThreads;
    const threadIds = createdThreads.map(({ id }) =>
      context.registerThread(id),
    );

    expect(threadIds).toHaveLength(2);

    for (const threadId of threadIds) {
      await expectCreatorInboxState(threadId);
    }
  });

  it('preserves an existing owner when creator initialization is repeated by another member', async () => {
    const threadId = await context.createThread();
    const participantsBefore = await context.readParticipants(threadId);
    const authContext = await context.sharingService.getAuthContext({
      workspaceId: context.workspaceId,
      workspaceMemberId: WORKSPACE_MEMBER_DATA_SEED_IDS.JONY,
    });

    await context.threadService.assignCreatedThreadsToCreator({
      authContext,
      threadIds: [threadId],
    });

    expect((await context.readThread(threadId)).workspaceMemberId).toBe(
      context.workspaceMemberId,
    );
    expect(await context.readParticipants(threadId)).toEqual(
      participantsBefore,
    );
  });

  it('initializes the same creator inbox state through createChatThread', async () => {
    const readCreationEvents = observeCreationEvents();
    const threadId = await context.createThread();

    await expectCreatorInboxState(threadId);
    expectParticipantCreatedBefore({
      events: readCreationEvents(threadId),
      threadAction: DatabaseEventAction.CREATED,
    });
  });
});