From e8bd46378ade0910b47d8f6a34d32ebd539eea5f Mon Sep 17 00:00:00 2001 From: Matti Bar-Zeev Date: Sun, 4 Aug 2024 23:34:47 +0300 Subject: [PATCH] chore: Add unit tests to claudeAi service --- jest.config.js | 2 +- services/claudeAi.js | 14 ++++----- services/claudeAi.test.js | 61 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 10 deletions(-) create mode 100644 services/claudeAi.test.js diff --git a/jest.config.js b/jest.config.js index 5d73bf1..9607661 100644 --- a/jest.config.js +++ b/jest.config.js @@ -1,6 +1,6 @@ module.exports = { testEnvironment: 'node', - roots: ['/tests'], + roots: [''], moduleFileExtensions: ['js', 'json', 'jsx', 'ts', 'tsx', 'node'], moduleNameMapper: { '^@/(.*)$': '/$1', diff --git a/services/claudeAi.js b/services/claudeAi.js index 6e7709f..3b12f8a 100644 --- a/services/claudeAi.js +++ b/services/claudeAi.js @@ -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") { +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 }; \ No newline at end of file +module.exports = {generateClaudeContent}; diff --git a/services/claudeAi.test.js b/services/claudeAi.test.js new file mode 100644 index 0000000..2afc3a8 --- /dev/null +++ b/services/claudeAi.test.js @@ -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); + }); +});