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.

16.0 KB
Raw Blame
import type {
  MessagePart,
  ToolDetail,
  ToolName,
} from '@service-agent-fold/generated/types';
import { describe, expect, it } from 'vitest';
import { rendersOwnView, segmentParts } from './tool-groups';

const text = (): MessagePart => ({ kind: 'text', text: 'hi' });
type ToolUsePart = Extract<MessagePart, { kind: 'tool_use' }>;
const tool = (overrides?: Partial<ToolUsePart>): ToolUsePart => ({
  kind: 'tool_use',
  id: 'call',
  name: { kind: 'native', name: 'Read' },
  status: 'completed',
  detail: { kind: 'read', paths: ['a.rs'] },
  ...overrides,
});
const thought = (): MessagePart => ({ kind: 'thought', text: 'thinking...' });
/**
 * `displayResults`: the model composes a dynamic-UI view and the call renders
 * it. The answer itself, not a card about one.
 */
const displayResults = (): Extract<MessagePart, { kind: 'tool_use' }> => ({
  kind: 'tool_use',
  id: 'view',
  name: { kind: 'native', name: 'DisplayResults' },
  status: 'completed',
  detail: { kind: 'view', view: { widgets: [] }, error: null },
});
const permission = (): MessagePart => ({
  kind: 'permission',
  requestId: 'permission-test',
  toolCall: 'call',
  options: [],
  outcome: { kind: 'pending' },
});

describe('segmentParts', () => {
  it('leaves a message without tool calls as one segment per part', () => {
    expect(segmentParts([text(), text()])).toEqual([
      { kind: 'part', start: 0, end: 1 },
      { kind: 'part', start: 1, end: 2 },
    ]);
  });

  it('starts a group with the first tool call', () => {
    expect(segmentParts([text(), tool(), text()])).toEqual([
      { kind: 'part', start: 0, end: 1 },
      { kind: 'tools', start: 1, end: 2 },
      { kind: 'part', start: 2, end: 3 },
    ]);
  });

  it('keeps the group kind and start as later tools extend a lone call', () => {
    expect(segmentParts([text(), tool()])).toEqual([
      { kind: 'part', start: 0, end: 1 },
      { kind: 'tools', start: 1, end: 2 },
    ]);
    expect(segmentParts([text(), tool(), tool(), tool()])).toEqual([
      { kind: 'part', start: 0, end: 1 },
      { kind: 'tools', start: 1, end: 4 },
    ]);
  });

  it('folds a run of consecutive tool calls into one group', () => {
    expect(segmentParts([text(), tool(), tool(), tool(), text()])).toEqual([
      { kind: 'part', start: 0, end: 1 },
      { kind: 'tools', start: 1, end: 4 },
      { kind: 'part', start: 4, end: 5 },
    ]);
  });

  it('breaks a run at anything that is not a tool call', () => {
    // The permission prompt is waiting on the reader; it must stay visible.
    expect(
      segmentParts([tool(), tool(), permission(), tool(), tool()])
    ).toEqual([
      { kind: 'tools', start: 0, end: 2 },
      { kind: 'part', start: 2, end: 3 },
      { kind: 'tools', start: 3, end: 5 },
    ]);
  });

  it('splits a running group around a compaction that landed inside it', () => {
    const compaction: MessagePart = {
      kind: 'control',
      control: { kind: 'compact' },
      outcome: { kind: 'accepted' },
    };
    expect(
      segmentParts([tool({ status: 'running' }), compaction, tool()])
    ).toEqual([
      { kind: 'tools', start: 0, end: 1 },
      { kind: 'part', start: 1, end: 2 },
      { kind: 'tools', start: 2, end: 3 },
    ]);
  });

  describe.each([
    {
      label: 'native',
      name: { kind: 'native', name: 'DisplayResults' } satisfies ToolName,
    },
    {
      label: 'MCP',
      name: {
        kind: 'mcp',
        server: 'macro',
        tool: 'DisplayResults',
      } satisfies ToolName,
    },
  ])('DisplayResults called through $label', ({ name }) => {
    it.each(['pending', 'running', 'completed'] as const)(
      'breaks tool groups before and after a %s call',
      (status) => {
        const details: ToolDetail[] = [
          { kind: 'view', view: null, error: null },
        ];
        for (const detail of details) {
          const display = tool({ name, status, detail });
          expect(
            segmentParts([tool(), tool(), display, tool(), tool()])
          ).toEqual([
            { kind: 'tools', start: 0, end: 2 },
            { kind: 'part', start: 2, end: 3 },
            { kind: 'tools', start: 3, end: 5 },
          ]);
        }
      }
    );
  });

  it('keeps adjacent thoughts in their own runs around inline results', () => {
    expect(
      segmentParts([tool(), thought(), displayResults(), thought(), tool()])
    ).toEqual([
      { kind: 'tools', start: 0, end: 2 },
      { kind: 'part', start: 2, end: 3 },
      { kind: 'tools', start: 3, end: 5 },
    ]);
  });

  it('groups a run that closes the message', () => {
    expect(segmentParts([text(), tool(), tool()])).toEqual([
      { kind: 'part', start: 0, end: 1 },
      { kind: 'tools', start: 1, end: 3 },
    ]);
  });

  it('is empty for no parts', () => {
    expect(segmentParts([])).toEqual([]);
  });

  it('groups thinking blocks with tool calls', () => {
    expect(segmentParts([text(), thought(), tool(), tool(), text()])).toEqual([
      { kind: 'part', start: 0, end: 1 },
      { kind: 'tools', start: 1, end: 4 },
      { kind: 'part', start: 4, end: 5 },
    ]);
  });

  it('groups consecutive thinking blocks with tool calls', () => {
    expect(
      segmentParts([thought(), tool(), thought(), tool(), thought()])
    ).toEqual([
      { kind: 'tools', start: 0, end: 4 },
      { kind: 'part', start: 4, end: 5 },
    ]);
  });

  it('leaves a trailing thought out of a run that closes the message', () => {
    expect(segmentParts([thought(), tool(), thought()])).toEqual([
      { kind: 'tools', start: 0, end: 2 },
      { kind: 'part', start: 2, end: 3 },
    ]);
  });

  it('keeps a trailing thought in a run that prose follows', () => {
    expect(segmentParts([tool(), tool(), thought(), text()])).toEqual([
      { kind: 'tools', start: 0, end: 3 },
      { kind: 'part', start: 3, end: 4 },
    ]);
  });

  it('leaves a thought after a single tool as its own part', () => {
    expect(segmentParts([tool(), thought()])).toEqual([
      { kind: 'tools', start: 0, end: 1 },
      { kind: 'part', start: 1, end: 2 },
    ]);
  });

  it('keeps every trailing thought visible after the last tool', () => {
    expect(segmentParts([tool(), thought(), thought()])).toEqual([
      { kind: 'tools', start: 0, end: 1 },
      { kind: 'part', start: 1, end: 2 },
      { kind: 'part', start: 2, end: 3 },
    ]);
  });

  it('keeps a thought-only run as individual visible parts', () => {
    expect(
      segmentParts([text(), thought(), thought(), thought(), text()])
    ).toEqual([
      { kind: 'part', start: 0, end: 1 },
      { kind: 'part', start: 1, end: 2 },
      { kind: 'part', start: 2, end: 3 },
      { kind: 'part', start: 3, end: 4 },
      { kind: 'part', start: 4, end: 5 },
    ]);
  });

  it('keeps a lone thinking block as its own part', () => {
    expect(segmentParts([text(), thought(), text()])).toEqual([
      { kind: 'part', start: 0, end: 1 },
      { kind: 'part', start: 1, end: 2 },
      { kind: 'part', start: 2, end: 3 },
    ]);
  });

  it('groups thinking blocks at the start with tool calls', () => {
    expect(segmentParts([thought(), thought(), tool()])).toEqual([
      { kind: 'tools', start: 0, end: 3 },
    ]);
  });

  it('breaks a run at permissions even with thinking blocks', () => {
    expect(
      segmentParts([thought(), tool(), permission(), thought(), tool()])
    ).toEqual([
      { kind: 'tools', start: 0, end: 2 },
      { kind: 'part', start: 2, end: 3 },
      { kind: 'tools', start: 3, end: 5 },
    ]);
  });
  // A collapsed group would put the composed view behind a closed caret,
  // indented in a row of muted chips — the one part of the turn the reader
  // actually came for.
  it('keeps a view-rendering call out of the run it interrupts', () => {
    expect(
      segmentParts([tool(), tool(), displayResults(), tool(), tool()])
    ).toEqual([
      { kind: 'tools', start: 0, end: 2 },
      { kind: 'part', start: 2, end: 3 },
      { kind: 'tools', start: 3, end: 5 },
    ]);
  });

  it('keeps a view-rendering call alone even between single calls', () => {
    expect(segmentParts([tool(), displayResults(), tool()])).toEqual([
      { kind: 'tools', start: 0, end: 1 },
      { kind: 'part', start: 1, end: 2 },
      { kind: 'tools', start: 2, end: 3 },
    ]);
  });

  it('does not fold two view-rendering calls into each other', () => {
    expect(segmentParts([displayResults(), displayResults()])).toEqual([
      { kind: 'part', start: 0, end: 1 },
      { kind: 'part', start: 1, end: 2 },
    ]);
  });

  it('keeps a thought before the dashboard in the preceding tool group', () => {
    expect(
      segmentParts([thought(), tool(), thought(), displayResults()])
    ).toEqual([
      { kind: 'tools', start: 0, end: 3 },
      { kind: 'part', start: 3, end: 4 },
    ]);
  });

  it.each(['pending', 'running', 'completed', 'failed'] as const)(
    'keeps %s subagents visible outside ordinary tool groups',
    (status) => {
      const subagent = tool({
        status,
        detail: {
          kind: 'subagent',
          title: 'Investigate a bug',
          description: null,
          agentType: null,
          prompt: null,
          background: false,
          children: [],
          result: null,
        },
      });
      expect(
        segmentParts([tool(), thought(), subagent, thought(), tool()])
      ).toEqual([
        { kind: 'tools', start: 0, end: 2 },
        { kind: 'part', start: 2, end: 3 },
        { kind: 'tools', start: 3, end: 5 },
      ]);
    }
  );

  it.each(['pending', 'running', 'completed', 'failed'] as const)(
    'keeps %s user tools visible outside ordinary tool groups',
    (status) => {
      const userTool = tool({
        status,
        detail: {
          kind: 'user_tool',
          input: {},
          outcome: { kind: 'pending' },
        },
      });
      expect(
        segmentParts([thought(), tool(), userTool, tool(), tool()])
      ).toEqual([
        { kind: 'tools', start: 0, end: 2 },
        { kind: 'part', start: 2, end: 3 },
        { kind: 'tools', start: 3, end: 5 },
      ]);
    }
  );
});

describe('rendersOwnView', () => {
  it('does not treat a same-named external MCP tool as a dashboard', () => {
    const part = tool({
      name: { kind: 'mcp', server: 'external', tool: 'DisplayResults' },
      detail: {
        kind: 'other',
        acpKind: 'other',
        input: { view: { widgets: [] } },
        output: null,
        result: null,
        error: null,
      },
    });
    expect(rendersOwnView(part)).toBe(false);
    expect(segmentParts([tool(), part, tool()])).toEqual([
      { kind: 'tools', start: 0, end: 3 },
    ]);
  });

  it('is true for a Macro displayResults call', () => {
    expect(rendersOwnView(displayResults())).toBe(true);
  });

  it('recognizes a Macro displayResults call with an MCP namespace', () => {
    const part = displayResults();
    expect(
      rendersOwnView({
        ...part,
        name: { kind: 'mcp', server: 'macro', tool: 'DisplayResults' },
      })
    ).toBe(true);
  });

  it('is false for any other tool call, and for a non-tool part', () => {
    expect(rendersOwnView(tool())).toBe(false);
    expect(rendersOwnView(text())).toBe(false);
    expect(rendersOwnView(undefined)).toBe(false);
  });

  // A harness tool that happens to share the name is not Macro's: the fold
  // decides whose shape a call is in, and only a `macro` detail carries the
  // tool's own arguments.
  it('is false for a same-named call the fold did not read as a Macro tool', () => {
    expect(
      rendersOwnView({
        kind: 'tool_use',
        id: 'other',
        name: { kind: 'native', name: 'DisplayResults' },
        status: 'completed',
        detail: {
          kind: 'other',
          acpKind: 'other',
          output: null,
          input: null,
          result: null,
          error: null,
        },
      })
    ).toBe(false);
  });
});

describe.each(['GenerateImage', 'DispatchCodingAgent'])(
  '%s grouping',
  (name) => {
    it.each(['native', 'mcp'] as const)(
      'keeps %s calls outside surrounding tool groups in every state',
      (kind) => {
        for (const status of [
          'pending',
          'running',
          'completed',
          'failed',
        ] as const) {
          const standalone = tool({
            name:
              kind === 'native'
                ? { kind, name }
                : { kind, server: 'macro', tool: name },
            status,
            detail:
              kind === 'native'
                ? { kind: 'macro', input: null, output: null, error: null }
                : {
                    kind: 'other',
                    acpKind: 'other',
                    input: null,
                    output: null,
                    result: null,
                    error: null,
                  },
          });
          expect(rendersOwnView(standalone)).toBe(true);
          expect(segmentParts([tool(), standalone, tool()])).toEqual([
            { kind: 'tools', start: 0, end: 1 },
            { kind: 'part', start: 1, end: 2 },
            { kind: 'tools', start: 2, end: 3 },
          ]);
        }
      }
    );

    it('keeps an unrelated MCP tool with the same name in its group', () => {
      const external = tool({
        name: { kind: 'mcp', server: 'external', tool: name },
        detail: {
          kind: 'other',
          acpKind: 'other',
          input: null,
          output: null,
          result: null,
          error: null,
        },
      });
      expect(rendersOwnView(external)).toBe(false);
      expect(segmentParts([tool(), external, tool()])).toEqual([
        { kind: 'tools', start: 0, end: 3 },
      ]);
    });
  }
);

it('keeps trusted code executions visible so their inner views stay outside collapsed groups', () => {
  const execution = tool({
    name: { kind: 'mcp', server: 'macro_internal', tool: 'ExecuteCode' },
    detail: {
      kind: 'other',
      input: null,
      output: null,
      result: null,
      error: null,
      acpKind: 'other',
    },
  });
  expect(rendersOwnView(execution)).toBe(true);
  expect(segmentParts([tool(), execution, tool()])).toEqual([
    { kind: 'tools', start: 0, end: 1 },
    { kind: 'part', start: 1, end: 2 },
    { kind: 'tools', start: 2, end: 3 },
  ]);
  execution.name = { kind: 'mcp', server: 'external', tool: 'ExecuteCode' };
  expect(rendersOwnView(execution)).toBe(false);
});

describe('persistent review presentation', () => {
  const id = '019a0000-0000-7000-8000-000000000005';
  const presentation = (
    server = 'macro_internal',
    callId = `review_${id}`
  ): ToolUsePart =>
    tool({
      id: callId,
      name: { kind: 'mcp', server, tool: 'PresentReview' },
      detail: {
        kind: 'other',
        acpKind: 'other',
        input: { reviewId: id, kind: 'email', title: 'Drafts' },
        output: null,
        result: { reviewId: id, message: 'Presented to the user' },
        error: null,
      },
    });
  it('keeps a completed presentation visible outside collapsed tool groups', () => {
    expect(rendersOwnView(presentation())).toBe(true);
    expect(segmentParts([tool(), presentation(), tool()])).toEqual([
      { kind: 'tools', start: 0, end: 1 },
      { kind: 'part', start: 1, end: 2 },
      { kind: 'tools', start: 2, end: 3 },
    ]);
  });
  it('does not turn the native invocation receipt or an external tool into another card', () => {
    expect(
      rendersOwnView(presentation('macro_internal', 'ordinary-call'))
    ).toBe(false);
    expect(rendersOwnView(presentation('untrusted'))).toBe(false);
  });
});

describe('presented views', () => {
  const presentation = (): ToolUsePart =>
    tool({
      id: 'view_019a0000-0000-7000-8000-000000000006',
      name: { kind: 'mcp', server: 'macro_internal', tool: 'PresentView' },
      detail: {
        kind: 'view',
        view: { title: 'Launch notes', widgets: [] },
        error: null,
      },
    });
  it('draws the view the harness presented, outside collapsed tool groups', () => {
    expect(rendersOwnView(presentation())).toBe(true);
    expect(segmentParts([tool(), presentation(), tool()])).toEqual([
      { kind: 'tools', start: 0, end: 1 },
      { kind: 'part', start: 1, end: 2 },
      { kind: 'tools', start: 2, end: 3 },
    ]);
  });
});