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.

9.4 KB
Raw Blame
import { ThrownResultError } from '@core/util/result';
import { cleanup, render } from '@solidjs/testing-library';
import { QueryClient, QueryClientProvider } from '@tanstack/solid-query';
import { errAsync, okAsync, ResultAsync } from 'neverthrow';
import { type Accessor, createSignal } from 'solid-js';
import { createStore } from 'solid-js/store';
import { afterEach, beforeEach, expect, it, vi } from 'vitest';
import type { SharingSource } from '../context/contracts';
import type { AccessRequest, RequestState, ShareEntity } from '../core/types';
import { createSharingSource } from './source';

const mocks = vi.hoisted(() => ({
  submit: vi.fn(),
  grants: vi.fn(),
  refresh: vi.fn(),
  invalidate: vi.fn(),
}));
let snapshot: {
  isSuccess: boolean;
  isPending: boolean;
  isFetching: boolean;
  isPaused: boolean;
  isFetchedAfterMount: boolean;
  isError: boolean;
  error: Error | null;
  data: RequestState;
  refetch: () => Promise<{ data: RequestState; error?: Error | null }>;
};
let offset: Accessor<number>;
vi.mock('@queries/access-requests/requests', () => ({
  accessRequestsClient: { submit: mocks.submit, grants: mocks.grants },
  refreshSharedAccess: mocks.refresh,
  invalidateAccessRequests: mocks.invalidate,
  useAccessRequestState: (
    _entity: unknown,
    _enabled: unknown,
    currentOffset: Accessor<number>
  ) => {
    offset = currentOffset;
    return snapshot;
  },
}));

const first: ShareEntity = { entityType: 'document', entityId: 'first' };
const second: ShareEntity = { entityType: 'document', entityId: 'second' };
const request: AccessRequest = {
  id: 'request',
  requesterId: 'macro|[email protected]',
  requestedLevel: 'edit',
  status: 'pending',
  revision: 1,
  createdAt: '2026-10-15T00:00:00Z',
  updatedAt: '2026-10-15T00:00:00Z',
};

beforeEach(() => {
  vi.clearAllMocks();
  mocks.grants.mockImplementation(() => okAsync([]));
  mocks.invalidate.mockResolvedValue(undefined);
});
afterEach(cleanup);

function mount(initial: Partial<RequestState> = {}) {
  const [entity, navigate] = createSignal<ShareEntity>(first);
  const [data, update] = createStore<RequestState>({
    canReview: false,
    accessLevel: null,
    supportedLevels: ['view', 'comment', 'edit'],
    requests: [],
    pendingCount: 0,
    nextOffset: 50,
    ...initial,
  });
  snapshot = {
    isSuccess: true,
    isPending: false,
    isFetching: false,
    isPaused: false,
    isFetchedAfterMount: true,
    isError: false,
    error: null,
    data,
    refetch: async () => ({ data }),
  };
  const client = new QueryClient({
    defaultOptions: { queries: { retry: false }, mutations: { retry: false } },
  });
  let source!: SharingSource;
  function Host() {
    source = createSharingSource(entity, () => true);
    return null;
  }
  render(() => (
    <QueryClientProvider client={client}>
      <Host />
    </QueryClientProvider>
  ));
  return { source, navigate, update };
}

it.each(['success', 'failure'] as const)(
  'keeps a pending mutation on its original document after navigation (%s)',
  async (outcome) => {
    const pending = Promise.withResolvers<AccessRequest>();
    const started = Promise.withResolvers<void>();
    mocks.submit.mockImplementation(() => {
      started.resolve();
      return ResultAsync.fromPromise(pending.promise, () => [
        { code: 'NETWORK_ERROR', message: 'offline' },
      ]);
    });
    const { source, navigate } = mount();
    const result = source.submit('edit');
    const settled =
      outcome === 'failure' ? expect(result).rejects.toThrow() : result;
    navigate(second);
    source.nextPage();
    expect(offset()).toBe(50);
    await started.promise;
    expect(mocks.submit).toHaveBeenCalledWith(first, 'edit', undefined);
    if (outcome === 'success') pending.resolve(request);
    else pending.reject(new Error('offline'));
    await settled;
    expect(offset()).toBe(50);
    if (outcome === 'success') {
      expect(mocks.refresh).toHaveBeenCalledExactlyOnceWith(first);
      expect(mocks.invalidate).not.toHaveBeenCalled();
    } else {
      expect(mocks.invalidate).toHaveBeenCalledExactlyOnceWith(first);
      expect(mocks.refresh).not.toHaveBeenCalled();
    }
  }
);

it('loads direct grants for an authorized reviewer without file access', async () => {
  const loaded = Promise.withResolvers<void>();
  mocks.grants.mockImplementation(() => {
    loaded.resolve();
    return okAsync([]);
  });
  const { update } = mount();
  expect(mocks.grants).not.toHaveBeenCalled();
  update('canReview', true);
  await loaded.promise;
  expect(mocks.grants).toHaveBeenCalledExactlyOnceWith(first);
});

it('keeps loaded request and grant rows available through a failed background refresh', async () => {
  const grants = [{ userId: 'macro|[email protected]', accessLevel: 'view' }];
  mocks.grants.mockImplementation(() => okAsync(grants));
  const { source } = mount({ accessLevel: 'view', mine: request });
  await source.refresh();
  expect(source.state()?.mine).toEqual(request);
  expect(source.grants()).toEqual(grants);
  snapshot.isSuccess = false;
  snapshot.isError = true;
  mocks.grants.mockImplementation(() =>
    errAsync([{ code: 'NETWORK_ERROR', message: 'offline' }])
  );
  await source.refresh();
  expect(source.error()).toBe('Could not load access requests.');
  expect(source.state()?.mine).toEqual(request);
  expect(source.grants()).toEqual(grants);
});

it('does not show the previous document grant rows while the new document is pending', async () => {
  const grants = [{ userId: 'macro|[email protected]', accessLevel: 'view' }];
  mocks.grants.mockImplementation(() => okAsync(grants));
  const { source, navigate } = mount({ accessLevel: 'view', mine: request });
  await source.refresh();
  expect(source.grants()).toEqual(grants);
  const pending = Promise.withResolvers<never[]>();
  mocks.grants.mockImplementation(() =>
    ResultAsync.fromSafePromise(pending.promise)
  );
  snapshot.isPending = true;
  snapshot.isSuccess = false;
  navigate(second);
  expect(source.state()).toBeUndefined();
  expect(source.grants()).toEqual([]);
  pending.resolve([]);
});

it('drops a grants failure from before the viewer lost access', async () => {
  const failed = Promise.withResolvers<void>();
  mocks.grants.mockImplementation(() => {
    failed.resolve();
    return errAsync([{ code: 'FORBIDDEN', message: 'no access' }]);
  });
  const { source, update } = mount({ accessLevel: 'comment' });
  await failed.promise;
  await vi.waitFor(() =>
    expect(source.error()).toBe('Could not load access requests.')
  );
  update('accessLevel', null);
  expect(source.error()).toBeUndefined();
  await source.refresh();
  expect(mocks.grants).toHaveBeenCalledOnce();
  expect(source.error()).toBeUndefined();
});

it('refreshes grants from the refetched state, not the cached one', async () => {
  const { source } = mount({ accessLevel: null });
  expect(mocks.grants).not.toHaveBeenCalled();
  snapshot.refetch = async () => ({
    data: { ...snapshot.data, accessLevel: 'comment' },
  });
  await source.refresh();
  expect(mocks.grants).toHaveBeenCalledOnce();
});

it('hides grant rows once the viewer can no longer read them', async () => {
  const grants = [{ userId: 'macro|[email protected]', accessLevel: 'view' }];
  mocks.grants.mockImplementation(() => okAsync(grants));
  const { source, update } = mount({ accessLevel: 'view' });
  await source.refresh();
  expect(source.grants()).toEqual(grants);
  update('accessLevel', null);
  expect(source.grants()).toEqual([]);
});

it('keeps grant rows while another page of requests loads', async () => {
  const grants = [{ userId: 'macro|[email protected]', accessLevel: 'view' }];
  mocks.grants.mockImplementation(() => okAsync(grants));
  const { source } = mount({ accessLevel: 'view' });
  await source.refresh();
  snapshot.isPending = true;
  expect(source.grants()).toEqual(grants);
});

it('does not refresh grants for an entity the refresh found deleted', async () => {
  const { source } = mount({ accessLevel: 'view' });
  await source.refresh();
  mocks.grants.mockClear();
  snapshot.refetch = async () => ({
    data: snapshot.data,
    error: new ThrownResultError([{ code: 'NOT_FOUND', message: 'gone' }]),
  });
  await source.refresh();
  expect(mocks.grants).not.toHaveBeenCalled();
});

it('keeps loading while a new mount refetches a cached state', () => {
  const { source } = mount({ accessLevel: 'view' });
  Object.assign(snapshot, { isFetching: true, isFetchedAfterMount: false });
  expect(source.loading()).toBe(true);
  Object.assign(snapshot, { isFetching: false, isFetchedAfterMount: true });
  expect(source.loading()).toBe(false);
  Object.assign(snapshot, { isFetching: true });
  expect(source.loading()).toBe(false);
});

it('keeps loading while an offline mount waits to refetch a cached state', () => {
  const { source } = mount({ accessLevel: 'view' });
  Object.assign(snapshot, { isPaused: true, isFetchedAfterMount: false });
  expect(source.loading()).toBe(true);
});

it('tells a deleted entity from a failed state read', () => {
  const { source } = mount({ accessLevel: null });
  expect(source.notFound()).toBe(false);
  Object.assign(snapshot, {
    isSuccess: false,
    isError: true,
    error: new ThrownResultError([
      { code: 'NETWORK_ERROR', message: 'offline' },
    ]),
  });
  expect(source.notFound()).toBe(false);
  Object.assign(snapshot, {
    error: new ThrownResultError([{ code: 'NOT_FOUND', message: 'gone' }]),
  });
  expect(source.notFound()).toBe(true);
  expect(source.state()?.accessLevel).toBeNull();
});