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.

8.3 KB
Raw Blame
import { afterEach, describe, expect, test } from 'bun:test';
import { Macro } from '../src/macro';
import { MacroApiError, MacroError, MacroNotFoundError } from '../src/utils';

const TEAM_ID = '0198a4cc-e138-7670-a308-a6b766602700';
const OTHER_TEAM_ID = '0198a4cc-e138-7670-a308-a6b766602701';
const INVITE_ID = '0198a4cc-e138-7670-a308-a6b766602702';

const originalFetch = globalThis.fetch;
afterEach(() => {
  globalThis.fetch = originalFetch;
});

type Route = (request: Request) => Response | Promise<Response>;

/** Serve `routes` by `METHOD /path`, recording every request. */
function serve(routes: Record<string, Route>): Request[] {
  const requests: Request[] = [];
  globalThis.fetch = Object.assign(
    async (input: RequestInfo | URL, init?: RequestInit) => {
      const request =
        input instanceof Request ? input : new Request(input, init);
      requests.push(request.clone());
      const key = `${request.method} ${new URL(request.url).pathname}`;
      const route = routes[key];
      if (!route) throw new Error(`unexpected request ${key}`);
      return route(request);
    },
    { preconnect: originalFetch.preconnect },
  );
  return requests;
}

function macro(): Macro {
  return new Macro({
    auth: { type: 'user', apiKey: 'mak_test' },
    hosts: { auth: 'https://auth.test' },
    webAppUrl: 'https://app.example.test',
  });
}

const currentTeam = () =>
  Response.json({
    team: {
      id: TEAM_ID,
      name: 'Acme',
      slug: 'acme',
      owner_id: 'macro|[email protected]',
    },
    members: [],
  });

const INVITE = {
  id: INVITE_ID,
  email: '[email protected]',
  team_id: TEAM_ID,
  team_role: 'member',
  invited_by: 'macro|[email protected]',
  created_at: '2026-01-01T00:00:00Z',
  last_sent_at: '2026-01-02T00:00:00Z',
};

describe('Team.invite', () => {
  test('invites trimmed addresses to the team the handle names', async () => {
    const requests = serve({
      'POST /team/invite': () => new Response(null, { status: 201 }),
    });
    const sent = await macro()
      .teams.byId(TEAM_ID)
      .invite([' [email protected] ', '', '[email protected]']);
    expect(sent).toBe(true);
    expect(requests).toHaveLength(1);
    expect(await requests[0]?.json()).toEqual({
      invites: [
        { email: '[email protected]' },
        { email: '[email protected]' },
      ],
      team_id: TEAM_ID,
    });
    expect(requests[0]?.headers.get('x-macro-user-api-key')).toBe('mak_test');
  });

  test('sends a repeated address once, whatever its letter case', async () => {
    const requests = serve({
      'POST /team/invite': () => new Response(null, { status: 201 }),
    });
    await macro()
      .teams.byId(TEAM_ID)
      .invite([
        '[email protected]',
        '[email protected] ',
        '[email protected]',
        '[email protected]',
      ]);
    expect(await requests[0]?.json()).toEqual({
      invites: [
        { email: '[email protected]' },
        { email: '[email protected]' },
      ],
      team_id: TEAM_ID,
    });
  });

  test('accepts a single address on the current team', async () => {
    const requests = serve({
      'GET /team': currentTeam,
      'POST /team/invite': () => new Response(null, { status: 201 }),
    });
    expect(
      await (await macro().teams.current()).invite('[email protected]'),
    ).toBe(true);
    const post = requests.find((request) => request.method === 'POST');
    expect(await post?.json()).toEqual({
      invites: [{ email: '[email protected]' }],
      team_id: TEAM_ID,
    });
  });

  test('reports false when every address was already a member or invited', async () => {
    serve({
      'POST /team/invite': () => new Response(null, { status: 304 }),
    });
    expect(
      await macro().teams.byId(TEAM_ID).invite('[email protected]'),
    ).toBe(false);
  });

  test("surfaces the refusal when the team is no longer the caller's", async () => {
    const requests = serve({
      'POST /team/invite': () =>
        Response.json(
          { message: "the requested team is not the caller's current team" },
          { status: 409 },
        ),
    });
    const error = await macro()
      .teams.byId(OTHER_TEAM_ID)
      .invite('[email protected]')
      .catch((caught: unknown) => caught);
    expect(error).toBeInstanceOf(MacroApiError);
    expect((error as MacroApiError).status).toBe(409);
    expect(requests).toHaveLength(1);
    expect(await requests[0]?.json()).toMatchObject({ team_id: OTHER_TEAM_ID });
  });

  test('refuses an empty list without calling the API', async () => {
    const requests = serve({});
    await expect(
      macro().teams.byId(TEAM_ID).invite(['  ', '']),
    ).rejects.toBeInstanceOf(MacroError);
    expect(requests).toHaveLength(0);
  });

  test('surfaces the server refusal', async () => {
    serve({
      'POST /team/invite': () =>
        Response.json(
          { message: 'only admins can invite to this team' },
          { status: 403 },
        ),
    });
    const error = await macro()
      .teams.byId(TEAM_ID)
      .invite('[email protected]')
      .catch((caught: unknown) => caught);
    expect(error).toBeInstanceOf(MacroApiError);
    expect((error as MacroApiError).status).toBe(403);
    expect((error as MacroApiError).message).toContain('only admins');
  });
});

describe('Team.current', () => {
  test('reports a caller without a team as not found', async () => {
    serve({ 'GET /team': () => new Response(null, { status: 204 }) });
    await expect(macro().teams.current()).rejects.toBeInstanceOf(
      MacroNotFoundError,
    );
  });
});

describe('Team.invites', () => {
  test('lists the pending invites of the team the handle names', async () => {
    const requests = serve({
      'GET /team/invites': () => Response.json({ invites: [INVITE] }),
    });
    const [pending, ...rest] = await macro().teams.byId(TEAM_ID).invites();
    expect(rest).toHaveLength(0);
    expect(pending?.id).toBe(INVITE_ID);
    expect(await pending?.email()).toBe('[email protected]');
    expect(await pending?.role()).toBe('member');
    expect(await pending?.lastSentAt()).toBe('2026-01-02T00:00:00Z');
    expect((await pending?.invitedBy())?.id).toBe('macro|[email protected]');
    expect(
      requests.map((request) =>
        new URL(request.url).searchParams.get('team_id'),
      ),
    ).toEqual([TEAM_ID]);
    // Seeded from the listing: reading fields fetched nothing more.
    expect(requests).toHaveLength(1);
  });

  test('revokes an invite, after which it no longer resolves', async () => {
    let revoked = false;
    const requests = serve({
      'GET /team/invites': () =>
        Response.json({ invites: revoked ? [] : [INVITE] }),
      [`DELETE /team/invite/${INVITE_ID}`]: () => {
        revoked = true;
        return new Response(null, { status: 200 });
      },
    });
    const [pending] = await macro().teams.byId(TEAM_ID).invites();
    await pending?.revoke();
    expect(
      requests.map(
        (request) => `${request.method} ${new URL(request.url).pathname}`,
      ),
    ).toEqual(['GET /team/invites', `DELETE /team/invite/${INVITE_ID}`]);
    await expect(pending?.email()).rejects.toBeInstanceOf(MacroNotFoundError);
  });
});

describe('macro.referrals', () => {
  test('builds the referral signup link from the caller code', async () => {
    serve({ 'GET /referral/code': () => Response.json('CODE+42') });
    expect(await macro().referrals.link()).toEqual({
      code: 'CODE+42',
      url: 'https://app.example.test/app/signup?referral_code=CODE%2B42',
    });
  });

  test('emails a referral invite to one trimmed address', async () => {
    const requests = serve({
      'POST /referral/send': () => new Response(null, { status: 204 }),
    });
    await macro().referrals.invite(' [email protected] ');
    expect(await requests[0]?.json()).toEqual({
      recipient: '[email protected]',
    });
  });

  test('refuses an empty address and surfaces rate limiting', async () => {
    serve({
      'POST /referral/send': () =>
        Response.json({ message: 'rate limit exceeded' }, { status: 429 }),
    });
    await expect(macro().referrals.invite('   ')).rejects.toBeInstanceOf(
      MacroError,
    );
    const error = await macro()
      .referrals.invite('[email protected]')
      .catch((caught: unknown) => caught);
    expect(error).toBeInstanceOf(MacroApiError);
    expect((error as MacroApiError).status).toBe(429);
  });
});