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.

2.4 KB
Raw Blame
import { describe, expect, mock, test } from 'bun:test';

const { connectRoomWith } = await import('../../src/calls/common');

describe('connectRoomWith', () => {
  test('starts a channel, connects, and leaves once on repeated disconnects', async () => {
    const leave = mock(async () => ({ callEnded: false }));
    const connection = {
      call: { id: 'call-1' },
      url: 'wss://example.invalid',
      token: 'secret',
      identity: 'account-1',
      roomName: 'room-1',
      started: true,
      leave,
    };
    const startCall = mock(async () => connection);
    const connect = mock(async () => {});
    let disconnect = () => {};
    const room = {};

    const result = await connectRoomWith(
      { startCall },
      { onOtherCall: 'fail' },
      room,
      connect,
      (_room, callback) => {
        disconnect = callback;
      },
    );

    expect(result).toEqual({ room, connection });
    expect(startCall).toHaveBeenCalledWith({ onOtherCall: 'fail' });
    expect(connect).toHaveBeenCalledWith(room, connection);
    disconnect();
    disconnect();
    expect(leave).toHaveBeenCalledTimes(1);
  });

  test('leaves after a room connection failure', async () => {
    const leave = mock(async () => ({ callEnded: false }));
    const connection = {
      call: { id: 'call-2' },
      url: 'wss://example.invalid',
      token: 'secret',
      identity: 'account-2',
      roomName: 'room-2',
      started: false,
      leave,
    };
    const join = mock(async () => connection);

    await expect(
      connectRoomWith(
        { join },
        undefined,
        {},
        async () => {
          throw new Error('connect failed');
        },
        () => {},
      ),
    ).rejects.toThrow('connect failed');
    expect(leave).toHaveBeenCalledTimes(1);
  });

  test('preserves the connection error when leaving also fails', async () => {
    const leave = mock(async () => {
      throw new Error('leave failed');
    });
    const connection = {
      call: { id: 'call-3' },
      url: 'wss://example.invalid',
      token: 'secret',
      identity: 'account-3',
      roomName: 'room-3',
      started: false,
      leave,
    };

    await expect(
      connectRoomWith(
        { join: async () => connection },
        undefined,
        {},
        async () => {
          throw new Error('connect failed');
        },
        () => {},
      ),
    ).rejects.toThrow('connect failed');
    expect(leave).toHaveBeenCalledTimes(1);
  });
});