From a6d97e58e29463714a7f3aef86f45bfc03eb8c3c Mon Sep 17 00:00:00 2001 From: mzaxd Date: Tue, 27 Jan 2026 21:34:35 +0800 Subject: [PATCH] test: add AIEvent validation tests --- src/lib/validations.test.ts | 64 +++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 src/lib/validations.test.ts diff --git a/src/lib/validations.test.ts b/src/lib/validations.test.ts new file mode 100644 index 0000000..78f7607 --- /dev/null +++ b/src/lib/validations.test.ts @@ -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(); + }); +});