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.

4.7 KB
Raw Blame
import { ok } from 'neverthrow';
import { beforeEach, describe, expect, it, vi } from 'vitest';

const requests = vi.hoisted(() => ({
  authenticated: vi.fn(),
  public: vi.fn(),
}));
vi.mock('@core/constant/servers', async (importOriginal) => ({
  ...(await importOriginal<typeof import('@core/constant/servers')>()),
  SERVER_HOSTS: { 'document-storage-service': 'https://gateway.example/dss' },
}));
vi.mock('@core/util/fetchWithToken', () => ({
  fetchWithToken: requests.authenticated,
}));
vi.mock('@core/util/safeFetch', () => ({ safeFetch: requests.public }));

import { callServiceClient } from './client';

beforeEach(() => {
  vi.clearAllMocks();
  requests.authenticated.mockResolvedValue(ok({}));
  requests.public.mockResolvedValue(ok({}));
});

describe('meeting transport authorization', () => {
  it('reads and posts guest chat with only the scoped RTC bearer', async () => {
    await callServiceClient.getGuestChat('call-id', 'room-token');
    await callServiceClient.postGuestChat('call-id', 'room-token', {
      id: 'message-id',
      content: 'Hello',
    });
    expect(requests.public).toHaveBeenNthCalledWith(
      1,
      'https://gateway.example/dss/call/guest-chat/call-id',
      {
        credentials: 'omit',
        headers: { Authorization: 'Bearer room-token' },
      }
    );
    expect(requests.public).toHaveBeenNthCalledWith(
      2,
      'https://gateway.example/dss/call/guest-chat/call-id',
      {
        method: 'POST',
        credentials: 'omit',
        headers: { Authorization: 'Bearer room-token' },
        body: JSON.stringify({ id: 'message-id', content: 'Hello' }),
      }
    );
    expect(requests.authenticated).not.toHaveBeenCalled();
  });
  it.each([
    [400, 'Bad request: Invalid channel ID format', 'MEETINGS_UNAVAILABLE'],
    [400, 'Bad request: Invalid title', 'HTTP_ERROR'],
    [403, 'Forbidden', 'FORBIDDEN'],
    [500, 'Internal error', 'SERVER_ERROR'],
  ])(
    'classifies the meeting list response %s / %s',
    async (status, message, code) => {
      requests.authenticated.mockResolvedValue(ok({ meetings: [] }));
      await callServiceClient.getMeetings();
      const [url, options] = requests.authenticated.mock.calls[0];
      expect(url).toBe('https://gateway.example/dss/call/meetings');
      const error = await options.errorResponseHandler(
        new Response(JSON.stringify({ message }), { status })
      );
      expect(error.code).toBe(code);
    }
  );
  it('loads public metadata without Macro session credentials', async () => {
    await callServiceClient.getMeeting('secret');
    expect(requests.public).toHaveBeenCalledWith(
      'https://gateway.example/dss/call/join/secret',
      { credentials: 'omit' }
    );
    expect(requests.authenticated).not.toHaveBeenCalled();
  });
  it('joins guests with only their display name and no session credentials', async () => {
    await callServiceClient.joinMeetingAsGuest('secret', 'Taylor');
    expect(requests.public).toHaveBeenCalledWith(
      'https://gateway.example/dss/call/join/secret',
      {
        method: 'POST',
        credentials: 'omit',
        body: JSON.stringify({ displayName: 'Taylor' }),
      }
    );
    expect(requests.authenticated).not.toHaveBeenCalled();
  });
  it('keeps signed-in joins on the authenticated endpoint', async () => {
    await callServiceClient.joinMeeting('secret');
    expect(requests.authenticated).toHaveBeenCalledWith(
      'https://gateway.example/dss/call/meetings/join/secret',
      { method: 'POST' }
    );
    expect(requests.public).not.toHaveBeenCalled();
  });
  it('leaves with the scoped room token and never account credentials', async () => {
    await callServiceClient.leaveMeeting('secret', 'scoped-room-token');
    expect(requests.public).toHaveBeenCalledWith(
      'https://gateway.example/dss/call/join/secret/leave',
      {
        method: 'POST',
        keepalive: true,
        credentials: 'omit',
        headers: { Authorization: 'Bearer scoped-room-token' },
      }
    );
    expect(requests.authenticated).not.toHaveBeenCalled();
  });
});

it('previews members through the authenticated endpoint without joining', async () => {
  await callServiceClient.getMeetingParticipants('secret', true);
  expect(requests.authenticated).toHaveBeenCalledWith(
    'https://gateway.example/dss/call/meetings/join/secret/participants'
  );
  expect(requests.public).not.toHaveBeenCalled();
});
it('previews guests without account credentials or a join request', async () => {
  await callServiceClient.getMeetingParticipants('secret', false);
  expect(requests.public).toHaveBeenCalledWith(
    'https://gateway.example/dss/call/join/secret/participants',
    { credentials: 'omit' }
  );
  expect(requests.authenticated).not.toHaveBeenCalled();
});