Tests refactor
This commit is contained in:
parent
245520fb50
commit
99134d8556
165 changed files with 911 additions and 531 deletions
|
|
@ -0,0 +1,259 @@
|
|||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
|
||||
const { mockFind, mockFindOne, mockAggregate, mockSave } = vi.hoisted(() => ({
|
||||
mockFind: vi.fn(),
|
||||
mockFindOne: vi.fn(),
|
||||
mockAggregate: vi.fn(),
|
||||
mockSave: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock('../../../src/schemas/medicine-price.schema.js', () => {
|
||||
const chain = () => ({
|
||||
sort: vi.fn().mockReturnThis(),
|
||||
limit: vi.fn().mockReturnThis(),
|
||||
lean: vi.fn().mockReturnThis(),
|
||||
exec: mockFind,
|
||||
});
|
||||
const findOneChain = () => ({
|
||||
sort: vi.fn().mockReturnThis(),
|
||||
lean: vi.fn().mockReturnThis(),
|
||||
exec: mockFindOne,
|
||||
});
|
||||
const aggregateChain = () => ({ exec: mockAggregate });
|
||||
|
||||
class FakeModel {
|
||||
data: unknown;
|
||||
constructor(data: unknown) {
|
||||
this.data = data;
|
||||
}
|
||||
save = mockSave;
|
||||
toObject() {
|
||||
return this.data;
|
||||
}
|
||||
static find = vi.fn(() => chain());
|
||||
static findOne = vi.fn(() => findOneChain());
|
||||
static aggregate = vi.fn(() => aggregateChain());
|
||||
}
|
||||
return { MedicinePriceModel: FakeModel };
|
||||
});
|
||||
|
||||
import { MedicinePricesRepository } from '../../../src/modules/medicine-prices/medicine-prices.repository.js';
|
||||
|
||||
describe(MedicinePricesRepository.name, () => {
|
||||
let repo: MedicinePricesRepository;
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
repo = new MedicinePricesRepository();
|
||||
});
|
||||
|
||||
describe('create', () => {
|
||||
it('saves and returns price record', async () => {
|
||||
const data = {
|
||||
householdId: 'hh1',
|
||||
medicineProductId: 'mp-1',
|
||||
medicineProductBrand: 'Tylenol',
|
||||
medicineId: 'med-1',
|
||||
medicineName: 'Acetaminophen',
|
||||
storeId: 'st-1',
|
||||
storeName: 'Walgreens',
|
||||
price: 10,
|
||||
currency: 'USD',
|
||||
quantity: 100,
|
||||
unit: 'tablet',
|
||||
pricePerUnit: 0.1,
|
||||
date: new Date(),
|
||||
isInsurancePrice: false,
|
||||
createdBy: 'user-1',
|
||||
};
|
||||
mockSave.mockImplementation(function (this: { toObject: () => unknown }) {
|
||||
return Promise.resolve(this);
|
||||
});
|
||||
|
||||
const result = await repo.create(data as never);
|
||||
|
||||
expect(mockSave).toHaveBeenCalled();
|
||||
expect(result).toBeTruthy();
|
||||
});
|
||||
});
|
||||
|
||||
describe('findByMedicine', () => {
|
||||
it('returns paginated items', async () => {
|
||||
const items = [{ _id: 'pr-1', medicineId: 'med-1' }];
|
||||
mockFind.mockResolvedValue(items);
|
||||
|
||||
const result = await repo.findByMedicine('hh1', 'med-1', { limit: 20 });
|
||||
|
||||
expect(result.data).toEqual(items);
|
||||
expect(result.pagination.hasMore).toBe(false);
|
||||
expect(result.pagination.cursor).toBeNull();
|
||||
});
|
||||
|
||||
it('sets hasMore when more items exist', async () => {
|
||||
const items = [{ _id: 'pr-1' }, { _id: 'pr-2' }, { _id: 'pr-3' }];
|
||||
mockFind.mockResolvedValue(items);
|
||||
|
||||
const result = await repo.findByMedicine('hh1', 'med-1', { limit: 2 });
|
||||
|
||||
expect(result.data).toHaveLength(2);
|
||||
expect(result.pagination.hasMore).toBe(true);
|
||||
expect(result.pagination.cursor).toBeTruthy();
|
||||
});
|
||||
|
||||
it('handles cursor', async () => {
|
||||
mockFind.mockResolvedValue([]);
|
||||
|
||||
const cursor = Buffer.from('pr-1').toString('base64');
|
||||
const result = await repo.findByMedicine('hh1', 'med-1', { cursor, limit: 20 });
|
||||
|
||||
expect(result.pagination.hasMore).toBe(false);
|
||||
});
|
||||
|
||||
it('returns null cursor when no data', async () => {
|
||||
mockFind.mockResolvedValue([]);
|
||||
|
||||
const result = await repo.findByMedicine('hh1', 'med-1', { limit: 20 });
|
||||
|
||||
expect(result.pagination.cursor).toBeNull();
|
||||
});
|
||||
|
||||
it('applies storeId filter', async () => {
|
||||
mockFind.mockResolvedValue([]);
|
||||
|
||||
await repo.findByMedicine('hh1', 'med-1', { storeId: 'st-1', limit: 20 });
|
||||
|
||||
expect(mockFind).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('applies startDate-only filter', async () => {
|
||||
mockFind.mockResolvedValue([]);
|
||||
|
||||
await repo.findByMedicine('hh1', 'med-1', {
|
||||
startDate: '2026-01-01T00:00:00.000Z',
|
||||
limit: 20,
|
||||
});
|
||||
|
||||
expect(mockFind).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('applies endDate-only filter', async () => {
|
||||
mockFind.mockResolvedValue([]);
|
||||
|
||||
await repo.findByMedicine('hh1', 'med-1', {
|
||||
endDate: '2026-12-31T00:00:00.000Z',
|
||||
limit: 20,
|
||||
});
|
||||
|
||||
expect(mockFind).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('compareStores', () => {
|
||||
it('returns store comparison results', async () => {
|
||||
const rows = [
|
||||
{
|
||||
_id: 'st-1',
|
||||
storeName: 'Walgreens',
|
||||
latestPrice: 10,
|
||||
latestPricePerUnit: 0.1,
|
||||
currency: 'USD',
|
||||
date: new Date(),
|
||||
isInsurancePrice: false,
|
||||
},
|
||||
];
|
||||
mockAggregate.mockResolvedValue(rows);
|
||||
|
||||
const result = await repo.compareStores('hh1', 'med-1');
|
||||
|
||||
expect(result).toHaveLength(1);
|
||||
expect(result[0].storeId).toBe('st-1');
|
||||
expect(result[0].storeName).toBe('Walgreens');
|
||||
});
|
||||
|
||||
it('returns empty array when no records', async () => {
|
||||
mockAggregate.mockResolvedValue([]);
|
||||
|
||||
const result = await repo.compareStores('hh1', 'med-1');
|
||||
|
||||
expect(result).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getLatestForMedicine', () => {
|
||||
it('returns latest record', async () => {
|
||||
const record = { _id: 'pr-1', pricePerUnit: 0.1 };
|
||||
mockFindOne.mockResolvedValue(record);
|
||||
|
||||
const result = await repo.getLatestForMedicine('hh1', 'med-1');
|
||||
|
||||
expect(result).toEqual(record);
|
||||
});
|
||||
|
||||
it('filters by storeId when provided', async () => {
|
||||
mockFindOne.mockResolvedValue(null);
|
||||
|
||||
const result = await repo.getLatestForMedicine('hh1', 'med-1', 'st-1');
|
||||
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it('returns null when not found', async () => {
|
||||
mockFindOne.mockResolvedValue(null);
|
||||
|
||||
expect(await repo.getLatestForMedicine('hh1', 'med-1')).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('getAnalytics', () => {
|
||||
it('returns analytics object with all fields', async () => {
|
||||
mockAggregate.mockResolvedValue([]);
|
||||
|
||||
const result = await repo.getAnalytics('hh1', { period: 'month' });
|
||||
|
||||
expect(result).toHaveProperty('spendingOverTime');
|
||||
expect(result).toHaveProperty('topBySpending');
|
||||
expect(result).toHaveProperty('spendingByStore');
|
||||
expect(result).toHaveProperty('priceAlerts');
|
||||
});
|
||||
|
||||
it('uses quarter date format', async () => {
|
||||
mockAggregate.mockResolvedValue([]);
|
||||
|
||||
const result = await repo.getAnalytics('hh1', { period: 'quarter' });
|
||||
|
||||
expect(result).toHaveProperty('spendingOverTime');
|
||||
});
|
||||
|
||||
it('uses year date format', async () => {
|
||||
mockAggregate.mockResolvedValue([]);
|
||||
|
||||
const result = await repo.getAnalytics('hh1', { period: 'year' });
|
||||
|
||||
expect(result).toHaveProperty('spendingOverTime');
|
||||
});
|
||||
|
||||
it('handles non-empty analytics results', async () => {
|
||||
mockAggregate
|
||||
.mockResolvedValueOnce([{ period: '2026-01', total: 50 }])
|
||||
.mockResolvedValueOnce([
|
||||
{
|
||||
medicineId: 'med-1',
|
||||
medicineName: 'Acetaminophen',
|
||||
totalSpent: 50,
|
||||
avgPricePerUnit: 0.1,
|
||||
},
|
||||
])
|
||||
.mockResolvedValueOnce([
|
||||
{ storeId: 'st-1', storeName: 'Walgreens', totalSpent: 50, purchaseCount: 5 },
|
||||
])
|
||||
.mockResolvedValueOnce([]);
|
||||
|
||||
const result = await repo.getAnalytics('hh1', { period: 'month' });
|
||||
|
||||
expect(result.spendingOverTime).toHaveLength(1);
|
||||
expect(result.topBySpending).toHaveLength(1);
|
||||
expect(result.spendingByStore).toHaveLength(1);
|
||||
expect(result.priceAlerts).toHaveLength(0);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
@ -0,0 +1,340 @@
|
|||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
||||
import Fastify from 'fastify';
|
||||
import { fastifyAwilixPlugin } from '@fastify/awilix';
|
||||
import { serializerCompiler, validatorCompiler } from 'fastify-type-provider-zod';
|
||||
|
||||
vi.mock('jose', () => ({
|
||||
createRemoteJWKSet: vi.fn(() => 'mock-jwks'),
|
||||
jwtVerify: vi.fn().mockResolvedValue({
|
||||
payload: {
|
||||
sub: 'kc-1',
|
||||
email: 'test@example.com',
|
||||
preferred_username: 'testuser',
|
||||
realm_access: { roles: ['member'] },
|
||||
householdIds: ['hh1'],
|
||||
},
|
||||
protectedHeader: { alg: 'RS256' },
|
||||
key: {},
|
||||
}),
|
||||
}));
|
||||
|
||||
const { mockRecordPrice, mockGetPriceHistory, mockCompareStores, mockGetAnalytics } = vi.hoisted(
|
||||
() => ({
|
||||
mockRecordPrice: vi.fn(),
|
||||
mockGetPriceHistory: vi.fn(),
|
||||
mockCompareStores: vi.fn(),
|
||||
mockGetAnalytics: vi.fn(),
|
||||
}),
|
||||
);
|
||||
|
||||
vi.mock('../../../src/modules/medicine-prices/medicine-prices.repository.js', () => ({
|
||||
MedicinePricesRepository: class {
|
||||
create = vi.fn();
|
||||
findByMedicine = vi.fn();
|
||||
compareStores = vi.fn();
|
||||
getLatestForMedicine = vi.fn();
|
||||
getAnalytics = vi.fn();
|
||||
},
|
||||
}));
|
||||
|
||||
vi.mock('../../../src/modules/medicine-prices/medicine-prices.service.js', () => ({
|
||||
MedicinePricesService: class {
|
||||
recordPrice = mockRecordPrice;
|
||||
getPriceHistory = mockGetPriceHistory;
|
||||
compareStores = mockCompareStores;
|
||||
getAnalytics = mockGetAnalytics;
|
||||
},
|
||||
}));
|
||||
|
||||
vi.mock('../../../src/modules/medicine-products/medicine-products.repository.js', () => ({
|
||||
MedicineProductsRepository: class {
|
||||
findById = vi.fn();
|
||||
},
|
||||
}));
|
||||
|
||||
vi.mock('../../../src/modules/stores/stores.repository.js', () => ({
|
||||
StoresRepository: class {
|
||||
findById = vi.fn();
|
||||
},
|
||||
}));
|
||||
|
||||
vi.mock('../../../src/modules/users/users.repository.js', () => ({
|
||||
UsersRepository: class {
|
||||
findByKeycloakId = vi.fn().mockResolvedValue({ householdIds: ['hh1'] });
|
||||
upsertFromToken = vi.fn().mockResolvedValue({ householdIds: ['hh1'] });
|
||||
},
|
||||
}));
|
||||
|
||||
import authPlugin from '../../../src/plugins/auth.plugin.js';
|
||||
import householdPlugin from '../../../src/plugins/household.plugin.js';
|
||||
import usersRoutes from '../../../src/modules/users/users.routes.js';
|
||||
import medicinePricesRoutes from '../../../src/modules/medicine-prices/medicine-prices.routes.js';
|
||||
|
||||
function makeFakePriceRecord(overrides = {}) {
|
||||
return {
|
||||
_id: 'pr-1',
|
||||
householdId: 'hh1',
|
||||
medicineProductId: 'mp-1',
|
||||
medicineProductBrand: 'Tylenol',
|
||||
medicineId: 'med-1',
|
||||
medicineName: 'Acetaminophen',
|
||||
storeId: 'st-1',
|
||||
storeName: 'Walgreens',
|
||||
price: 10,
|
||||
currency: 'USD',
|
||||
quantity: 100,
|
||||
unit: 'tablet',
|
||||
pricePerUnit: 0.1,
|
||||
date: '2026-01-15T00:00:00.000Z',
|
||||
isInsurancePrice: false,
|
||||
createdBy: 'kc-1',
|
||||
createdAt: '2026-01-15T00:00:00.000Z',
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
describe('medicine-prices.routes', () => {
|
||||
let app: Awaited<ReturnType<typeof buildTestApp>>;
|
||||
|
||||
async function buildTestApp() {
|
||||
const instance = Fastify({ logger: false });
|
||||
instance.setValidatorCompiler(validatorCompiler);
|
||||
instance.setSerializerCompiler(serializerCompiler);
|
||||
await instance.register(fastifyAwilixPlugin, {
|
||||
disposeOnClose: true,
|
||||
disposeOnResponse: true,
|
||||
strictBooleanEnforced: true,
|
||||
});
|
||||
await instance.register(authPlugin);
|
||||
await instance.register(householdPlugin);
|
||||
await instance.register(usersRoutes);
|
||||
await instance.register(medicinePricesRoutes);
|
||||
await instance.ready();
|
||||
return instance;
|
||||
}
|
||||
|
||||
const authHeaders = { authorization: 'Bearer valid-token' };
|
||||
|
||||
beforeEach(async () => {
|
||||
vi.clearAllMocks();
|
||||
app = await buildTestApp();
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
if (app) await app.close();
|
||||
});
|
||||
|
||||
describe('POST /api/v1/households/:householdId/medicine-prices', () => {
|
||||
const validBody = {
|
||||
medicineProductId: 'mp-1',
|
||||
medicineId: 'med-1',
|
||||
storeId: 'st-1',
|
||||
price: 10,
|
||||
currency: 'USD',
|
||||
quantity: 100,
|
||||
unit: 'tablet',
|
||||
isInsurancePrice: false,
|
||||
};
|
||||
|
||||
it('records price and returns 201', async () => {
|
||||
mockRecordPrice.mockResolvedValue(makeFakePriceRecord());
|
||||
|
||||
const res = await app.inject({
|
||||
method: 'POST',
|
||||
url: '/api/v1/households/hh1/medicine-prices',
|
||||
headers: authHeaders,
|
||||
payload: validBody,
|
||||
});
|
||||
|
||||
expect(res.statusCode).toBe(201);
|
||||
const body = res.json();
|
||||
expect(body._id).toBe('pr-1');
|
||||
expect(body.pricePerUnit).toBe(0.1);
|
||||
});
|
||||
|
||||
it('passes householdId and userId to service', async () => {
|
||||
mockRecordPrice.mockResolvedValue(makeFakePriceRecord());
|
||||
|
||||
await app.inject({
|
||||
method: 'POST',
|
||||
url: '/api/v1/households/hh1/medicine-prices',
|
||||
headers: authHeaders,
|
||||
payload: validBody,
|
||||
});
|
||||
|
||||
expect(mockRecordPrice).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ medicineProductId: 'mp-1' }),
|
||||
'hh1',
|
||||
'kc-1',
|
||||
);
|
||||
});
|
||||
|
||||
it('includes notes in response when present', async () => {
|
||||
mockRecordPrice.mockResolvedValue(makeFakePriceRecord({ notes: 'insurance price' }));
|
||||
|
||||
const res = await app.inject({
|
||||
method: 'POST',
|
||||
url: '/api/v1/households/hh1/medicine-prices',
|
||||
headers: authHeaders,
|
||||
payload: validBody,
|
||||
});
|
||||
|
||||
expect(res.statusCode).toBe(201);
|
||||
expect(res.json().notes).toBe('insurance price');
|
||||
});
|
||||
|
||||
it('returns 400 for missing required fields', async () => {
|
||||
const res = await app.inject({
|
||||
method: 'POST',
|
||||
url: '/api/v1/households/hh1/medicine-prices',
|
||||
headers: authHeaders,
|
||||
payload: { price: 10 },
|
||||
});
|
||||
|
||||
expect(res.statusCode).toBe(400);
|
||||
});
|
||||
|
||||
it('handles Date objects in response', async () => {
|
||||
mockRecordPrice.mockResolvedValue(
|
||||
makeFakePriceRecord({
|
||||
_id: { toString: () => 'pr-obj' },
|
||||
date: new Date('2026-01-15T00:00:00.000Z'),
|
||||
createdAt: new Date('2026-01-15T00:00:00.000Z'),
|
||||
}),
|
||||
);
|
||||
|
||||
const res = await app.inject({
|
||||
method: 'POST',
|
||||
url: '/api/v1/households/hh1/medicine-prices',
|
||||
headers: authHeaders,
|
||||
payload: validBody,
|
||||
});
|
||||
|
||||
expect(res.statusCode).toBe(201);
|
||||
const body = res.json();
|
||||
expect(body._id).toBe('pr-obj');
|
||||
expect(body.date).toBe('2026-01-15T00:00:00.000Z');
|
||||
});
|
||||
});
|
||||
|
||||
describe('GET /api/v1/households/:householdId/medicine-prices/history/:medicineId', () => {
|
||||
it('returns paginated price history', async () => {
|
||||
mockGetPriceHistory.mockResolvedValue({
|
||||
data: [makeFakePriceRecord()],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
const res = await app.inject({
|
||||
method: 'GET',
|
||||
url: '/api/v1/households/hh1/medicine-prices/history/med-1',
|
||||
headers: authHeaders,
|
||||
});
|
||||
|
||||
expect(res.statusCode).toBe(200);
|
||||
const body = res.json();
|
||||
expect(body.data).toHaveLength(1);
|
||||
expect(body.pagination.hasMore).toBe(false);
|
||||
});
|
||||
|
||||
it('passes query params to service', async () => {
|
||||
mockGetPriceHistory.mockResolvedValue({
|
||||
data: [],
|
||||
pagination: { cursor: null, hasMore: false },
|
||||
});
|
||||
|
||||
await app.inject({
|
||||
method: 'GET',
|
||||
url: '/api/v1/households/hh1/medicine-prices/history/med-1?storeId=st-1&limit=10',
|
||||
headers: authHeaders,
|
||||
});
|
||||
|
||||
expect(mockGetPriceHistory).toHaveBeenCalledWith(
|
||||
'hh1',
|
||||
'med-1',
|
||||
expect.objectContaining({ storeId: 'st-1', limit: 10 }),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('GET /api/v1/households/:householdId/medicine-prices/compare/:medicineId', () => {
|
||||
it('returns store comparison', async () => {
|
||||
mockCompareStores.mockResolvedValue([
|
||||
{
|
||||
storeId: 'st-1',
|
||||
storeName: 'Walgreens',
|
||||
latestPrice: 10,
|
||||
latestPricePerUnit: 0.1,
|
||||
currency: 'USD',
|
||||
date: new Date('2026-01-15T00:00:00.000Z'),
|
||||
isInsurancePrice: false,
|
||||
},
|
||||
]);
|
||||
|
||||
const res = await app.inject({
|
||||
method: 'GET',
|
||||
url: '/api/v1/households/hh1/medicine-prices/compare/med-1',
|
||||
headers: authHeaders,
|
||||
});
|
||||
|
||||
expect(res.statusCode).toBe(200);
|
||||
const body = res.json();
|
||||
expect(body.data).toHaveLength(1);
|
||||
expect(body.data[0].storeId).toBe('st-1');
|
||||
expect(body.data[0].date).toBe('2026-01-15T00:00:00.000Z');
|
||||
});
|
||||
|
||||
it('passes householdId and medicineId to service', async () => {
|
||||
mockCompareStores.mockResolvedValue([]);
|
||||
|
||||
await app.inject({
|
||||
method: 'GET',
|
||||
url: '/api/v1/households/hh1/medicine-prices/compare/med-99',
|
||||
headers: authHeaders,
|
||||
});
|
||||
|
||||
expect(mockCompareStores).toHaveBeenCalledWith('hh1', 'med-99');
|
||||
});
|
||||
});
|
||||
|
||||
describe('GET /api/v1/households/:householdId/medicine-prices/analytics', () => {
|
||||
it('returns analytics', async () => {
|
||||
mockGetAnalytics.mockResolvedValue({
|
||||
spendingOverTime: [{ period: '2026-01', total: 50 }],
|
||||
topBySpending: [],
|
||||
spendingByStore: [],
|
||||
priceAlerts: [],
|
||||
});
|
||||
|
||||
const res = await app.inject({
|
||||
method: 'GET',
|
||||
url: '/api/v1/households/hh1/medicine-prices/analytics',
|
||||
headers: authHeaders,
|
||||
});
|
||||
|
||||
expect(res.statusCode).toBe(200);
|
||||
const body = res.json();
|
||||
expect(body.spendingOverTime).toHaveLength(1);
|
||||
expect(body.spendingOverTime[0].total).toBe(50);
|
||||
});
|
||||
|
||||
it('passes period query param to service', async () => {
|
||||
mockGetAnalytics.mockResolvedValue({
|
||||
spendingOverTime: [],
|
||||
topBySpending: [],
|
||||
spendingByStore: [],
|
||||
priceAlerts: [],
|
||||
});
|
||||
|
||||
await app.inject({
|
||||
method: 'GET',
|
||||
url: '/api/v1/households/hh1/medicine-prices/analytics?period=year',
|
||||
headers: authHeaders,
|
||||
});
|
||||
|
||||
expect(mockGetAnalytics).toHaveBeenCalledWith(
|
||||
'hh1',
|
||||
expect.objectContaining({ period: 'year' }),
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
@ -0,0 +1,181 @@
|
|||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { MedicinePricesService } from '../../../src/modules/medicine-prices/medicine-prices.service.js';
|
||||
|
||||
describe(MedicinePricesService.name, () => {
|
||||
const mockPricesRepo = {
|
||||
create: vi.fn(),
|
||||
findByMedicine: vi.fn(),
|
||||
compareStores: vi.fn(),
|
||||
getLatestForMedicine: vi.fn(),
|
||||
getAnalytics: vi.fn(),
|
||||
};
|
||||
const mockProductsRepo = {
|
||||
findById: vi.fn(),
|
||||
};
|
||||
const mockStoresRepo = {
|
||||
findById: vi.fn(),
|
||||
};
|
||||
|
||||
let service: MedicinePricesService;
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
service = new MedicinePricesService({
|
||||
medicinePricesRepository: mockPricesRepo as never,
|
||||
medicineProductsRepository: mockProductsRepo as never,
|
||||
storesRepository: mockStoresRepo as never,
|
||||
});
|
||||
});
|
||||
|
||||
describe('recordPrice', () => {
|
||||
const validInput = {
|
||||
medicineProductId: 'mp-1',
|
||||
medicineId: 'med-1',
|
||||
storeId: 'st-1',
|
||||
price: 10,
|
||||
currency: 'USD',
|
||||
quantity: 100,
|
||||
unit: 'tablet' as never,
|
||||
isInsurancePrice: false,
|
||||
};
|
||||
|
||||
it('creates price record with computed pricePerUnit', async () => {
|
||||
mockProductsRepo.findById.mockResolvedValue({
|
||||
medicineName: 'Acetaminophen',
|
||||
brand: 'Tylenol',
|
||||
});
|
||||
mockStoresRepo.findById.mockResolvedValue({ name: 'Walgreens' });
|
||||
const record = { _id: 'pr-1', pricePerUnit: 0.1 };
|
||||
mockPricesRepo.create.mockResolvedValue(record);
|
||||
|
||||
const result = await service.recordPrice(validInput, 'hh1', 'user-1');
|
||||
|
||||
expect(result).toEqual(record);
|
||||
expect(mockPricesRepo.create).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
pricePerUnit: 0.1,
|
||||
medicineName: 'Acetaminophen',
|
||||
storeName: 'Walgreens',
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it('uses medicineName as brand fallback when brand is not set', async () => {
|
||||
mockProductsRepo.findById.mockResolvedValue({ medicineName: 'Generic', brand: undefined });
|
||||
mockStoresRepo.findById.mockResolvedValue({ name: 'CVS' });
|
||||
mockPricesRepo.create.mockResolvedValue({ _id: 'pr-1' });
|
||||
|
||||
await service.recordPrice(validInput, 'hh1', 'user-1');
|
||||
|
||||
expect(mockPricesRepo.create).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ medicineProductBrand: 'Generic' }),
|
||||
);
|
||||
});
|
||||
|
||||
it('uses provided date when given', async () => {
|
||||
mockProductsRepo.findById.mockResolvedValue({
|
||||
medicineName: 'Acetaminophen',
|
||||
brand: 'Tylenol',
|
||||
});
|
||||
mockStoresRepo.findById.mockResolvedValue({ name: 'Walgreens' });
|
||||
mockPricesRepo.create.mockResolvedValue({ _id: 'pr-1' });
|
||||
|
||||
await service.recordPrice(
|
||||
{ ...validInput, date: '2026-01-15T00:00:00.000Z' },
|
||||
'hh1',
|
||||
'user-1',
|
||||
);
|
||||
|
||||
expect(mockPricesRepo.create).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ date: new Date('2026-01-15T00:00:00.000Z') }),
|
||||
);
|
||||
});
|
||||
|
||||
it('throws NotFoundError when product not found', async () => {
|
||||
mockProductsRepo.findById.mockResolvedValue(null);
|
||||
|
||||
await expect(service.recordPrice(validInput, 'hh1', 'user-1')).rejects.toThrow(
|
||||
'Medicine product not found',
|
||||
);
|
||||
});
|
||||
|
||||
it('throws NotFoundError when store not found', async () => {
|
||||
mockProductsRepo.findById.mockResolvedValue({
|
||||
medicineName: 'Acetaminophen',
|
||||
brand: 'Tylenol',
|
||||
});
|
||||
mockStoresRepo.findById.mockResolvedValue(null);
|
||||
|
||||
await expect(service.recordPrice(validInput, 'hh1', 'user-1')).rejects.toThrow(
|
||||
'Store not found',
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getPriceHistory', () => {
|
||||
it('delegates to repository', async () => {
|
||||
const result = { data: [], pagination: { cursor: null, hasMore: false } };
|
||||
mockPricesRepo.findByMedicine.mockResolvedValue(result);
|
||||
|
||||
const response = await service.getPriceHistory('hh1', 'med-1', { limit: 20 });
|
||||
|
||||
expect(response).toEqual(result);
|
||||
expect(mockPricesRepo.findByMedicine).toHaveBeenCalledWith('hh1', 'med-1', { limit: 20 });
|
||||
});
|
||||
});
|
||||
|
||||
describe('compareStores', () => {
|
||||
it('delegates to repository', async () => {
|
||||
const comparisons = [{ storeId: 'st-1', storeName: 'Walgreens', latestPrice: 10 }];
|
||||
mockPricesRepo.compareStores.mockResolvedValue(comparisons);
|
||||
|
||||
const result = await service.compareStores('hh1', 'med-1');
|
||||
|
||||
expect(result).toEqual(comparisons);
|
||||
expect(mockPricesRepo.compareStores).toHaveBeenCalledWith('hh1', 'med-1');
|
||||
});
|
||||
});
|
||||
|
||||
describe('estimatePrice', () => {
|
||||
it('returns pricePerUnit of latest record', async () => {
|
||||
mockPricesRepo.getLatestForMedicine.mockResolvedValue({ pricePerUnit: 0.15 });
|
||||
|
||||
const result = await service.estimatePrice('hh1', 'med-1');
|
||||
|
||||
expect(result).toBe(0.15);
|
||||
});
|
||||
|
||||
it('returns null when no records', async () => {
|
||||
mockPricesRepo.getLatestForMedicine.mockResolvedValue(null);
|
||||
|
||||
const result = await service.estimatePrice('hh1', 'med-1');
|
||||
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it('filters by storeId when provided', async () => {
|
||||
mockPricesRepo.getLatestForMedicine.mockResolvedValue({ pricePerUnit: 0.2 });
|
||||
|
||||
await service.estimatePrice('hh1', 'med-1', 'st-1');
|
||||
|
||||
expect(mockPricesRepo.getLatestForMedicine).toHaveBeenCalledWith('hh1', 'med-1', 'st-1');
|
||||
});
|
||||
});
|
||||
|
||||
describe('getAnalytics', () => {
|
||||
it('delegates to repository', async () => {
|
||||
const analytics = {
|
||||
spendingOverTime: [],
|
||||
topBySpending: [],
|
||||
spendingByStore: [],
|
||||
priceAlerts: [],
|
||||
};
|
||||
mockPricesRepo.getAnalytics.mockResolvedValue(analytics);
|
||||
|
||||
const result = await service.getAnalytics('hh1', { period: 'month' });
|
||||
|
||||
expect(result).toEqual(analytics);
|
||||
expect(mockPricesRepo.getAnalytics).toHaveBeenCalledWith('hh1', { period: 'month' });
|
||||
});
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue