Skip to content

The open alternative to Salesforce, designed for AI.

1.4 KB
Raw Blame
import { vi } from 'vitest';

export const USER_WORKSPACE_ID = '11111111-1111-1111-1111-111111111111';

type LinearFetchInit = {
  method: string;
  headers: Record<string, string>;
  body: string;
};

export const buildConnection = (
  overrides: Partial<Record<string, unknown>> = {},
) => ({
  id: 'conn_1',
  name: '[email protected]',
  visibility: 'user' as const,
  providerName: 'linear',
  userWorkspaceId: USER_WORKSPACE_ID,
  accessToken: 'lin_test_access_token',
  scopes: ['read', 'write'],
  handle: '[email protected]',
  lastRefreshedAt: '2024-01-01T00:00:00.000Z',
  authFailedAt: null,
  ...overrides,
});

// Stubs `fetch` to first answer the SDK's `listConnections` GraphQL call to
// `/metadata`, then the handler's downstream Linear GraphQL request.
export const stubConnectionsThenLinear = (
  connections: ReturnType<typeof buildConnection>[],
  linearJson: unknown,
) => {
  const fetchMock = vi.fn(async (url: string, _init: LinearFetchInit) => {
    if (url.endsWith('/metadata')) {
      const connectionsJson = { data: { appConnections: connections } };

      return {
        ok: true,
        status: 200,
        json: async () => connectionsJson,
        text: async () => JSON.stringify(connectionsJson),
      };
    }

    return {
      ok: true,
      status: 200,
      json: async () => linearJson,
      text: async () => JSON.stringify(linearJson),
    };
  });

  vi.stubGlobal('fetch', fetchMock);

  return fetchMock;
};