test: add AIEvent validation tests

This commit is contained in:
2026-01-27 21:34:35 +08:00
parent 65b84e641d
commit a6d97e58e2
+64
View File
@@ -0,0 +1,64 @@
import { describe, it, expect } from 'vitest';
import { AIEventInputSchema } from './validations';
describe('AIEventInputSchema', () => {
const validEvent = {
title: 'GPT-4 发布',
eventDate: '2023-03-14T00:00:00Z',
description: 'OpenAI 发布多模态大语言模型',
imageUrl: 'https://example.com/gpt4.jpg',
};
it('should validate valid event', () => {
expect(() => AIEventInputSchema.parse(validEvent)).not.toThrow();
});
it('should accept event with optional English fields', () => {
const eventWithEn = {
...validEvent,
titleEn: 'GPT-4 Release',
descriptionEn: 'OpenAI launches multimodal LLM',
sourceUrl: 'https://openai.com/blog/gpt-4',
};
expect(() => AIEventInputSchema.parse(eventWithEn)).not.toThrow();
});
it('should reject empty title', () => {
expect(() => AIEventInputSchema.parse({ ...validEvent, title: '' }))
.toThrow();
});
it('should reject title exceeding 200 characters', () => {
const longTitle = 'A'.repeat(201);
expect(() => AIEventInputSchema.parse({ ...validEvent, title: longTitle }))
.toThrow();
});
it('should reject description shorter than 10 characters', () => {
expect(() => AIEventInputSchema.parse({ ...validEvent, description: '太短' }))
.toThrow();
});
it('should reject description exceeding 500 characters', () => {
const longDesc = 'A'.repeat(501);
expect(() => AIEventInputSchema.parse({ ...validEvent, description: longDesc }))
.toThrow();
});
it('should reject invalid eventDate format', () => {
expect(() => AIEventInputSchema.parse({ ...validEvent, eventDate: '2023-03-14' }))
.toThrow();
});
it('should reject invalid imageUrl', () => {
expect(() => AIEventInputSchema.parse({ ...validEvent, imageUrl: 'not-a-url' }))
.toThrow();
});
it('should reject invalid sourceUrl format', () => {
expect(() => AIEventInputSchema.parse({
...validEvent,
sourceUrl: 'not-a-url'
})).toThrow();
});
});