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.9 KB
Raw Blame
import type { ItemMention } from '@core/component/LexicalMarkdown/plugins';
import { verifyBlockName } from '@core/constant/allBlocks';
import { extractChannelMentionsFromMarkdown } from '@macro-inc/lexical-core/utils/markdown-mentions';
import { blockNameToItemType } from '@service-storage/client';
import { describe, expect, it, vi } from 'vitest';
import { buildPostMessageRequest } from '../message-payload';

const snap = (mentions: ItemMention[]) => ({
  value: '',
  mentions,
  attachments: [],
});

vi.mock('@service-storage/websocket', () => ({
  storageWS: { reconnectIfDisconnected: vi.fn() },
  createWebSocketJob: vi.fn(),
}));
vi.mock('@service-connection/websocket', () => ({
  ws: { addEventListener: vi.fn(), send: vi.fn() },
  state: () => 'closed',
  createConnectionBlockWebsocketEffect: vi.fn(),
  createConnectionWebsocketEffect: vi.fn(),
}));

describe('authored group references', () => {
  it('sends a mentioned WhatsApp group so posting shares it with the channel', () => {
    const result = buildPostMessageRequest({
      snapshot: snap([
        {
          itemType: 'external_chat',
          itemId: '01a16445-9fdc-7bb1-80b4-8935a1243c98',
        },
      ]),
    });
    expect(result.mentions).toEqual([
      {
        entity_type: 'external_chat',
        entity_id: '01a16445-9fdc-7bb1-80b4-8935a1243c98',
      },
    ]);
  });

  it('sends routine references with the same wire type as headless extraction, including saved mentions', () => {
    const blockNames = ['routine', 'automation'];
    const snapshot = snap(
      blockNames.map((blockName) => {
        const itemType = blockNameToItemType(verifyBlockName(blockName));
        if (itemType !== 'routine')
          throw new Error('Routine misclassified as ' + itemType);
        return { itemType, itemId: 'routine-1' };
      })
    );
    const result = buildPostMessageRequest({ snapshot });
    const markdown = blockNames
      .map(
        (blockName) =>
          `<m-document-mention>${JSON.stringify({ documentId: 'routine-1', blockName, documentName: 'Review tasks', blockParams: {} })}</m-document-mention>`
      )
      .join(' ');
    expect(result.mentions).toEqual([
      { entity_type: 'automation', entity_id: 'routine-1' },
    ]);
    expect(result.mentions).toEqual(
      extractChannelMentionsFromMarkdown(markdown).map(
        ({ entityType, entityId }) => ({
          entity_type: entityType,
          entity_id: entityId,
        })
      )
    );
  });

  it('preserves display-only mention chips in the body without blocking a send', () => {
    const snapshot = {
      ...snap([
        { itemType: 'date', itemId: '2026-09-09' },
        { itemType: 'contact', itemId: 'contact-1' },
        {
          itemType: 'foreign',
          itemId: 'https://github.com/example/repo/pull/1',
        },
        { itemType: 'skill', itemId: 'builtin:review' },
        { itemType: 'document', itemId: 'doc-1' },
        { itemType: 'user', itemId: 'macro|[email protected]' },
      ]),
      value:
        'Review the PR with this contact on <m-date-mention>{"date":"2026-09-09"}</m-date-mention>.',
    };
    const result = buildPostMessageRequest({ snapshot });
    expect(result.content).toBe(snapshot.value);
    expect(result.mentions).toEqual([
      { entity_type: 'document', entity_id: 'doc-1' },
      { entity_type: 'user', entity_id: 'macro|[email protected]' },
    ]);
  });

  it('retains @here and explicit users without depending on a cached participant roster', () => {
    const result = buildPostMessageRequest({
      snapshot: snap([
        { itemType: 'user', itemId: 'macro|[email protected]' },
        { itemType: 'group', itemId: 'here', groupAlias: 'here' },
        { itemType: 'group', itemId: 'here', groupAlias: 'here' },
      ]),
    });
    expect(result.mentions).toEqual([
      { entity_type: 'user', entity_id: 'macro|[email protected]' },
      { entity_type: 'group', entity_id: 'here' },
    ]);
  });
  it('normalizes email, call, and calendar references with the same vocabulary as Markdown extraction', () => {
    const result = buildPostMessageRequest({
      snapshot: snap([
        { itemType: 'thread', itemId: 'email-1' },
        { itemType: 'call', itemId: 'call-1' },
        { itemType: 'calendar_event', itemId: 'event-1' },
      ]),
    });
    expect(result.mentions?.map((mention) => mention.entity_type)).toEqual([
      'thread',
      'call',
      'calendar_event',
    ]);
  });
});

describe('buildPostMessageRequest — bot mentions', () => {
  const MACRO_AI_PRINCIPAL = 'bot|00000000-0000-0000-0000-00000000a1a1';
  const BOT_PRINCIPAL = 'bot|11111111-1111-1111-1111-111111111111';

  it('re-tags bot-principal user mentions as bot mentions', () => {
    const result = buildPostMessageRequest({
      snapshot: snap([
        { itemType: 'user', itemId: BOT_PRINCIPAL },
        { itemType: 'user', itemId: 'macro|[email protected]' },
        { itemType: 'user', itemId: MACRO_AI_PRINCIPAL },
      ]),
    });

    expect(result.mentions).toEqual([
      { entity_type: 'bot', entity_id: BOT_PRINCIPAL },
      { entity_type: 'user', entity_id: 'macro|[email protected]' },
      { entity_type: 'bot', entity_id: MACRO_AI_PRINCIPAL },
    ]);
  });

  it('deduplicates repeated bot mentions', () => {
    const result = buildPostMessageRequest({
      snapshot: snap([
        { itemType: 'user', itemId: BOT_PRINCIPAL },
        { itemType: 'user', itemId: BOT_PRINCIPAL },
      ]),
    });

    expect(result.mentions).toEqual([
      { entity_type: 'bot', entity_id: BOT_PRINCIPAL },
    ]);
  });

  it('does not fan bots out through @here', () => {
    const result = buildPostMessageRequest({
      snapshot: snap([
        { itemType: 'group', itemId: 'here', groupAlias: 'here' },
      ]),
      // Recipient expansion happens on the server; no bot principal is authored.
    });

    expect(result.mentions).toEqual([
      { entity_type: 'group', entity_id: 'here' },
    ]);
  });
});