Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

4.2 KB
Raw Blame
// @vitest-environment node
import { describe, expect, it, vi } from 'vitest';

import { getInstallationStore, messengerConnectionId, messengerConnectionIdForUser } from './index';

vi.mock('./slack', () => ({
  SlackInstallationStore: vi.fn(function () {
    return { kind: 'slack' };
  }),
}));

vi.mock('./telegram', () => ({
  TELEGRAM_INSTALLATION_KEY: 'telegram:singleton',
  TelegramInstallationStore: vi.fn(function () {
    return { kind: 'telegram' };
  }),
}));

vi.mock('./discord', () => ({
  DISCORD_INSTALLATION_KEY: 'discord:singleton',
  DiscordInstallationStore: vi.fn(function () {
    return { kind: 'discord' };
  }),
}));

vi.mock('./wechat', () => ({
  WechatInstallationStore: vi.fn(function () {
    return { kind: 'wechat' };
  }),
  wechatInstallationKey: (tenantId: string) => `wechat:${tenantId}`,
}));

describe('getInstallationStore', () => {
  it('returns the slack store for platform=slack', () => {
    const store = getInstallationStore('slack');
    expect(store).toEqual({ kind: 'slack' });
  });

  it('returns the telegram store for platform=telegram', () => {
    const store = getInstallationStore('telegram');
    expect(store).toEqual({ kind: 'telegram' });
  });

  it('returns the discord store for platform=discord', () => {
    const store = getInstallationStore('discord');
    expect(store).toEqual({ kind: 'discord' });
  });

  it('returns the wechat store for platform=wechat', () => {
    const store = getInstallationStore('wechat');
    expect(store).toEqual({ kind: 'wechat' });
  });

  it('memoizes the store across calls (one instance per process)', async () => {
    const a = getInstallationStore('slack');
    const b = getInstallationStore('slack');
    expect(a).toBe(b);
  });

  it('returns null for an unknown platform', () => {
    expect(getInstallationStore('unknown' as any)).toBeNull();
  });
});

describe('messengerConnectionId', () => {
  it('returns the singleton connectionId for a platform', () => {
    expect(messengerConnectionId('discord')).toBe('messenger:discord:singleton');
    expect(messengerConnectionId('telegram')).toBe('messenger:telegram:singleton');
  });
});

describe('messengerConnectionIdForUser', () => {
  it('drops the :singleton segment for telegram (no tenant)', () => {
    expect(
      messengerConnectionIdForUser({ installationKey: 'telegram:singleton', userId: 'u_abc' }),
    ).toBe('messenger:telegram:user-u_abc');
  });

  it('drops the :singleton segment for discord (no tenant)', () => {
    expect(
      messengerConnectionIdForUser({ installationKey: 'discord:singleton', userId: 'u_abc' }),
    ).toBe('messenger:discord:user-u_abc');
  });

  it('preserves the tenantId for slack workspaces', () => {
    expect(messengerConnectionIdForUser({ installationKey: 'slack:T0123', userId: 'u_abc' })).toBe(
      'messenger:slack:T0123:user-u_abc',
    );
  });

  it('preserves the WeChat user tenant for per-user polling installs', () => {
    expect(
      messengerConnectionIdForUser({
        connectionMode: 'polling',
        installationKey: 'wechat:[email protected]',
        userId: 'u_abc',
      }),
    ).toBe('messenger:wechat:[email protected]:user-u_abc');
  });

  it('routes to the singleton connectionId when websocket mode + singleton install (discord)', () => {
    expect(
      messengerConnectionIdForUser({
        connectionMode: 'websocket',
        installationKey: 'discord:singleton',
        userId: 'u_abc',
      }),
    ).toBe('messenger:discord:singleton');
  });

  it('keeps per-user sharding for webhook mode even with singleton install (telegram)', () => {
    expect(
      messengerConnectionIdForUser({
        connectionMode: 'webhook',
        installationKey: 'telegram:singleton',
        userId: 'u_abc',
      }),
    ).toBe('messenger:telegram:user-u_abc');
  });

  it('keeps per-user sharding for websocket mode on multi-tenant installs (slack)', () => {
    // Defensive: websocket + non-singleton tenant install should still shard
    // by user, since each tenant has its own connection on the gateway.
    expect(
      messengerConnectionIdForUser({
        connectionMode: 'websocket',
        installationKey: 'slack:T0123',
        userId: 'u_abc',
      }),
    ).toBe('messenger:slack:T0123:user-u_abc');
  });
});