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.

3.3 KB
Raw Blame
import { type Client, CombinedError } from '@urql/core';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import {
  MarkEmailThreadSeenDocument,
  MarkEmailThreadUnreadDocument,
} from './graphql/generated/graphql';
import {
  markGraphqlEmailThreadSeen,
  markGraphqlEmailThreadUnread,
} from './graphql-email-read-state';

const mutation = vi.fn();
const client = { mutation } as unknown as Client;

beforeEach(() => {
  mutation.mockReset().mockImplementation((document) => ({
    toPromise: async () => ({
      data:
        document === MarkEmailThreadSeenDocument
          ? { markEmailThreadSeen: { id: 'thread', isRead: true } }
          : { markEmailThreadUnread: { id: 'thread', isRead: false } },
    }),
  }));
});

describe('GraphQL email read-state mutations', () => {
  it('uses ordinary mutations; the exchange supplies local semantics', async () => {
    await expect(markGraphqlEmailThreadSeen(client, 'thread')).resolves.toBe(
      'committed'
    );
    await markGraphqlEmailThreadUnread(client, 'thread');
    expect(mutation.mock.calls.map((call) => [call[0], call[1]])).toEqual([
      [MarkEmailThreadSeenDocument, { input: { threadId: 'thread' } }],
      [MarkEmailThreadUnreadDocument, { input: { threadId: 'thread' } }],
    ]);
    expect(
      mutation.mock.calls.every(
        (call) => call[2].optimisticMutation.revalidations.length === 0
      )
    ).toBe(true);
  });

  it.each(['seen', 'unread'] as const)(
    'propagates a permanent %s error for the exchange-owned rollback',
    async (action) => {
      const error = new CombinedError({
        graphQLErrors: [new Error('forbidden')],
      });
      mutation.mockReturnValue({
        toPromise: async () => ({
          error,
          extensions: {
            normalizedCacheMutationDisposition: { kind: 'permanently-failed' },
          },
        }),
      });
      await expect(
        action === 'seen'
          ? markGraphqlEmailThreadSeen(client, 'thread')
          : markGraphqlEmailThreadUnread(client, 'thread')
      ).rejects.toBe(error);
    }
  );

  it.each(['seen', 'unread'] as const)(
    'accepts a durable queued %s result without claiming it committed remotely',
    async (action) => {
      mutation.mockReturnValue({
        toPromise: async () => ({
          extensions: {
            normalizedCacheMutationDisposition: {
              kind: 'queued',
              transactionId: 'tx',
            },
          },
        }),
      });
      await expect(
        action === 'seen'
          ? markGraphqlEmailThreadSeen(client, 'thread')
          : markGraphqlEmailThreadUnread(client, 'thread')
      ).resolves.toBe('queued');
    }
  );

  it('rejects an empty response payload instead of claiming the write committed', async () => {
    mutation.mockReturnValue({ toPromise: async () => ({ data: {} }) });
    await expect(markGraphqlEmailThreadSeen(client, 'thread')).rejects.toThrow(
      'returned no data'
    );
    await expect(
      markGraphqlEmailThreadUnread(client, 'thread')
    ).rejects.toThrow('returned no data');
  });

  it('rejects a response with neither data nor a queue disposition', async () => {
    mutation.mockReturnValue({ toPromise: async () => ({}) });
    await expect(markGraphqlEmailThreadSeen(client, 'thread')).rejects.toThrow(
      'returned no data'
    );
  });
});