MeshiTrack/packages/shared/src/validation/refill.schemas.test.ts

113 lines
3.3 KiB
TypeScript
Raw Normal View History

import { describe, it, expect } from 'vitest';
import { DosageUnit } from '../enums/medicine.enums.js';
import { RefillListStatus } from '../enums/refill.enums.js';
import {
CreateRefillListSchema,
UpdateRefillListSchema,
UpdateRefillListItemSchema,
RefillListQuerySchema,
RefillAlertQuerySchema,
} from './refill.schemas.js';
describe('CreateRefillListSchema', () => {
it('accepts minimal valid input', () => {
const result = CreateRefillListSchema.parse({ name: 'Weekly Refills' });
expect(result.fromAlerts).toBe(false);
expect(result.thresholdDays).toBe(7);
});
it('accepts fromAlerts with threshold', () => {
const result = CreateRefillListSchema.parse({
name: 'Auto List',
fromAlerts: true,
thresholdDays: 14,
});
expect(result.fromAlerts).toBe(true);
expect(result.thresholdDays).toBe(14);
});
it('accepts items array', () => {
const result = CreateRefillListSchema.safeParse({
name: 'Manual List',
items: [
{
medicineId: 'med-1',
medicineName: 'Metformin',
quantity: 30,
unit: DosageUnit.TABLET,
},
],
});
expect(result.success).toBe(true);
});
it('rejects empty name', () => {
expect(CreateRefillListSchema.safeParse({ name: '' }).success).toBe(false);
});
it('rejects item with non-positive quantity', () => {
expect(
CreateRefillListSchema.safeParse({
name: 'List',
items: [{ medicineId: 'med-1', medicineName: 'X', quantity: 0, unit: DosageUnit.TABLET }],
}).success,
).toBe(false);
});
it('trims name whitespace', () => {
const result = CreateRefillListSchema.parse({ name: ' My List ' });
expect(result.name).toBe('My List');
});
});
describe('UpdateRefillListSchema', () => {
it('accepts empty object', () => {
expect(UpdateRefillListSchema.safeParse({}).success).toBe(true);
});
it('accepts status update', () => {
const result = UpdateRefillListSchema.parse({ status: RefillListStatus.SHOPPING });
expect(result.status).toBe(RefillListStatus.SHOPPING);
});
it('rejects invalid status', () => {
expect(UpdateRefillListSchema.safeParse({ status: 'invalid' }).success).toBe(false);
});
});
describe('UpdateRefillListItemSchema', () => {
it('accepts checked=true', () => {
expect(UpdateRefillListItemSchema.safeParse({ checked: true }).success).toBe(true);
});
it('accepts actualPrice', () => {
const result = UpdateRefillListItemSchema.parse({ actualPrice: 12.50 });
expect(result.actualPrice).toBe(12.50);
});
it('rejects negative actualPrice', () => {
expect(UpdateRefillListItemSchema.safeParse({ actualPrice: -1 }).success).toBe(false);
});
});
describe('RefillListQuerySchema', () => {
it('applies default limit', () => {
expect(RefillListQuerySchema.parse({}).limit).toBe(20);
});
it('accepts status filter', () => {
const result = RefillListQuerySchema.parse({ status: RefillListStatus.ACTIVE });
expect(result.status).toBe(RefillListStatus.ACTIVE);
});
});
describe('RefillAlertQuerySchema', () => {
it('defaults thresholdDays to 7', () => {
expect(RefillAlertQuerySchema.parse({}).thresholdDays).toBe(7);
});
it('coerces thresholdDays from string', () => {
expect(RefillAlertQuerySchema.parse({ thresholdDays: '14' }).thresholdDays).toBe(14);
});
});