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.

5.1 KB
Raw Blame
import { ChannelUnreadPresenceDocument } from '@service-storage/graphql/generated/graphql';
import { createClient, type Operation, type OperationResult } from '@urql/core';
import { createRoot, createSignal } from 'solid-js';
import { describe, expect, it, vi } from 'vitest';
import { makeSubject, pipe, subscribe } from 'wonka';
import {
  getActiveGraphqlSoupRevalidations,
  refreshActiveGraphqlSoupQueries,
} from '../soup/graphql/active-queries';
import {
  getChannelListRevalidations,
  revalidateChannelLists,
} from '../soup/graphql/channel-list-revalidation';
import { createChannelUnreadQuery } from './unread-presence';

const clientMock = vi.hoisted(() => vi.fn());
const cache = vi.hoisted(() => ({ host: false }));
vi.mock('@service-storage/graphql-soup', () => ({
  getGraphqlSoupClient: clientMock,
  hasGraphqlCacheHost: () => cache.host,
}));

function setup() {
  const operations: Operation[] = [];
  const network = makeSubject<OperationResult>();
  const client = createClient({
    url: 'http://test/graphql',
    exchanges: [
      () => (ops) => {
        pipe(
          ops,
          subscribe((op) => operations.push(op))
        );
        return network.source;
      },
    ],
  });
  clientMock.mockReturnValue(client);
  const root = createRoot((dispose) => {
    const [enabled, setEnabled] = createSignal(false);
    const query = createChannelUnreadQuery(
      { initial: { limit: 500 } },
      enabled
    );
    return { dispose, query, setEnabled };
  });
  const respond = (state: 'UNSEEN' | 'SEEN') =>
    network.next({
      operation: operations.filter((op) => op.kind === 'query').at(-1)!,
      stale: false,
      hasNext: false,
      data: {
        user: {
          id: 'viewer',
          soup: {
            items: [
              {
                __typename: 'GraphqlSoupChannel',
                id: 'channel',
                unreadNotifications: [{ id: 'one', state }],
              },
            ],
          },
        },
      },
    });
  return { ...root, operations, respond, client };
}

describe('channel unread presence query', () => {
  it('loads only when enabled and retains reactive witness states', async () => {
    const f = setup();
    try {
      expect(f.operations.filter((op) => op.kind === 'query')).toHaveLength(0);
      expect(getChannelListRevalidations()).toHaveLength(0);
      f.setEnabled(true);
      expect(f.query.isLoading).toBe(true);
      await vi.waitFor(() =>
        expect(f.operations.filter((op) => op.kind === 'query')).toHaveLength(1)
      );
      expect(f.operations[0].query).toEqual(ChannelUnreadPresenceDocument);
      f.respond('UNSEEN');
      expect(f.query.data).toEqual([{ id: 'one', state: 'UNSEEN' }]);
      f.respond('SEEN');
      expect(f.query.data).toEqual([{ id: 'one', state: 'SEEN' }]);
      expect(getChannelListRevalidations()).toEqual([
        {
          document: ChannelUnreadPresenceDocument,
          variables: { input: { initial: { limit: 500 } } },
        },
      ]);
      f.setEnabled(false);
      expect(getChannelListRevalidations()).toHaveLength(0);
    } finally {
      f.dispose();
    }
  });

  it('shares bounded notification revalidation and unregisters on disposal', async () => {
    const visible = vi.spyOn(document, 'hidden', 'get').mockReturnValue(false);
    const f = setup();
    try {
      f.setEnabled(true);
      await vi.waitFor(() =>
        expect(f.operations.filter((op) => op.kind === 'query')).toHaveLength(1)
      );
      f.respond('UNSEEN');
      const query = vi.fn(() => ({ toPromise: async () => ({ data: {} }) }));
      await revalidateChannelLists({ query } as unknown as Pick<
        typeof f.client,
        'query'
      >);
      expect(query).not.toHaveBeenCalled();
      const ownQuery = vi.spyOn(f.client, 'query');
      await revalidateChannelLists(f.client);
      await vi.waitFor(() => expect(ownQuery).toHaveBeenCalledOnce());
      expect(ownQuery).toHaveBeenCalledWith(
        ChannelUnreadPresenceDocument,
        { input: { initial: { limit: 500 } } },
        { requestPolicy: 'network-only' }
      );
      f.respond('SEEN');
      expect(f.query.data).toEqual([{ id: 'one', state: 'SEEN' }]);
    } finally {
      visible.mockRestore();
      f.dispose();
    }
    expect(getChannelListRevalidations()).toHaveLength(0);
  });

  it('counts as kept current by the cache, which writes its witness states', async () => {
    const visible = vi.spyOn(document, 'hidden', 'get').mockReturnValue(false);
    const f = setup();
    try {
      f.setEnabled(true);
      await vi.waitFor(() =>
        expect(f.operations.filter((op) => op.kind === 'query')).toHaveLength(1)
      );
      f.respond('UNSEEN');
      const unmaintained = () =>
        getActiveGraphqlSoupRevalidations(undefined, {
          unmaintainedOnly: true,
        });

      cache.host = false;
      expect(unmaintained()).toHaveLength(1);

      cache.host = true;
      expect(unmaintained()).toHaveLength(0);
      const reads = f.operations.length;
      await refreshActiveGraphqlSoupQueries({ unmaintainedOnly: true });
      expect(f.operations).toHaveLength(reads);
    } finally {
      cache.host = false;
      visible.mockRestore();
      f.dispose();
    }
  });
});