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.

18.2 KB
Raw Blame
import { optimisticContextOf } from '@graphql-cache/exchange/optimistic';
import {
  type SaveEmailDraftMutation,
  SoupDocument,
} from '@service-storage/graphql/generated/graphql';
import { createClient, type Operation } from '@urql/core';
import { describe, expect, it } from 'vitest';
import { map, pipe } from 'wonka';
import { registerGraphqlSoupRevalidations } from '../../soup/graphql/active-queries';
import {
  executeGraphqlDeleteEmailDraft,
  executeGraphqlSaveEmailDraft,
  type GraphqlSaveEmailDraftArgs,
} from './draft';
import {
  draftThreadIsEmpty,
  removeDraftFromThread,
  updateDraftThread,
} from './optimistic-thread';

const args: GraphqlSaveEmailDraftArgs = {
  draftId: '01991e2a-3111-7000-8000-000000000001',
  threadDbId: 'thread',
  senderLinkId: 'inbox',
  senderEmail: '[email protected]',
  subject: 'Updated subject',
  optimisticBodyHtml: '<p>Updated body</p>',
};
function queuedClient() {
  const operations: Operation[] = [];
  const client = createClient({
    url: 'http://example.test/graphql',
    exchanges: [
      () => (source) =>
        pipe(
          source,
          map((operation) => {
            operations.push(operation);
            return {
              operation,
              stale: false,
              hasNext: false,
              extensions: {
                normalizedCacheMutationDisposition: {
                  kind: 'queued',
                  transactionId: 'queued',
                },
              },
            };
          })
        ),
    ],
  });
  return { client, operations };
}

describe('optimistic draft saves', () => {
  it('preserves attachments and scheduling through offline body edits and strips client state from the wire', async () => {
    const { client, operations } = queuedClient();
    await expect(executeGraphqlSaveEmailDraft(client, args)).resolves.toEqual({
      kind: 'queued',
      transactionId: 'queued',
    });
    const first = optimisticContextOf(operations[0])
      ?.optimisticResponse as SaveEmailDraftMutation;
    expect(first.saveEmailDraft.draft.calendarInvitations).toEqual([]);
    const existing = {
      ...first.saveEmailDraft.draft,
      calendarInvitations: [{ id: 'saved-invitation' }],
      createdAt: '2026-01-01T00:00:00Z',
      scheduledSendTime: '2027-01-01T12:00:00Z',
      scheduledSendStatus: 'FAILED' as const,
      hasAttachments: true,
      attachmentsDraft: [
        {
          uploadPending: false,
          contentId: null,
          isInline: false,
          __typename: 'GraphqlSoupEmailDraftAttachment' as const,
          id: 'attachment',
          draftId: String(args.draftId),
          fileName: 'notes.txt',
          contentType: 'text/plain',
          sha: 'sha',
          size: 5,
          s3Key: 'key',
        },
      ],
    };
    await executeGraphqlSaveEmailDraft(client, {
      ...args,
      existingDraft: existing,
    });
    const variables = operations[1].variables;
    const optimistic = optimisticContextOf(operations[1])?.optimisticResponse;
    expect(variables).toEqual({
      input: {
        draftId: args.draftId,
        senderEmail: args.senderEmail,
        threadDbId: 'thread',
        subject: 'Updated subject',
      },
    });
    expect(optimistic).toMatchObject({
      saveEmailDraft: {
        draft: {
          bodyHtmlSanitized: '<p>Updated body</p>',
          attachmentsDraft: existing.attachmentsDraft,
          hasAttachments: true,
          createdAt: existing.createdAt,
          scheduledSendTime: existing.scheduledSendTime,
          scheduledSendStatus: existing.scheduledSendStatus,
          calendarInvitations: existing.calendarInvitations,
        },
      },
    });
  });
});

async function standalone() {
  const { client, operations } = queuedClient();
  await executeGraphqlSaveEmailDraft(client, {
    ...args,
    newThreadOwnerId: 'macro|[email protected]',
    senderIsSignal: false,
  });
  const response = optimisticContextOf(operations[0])!
    .optimisticResponse as SaveEmailDraftMutation;
  return { ...response.saveEmailDraft, client, operations };
}

it('creates a complete standalone thread with the account classification and stable identity bindings', async () => {
  const { draft, thread, operations } = await standalone();
  expect(thread).toMatchObject({
    ownerId: 'macro|[email protected]',
    isSignal: false,
    isRead: true,
    // A draft with no received mail belongs in Drafts, not the Inbox.
    inboxVisible: false,
    hasReceivedMail: false,
    latestInboundMessageTs: null,
    latestNonSpamMessageTs: draft.updatedAt,
    mailAllPreview: { id: args.draftId },
    mailDraftPreview: { id: args.draftId },
    mailSentPreview: null,
    messages: [draft],
    properties: [],
    notifications: [],
    viewerPermission: { accessLevel: 'OWNER' },
    cacheProjection: null,
  });
  expect(thread.mailDraftState?.baseline.messageCount).toBe(0);
  expect(optimisticContextOf(operations[0])?.revalidations).toEqual([]);
  expect(optimisticContextOf(operations[0])?.identityBindings).toEqual(
    expect.arrayContaining([
      expect.objectContaining({
        localKey: `GraphqlSoupEmailThread:${args.threadDbId}`,
        responsePath: ['saveEmailDraft', 'thread'],
        revalidationVariables: ['threadId'],
      }),
      expect.objectContaining({
        localKey: `GraphqlSoupEmailMessage:${args.draftId}`,
        referenceFields: expect.arrayContaining([
          'GraphqlMailPreviewMessage.id',
          'GraphqlMailDraftEntry.id',
        ]),
      }),
    ])
  );
});

it.each(['new reply', 'existing reply', 'standalone draft'])(
  'does not redirect a canonical conversation or insert a moved %s into it',
  async (kind) => {
    const { client, operations, draft, thread } = await standalone();
    const existingThread = { ...thread, cacheProjection: 'server-capsule' };
    await executeGraphqlSaveEmailDraft(client, {
      ...args,
      senderLinkId: 'other-inbox',
      replyingToId: kind === 'standalone draft' ? undefined : 'reply-target',
      existingDraft: kind === 'new reply' ? undefined : draft,
      existingThread,
      // A settlement can resolve the snapshot after the caller prepared its
      // new-thread hint. The server capsule takes precedence over that hint.
      newThreadOwnerId: 'macro|[email protected]',
    });
    const context = optimisticContextOf(operations[1])!;
    const response = context.optimisticResponse as SaveEmailDraftMutation;
    expect(context.identityBindings).toContainEqual({
      localKey: `GraphqlSoupEmailThread:${args.threadDbId}`,
      responsePath: [],
      referenceFields: ['GraphqlSoupEmailMessage.threadId'],
      revalidationVariables: ['threadId'],
    });
    expect(context.identityBindings).toContainEqual(
      expect.objectContaining({
        localKey: `GraphqlSoupEmailMessage:${args.draftId}`,
        responsePath: ['saveEmailDraft', 'draft'],
      })
    );
    expect(context.linkPatches).toEqual([]);
    // The optimistic snapshot keeps the queued draft discoverable/reopenable;
    // without a commit patch it cannot splice the moved draft into the source.
    expect(response.saveEmailDraft.thread).toMatchObject({
      id: args.threadDbId,
      messages: [{ id: args.draftId }],
      mailDraftState: { drafts: [{ id: args.draftId }] },
    });
    expect(context.revalidations).toContainEqual(
      expect.objectContaining({
        variablesJson: JSON.stringify({
          threadId: args.threadDbId,
          offset: 0,
          limit: 20,
        }),
      })
    );
  }
);

it('keeps canonical same-inbox reply updates without aliasing the conversation', async () => {
  const { client, operations, thread } = await standalone();
  await executeGraphqlSaveEmailDraft(client, {
    ...args,
    replyingToId: 'reply-target',
    existingThread: { ...thread, cacheProjection: 'server-capsule' },
  });
  const context = optimisticContextOf(operations[1])!;
  expect(context.identityBindings).toContainEqual(
    expect.objectContaining({
      localKey: `GraphqlSoupEmailThread:${args.threadDbId}`,
      responsePath: [],
    })
  );
  expect(context.linkPatches).toHaveLength(1);
  expect(context.revalidations).toEqual([]);
  expect(context.optimisticResponse).toMatchObject({
    saveEmailDraft: {
      thread: { id: args.threadDbId, messages: [{ id: args.draftId }] },
    },
  });
});

it('does not infer a local thread from an uncached reply hint', async () => {
  const { client, operations } = queuedClient();
  await executeGraphqlSaveEmailDraft(client, {
    ...args,
    replyingToId: 'reply-target',
  });
  const context = optimisticContextOf(operations[0])!;
  expect(context.identityBindings).toContainEqual(
    expect.objectContaining({
      localKey: `GraphqlSoupEmailThread:${args.threadDbId}`,
      responsePath: [],
    })
  );
  expect(context.linkPatches).toEqual([]);
  expect(context.revalidations).toContainEqual(
    expect.objectContaining({ operationName: 'EmailThreadPage' })
  );
});

it.each([args.senderLinkId, 'other-inbox'])(
  'keeps local identity reconciliation for a reopened queued draft saved from %s',
  async (senderLinkId) => {
    const { client, operations, draft, thread } = await standalone();
    await executeGraphqlSaveEmailDraft(client, {
      ...args,
      senderLinkId,
      existingDraft: draft,
      existingThread: thread,
    });
    const context = optimisticContextOf(operations[1])!;
    expect(context.identityBindings).toContainEqual(
      expect.objectContaining({
        localKey: `GraphqlSoupEmailThread:${args.threadDbId}`,
        responsePath: ['saveEmailDraft', 'thread'],
        referenceFields: ['GraphqlSoupEmailMessage.threadId'],
        revalidationVariables: ['threadId'],
      })
    );
    expect(context.linkPatches).toHaveLength(1);
  }
);

it.each([true, false])(
  'a reply draft returns an archived conversation to the inbox only when it has received mail (received=%s)',
  async (hasReceivedMail) => {
    const { draft, thread } = await standalone();
    const archived = {
      ...thread,
      hasReceivedMail,
      messages: [],
      mailDraftState: {
        baseline: {
          ...thread.mailDraftState!.baseline,
          messageCount: 1,
          latestNonSpamMessageTs: '2026-02-01T00:00:00Z',
        },
        drafts: [],
      },
    };
    const started = updateDraftThread(archived, draft, false);
    const edited = updateDraftThread(
      started,
      { ...draft, updatedAt: '2026-09-23T12:00:00Z' },
      false
    );
    for (const [state, updatedAt] of [
      [started, draft.updatedAt],
      [edited, '2026-09-23T12:00:00Z'],
    ] as const) {
      expect(state).toMatchObject({
        inboxVisible: hasReceivedMail,
        latestInboundMessageTs: hasReceivedMail ? updatedAt : null,
        latestNonSpamMessageTs: updatedAt,
      });
    }
  }
);

it.each([true, false])(
  'editing a draft follows received mail that changed after the draft was saved (received=%s)',
  async (hasReceivedMail) => {
    const { draft, thread } = await standalone();
    const saved = updateDraftThread(
      {
        ...thread,
        hasReceivedMail: !hasReceivedMail,
        messages: [],
        mailDraftState: {
          baseline: { ...thread.mailDraftState!.baseline, messageCount: 1 },
          drafts: [],
        },
      },
      draft,
      false
    );
    const edited = updateDraftThread(
      { ...saved, hasReceivedMail },
      { ...draft, updatedAt: '2026-09-23T12:00:00Z' },
      false
    );
    expect(edited).toMatchObject({
      inboxVisible: hasReceivedMail,
      latestInboundMessageTs: hasReceivedMail ? '2026-09-23T12:00:00Z' : null,
    });
  }
);

it('editing an older draft keeps the later received preview and discarding restores the full baseline', async () => {
  const { draft, thread } = await standalone();
  const received = {
    ...thread.mailAllPreview!,
    id: 'received',
    isDraft: false,
    subject: 'Incoming',
  };
  const state = thread.mailDraftState!;
  const older = {
    ...draft,
    createdAt: '2026-01-01T00:00:00Z',
    updatedAt: '2026-09-22T12:00:00Z',
  };
  state.drafts[0].facts.previewTs = older.createdAt;
  state.baseline = {
    ...state.baseline,
    messageCount: 24,
    isRead: false,
    isSignal: false,
    hasCalendarAttachment: true,
    preview: received,
    previewTs: '2026-02-01T00:00:00Z',
    latestNonSpamMessageTs: '2026-02-01T00:00:00Z',
    latestOutboundMessageTs: '2026-01-31T00:00:00Z',
  };
  const sent = { ...received, id: 'sent', subject: 'Sent' };
  const edited = updateDraftThread(
    { ...thread, isRead: false, mailSentPreview: sent },
    older,
    true
  );
  expect(edited.mailAllPreview).toEqual(received);
  expect(edited.mailDraftPreview?.id).toBe(draft.id);
  expect(edited.mailSentPreview).toEqual(sent);
  expect(edited.isRead).toBe(false);
  expect(edited.isSignal).toBe(true);
  const removed = removeDraftFromThread(edited, draft.id)!;
  expect(removed).toMatchObject({
    inboxVisible: false,
    isSignal: false,
    isRead: false,
    mailDraftPreview: null,
    mailAllPreview: received,
    mailSentPreview: sent,
    latestInboundMessageTs: null,
    latestNonSpamMessageTs: '2026-02-01T00:00:00Z',
    latestOutboundMessageTs: '2026-01-31T00:00:00Z',
    messages: [],
  });
  expect(removed.mailDraftState?.baseline.hasCalendarAttachment).toBe(true);
  expect(draftThreadIsEmpty(removed)).toBe(false);
});

it('discards a standalone draft with the original coalescing key and bindings for an in-flight save', async () => {
  const { client, operations, thread } = await standalone();
  await executeGraphqlDeleteEmailDraft(client, {
    draftId: String(args.draftId),
    threadDbId: args.threadDbId,
    existingThread: thread,
    mutationUuid: '01991e2a-3111-7000-8000-000000000099',
  });
  const context = optimisticContextOf(operations[1]);
  expect(context?.uuid).toBe('01991e2a-3111-7000-8000-000000000099');
  expect(context?.optimisticResponse).toMatchObject({
    deleteEmailDraft: {
      threadDeleted: true,
      thread: { mailAllPreview: null, mailDraftPreview: null, messages: [] },
    },
  });
  expect(
    context?.identityBindings?.every(
      (binding) => binding.responsePath.length === 0
    )
  ).toBe(true);
});

it('saves and discards without refetching the lists the cache maintains', async () => {
  // A mounted list's pages. The returned draft and thread records update it.
  const unregister = registerGraphqlSoupRevalidations(
    () => [
      {
        document: SoupDocument,
        variables: { input: { initial: { limit: 50 } } },
      },
    ],
    undefined,
    () => true
  );
  try {
    const { client, operations, thread } = await standalone();
    await executeGraphqlSaveEmailDraft(client, {
      ...args,
      existingThread: { ...thread, cacheProjection: 'server-capsule' },
    });
    await executeGraphqlDeleteEmailDraft(client, {
      draftId: String(args.draftId),
      threadDbId: args.threadDbId,
      existingThread: thread,
    });
    for (const operation of operations) {
      const documents = (
        optimisticContextOf(operation)?.revalidations ?? []
      ).map(({ operationName }) => operationName);
      expect(documents).not.toContain('Soup');
    }
  } finally {
    unregister();
  }
});

it.each([false, true])(
  'preserves a newer read intent through repeated draft saves and discard (read=%s)',
  async (isRead) => {
    const { draft, thread } = await standalone();
    const state = thread.mailDraftState!;
    state.baseline = {
      ...state.baseline,
      messageCount: 1,
      isRead: !isRead,
    };
    // Read/unread mutations patch the thread while the server's aggregate
    // remains unchanged until the queue is replayed and revalidated.
    thread.isRead = isRead;
    const first = updateDraftThread(thread, draft, true);
    const second = updateDraftThread(
      first,
      { ...draft, subject: 'Edited' },
      true
    );
    expect(first.isRead).toBe(isRead);
    expect(second.isRead).toBe(isRead);
    expect(removeDraftFromThread(second, draft.id)?.isRead).toBe(isRead);
  }
);

it('marks a conversation read when its only unread draft is edited or discarded', async () => {
  const { draft, thread } = await standalone();
  const state = thread.mailDraftState!;
  state.baseline.messageCount = 1;
  state.drafts[0].facts.isRead = false;
  thread.isRead = false;
  expect(updateDraftThread(thread, draft, true).isRead).toBe(true);
  expect(removeDraftFromThread(thread, draft.id)?.isRead).toBe(true);
});

it('discarding a local draft does not bring a junk provider draft into All Mail', async () => {
  const { draft, thread } = await standalone();
  const state = thread.mailDraftState!;
  const junk = {
    ...state.drafts[0],
    id: 'junk-draft',
    macroDraft: false,
    facts: {
      ...state.drafts[0].facts,
      latestNonSpamMessageTs: null,
      preview: { ...state.drafts[0].facts.preview!, id: 'junk-draft' },
    },
  };
  state.drafts.push(junk);
  const result = removeDraftFromThread(thread, draft.id)!;
  expect(result.mailAllPreview).toBeNull();
  expect(result.mailDraftPreview?.id).toBe('junk-draft');
  expect(draftThreadIsEmpty(result)).toBe(false);
});

it.each([
  { folder: 'junk', mixed: false },
  { folder: 'trash', mixed: false },
  { folder: 'junk', mixed: true },
  { folder: 'trash', mixed: true },
])(
  'editing a synced Macro draft in $folder preserves All Mail eligibility (mixed=$mixed)',
  async ({ folder, mixed }) => {
    const { draft, thread } = await standalone();
    const state = thread.mailDraftState!;
    const entry = state.drafts[0];
    const cleanTimestamp = '2026-01-01T00:00:00Z';
    if (mixed) {
      state.baseline = {
        ...state.baseline,
        messageCount: 1,
        latestNonSpamMessageTs: cleanTimestamp,
        previewTs: cleanTimestamp,
        preview: {
          ...entry.facts.preview!,
          id: 'clean-message',
          isDraft: false,
        },
      };
    }
    entry.facts.latestNonSpamMessageTs = null;
    if (folder === 'trash') entry.facts.preview = null;
    thread.mailAllPreview = state.baseline.preview;
    thread.latestNonSpamMessageTs = state.baseline.latestNonSpamMessageTs;
    const edited = updateDraftThread(
      thread,
      { ...draft, updatedAt: '2027-01-01T00:00:00Z' },
      true
    );
    expect(edited.latestNonSpamMessageTs).toBe(mixed ? cleanTimestamp : null);
    expect(edited.mailAllPreview?.id ?? null).toBe(
      mixed ? 'clean-message' : null
    );
    expect(edited.mailDraftPreview?.id ?? null).toBe(
      folder === 'junk' ? draft.id : null
    );
  }
);