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.9 KB
Raw Blame
import { createRoot, createSignal } from 'solid-js';
import { expect, it } from 'vitest';
import { message } from '../../email-message/tests/messages';
import type { SenderInbox } from '../core/email-sender';
import { createEmailFormState } from './email-form-state';

const primary: SenderInbox = { id: 'inbox', email_address: '[email protected]' };
const loaded: SenderInbox = {
  ...primary,
  sendAs: [{ email_address: '[email protected]' }],
};
const parent = message('parent', {
  link_id: 'inbox',
  from: { email: '[email protected]' },
  to: [{ email: '[email protected]' }],
  cc: [{ email: '[email protected]' }],
});

it('late aliases update the default without writing a draft or keeping self in reply-all', () =>
  createRoot((dispose) => {
    try {
      const [inboxes, setInboxes] = createSignal<SenderInbox[]>([primary]);
      const form = createEmailFormState(
        { viewerEmail: () => '[email protected]', inboxes },
        { type: 'replying_to', messageId: 'parent' },
        {
          getMessageById: () => parent,
          getDraftForMessageReply: () => undefined,
        }
      );
      setInboxes([loaded]);
      expect(form.senderEmail()).toBe('[email protected]');
      expect(form.recipients().to.map((item) => item.data.email)).toEqual([
        '[email protected]',
      ]);
      expect(form.recipients().cc).toEqual([]);
      expect(form.editRevision()).toBe(0);
    } finally {
      dispose();
    }
  }));

it('a manual primary choice wins over late aliases and refetches', () =>
  createRoot((dispose) => {
    try {
      const [inboxes, setInboxes] = createSignal<SenderInbox[]>([primary]);
      const form = createEmailFormState(
        { viewerEmail: () => '[email protected]', inboxes },
        { type: 'replying_to', messageId: 'parent' },
        {
          getMessageById: () => parent,
          getDraftForMessageReply: () => undefined,
        }
      );
      form.setSelectedInbox('inbox', '[email protected]');
      setInboxes([loaded]);
      expect(form.senderEmail()).toBe('[email protected]');
      form.setSelectedInbox('inbox', '[email protected]');
      setInboxes([primary]);
      expect(form.senderEmail()).toBe('[email protected]');
    } finally {
      dispose();
    }
  }));

it.each(['[email protected]', '[email protected]'])(
  'restores the saved sender %s without reselecting from recipients',
  (email) =>
    createRoot((dispose) => {
      try {
        const draft = message('draft', {
          ...parent,
          db_id: 'draft',
          is_draft: true,
          from: { email },
        });
        const form = createEmailFormState(
          { viewerEmail: () => '[email protected]', inboxes: () => [loaded] },
          { type: 'replying_to', messageId: 'parent' },
          {
            getMessageById: () => parent,
            getDraftForMessageReply: () => draft,
          }
        );
        expect(form.senderEmail()).toBe(email);
      } finally {
        dispose();
      }
    })
);

it('preserves the alias sender when forwarding and retains only attachments not linked in the quote', () =>
  createRoot((dispose) => {
    try {
      const original = message('parent', {
        ...parent,
        body_html_sanitized: '<img src="cid:logo"><img src="cid:chart">',
        attachments: [
          {
            db_id: 'logo',
            mime_type: 'image/png',
            content_id: '<logo>',
            sfs_id: 'logo-file',
          },
          { db_id: 'chart', mime_type: 'image/png', content_id: '<chart>' },
          { db_id: 'report', mime_type: 'application/pdf' },
        ],
      });
      const form = createEmailFormState(
        { viewerEmail: () => '[email protected]', inboxes: () => [loaded] },
        { type: 'replying_to', messageId: 'parent' },
        {
          getMessageById: () => original,
          getDraftForMessageReply: () => undefined,
        }
      );
      form.setReplyType('forward');
      expect(form.senderEmail()).toBe('[email protected]');
      expect(
        form.attachments.list().map((attachment) => attachment.attachmentId)
      ).toEqual(['chart', 'report']);
      expect(form.recipients()).toEqual({ to: [], cc: [], bcc: [] });
    } finally {
      dispose();
    }
  }));

it('does not pick an alias belonging only to another connected inbox', () =>
  createRoot((dispose) => {
    try {
      const form = createEmailFormState(
        {
          viewerEmail: () => '[email protected]',
          inboxes: () => [primary, { ...loaded, id: 'other' }],
        },
        { type: 'replying_to', messageId: 'parent' },
        {
          getMessageById: () => parent,
          getDraftForMessageReply: () => undefined,
        }
      );
      expect(form.senderEmail()).toBe('[email protected]');
    } finally {
      dispose();
    }
  }));

it.each(['[email protected]', '[email protected]'])(
  'restores sender %s and edited recipients into a new reply after cancellation',
  (email) =>
    createRoot((dispose) => {
      try {
        const [inboxes, setInboxes] = createSignal<SenderInbox[]>([loaded]);
        const form = createEmailFormState(
          { viewerEmail: () => '[email protected]', inboxes },
          { type: 'replying_to', messageId: 'parent' },
          {
            getMessageById: () => parent,
            getDraftForMessageReply: () => undefined,
          }
        );
        form.setSelectedInbox(
          'inbox',
          email === '[email protected]'
            ? '[email protected]'
            : '[email protected]'
        );
        form.clear();
        const revision = form.editRevision();
        form.restoreDraft(
          message('restored', {
            is_draft: true,
            link_id: 'inbox',
            from: { email },
            to: [{ email: '[email protected]' }],
            cc: [{ email: '[email protected]' }],
            bcc: [{ email: '[email protected]' }],
            body_html_sanitized: null,
          })
        );
        // Catalog refreshes must not re-derive explicit restored choices.
        setInboxes([primary]);
        setInboxes([loaded]);
        expect(form.senderEmail()).toBe(email);
        expect(form.recipients().to.map((item) => item.data.email)).toEqual([
          '[email protected]',
        ]);
        expect(form.recipients().cc.map((item) => item.data.email)).toEqual([
          '[email protected]',
        ]);
        expect(form.recipients().bcc.map((item) => item.data.email)).toEqual([
          '[email protected]',
        ]);
        expect(form.editRevision()).toBe(revision);
      } finally {
        dispose();
      }
    })
);

it("replies to a shared thread from the viewer's inbox, not its owner's", () =>
  createRoot((dispose) => {
    try {
      const shared = message('shared', {
        link_id: 'owner-inbox',
        from: { email: '[email protected]' },
        to: [{ email: '[email protected]' }],
      });
      const form = createEmailFormState(
        { viewerEmail: () => '[email protected]', inboxes: () => [primary] },
        { type: 'replying_to', messageId: 'shared' },
        {
          getMessageById: () => shared,
          getDraftForMessageReply: () => undefined,
        }
      );
      expect(form.selectedInboxId()).toBeUndefined();
      expect(form.recipients().to.map((item) => item.data.email)).toEqual([
        '[email protected]',
      ]);
    } finally {
      dispose();
    }
  }));

it('replies from the inbox holding the message when it is the viewer’s', () =>
  createRoot((dispose) => {
    try {
      const form = createEmailFormState(
        { viewerEmail: () => '[email protected]', inboxes: () => [primary] },
        { type: 'replying_to', messageId: 'parent' },
        {
          getMessageById: () => parent,
          getDraftForMessageReply: () => undefined,
        }
      );
      expect(form.selectedInboxId()).toBe('inbox');
    } finally {
      dispose();
    }
  }));

it('judges a shared-thread reply against the inbox it sends from', () =>
  createRoot((dispose) => {
    try {
      const sending: SenderInbox = {
        id: 'viewer-inbox',
        email_address: '[email protected]',
      };
      // The viewer wrote this message from their own inbox to the owner, who
      // shared the thread back.
      const shared = message('shared', {
        link_id: 'owner-inbox',
        from: { email: '[email protected]' },
        to: [{ email: '[email protected]' }],
      });
      const form = createEmailFormState(
        {
          viewerEmail: () => '[email protected]',
          inboxes: () => [sending],
          primaryInboxId: () => 'viewer-inbox',
        },
        { type: 'replying_to', messageId: 'shared' },
        {
          getMessageById: () => shared,
          getDraftForMessageReply: () => undefined,
        }
      );
      expect(form.senderEmail()).toBe('[email protected]');
      expect(form.recipients().to.map((item) => item.data.email)).toEqual([
        '[email protected]',
      ]);
    } finally {
      dispose();
    }
  }));