Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions frontend/__tests__/lib/langgraph-server.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@

import { Client } from '@langchain/langgraph-sdk';

// Mock the dependencies
jest.mock('@langchain/langgraph-sdk', () => {
return {
Client: jest.fn().mockImplementation(() => ({
threads: {
create: jest.fn(),
},
})),
};
});

jest.mock('../../lib/langgraph-base', () => {
return {
LangGraphBase: jest.fn(),
};
});

describe('langgraph-server', () => {
const originalEnv = process.env;

beforeEach(() => {
jest.resetModules();
process.env = { ...originalEnv };
process.env.NEXT_PUBLIC_LANGGRAPH_API_URL = 'http://localhost:2024';
});

afterAll(() => {
process.env = originalEnv;
});

it('should throw if NEXT_PUBLIC_LANGGRAPH_API_URL is missing', () => {
delete process.env.NEXT_PUBLIC_LANGGRAPH_API_URL;

expect(() => {
jest.isolateModules(() => {
require('../../lib/langgraph-server');
});
}).toThrow('NEXT_PUBLIC_LANGGRAPH_API_URL is not set');
});

it('should initialize without LANGCHAIN_API_KEY', () => {
delete process.env.LANGCHAIN_API_KEY;

expect(() => {
jest.isolateModules(() => {
require('../../lib/langgraph-server');
});
}).not.toThrow();
});

it('should initialize with LANGCHAIN_API_KEY if provided', () => {
process.env.LANGCHAIN_API_KEY = 'test-key';

expect(() => {
jest.isolateModules(() => {
require('../../lib/langgraph-server');
});
}).not.toThrow();
});

it('should pass API key to Client if provided', () => {
process.env.LANGCHAIN_API_KEY = 'test-key';

let ClientMock;
jest.isolateModules(() => {
require('../../lib/langgraph-server');
ClientMock = require('@langchain/langgraph-sdk').Client;
});

expect(ClientMock).toHaveBeenCalledWith(
expect.objectContaining({
defaultHeaders: expect.objectContaining({
'X-Api-Key': 'test-key',
}),
})
);
});

it('should not pass X-Api-Key header if LANGCHAIN_API_KEY is missing', () => {
delete process.env.LANGCHAIN_API_KEY;

let ClientMock;
jest.isolateModules(() => {
require('../../lib/langgraph-server');
ClientMock = require('@langchain/langgraph-sdk').Client;
});

expect(ClientMock).toHaveBeenCalledWith(
expect.objectContaining({
defaultHeaders: expect.not.objectContaining({
'X-Api-Key': expect.anything(),
}),
})
);
});
});
13 changes: 7 additions & 6 deletions frontend/lib/langgraph-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@ export const createServerClient = () => {
throw new Error('NEXT_PUBLIC_LANGGRAPH_API_URL is not set');
}

if (!process.env.LANGCHAIN_API_KEY) {
throw new Error('LANGCHAIN_API_KEY is not set');
const defaultHeaders: Record<string, string> = {
'Content-Type': 'application/json',
};

if (process.env.LANGCHAIN_API_KEY) {
defaultHeaders['X-Api-Key'] = process.env.LANGCHAIN_API_KEY;
}

const client = new Client({
apiUrl: process.env.NEXT_PUBLIC_LANGGRAPH_API_URL,
defaultHeaders: {
'Content-Type': 'application/json',
'X-Api-Key': process.env.LANGCHAIN_API_KEY,
},
defaultHeaders,
});

clientInstance = new LangGraphBase(client);
Expand Down
Loading