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.

6.0 KB
Raw Blame
import { MACRO_AGENT_PRINCIPAL_ID } from '@core/constant/macroAgent';
import {
  MACRO_CODER_NAME,
  MACRO_CODER_PRINCIPAL_ID,
} from '@core/constant/macroCoder';
import {
  MACRO_SYSTEM_BOT_ID,
  MACRO_SYSTEM_NAME,
  MACRO_SYSTEM_PRINCIPAL_ID,
} from '@core/constant/macroSystem';
import { describe, expect, it } from 'vitest';
import {
  type ChannelMessageWithMaybeSender,
  firstPartyBotName,
  getBotDisplayName,
  normalizeChannelMessageSender,
  normalizeMessageThreadSenders,
  normalizeThreadReplySender,
  senderFromStorageId,
} from '../../messages/message-sender';

function legacyMessage(
  senderId: string,
  replySenderId = 'macro|[email protected]'
): ChannelMessageWithMaybeSender {
  return {
    id: 'message-1',
    parent: { type: 'channel', id: 'channel-1' },
    mentions: [],
    state: {
      root_id: 'message-1',
      user_id: senderId,
      created_at: '2026-05-28T10:00:00Z',
      updated_at: '2026-05-28T10:00:00Z',
      resolved: false,
    },
    sender_id: senderId,
    content: 'hello',
    created_at: '2026-05-28T10:00:00.000Z',
    updated_at: '2026-05-28T10:00:00.000Z',
    attachments: [],
    reactions: [],
    thread: {
      preview: [
        {
          id: 'reply-1',
          parent: { type: 'channel', id: 'channel-1' },
          mentions: [],
          sender_id: replySenderId,
          content: 'reply',
          created_at: '2026-05-28T10:01:00.000Z',
          updated_at: '2026-05-28T10:01:00.000Z',
          attachments: [],
          reactions: [],
        },
      ],
      reply_count: 1,
      latest_reply_at: '2026-05-28T10:01:00.000Z',
    },
  };
}

describe('message sender normalization', () => {
  it('derives user senders for old channel message payloads', () => {
    const message = normalizeChannelMessageSender(
      legacyMessage('macro|[email protected]')
    );

    expect(message.sender).toEqual({
      type: 'user',
      id: 'macro|[email protected]',
    });
    expect(
      normalizeThreadReplySender(message.thread.preview[0]).sender
    ).toEqual({
      type: 'user',
      id: 'macro|[email protected]',
    });
  });

  it('derives bot sender ids from storage ids', () => {
    expect(
      senderFromStorageId('bot|00000000-0000-0000-0000-000000000001')
    ).toEqual({
      type: 'bot',
      id: '00000000-0000-0000-0000-000000000001',
    });
  });

  it('keeps enriched bot name and avatar from the API sender', () => {
    const enriched = {
      ...legacyMessage('bot|00000000-0000-0000-0000-000000000001'),
      sender: {
        type: 'bot' as const,
        id: '00000000-0000-0000-0000-000000000001',
        name: 'Deploy Bot',
        avatar_url: 'https://example.com/bot.png',
      },
    };

    const message = normalizeChannelMessageSender(enriched);

    expect(message.sender).toEqual({
      type: 'bot',
      id: '00000000-0000-0000-0000-000000000001',
      name: 'Deploy Bot',
      avatar_url: 'https://example.com/bot.png',
    });
  });

  it('resolves bot names from channel bot data', () => {
    expect(
      getBotDisplayName('bot|00000000-0000-0000-0000-000000000001', undefined, [
        {
          id: '00000000-0000-0000-0000-000000000001',
          name: 'Deploy Bot',
        },
      ])
    ).toBe('Deploy Bot');
  });

  it('resolves built-in agent names without channel bot data', () => {
    expect(getBotDisplayName(MACRO_CODER_PRINCIPAL_ID)).toBe(MACRO_CODER_NAME);
  });

  it('names the system principal without channel bot data', () => {
    expect(getBotDisplayName(MACRO_SYSTEM_PRINCIPAL_ID)).toBe(
      MACRO_SYSTEM_NAME
    );
    expect(firstPartyBotName(MACRO_SYSTEM_BOT_ID)).toBe(MACRO_SYSTEM_NAME);
    expect(firstPartyBotName('00000000-0000-0000-0000-000000000002')).toBe(
      undefined
    );
  });

  it('names a post made through an MCP client after the client', () => {
    const message = normalizeThreadReplySender({
      id: 'reply-1',
      parent: { type: 'channel', id: 'channel-1' },
      mentions: [],
      sender_id: MACRO_AGENT_PRINCIPAL_ID,
      bot_profile: { name: 'Macro' },
      triggered_by: 'macro|[email protected]',
      triggered_via: 'Claude Code',
      content: 'posted from Claude Code',
      created_at: '2026-11-02T10:00:00.000Z',
      updated_at: '2026-11-02T10:00:00.000Z',
      attachments: [],
      reactions: [],
    });

    expect(getBotDisplayName(message.sender_id, message.sender)).toBe(
      'Claude Code'
    );
    expect(getBotDisplayName(MACRO_AGENT_PRINCIPAL_ID)).toBe('Macro');
  });

  it('uses a generic label rather than exposing an unknown bot UUID', () => {
    expect(getBotDisplayName('bot|00000000-0000-0000-0000-000000000002')).toBe(
      'Bot'
    );
  });

  it('derives agent senders for a fetched thread from its bot profiles', () => {
    const {
      state,
      thread: _thread,
      ...root
    } = legacyMessage('bot|00000000-0000-0000-0000-000000000001');
    const bot = {
      ...root,
      bot_profile: { name: 'Bingus', avatar_url: 'https://example.com/b.png' },
      triggered_by: 'macro|[email protected]',
    };
    const reply = {
      ...root,
      id: 'reply-1',
      thread_id: root.id,
      sender_id: 'bot|00000000-0000-0000-0000-000000000003',
      bot_profile: { name: 'Deploy Bot', avatar_url: null },
    };

    const thread = normalizeMessageThreadSenders({
      state,
      root: bot,
      replies: [
        reply,
        {
          ...root,
          id: 'reply-2',
          sender_id: 'macro|[email protected]',
          bot_profile: null,
        },
      ],
    });

    expect(thread.root.sender).toEqual({
      type: 'bot',
      id: '00000000-0000-0000-0000-000000000001',
      name: 'Bingus',
      avatar_url: 'https://example.com/b.png',
      triggered_by: 'macro|[email protected]',
    });
    expect(getBotDisplayName(thread.root.sender_id, thread.root.sender)).toBe(
      'Bingus'
    );
    expect(thread.replies.map((message) => message.sender)).toEqual([
      expect.objectContaining({ type: 'bot', name: 'Deploy Bot' }),
      expect.objectContaining({ type: 'user', id: 'macro|[email protected]' }),
    ]);
  });
});