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.

10.6 KB
Raw Blame
import { useInfiniteQuery } from '@tanstack/solid-query';
import { createRoot } from 'solid-js';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';

const testState = vi.hoisted(() => ({
  graphqlEnabled: false,
  restPending: false,
  restNextPageError: false,
}));
const restRefetch = vi.hoisted(() => vi.fn(async () => undefined));
const fetchSoup = vi.hoisted(() => vi.fn());
const flatQuery = vi.hoisted(() => makeGraphqlQuery(false));
const groupedQuery = vi.hoisted(() => makeGraphqlQuery(true));

function makeGraphqlQuery(enabled: boolean) {
  return {
    data: vi.fn(),
    error: vi.fn(),
    isSupported: vi.fn(() => true),
    isEnabled: vi.fn(() => enabled),
    isLoading: vi.fn(() => false),
    isFetching: vi.fn(() => false),
    isFetchingNextPage: vi.fn(() => false),
    isFetchNextPageError: vi.fn(() => false),
    isPlaceholderData: vi.fn(() => false),
    hasNextPage: vi.fn(() => false),
    fetchNextPage: vi.fn(async () => undefined),
    resetToInitialPage: vi.fn(),
    refresh: vi.fn(async () => undefined),
  };
}

vi.mock('@app/features/next-soup/filters/query-filters', () => ({
  filterSoupItemByRequestBody: vi.fn(() => true),
}));
vi.mock('@app/lib/analytics/posthog', () => ({
  useFeatureFlag: vi.fn(() => () => ({ enabled: testState.graphqlEnabled })),
}));
vi.mock('@core/constant/featureFlags', () => ({
  enableGraphqlSoup: { key: 'enable-graphql-soup' },
}));
vi.mock('@core/util/result', () => ({
  throwOnErr: vi.fn(async (run: () => Promise<unknown>) => await run()),
}));
vi.mock('@queries/soup/grouped/api', () => ({
  groupedSortMethod: vi.fn(),
  makeGroupComparator: vi.fn(),
  parseGroupMeta: vi.fn(),
  serializeGroupByField: vi.fn(),
}));
vi.mock('@queries/soup/keys', () => ({
  soupKeys: {
    astItems: vi.fn(() => ({ queryKey: ['soup', 'ast'] })),
  },
}));
vi.mock('@queries/soup/transform-utils', () => ({
  isDisplayableSoupItem: vi.fn(() => true),
  mapApiSoupItemToEntity: vi.fn((item) => ({
    ...item.data,
    touchedAt: item.touched_at,
  })),
  mapSoupPageToEntityList: vi.fn((page) =>
    page.items.map((item: { data: unknown }) => item.data)
  ),
}));
vi.mock('@service-storage/client', () => ({
  storageServiceClient: { getSoupItems: vi.fn(), getSoupAstItems: fetchSoup },
}));
vi.mock('@tanstack/solid-query', () => ({
  infiniteQueryOptions: vi.fn((options: unknown) => options),
  useInfiniteQuery: vi.fn(() => ({
    data: undefined,
    error: null,
    get isPending() {
      return testState.restPending;
    },
    isLoading: false,
    isFetching: false,
    isPlaceholderData: false,
    isFetchingNextPage: false,
    get isFetchNextPageError() {
      return testState.restNextPageError;
    },
    isEnabled: true,
    hasNextPage: false,
    fetchNextPage: vi.fn(async () => undefined),
    refetch: restRefetch,
  })),
}));
vi.mock('../client', () => ({
  queryClient: { setQueryData: vi.fn() },
}));
vi.mock('./graphql/items', () => ({
  createGraphqlSoupAstItemsQuery: vi.fn(() => flatQuery),
}));
vi.mock('./graphql/grouped-items', () => ({
  createGraphqlGroupedSoupAstItemsQuery: vi.fn(() => groupedQuery),
}));

import { refreshActiveGraphqlSoupQueries } from './graphql/active-queries';
import { makeGraphqlGroupedSoupInput } from './graphql/ast';
import { createGraphqlGroupedSoupAstItemsQuery } from './graphql/grouped-items';
import { createGraphqlSoupAstItemsQuery } from './graphql/items';
import {
  type SoupAstItemsData,
  type SoupAstItemsPage,
  type SoupAstItemsQuery,
  useSoupAstItemsQuery,
} from './items';

let disposeRoot: (() => void) | undefined;

function mountAutoTransportQuery(networkPaused = false): SoupAstItemsQuery {
  let query: SoupAstItemsQuery | undefined;
  createRoot((dispose) => {
    disposeRoot = dispose;
    query = useSoupAstItemsQuery(
      () => ({
        params: {},
        body: {},
        groupBy: {
          type: 'property',
          propertyDefinitionId: 'priority',
          entityType: 'TASK',
        },
      }),
      () => ({ networkPaused })
    );
  });
  return query!;
}

describe('Soup refetch transport selection', () => {
  beforeEach(() => {
    testState.graphqlEnabled = false;
    testState.restPending = false;
    testState.restNextPageError = false;
    vi.clearAllMocks();
  });

  afterEach(() => {
    disposeRoot?.();
    disposeRoot = undefined;
  });

  it('exposes paused REST requests as pending even when they are not loading', () => {
    testState.restPending = true;
    const query = mountAutoTransportQuery();
    expect(query.isLoading).toBe(false);
    expect(query.isPending).toBe(true);
  });

  it('reports continuation-page failures from the selected transport', () => {
    const rest = mountAutoTransportQuery();
    expect(rest.isFetchNextPageError).toBe(false);
    testState.restNextPageError = true;
    expect(rest.isFetchNextPageError).toBe(true);
    disposeRoot?.();

    testState.graphqlEnabled = true;
    const graphql = mountAutoTransportQuery();
    expect(graphql.transport).toBe('graphql');
    groupedQuery.isFetchNextPageError.mockReturnValueOnce(true);
    expect(graphql.isFetchNextPageError).toBe(true);
    expect(graphql.isFetchNextPageError).toBe(false);
  });

  it('keeps paused GraphQL requests pending until data or an error arrives, unless disabled', () => {
    testState.graphqlEnabled = true;
    const query = mountAutoTransportQuery(true);
    expect(query.transport).toBe('graphql');
    expect(query.isLoading).toBe(false);
    expect(query.isPending).toBe(true);

    groupedQuery.data.mockReturnValueOnce({ entities: [] });
    expect(query.isPending).toBe(false);

    groupedQuery.error.mockReturnValueOnce(new Error('Request failed'));
    expect(query.isPending).toBe(false);

    groupedQuery.isEnabled.mockReturnValueOnce(false);
    expect(query.isPending).toBe(false);
  });

  it('forwards the channel list projection only to the flat GraphQL query', () => {
    createRoot((dispose) => {
      disposeRoot = dispose;
      useSoupAstItemsQuery(
        () => ({ params: {}, body: {} }),
        () => ({ enabled: true, graphqlProjection: 'channel-list' })
      );
    });
    const options = vi
      .mocked(createGraphqlSoupAstItemsQuery)
      .mock.calls[0][1]();
    expect(options.projection).toBe('channel-list');
  });

  it('preserves fetched page coverage when optimistic inserts change cached membership', async () => {
    const item = {
      tag: 'chat',
      frecency_score: 0,
      is_favorited: false,
      touched_at: '2026-09-08T00:00:00Z',
      data: {
        id: 'fetched',
        name: 'Fetched',
        ownerId: 'alice',
        isPersistent: true,
        properties: [],
        createdAt: '2026-09-08T00:00:00Z',
        updatedAt: '2026-09-08T00:00:00Z',
      },
    } as const;
    fetchSoup.mockResolvedValue({ items: [item], next_cursor: 'next' });
    createRoot((dispose) => {
      disposeRoot = dispose;
      useSoupAstItemsQuery(() => ({
        params: { sort_method: 'touched_by_me' },
        body: {},
        transport: 'rest',
      }));
    });
    const createOptions = vi.mocked(useInfiniteQuery).mock.calls.at(-1)?.[0];
    if (!createOptions) throw new Error('REST query was not created');
    const options = createOptions() as unknown as {
      queryFn: (context: {
        signal: AbortSignal;
        pageParam: null;
      }) => Promise<SoupAstItemsPage>;
      select: (data: { pages: SoupAstItemsPage[] }) => SoupAstItemsData;
    };
    const page = await options.queryFn({
      signal: new AbortController().signal,
      pageParam: null,
    });
    if (page.kind !== 'flat') throw new Error('Expected a flat page');
    page.items.push({
      ...item,
      touched_at: '2025-01-01T00:00:00Z',
      data: {
        ...item.data,
        properties: [],
        id: 'cached',
      },
    });
    const selected = options.select({ pages: [page] });
    expect(selected.entities).toHaveLength(2);
    expect(selected.oldestFetchedTimestamp).toBe(
      Date.parse('2026-09-08T00:00:00Z')
    );
  });

  it('uses REST refetch and skips mutation-driven GraphQL refresh when the flag is off', async () => {
    const query = mountAutoTransportQuery();

    expect(query.transport).toBe('rest');
    await query.refetch();
    await refreshActiveGraphqlSoupQueries();

    expect(restRefetch).toHaveBeenCalledOnce();
    expect(groupedQuery.refresh).not.toHaveBeenCalled();
  });

  it.each(['inbox', 'drafts', 'sent', 'all'] as const)(
    'keeps grouped %s mail on REST even when GraphQL is explicitly requested and cached mail exists',
    async (emailView) => {
      testState.graphqlEnabled = true;
      vi.mocked(createGraphqlGroupedSoupAstItemsQuery).mockImplementationOnce(
        (args) => ({
          ...groupedQuery,
          // Use the real AST eligibility boundary, not the default supported mock.
          isSupported: () => {
            const request = args();
            if (!request.groupBy) return false;
            try {
              makeGraphqlGroupedSoupInput({
                ...request,
                groupBy: request.groupBy,
              });
              return true;
            } catch {
              return false;
            }
          },
          data: () => ({ cachedMail: true, entities: [], groups: [] }),
        })
      );
      const query = createRoot((dispose) => {
        disposeRoot = dispose;
        return useSoupAstItemsQuery(() => ({
          params: { sort_method: 'updated_at' },
          body: { emailView },
          groupBy: { type: 'date' },
          transport: 'graphql',
        }));
      });
      expect(query.transport).toBe('rest');
      expect(query.data).toBeUndefined(); // Never exposes the private cached-Mail projection.
      const restOptions = vi.mocked(useInfiniteQuery).mock.calls.at(-1)?.[0];
      expect(restOptions?.().enabled).toBe(true);
      await query.refetch();
      await refreshActiveGraphqlSoupQueries();
      expect(restRefetch).toHaveBeenCalledOnce();
      expect(groupedQuery.refresh).not.toHaveBeenCalled();
    }
  );

  it('uses GraphQL refetch and mutation-driven refresh when the flag is on', async () => {
    testState.graphqlEnabled = true;
    const query = mountAutoTransportQuery();

    expect(query.transport).toBe('graphql');
    await query.refetch();
    expect(groupedQuery.refresh).toHaveBeenCalledOnce();
    expect(restRefetch).not.toHaveBeenCalled();

    vi.clearAllMocks();
    await refreshActiveGraphqlSoupQueries();

    // The refresh rebuilds every loaded page; resetting first would collapse
    // a scrolled list to its first page after each mutation.
    expect(groupedQuery.resetToInitialPage).not.toHaveBeenCalled();
    expect(groupedQuery.refresh).toHaveBeenCalledOnce();
    expect(restRefetch).not.toHaveBeenCalled();
  });
});