Skip to content

The open alternative to Salesforce, designed for AI.

7.8 KB
Raw Blame
import { beforeEach, describe, expect, it, vi } from 'vitest';

import { type UnipileMessage } from 'src/unipile/types/unipile-message.type';
import { type UnipileWebhookEvent } from 'src/unipile/types/unipile-webhook-event.type';

const mocks = vi.hoisted(() => ({
  listConnections: vi.fn(),
  listMessageChannels: vi.fn(),
  ingestMessages: vi.fn(),
  reportConnectionAuthFailure: vi.fn(),
  kvGet: vi.fn(),
  kvSet: vi.fn(),
  getUnipileUser: vi.fn(),
  getUnipileChat: vi.fn(),
  downloadUnipileMessageAttachment: vi.fn(),
  query: vi.fn(),
  mutation: vi.fn(),
}));

vi.mock('twenty-sdk/define', () => ({
  defineLogicFunction: (config: unknown) => config,
}));

vi.mock('twenty-sdk/logic-function', () => ({
  listConnections: mocks.listConnections,
  listMessageChannels: mocks.listMessageChannels,
  ingestMessages: mocks.ingestMessages,
  reportConnectionAuthFailure: mocks.reportConnectionAuthFailure,
  kv: { get: mocks.kvGet, set: mocks.kvSet },
}));

vi.mock('twenty-client-sdk/core', () => ({
  CoreApiClient: class {
    query = mocks.query;
    mutation = mocks.mutation;
  },
}));

vi.mock('src/unipile/utils/get-unipile-user.util', () => ({
  getUnipileUser: mocks.getUnipileUser,
}));

vi.mock('src/unipile/utils/get-unipile-chat.util', () => ({
  getUnipileChat: mocks.getUnipileChat,
}));

vi.mock('src/unipile/utils/download-unipile-message-attachment.util', () => ({
  downloadUnipileMessageAttachment: mocks.downloadUnipileMessageAttachment,
}));

const { linkedinHandleWebhookEventHandler } =
  await import('src/logic-functions/linkedin-handle-webhook-event');

const CHANNEL_HANDLE = 'linkedin.com/in/martin-muller';

const DAPHNE = {
  id: 'ACoDAPHNE',
  display_name: 'Daphné Müller',
  profile_url: 'https://www.linkedin.com/in/ACoDAPHNE',
};

const buildMessageEvent = (
  payload: Pick<UnipileMessage, 'sender_id' | 'is_sender'> &
    Partial<UnipileMessage>,
): UnipileWebhookEvent => ({
  id: 'evt_1',
  created_at: '2026-10-29T07:44:08.382Z',
  account_id: 'acc_1',
  account_provider: 'linkedin',
  type: 'message.new',
  payload: {
    id: 'message-1',
    chat_id: 'chat-1',
    text: 'Hello',
    timestamp: '2026-10-29T07:44:08.382Z',
    is_deleted: false,
    is_event: false,
    provider: 'linkedin',
    ...payload,
  },
});

describe('linkedinHandleWebhookEventHandler', () => {
  beforeEach(() => {
    vi.clearAllMocks();
    mocks.listConnections.mockResolvedValue([
      { id: 'connection-id', externalAccountId: 'acc_1' },
    ]);
    mocks.listMessageChannels.mockResolvedValue([
      { id: 'channel-id', handle: CHANNEL_HANDLE },
    ]);
    mocks.kvGet.mockResolvedValue(null);
    mocks.query.mockResolvedValue({ people: { edges: [] } });
    mocks.getUnipileChat.mockResolvedValue({
      id: 'chat-1',
      name: 'Daphné Müller',
      is_group: false,
      is_1to1: true,
      user: DAPHNE,
    });
    mocks.mutation.mockResolvedValue({ createPerson: { id: 'created-id' } });
    mocks.getUnipileUser.mockResolvedValue({
      ...DAPHNE,
      public_identifier: 'daphne-muller',
    });
  });

  it('ingests a received message under the sender public profile URL', async () => {
    await expect(
      linkedinHandleWebhookEventHandler(
        buildMessageEvent({
          sender_id: DAPHNE.id,
          sender: DAPHNE,
          is_sender: false,
        }),
      ),
    ).resolves.toEqual({ handled: true });

    expect(mocks.ingestMessages).toHaveBeenCalledExactlyOnceWith({
      messageChannelId: 'channel-id',
      messages: [
        expect.objectContaining({
          externalId: 'message-1',
          threadExternalId: 'chat-1',
          participants: [
            {
              role: 'FROM',
              handle: 'https://www.linkedin.com/in/daphne-muller',
              displayName: 'Daphné Müller',
            },
            { role: 'TO', handle: CHANNEL_HANDLE },
          ],
        }),
      ],
    });
  });

  it('imports the image of a received message', async () => {
    mocks.downloadUnipileMessageAttachment.mockResolvedValue(
      Buffer.from('png'),
    );
    const consoleLog = vi.spyOn(console, 'log').mockImplementation(() => {});

    await expect(
      linkedinHandleWebhookEventHandler(
        buildMessageEvent({
          sender_id: DAPHNE.id,
          sender: DAPHNE,
          is_sender: false,
          text: '',
          attachments: [
            { id: 'attachment-1', type: 'img', mimetype: 'image/png' },
          ],
        }),
      ),
    ).resolves.toEqual({ handled: true });

    expect(
      mocks.downloadUnipileMessageAttachment,
    ).toHaveBeenCalledExactlyOnceWith({
      accountId: 'acc_1',
      chatId: 'chat-1',
      messageId: 'message-1',
      attachmentId: 'attachment-1',
      maxSizeBytes: 25 * 1024 * 1024,
      deadlineAtMs: expect.any(Number),
    });

    consoleLog.mockRestore();
  });

  it('imports a group message from a stranger when another member is a contact', async () => {
    const ada = {
      id: 'ACoADA',
      display_name: 'Ada Lovelace',
      public_identifier: 'ada',
    };

    mocks.getUnipileChat.mockResolvedValue({
      id: 'chat-1',
      name: 'Founders',
      is_group: true,
      is_1to1: false,
      participants: [
        { is_self: false, user: ada },
        { is_self: false, user: DAPHNE },
      ],
    });
    mocks.query.mockImplementation(async (query: unknown) =>
      JSON.stringify(query).includes('in/ada')
        ? {
            people: {
              edges: [
                {
                  node: {
                    id: 'ada-id',
                    linkedinLink: {
                      primaryLinkUrl: 'https://www.linkedin.com/in/ada',
                    },
                  },
                },
              ],
            },
          }
        : { people: { edges: [] } },
    );

    await expect(
      linkedinHandleWebhookEventHandler(
        buildMessageEvent({
          sender_id: DAPHNE.id,
          sender: DAPHNE,
          is_sender: false,
        }),
      ),
    ).resolves.toEqual({ handled: true });

    expect(mocks.ingestMessages).toHaveBeenCalledOnce();
    expect(mocks.getUnipileUser).not.toHaveBeenCalled();
  });

  it('ingests a sent message to the chat counterpart public profile URL', async () => {
    mocks.getUnipileChat.mockResolvedValue({
      id: 'chat-1',
      name: 'Daphné Müller',
      is_group: false,
      is_1to1: true,
      user: DAPHNE,
    });

    await linkedinHandleWebhookEventHandler(
      buildMessageEvent({ sender_id: 'ACoMARTIN', is_sender: true }),
    );

    expect(mocks.ingestMessages).toHaveBeenCalledTimes(1);
    expect(mocks.getUnipileChat).toHaveBeenCalledExactlyOnceWith({
      accountId: 'acc_1',
      chatId: 'chat-1',
    });
    expect(
      mocks.ingestMessages.mock.calls[0][0].messages[0].participants,
    ).toEqual([
      { role: 'FROM', handle: CHANNEL_HANDLE },
      {
        role: 'TO',
        handle: 'https://www.linkedin.com/in/daphne-muller',
        displayName: 'Daphné Müller',
        personId: 'created-id',
      },
    ]);
    expect(mocks.mutation).toHaveBeenCalledExactlyOnceWith({
      createPerson: {
        __args: {
          data: {
            id: expect.stringMatching(
              /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/,
            ),
            name: { firstName: 'Daphné', lastName: 'Müller' },
            linkedinLink: {
              primaryLinkUrl: 'https://www.linkedin.com/in/daphne-muller',
            },
          },
        },
        id: true,
      },
    });
  });

  it('ignores events of accounts no connection owns', async () => {
    mocks.listConnections.mockResolvedValue([]);

    await expect(
      linkedinHandleWebhookEventHandler(
        buildMessageEvent({ sender_id: DAPHNE.id, is_sender: false }),
      ),
    ).resolves.toEqual({ handled: false, reason: 'Unknown LinkedIn account' });
    expect(mocks.ingestMessages).not.toHaveBeenCalled();
  });
});