Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Add unit tests to claudeAi service #6

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = {
testEnvironment: 'node',
roots: ['<rootDir>/tests'],
roots: ['<rootDir>'],
moduleFileExtensions: ['js', 'json', 'jsx', 'ts', 'tsx', 'node'],
moduleNameMapper: {
'^@/(.*)$': '<rootDir>/$1',
Expand Down
14 changes: 5 additions & 9 deletions services/claudeAi.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
const Anthropic = require("@anthropic-ai/sdk");
const Anthropic = require('@anthropic-ai/sdk');
require('dotenv').config();

const anthropic = new Anthropic({
apiKey: process.env['ANTHROPIC_API_KEY'], // This is the default and can be omitted
});


async function generateClaudeContent(content, model_type = "claude-3-5-sonnet-20240620") {
Copy link
Author

@mbarzeev mbarzeev Aug 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've noticed that you're not using the model_type arg at all, so I fixed that

async function generateClaudeContent(content, model_type = 'claude-3-opus-20240229') {
try {
const result = await anthropic.messages.create({
max_tokens: 4096, // This is the maximum number of tokens that can be generated
messages: content,
model: 'claude-3-opus-20240229',
model: model_type,
});

return result.content[0].text;

} catch (error) {
console.error("Error in generateClaudeContent:", error);
console.error('Error in generateClaudeContent:', error);
}

}
module.exports = { generateClaudeContent };
module.exports = {generateClaudeContent};
61 changes: 61 additions & 0 deletions services/claudeAi.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
const createSpy = jest.fn().mockResolvedValue({
content: [
{
text: 'mock-result-text',
},
],
});
jest.doMock('@anthropic-ai/sdk', () => {
return jest.fn().mockImplementation(() => {
return {
messages: {
create: createSpy,
},
};
});
});

const {generateClaudeContent} = require('./claudeAi');

describe('claudeAi service', () => {
afterEach(() => {
createSpy.mockClear();
});
it('should call Anthropic.messages.create with the given args', async () => {
const mockContent = 'this is a mock content';

await generateClaudeContent(mockContent);

expect(createSpy).toHaveBeenCalledWith({
max_tokens: 4096,
messages: 'this is a mock content',
model: 'claude-3-opus-20240229',
});
});

it('should call Anthropic.messages.create with a given model', async () => {
const mockContent = 'this is a mock content';
const mockModel = 'claude-3-5-sonnet-20240620';

await generateClaudeContent(mockContent, mockModel);

expect(createSpy).toHaveBeenCalledWith({
max_tokens: 4096,
messages: 'this is a mock content',
model: 'claude-3-5-sonnet-20240620',
});
});

it('should console error if something went wrong', async () => {
const mockContent = 'this is a mock content';
const mockError = new Error('Something went wrong');
jest.spyOn(console, 'error');
createSpy.mockImplementationOnce(() => {
console.log('throwing :>> ');
throw mockError;
});

await generateClaudeContent(mockContent);
expect(console.error).toHaveBeenCalledWith('Error in generateClaudeContent:', mockError);
});
});