Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

4.0 KB
Raw Blame
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { createElement } from 'react';
import { renderToStaticMarkup } from 'react-dom/server';
import { describe, expect, it, vi } from 'vitest';
import { CreateRoomModal } from './CreateRoomModal';

const flags = vi.hoisted(() => ({ group_chats: true }));
vi.mock('@renderer/lib/hooks/useFeatureFlags', () => ({ useFeatureFlags: () => flags }));
vi.mock('@renderer/lib/ipc', () => ({ events: { on: vi.fn() }, rpc: {} }));
vi.mock('@renderer/features/chats/stores/chats', () => ({ chatsStore: {} }));
vi.mock('./workspace-admin', () => ({ administersWorkspaceById: () => true }));
vi.mock('@renderer/features/workspaces/workspaces-store', () => ({
  workspacesStore: {
    workspaces: [{ id: 'ws', tenantId: null }],
    idOnServerInScope: (serverId: string | null) => (serverId ? 'ws' : null),
  },
}));
vi.mock('@renderer/lib/modal/modal-provider', () => ({
  useModalContext: () => ({ setCloseGuard: () => {} }),
}));

// The dialog's frame needs an open dialog around it; its contents are the subject here.
vi.mock('@renderer/lib/ui/dialog', () => {
  const pass = ({ children }: { children?: unknown }) => children;
  return {
    DialogContentArea: pass,
    DialogFooter: pass,
    DialogHeader: pass,
    DialogTitle: pass,
  };
});

describe('CreateRoomModal', () => {
  it('labels its fields as the other forms do: Name, then Participants', () => {
    const html = renderToStaticMarkup(
      createElement(
        QueryClientProvider,
        { client: new QueryClient() },
        createElement(CreateRoomModal, { onSuccess: () => {}, onClose: () => {} })
      )
    );
    expect(html).toMatch(/<label[^>]*for="new-room-name"[^>]*>Name<\/label>/);
    expect(html).toContain('placeholder="Name this room"');
    expect(html).toMatch(/<label[^>]*for="new-room-participants"[^>]*>Participants<\/label>/);
    expect(html).not.toContain('uppercase');
    expect(html).not.toContain('does not offer Switch Console rooms');
  });

  it('says there is nowhere to create a room without group chats or a messaging app', () => {
    flags.group_chats = false;
    try {
      const html = renderToStaticMarkup(
        createElement(
          QueryClientProvider,
          { client: new QueryClient() },
          createElement(CreateRoomModal, { onSuccess: () => {}, onClose: () => {} })
        )
      );
      expect(html).toContain('does not offer Switch Console rooms');
    } finally {
      flags.group_chats = true;
    }
  });

  const render = (client: QueryClient) =>
    renderToStaticMarkup(
      createElement(
        QueryClientProvider,
        { client },
        createElement(CreateRoomModal, {
          serverId: 'srv',
          agentId: 'a1',
          onSuccess: () => {},
          onClose: () => {},
        })
      )
    );

  it('starts with the agent it is made from picked, once the agents are read', () => {
    const client = new QueryClient();
    client.setQueryData(
      ['remote-agents', 'ws'],
      [{ id: 'a1', name: 'agent-one', displayName: null, iconUrl: null, knownAgentType: null }]
    );
    client.setQueryData(['chat-agent-candidates', 'srv', 'ws', null], ['a1']);
    expect(render(client)).toContain('aria-label="Remove agent-one"');
  });
  it('labels a bridged room’s description and instructions as optional', () => {
    flags.group_chats = false;
    try {
      const client = new QueryClient();
      client.setQueryData(
        ['remote-bridges', 'ws'],
        [
          {
            id: 'b1',
            displayName: 'Discord',
            type: 'discord',
            status: 'active',
            canCreateChannels: true,
          },
        ]
      );
      const html = render(client);
      expect(html).toMatch(
        /<label[^>]*for="new-room-description"[^>]*>Description \(optional\)<\/label>/
      );
      expect(html).toMatch(
        /<label[^>]*for="new-room-instructions"[^>]*>Instructions \(optional\)<\/label>/
      );
    } finally {
      flags.group_chats = true;
    }
  });
});