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.4 KB
Raw Blame
import { batch, createRoot, createSignal } from 'solid-js';
import { afterEach, describe, expect, it } from 'vitest';
import type { EmailMessage } from '../../email-message/core/email-message';
import { message } from '../tests/fixtures';
import { createThreadReplyArea } from './thread-reply-area';

const disposers: (() => void)[] = [];
afterEach(() => disposers.splice(0).forEach((dispose) => dispose()));

function setup(touch: boolean) {
  return createRoot((dispose) => {
    disposers.push(dispose);
    const parent = message('parent');
    const draft = message('draft', {
      is_draft: true,
      replying_to_id: 'parent',
    });
    const [threadId, setThreadId] = createSignal('first');
    const [messages, setMessages] = createSignal([parent]);
    const [allMessages, setAllMessages] = createSignal([parent, draft]);
    const [drafts, setDrafts] = createSignal<Record<string, EmailMessage>>({
      parent: draft,
    });
    const [owner, setOwner] = createSignal(true);
    const [settled, setSettled] = createSignal(true);
    const [bottomOpen, setBottomOpen] = createSignal(false);
    const [mobileId, setMobileId] = createSignal<string>();
    const area = createThreadReplyArea({
      threadId,
      isTouch: () => touch,
      messages,
      allMessages,
      canCompose: owner,
      drafts: {
        getDraftForMessage: (id) => drafts()[id],
        initialDraftsSettled: settled,
      },
      bottomReply: { open: bottomOpen, setOpen: setBottomOpen },
      mobileReply: {
        open: () => mobileId() !== undefined,
        openForMessage: setMobileId,
        close: () => setMobileId(undefined),
      },
    });
    return {
      area,
      draft,
      close: () => setMobileId(undefined),
      bottomOpen,
      mobileId,
      setThreadId,
      setMessages,
      setAllMessages,
      setDrafts,
      setOwner,
      setSettled,
    };
  });
}

describe('thread reply placement', () => {
  it.each([false, true])(
    'opens cached drafts without the thread reset overriding them (touch=%s)',
    (touch) => {
      const state = setup(touch);
      expect(state.bottomOpen()).toBe(!touch);
      expect(state.mobileId()).toBe(touch ? 'parent' : undefined);
      const next = message('next');
      const nextDraft = message('next-draft', {
        is_draft: true,
        replying_to_id: 'next',
      });
      batch(() => {
        state.setThreadId('second');
        state.setMessages([next]);
        state.setAllMessages([next, nextDraft]);
        state.setDrafts({ next: nextDraft });
      });
      expect(state.area.info()?.draft?.db_id).toBe('next-draft');
      expect(state.bottomOpen()).toBe(!touch);
      expect(state.mobileId()).toBe(touch ? 'next' : undefined);
      batch(() => {
        state.setThreadId('third');
        state.setDrafts({});
      });
      expect(state.bottomOpen()).toBe(false);
      expect(state.mobileId()).toBeUndefined();
    }
  );

  it('keeps a closed touch composer closed when the saved draft updates', () => {
    const state = setup(true);
    const parent = message('parent');
    expect(state.mobileId()).toBe('parent');
    state.close();
    state.setDrafts({
      parent: { ...state.draft, updated_at: '2026-10-31T19:13:00.000Z' },
    });
    state.setMessages([{ ...parent }]);
    expect(state.mobileId()).toBeUndefined();

    // A draft that appears before the composer was ever shown still opens it.
    batch(() => {
      state.setThreadId('second');
      state.setDrafts({});
    });
    expect(state.mobileId()).toBeUndefined();
    state.setDrafts({ parent: state.draft });
    expect(state.mobileId()).toBe('parent');
  });

  it('uses draft-only threads while respecting ownership and settlement gates', () => {
    const state = setup(false);
    state.setMessages([]);
    state.setAllMessages([state.draft]);
    expect(state.area.info()).toEqual({
      replyingTo: undefined,
      draft: state.draft,
    });
    expect(state.area.inFlow()).toBe(true);
    state.setOwner(false);
    expect(state.area.inFlow()).toBe(false);
    state.setOwner(true);
    state.setSettled(false);
    expect(state.area.inFlow()).toBe(false);
    state.setAllMessages([]);
    expect(state.area.info()).toBeUndefined();
  });

  it('never reports an in-flow reply input on touch', () => {
    const state = setup(true);
    state.setMessages([]);
    state.setAllMessages([state.draft]);
    expect(state.area.inFlow()).toBe(false);
  });
});