Some checks failed
Deploy to Test Environment / deploy-to-test (push) Failing after 58s
169 lines
5.7 KiB
TypeScript
169 lines
5.7 KiB
TypeScript
// src/schemas/flyer.schemas.test.ts
|
|
import { describe, it, expect } from 'vitest';
|
|
import { flyerInsertSchema, flyerDbInsertSchema } from './flyer.schemas';
|
|
|
|
describe('flyerInsertSchema', () => {
|
|
const validFlyer = {
|
|
file_name: 'flyer.jpg',
|
|
image_url: 'https://example.com/flyer.jpg',
|
|
icon_url: 'https://example.com/icon.jpg',
|
|
checksum: 'a'.repeat(64),
|
|
store_name: 'Test Store',
|
|
valid_from: '2023-01-01T00:00:00Z',
|
|
valid_to: '2023-01-07T00:00:00Z',
|
|
store_address: '123 Main St',
|
|
status: 'processed',
|
|
item_count: 10,
|
|
uploaded_by: '123e4567-e89b-12d3-a456-426614174000',
|
|
};
|
|
|
|
it('should validate a correct flyer object', () => {
|
|
const result = flyerInsertSchema.safeParse(validFlyer);
|
|
expect(result.success).toBe(true);
|
|
});
|
|
|
|
it('should fail if file_name is missing or empty', () => {
|
|
const invalid = { ...validFlyer, file_name: '' };
|
|
const result = flyerInsertSchema.safeParse(invalid);
|
|
expect(result.success).toBe(false);
|
|
if (!result.success) {
|
|
expect(result.error.issues[0].message).toBe('File name is required');
|
|
}
|
|
});
|
|
|
|
it('should fail if image_url is invalid', () => {
|
|
const invalid = { ...validFlyer, image_url: 'ftp://invalid.com' };
|
|
const result = flyerInsertSchema.safeParse(invalid);
|
|
expect(result.success).toBe(false);
|
|
if (!result.success) {
|
|
expect(result.error.issues[0].message).toBe(
|
|
'Flyer image URL must be a valid HTTP or HTTPS URL',
|
|
);
|
|
}
|
|
});
|
|
|
|
it('should fail if icon_url is invalid', () => {
|
|
const invalid = { ...validFlyer, icon_url: 'not-a-url' };
|
|
const result = flyerInsertSchema.safeParse(invalid);
|
|
expect(result.success).toBe(false);
|
|
});
|
|
|
|
it('should fail if checksum length is incorrect', () => {
|
|
const invalid = { ...validFlyer, checksum: 'abc' };
|
|
const result = flyerInsertSchema.safeParse(invalid);
|
|
expect(result.success).toBe(false);
|
|
if (!result.success) {
|
|
expect(result.error.issues[0].message).toBe('Checksum must be 64 characters');
|
|
}
|
|
});
|
|
|
|
it('should fail if checksum is not hex', () => {
|
|
const invalid = { ...validFlyer, checksum: 'z'.repeat(64) };
|
|
const result = flyerInsertSchema.safeParse(invalid);
|
|
expect(result.success).toBe(false);
|
|
if (!result.success) {
|
|
expect(result.error.issues[0].message).toBe('Checksum must be a valid hexadecimal string');
|
|
}
|
|
});
|
|
|
|
it('should allow null checksum', () => {
|
|
const valid = { ...validFlyer, checksum: null };
|
|
const result = flyerInsertSchema.safeParse(valid);
|
|
expect(result.success).toBe(true);
|
|
});
|
|
|
|
it('should fail if store_name is missing', () => {
|
|
const invalid = { ...validFlyer, store_name: '' };
|
|
const result = flyerInsertSchema.safeParse(invalid);
|
|
expect(result.success).toBe(false);
|
|
});
|
|
|
|
it('should validate valid_from and valid_to as datetimes', () => {
|
|
const invalid = { ...validFlyer, valid_from: 'not-a-date' };
|
|
const result = flyerInsertSchema.safeParse(invalid);
|
|
expect(result.success).toBe(false);
|
|
});
|
|
|
|
it('should allow null valid_from, valid_to, store_address', () => {
|
|
const valid = {
|
|
...validFlyer,
|
|
valid_from: null,
|
|
valid_to: null,
|
|
store_address: null,
|
|
};
|
|
const result = flyerInsertSchema.safeParse(valid);
|
|
expect(result.success).toBe(true);
|
|
});
|
|
|
|
it('should validate status enum', () => {
|
|
const invalid = { ...validFlyer, status: 'invalid_status' };
|
|
const result = flyerInsertSchema.safeParse(invalid);
|
|
expect(result.success).toBe(false);
|
|
});
|
|
|
|
it('should fail if item_count is negative', () => {
|
|
const invalid = { ...validFlyer, item_count: -1 };
|
|
const result = flyerInsertSchema.safeParse(invalid);
|
|
expect(result.success).toBe(false);
|
|
if (!result.success) {
|
|
expect(result.error.issues[0].message).toBe('Item count must be non-negative');
|
|
}
|
|
});
|
|
|
|
it('should validate uploaded_by as UUID if present', () => {
|
|
const invalid = { ...validFlyer, uploaded_by: 'not-a-uuid' };
|
|
const result = flyerInsertSchema.safeParse(invalid);
|
|
expect(result.success).toBe(false);
|
|
});
|
|
|
|
it('should allow null or undefined uploaded_by', () => {
|
|
const validNull = { ...validFlyer, uploaded_by: null };
|
|
expect(flyerInsertSchema.safeParse(validNull).success).toBe(true);
|
|
|
|
const validUndefined = { ...validFlyer, uploaded_by: undefined };
|
|
expect(flyerInsertSchema.safeParse(validUndefined).success).toBe(true);
|
|
});
|
|
});
|
|
|
|
describe('flyerDbInsertSchema', () => {
|
|
const validDbFlyer = {
|
|
file_name: 'flyer.jpg',
|
|
image_url: 'https://example.com/flyer.jpg',
|
|
icon_url: 'https://example.com/icon.jpg',
|
|
checksum: 'a'.repeat(64),
|
|
store_id: 1,
|
|
valid_from: '2023-01-01T00:00:00Z',
|
|
valid_to: '2023-01-07T00:00:00Z',
|
|
store_address: '123 Main St',
|
|
status: 'processed',
|
|
item_count: 10,
|
|
uploaded_by: '123e4567-e89b-12d3-a456-426614174000',
|
|
};
|
|
|
|
it('should validate a correct DB flyer object', () => {
|
|
const result = flyerDbInsertSchema.safeParse(validDbFlyer);
|
|
expect(result.success).toBe(true);
|
|
});
|
|
|
|
it('should fail if store_id is missing', () => {
|
|
const { store_id: _store_id, ...invalid } = validDbFlyer;
|
|
const result = flyerDbInsertSchema.safeParse(invalid);
|
|
expect(result.success).toBe(false);
|
|
});
|
|
|
|
it('should fail if store_id is not positive', () => {
|
|
const invalid = { ...validDbFlyer, store_id: 0 };
|
|
const result = flyerDbInsertSchema.safeParse(invalid);
|
|
expect(result.success).toBe(false);
|
|
if (!result.success) {
|
|
expect(result.error.issues[0].message).toBe('Store ID must be a positive integer');
|
|
}
|
|
});
|
|
|
|
it('should fail if store_id is not an integer', () => {
|
|
const invalid = { ...validDbFlyer, store_id: 1.5 };
|
|
const result = flyerDbInsertSchema.safeParse(invalid);
|
|
expect(result.success).toBe(false);
|
|
});
|
|
});
|