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.3 KB
Raw Blame
import {
  GroupSoupMembershipDocument,
  type GroupSoupMembershipQuery,
  type GroupSoupMembershipQueryVariables,
} from '@service-storage/graphql/generated/graphql';
import { describe, expect, expectTypeOf, it, vi } from 'vitest';
import { createNoopCacheHost } from '../host/noop-host';
import type { CacheHost } from '../host/types';
import { inspect, inspectVariants, selectAll } from './inspection';

const input = {
  initial: {
    groupBy: {
      field: 'PROPERTY' as const,
      propertyDefinitionId: 'status-def',
    },
    limit: 20,
  },
};

describe('typed generated query inspection', () => {
  it('discovers typed variables without materializing query values', async () => {
    const inspectQueryVariants = async (request: unknown) => {
      expect(request).toMatchObject({
        operationName: 'GroupSoupMembership',
        path: [{ field: 'user' }, { field: 'groupSoup' }],
      });
      expect(request).not.toHaveProperty('variables');
      expect(request).not.toHaveProperty('variableFilters');
      return [{ variables: { input } }];
    };
    const host = { inspectQueryVariants } as unknown as CacheHost;

    const result = await inspectVariants(
      host,
      selectAll(GroupSoupMembershipDocument).field('user').field('groupSoup')
    );

    expect(result).toEqual([{ variables: { input } }]);
    expect(result[0]).not.toHaveProperty('value');
  });

  it('serializes the document, field path, and typed variable filters', async () => {
    const inspectQuery = async (request: unknown) => {
      expect(request).toMatchObject({
        operationName: 'GroupSoupMembership',
        path: [{ field: 'user' }, { field: 'groupSoup' }],
        variableFilters: [
          {
            input: {
              initial: {
                groupBy: { propertyDefinitionId: 'status-def' },
              },
            },
          },
        ],
      });
      expect(request).not.toHaveProperty('variables');
      expect(request).not.toHaveProperty('entityKey');
      return [
        {
          variables: { input },
          value: { bins: [{ key: 'status', items: [] }] },
        },
        {
          variables: {
            input: { ...input, initial: { ...input.initial, limit: 50 } },
          },
        },
      ];
    };
    const host = { inspectQuery } as unknown as CacheHost;

    const result = await inspect(
      host,
      selectAll(GroupSoupMembershipDocument).field('user').field('groupSoup'),
      {
        variableFilters: [
          {
            input: {
              initial: {
                groupBy: { propertyDefinitionId: 'status-def' },
              },
            },
          },
        ],
      }
    );

    expect(result).toHaveLength(2);
    expect(result[0]?.variables).toEqual({ input });
    expect(result[1]).not.toHaveProperty('value');
  });

  it('infers generated response and variable types', () => {
    const typeAssertions = async (host: CacheHost) => {
      const root = selectAll(GroupSoupMembershipDocument);
      // @ts-expect-error `missing` is not selected by the generated query.
      root.field('missing');

      const selected = root.field('user').field('groupSoup');
      // @ts-expect-error Lists cannot be traversed by v1 inspection paths.
      selected.field('bins').field('length');

      const variants = await inspectVariants(host, selected);
      const results = await inspect(host, selected, {
        variableFilters: [{ input: { initial: { limit: 20 } } }],
      });
      // @ts-expect-error Filters are typed from the generated operation variables.
      await inspect(host, selected, { variableFilters: [{ missing: true }] });
      expectTypeOf(
        variants[0]!.variables
      ).toEqualTypeOf<GroupSoupMembershipQueryVariables>();
      expectTypeOf(
        results[0]!.variables
      ).toEqualTypeOf<GroupSoupMembershipQueryVariables>();
      expectTypeOf(results[0]!.value).toEqualTypeOf<
        GroupSoupMembershipQuery['user']['groupSoup'] | undefined
      >();
      // @ts-expect-error Variables are inferred, not an arbitrary record.
      results[0]!.variables.missing;
      // @ts-expect-error The selected value is groupSoup, not the query root.
      results[0]!.value?.user;
    };

    expect(typeAssertions).toBeTypeOf('function');
  });

  it('returns no selections through the no-op host', async () => {
    const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
    try {
      const host = createNoopCacheHost();
      const selection = selectAll(GroupSoupMembershipDocument)
        .field('user')
        .field('groupSoup');
      const variants = await inspectVariants(host, selection);
      const result = await inspect(host, selection);
      expect(variants).toEqual([]);
      expect(result).toEqual([]);
    } finally {
      warn.mockRestore();
    }
  });

  it('rejects malformed host results', async () => {
    const selection = selectAll(GroupSoupMembershipDocument)
      .field('user')
      .field('groupSoup');
    const host = {
      inspectQuery: async () => [{ variables: null }],
      inspectQueryVariants: async () => [{ variables: null }],
    } as unknown as CacheHost;
    await expect(inspectVariants(host, selection)).rejects.toThrow(
      'invalid cache query inspection variant'
    );
    await expect(inspect(host, selection)).rejects.toThrow(
      'invalid cache query inspection instance'
    );
  });
});