Skip to content

Commit

Permalink
* fix(create-review): fix failing create review function
Browse files Browse the repository at this point in the history
-add validation before processing data

[Fixes #116]

* fix(create-review): fix failing create review function

-add validation before processing data

[Fixes #116]
  • Loading branch information
jkarenzi authored and ambroisegithub committed May 31, 2024
1 parent 1b0a766 commit da959cd
Show file tree
Hide file tree
Showing 26 changed files with 1,295 additions and 82 deletions.
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,16 @@
],
"coveragePathIgnorePatterns": [
"/node_modules/",
"/src/emails/"
"/src/emails/",
"/src/utilis/"

],
"testPathIgnorePatterns": [
"/node_modules/",
"/src/emails/",
"/src/middlewares/"
"/src/middlewares/",
"/src/utilis/"

]
},
"devDependencies": {
Expand Down
4 changes: 0 additions & 4 deletions src/__test__/cartController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,6 @@ describe('Checkout Tests', () => {
.set('Authorization', `Bearer ${buyerToken}`)
.send({
deliveryInfo: '',
paymentInfo: '',
couponCode: 'DISCOUNT10',
});

Expand All @@ -244,7 +243,6 @@ describe('Checkout Tests', () => {
.set('Authorization', `Bearer ${buyerToken}`)
.send({
deliveryInfo: '123 Delivery St.',
paymentInfo: 'VISA 1234',
couponCode: 'DISCOUNT10',
});

Expand Down Expand Up @@ -283,7 +281,6 @@ describe('Checkout Tests', () => {
.set('Authorization', invalidUserToken)
.send({
deliveryInfo: '123 Delivery St.',
paymentInfo: 'VISA 1234',
couponCode: 'DISCOUNT10',
});

Expand All @@ -309,7 +306,6 @@ describe('Checkout Tests', () => {
.set('Authorization', `Bearer ${buyerToken}`)
.send({
deliveryInfo: '123 Delivery St.',
paymentInfo: 'VISA 1234',
couponCode: 'DISCOUNT10',
});

Expand Down
42 changes: 42 additions & 0 deletions src/__test__/chatbot.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import request from 'supertest';
import app from '../app';
import { afterAllHook, beforeAllHook, getBuyerToken } from './testSetup';

beforeAll(beforeAllHook);
afterAll(afterAllHook);
let buyerToken: string;

describe('Chatbot Interactions', () => {
beforeAll(async () => {
buyerToken = await getBuyerToken();
});

describe('Ask Question Endpoint', () => {
it('should respond to a valid question with the correct answer for logged-in users', async () => {
const chatData = {
message: 'What do you sell?',
};

const response = await request(app)
.post('/api/v1/chat')
.set('Authorization', `Bearer ${buyerToken}`)
.send(chatData);

expect(response.statusCode).toEqual(200);
expect(response.body.message).toContain(
'We sell the following products:'
);
});
});

describe('Fetch All Chat History', () => {
it('should return the full chat history for the authenticated user', async () => {
const response = await request(app)
.get('/api/v1/chat/history')
.set('Authorization', `Bearer ${buyerToken}`);

expect(response.statusCode).toEqual(200);
expect(Array.isArray(response.body.history)).toBeTruthy();
});
});
});
184 changes: 184 additions & 0 deletions src/__test__/chatbotService.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
import {
getProducts,
getProductByName,
getOrders,
getOrderByUserId,
getOrderStatusByTrackingNumber,
getServices,
getServiceByName,
getChatHistory,
} from '../service/chatbotService';
import dbConnection from '../database';
import { analyzeMessage, generateResponse } from '../utilis/nlp';
import User from '../database/models/userModel';
import Chat from '../database/models/chatbotModel';
import { processMessage } from '../service/chatbotService';
jest.mock('../database');
jest.mock('../utilis/nlp');

describe('Service Service', () => {
beforeEach(() => {
jest.clearAllMocks();
});

describe('processMessage', () => {
const userId = 1;
const user = { id: userId, firstName: 'John', lastName: 'Doe' } as User;
const mockDate = (hour: number) => {
const RealDate = Date;
const mockDate = new RealDate();
jest.spyOn(global, 'Date').mockImplementation(() => {
mockDate.setHours(hour);
return mockDate;
});
};

const setupMocks = () => {
const userRepoMock = {
findOne: jest.fn().mockResolvedValue(user),
};
const chatRepoMock = {
save: jest.fn().mockImplementation(async (chat: Chat) => chat),
};
(dbConnection.getRepository as jest.Mock).mockImplementation((entity) => {
if (entity === User) return userRepoMock;
if (entity === Chat) return chatRepoMock;
});
(analyzeMessage as jest.Mock).mockReturnValue('analyzed message');
(generateResponse as jest.Mock).mockResolvedValue('response');
};

it('should return a complete response with a morning greeting', async () => {
mockDate(9);
setupMocks();

const expectedGreeting = 'Good morning, John Doe! response';
const result = await processMessage(userId, 'test message');

expect(result.trim()).toEqual(expectedGreeting.trim());
});

it('should return a complete response with an afternoon greeting', async () => {
mockDate(15);
setupMocks();

const expectedGreeting = 'Good afternoon, John Doe! response';
const result = await processMessage(userId, 'test message');

expect(result.trim()).toEqual(expectedGreeting.trim());
});

it('should return a complete response with an evening greeting', async () => {
mockDate(20);
setupMocks();

const expectedGreeting = 'Good evening, John Doe! response';
const result = await processMessage(userId, 'test message');

expect(result.trim()).toEqual(expectedGreeting.trim());
});

it('should throw an error if user is not found', async () => {
const userRepoMock = {
findOne: jest.fn().mockResolvedValue(null),
};
(dbConnection.getRepository as jest.Mock).mockReturnValue(userRepoMock);

await expect(processMessage(userId, 'test message')).rejects.toThrow('User not found');

expect(userRepoMock.findOne).toHaveBeenCalledWith({ where: { id: userId } });
});
});

it('should return a list of products', async () => {
const productRepo = {
find: jest.fn().mockResolvedValue([{ name: 'Product1' }]),
};
dbConnection.getRepository = jest.fn().mockReturnValue(productRepo);

const products = await getProducts();
expect(products).toEqual([{ name: 'Product1' }]);
});
});

describe('getProductByName', () => {
it('should return a product by name', async () => {
const productRepo = {
findOne: jest.fn().mockResolvedValue({ name: 'Product1' }),
};
dbConnection.getRepository = jest.fn().mockReturnValue(productRepo);

const product = await getProductByName('Product1');
expect(product).toEqual({ name: 'Product1' });
});
});

describe('getOrders', () => {
it('should return a list of orders for a user', async () => {
const orderRepo = { find: jest.fn().mockResolvedValue([{ id: 1 }]) };
dbConnection.getRepository = jest.fn().mockReturnValue(orderRepo);

const orders = await getOrders(1);
expect(orders).toEqual([{ id: 1 }]);
});
});

describe('getOrderByUserId', () => {
it('should return an order for a user by userId', async () => {
const orderRepo = { findOne: jest.fn().mockResolvedValue({ id: 1 }) };
dbConnection.getRepository = jest.fn().mockReturnValue(orderRepo);

const order = await getOrderByUserId(1);
expect(order).toEqual({ id: 1 });
});
});

describe('getOrderStatusByTrackingNumber', () => {
it('should return the status of an order by tracking number', async () => {
const orderRepo = { findOne: jest.fn().mockResolvedValue({ status: 'Shipped' }) };
dbConnection.getRepository = jest.fn().mockReturnValue(orderRepo);

const status = await getOrderStatusByTrackingNumber('12345');
expect(status).toBe('Shipped');
});

it('should return "Tracking number not found" if order is not found', async () => {
const orderRepo = { findOne: jest.fn().mockResolvedValue(null) };
dbConnection.getRepository = jest.fn().mockReturnValue(orderRepo);

const status = await getOrderStatusByTrackingNumber('12345');
expect(status).toBe('Tracking number not found');
});
});

describe('getServices', () => {
it('should return a list of services', async () => {
const serviceRepo = { find: jest.fn().mockResolvedValue([{ name: 'Service1' }]) };
dbConnection.getRepository = jest.fn().mockReturnValue(serviceRepo);

const services = await getServices();
expect(services).toEqual([{ name: 'Service1' }]);
});
});

describe('getServiceByName', () => {
it('should return a service by name', async () => {
const serviceRepo = { findOne: jest.fn().mockResolvedValue({ name: 'Service1' }) };
dbConnection.getRepository = jest.fn().mockReturnValue(serviceRepo);

const service = await getServiceByName('Service1');
expect(service).toEqual({ name: 'Service1' });
});
});

describe('getChatHistory', () => {
it('should return chat history for a user', async () => {
const userId = 1;
const chatRepo = { find: jest.fn().mockResolvedValue([{ message: 'Hello', createdAt: '2024-05-31' }]) };
dbConnection.getRepository = jest.fn().mockReturnValue(chatRepo);

const chatHistory = await getChatHistory(userId);
expect(chatHistory).toEqual([{ message: 'Hello', createdAt: '2024-05-31' }]);
});
});

Loading

0 comments on commit da959cd

Please sign in to comment.