Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

5.1 KB
Raw Blame
/**
 * @vitest-environment jsdom
 */

import type { MessageData } from '@core/messages/types';
import { render, screen } from '@solidjs/testing-library';
import userEvent from '@testing-library/user-event';
import { createSignal } from 'solid-js';
import { describe, expect, it, vi } from 'vitest';
import { formatReactorNames } from '../ReactionChip';
import { Reactions } from '../Reactions';
import { Root } from '../Root';

vi.mock('@core/context/user', () => ({
  useUserId: () => () => 'user-1',
}));

vi.mock('@core/user', () => ({
  idToDisplayName: (id: string) => id.replace('macro|', ''),
}));

const baseMessage: MessageData = {
  id: 'message-1',
  content: 'hello',
  sender_id: 'user-2',
  created_at: '2026-02-25T00:00:00.000Z',
  updated_at: '2026-02-25T00:00:00.000Z',
  attachments: [],
  reactions: [],
};

describe('Reactions', () => {
  it('does not render row when there are no reactions', () => {
    const { container } = render(() => (
      <Root
        message={{
          ...baseMessage,
          reactions: [],
        }}
        actions={{
          onReact: () => undefined,
        }}
      >
        <Reactions />
      </Root>
    ));

    expect(container.querySelector('[data-message-reactions-row]')).toBeNull();
    expect(screen.queryByRole('button', { name: 'Add reaction' })).toBeNull();
  });

  it('marks only the current user’s reactions as pressed and updates with membership', () => {
    const [reactions, setReactions] = createSignal([
      { emoji: '❤️', users: ['user-1', 'user-2'] },
      { emoji: '👍', users: ['user-3'] },
    ]);
    render(() => (
      <Root
        message={{
          ...baseMessage,
          reactions: reactions(),
        }}
        actions={{
          onReact: () => undefined,
        }}
      >
        <Reactions />
      </Root>
    ));

    const heart = screen.getByRole('button', { name: /❤️/u, pressed: true });
    const thumb = screen.getByRole('button', { name: /👍/u, pressed: false });

    expect(heart.hasAttribute('data-user-reacted')).toBe(true);
    expect(thumb.hasAttribute('data-user-reacted')).toBe(false);

    setReactions([
      { emoji: '❤️', users: ['user-2'] },
      { emoji: '👍', users: ['user-3', 'user-1'] },
    ]);

    expect(
      screen
        .getByRole('button', { name: /❤️/u, pressed: false })
        .hasAttribute('data-user-reacted')
    ).toBe(false);
    expect(
      screen
        .getByRole('button', { name: /👍/u, pressed: true })
        .hasAttribute('data-user-reacted')
    ).toBe(true);
  });

  it('calls onReact with chip emoji when a reaction chip is clicked', async () => {
    const user = userEvent.setup({ skipHover: true });
    const onReact = vi.fn();

    render(() => (
      <Root
        message={{
          ...baseMessage,
          reactions: [{ emoji: '😂', users: ['user-3'] }],
        }}
        actions={{
          onReact,
        }}
      >
        <Reactions />
      </Root>
    ));

    const chip = screen
      .getAllByRole('button', { name: /😂/u })
      .find((el) => el.hasAttribute('data-message-reaction-chip'))!;
    await user.click(chip);

    expect(onReact).toHaveBeenCalledTimes(1);
    expect(onReact.mock.calls[0]?.[0]?.message?.id).toBe('message-1');
    expect(onReact.mock.calls[0]?.[0]?.emoji).toBe('😂');
  });
});

describe('formatReactorNames', () => {
  it('returns empty string for empty input', () => {
    expect(formatReactorNames([], 'user-1')).toBe('');
  });

  it('shows "You" when only the current user reacted', () => {
    expect(formatReactorNames(['user-1'], 'user-1')).toBe('You');
  });

  it('shows the display name for a single other user', () => {
    expect(formatReactorNames(['macro|[email protected]'], 'user-1')).toBe(
      '[email protected]'
    );
  });

  it('joins two users with "and"', () => {
    expect(
      formatReactorNames(['user-1', 'macro|[email protected]'], 'user-1')
    ).toBe('You and [email protected]');
  });

  it('uses Oxford comma for three or more users', () => {
    expect(
      formatReactorNames(
        ['user-1', 'macro|[email protected]', 'macro|[email protected]'],
        'user-1'
      )
    ).toBe('You, [email protected], and [email protected]');
  });

  it('handles no current user match', () => {
    expect(
      formatReactorNames(
        ['macro|[email protected]', 'macro|[email protected]'],
        'other-user'
      )
    ).toBe('[email protected] and [email protected]');
  });

  it('counts reactors outside Macro instead of naming them', () => {
    expect(
      formatReactorNames(
        ['whatsapp-reactor:1', 'user-1', 'whatsapp-reactor:2'],
        'user-1'
      )
    ).toBe('You and 2 other people');
    expect(formatReactorNames(['whatsapp-reactor:1'], 'user-1')).toBe(
      '1 other person'
    );
  });

  it('puts current user first regardless of input order', () => {
    expect(
      formatReactorNames(['macro|[email protected]', 'user-1'], 'user-1')
    ).toBe('You and [email protected]');

    expect(
      formatReactorNames(
        ['macro|[email protected]', 'macro|[email protected]', 'user-1'],
        'user-1'
      )
    ).toBe('You, [email protected], and [email protected]');
  });
});