@chargetrip/mcp
Version:
Chargetrip MCP server
155 lines (127 loc) • 5.04 kB
text/typescript
import { createClient, subscriptionExchange } from '@urql/core';
import { createClient as wsCreateClient } from 'graphql-ws';
import { GraphQLClient } from '../graphql-client';
jest.mock('graphql-ws', () => ({
createClient: jest.fn().mockReturnValue({ subscribe: jest.fn() }),
}));
jest.mock('@urql/core', () => ({
...jest.requireActual('@urql/core'),
createClient: jest.fn(),
subscriptionExchange: jest.fn(),
}));
describe('GraphQLClient', () => {
const originalEnv = process.env;
beforeEach(() => {
jest.resetModules();
process.env = {
...originalEnv,
GRAPHQL_ENDPOINT: 'http://localhost:4000/graphql',
GRAPHQL_WS_ENDPOINT: 'ws://localhost:4000/graphql',
};
});
afterAll(() => {
process.env = originalEnv;
});
describe('constructor', () => {
it('should create a new instance with required parameters', () => {
process.env.CLIENT_ID = 'test-client';
process.env.APP_ID = 'test-app';
const client = new GraphQLClient();
expect(client).toBeInstanceOf(GraphQLClient);
});
it('should create instance with optional parameters', () => {
process.env.CLIENT_ID = 'test-client';
process.env.APP_ID = 'test-app';
process.env.APP_IDENTIFIER = 'test-identifier';
process.env.APP_FINGERPRINT = 'test-fingerprint';
const client = new GraphQLClient();
expect(client).toBeInstanceOf(GraphQLClient);
});
it('should make sure websocket retries', () => {
process.env.CLIENT_ID = 'test-client';
process.env.APP_ID = 'test-app';
process.env.APP_IDENTIFIER = 'test-identifier';
process.env.APP_FINGERPRINT = 'test-fingerprint';
const client = new GraphQLClient();
expect(client).toBeInstanceOf(GraphQLClient);
expect(wsCreateClient).toHaveBeenCalledTimes(1);
expect((wsCreateClient as jest.Mock).mock.calls[0][0].shouldRetry()).toEqual(true);
});
it('should make all callbacks are working', () => {
process.env.CLIENT_ID = 'test-client';
process.env.APP_ID = 'test-app';
process.env.APP_IDENTIFIER = 'test-identifier';
process.env.APP_FINGERPRINT = 'test-fingerprint';
const mockSubscribe = jest.fn();
(wsCreateClient as jest.Mock).mockReturnValue({ subscribe: mockSubscribe });
const client = new GraphQLClient();
expect(client).toBeInstanceOf(GraphQLClient);
expect(createClient).toHaveBeenCalledTimes(1);
expect((createClient as jest.Mock).mock.calls[0][0].fetchOptions()).toEqual({
cache: 'no-cache',
method: 'POST',
headers: {
'x-app-fingerprint': 'test-fingerprint',
'x-app-id': 'test-app',
'x-app-identifier': 'test-identifier',
'x-client-id': 'test-client',
},
});
expect((createClient as jest.Mock).mock.calls[0][0].exchanges.length).toEqual(2);
expect((subscriptionExchange as jest.Mock).mock.calls[0][0]).toEqual({
forwardSubscription: expect.any(Function),
});
const callbackWithQuery = (
subscriptionExchange as jest.Mock
).mock.calls[0][0].forwardSubscription({
id: 'request',
query: 'query',
variables: {},
});
const resultWithQuery = callbackWithQuery.subscribe({
next: jest.fn(),
error: jest.fn(),
complete: jest.fn(),
});
expect(resultWithQuery).toBeDefined();
const callbackWithoutQuery = (
subscriptionExchange as jest.Mock
).mock.calls[0][0].forwardSubscription({
id: 'request',
});
const resultWithoutQuery = callbackWithoutQuery.subscribe({
next: jest.fn(),
error: jest.fn(),
complete: jest.fn(),
});
expect(resultWithoutQuery).toBeDefined();
});
it('should build the headers without app identifier and fingerprint', () => {
process.env.CLIENT_ID = 'test-client';
process.env.APP_ID = 'test-app';
const backupIdentifier = process.env.APP_IDENTIFIER;
const backupFingerprint = process.env.APP_FINGERPRINT;
delete process.env.APP_IDENTIFIER;
delete process.env.APP_FINGERPRINT;
const mockSubscribe = jest.fn();
(wsCreateClient as jest.Mock).mockReturnValue({ subscribe: mockSubscribe });
const client = new GraphQLClient();
expect(client).toBeInstanceOf(GraphQLClient);
expect(createClient).toHaveBeenCalledTimes(1);
expect((createClient as jest.Mock).mock.calls[0][0].fetchOptions()).toEqual({
cache: 'no-cache',
method: 'POST',
headers: {
'x-app-id': 'test-app',
'x-client-id': 'test-client',
},
});
expect((createClient as jest.Mock).mock.calls[0][0].exchanges.length).toEqual(2);
expect((subscriptionExchange as jest.Mock).mock.calls[0][0]).toEqual({
forwardSubscription: expect.any(Function),
});
process.env.APP_IDENTIFIER = backupIdentifier;
process.env.APP_FINGERPRINT = backupFingerprint;
});
});
});