50 lines
1.8 KiB
TypeScript
50 lines
1.8 KiB
TypeScript
import { describe, expect, it } from 'vitest';
|
|
import { z } from 'zod';
|
|
|
|
import {
|
|
GetFeedDetailSchema,
|
|
ListFeedsSchema,
|
|
PostCommentSchema,
|
|
ReplyCommentSchema,
|
|
SearchSchema,
|
|
SetFavoriteStateSchema,
|
|
SetLikeStateSchema,
|
|
} from '../src/platforms/xiaoheihe/schemas.js';
|
|
|
|
describe('xhh schemas', () => {
|
|
it('validates list/query boundaries', () => {
|
|
const schema = z.object(ListFeedsSchema);
|
|
expect(schema.parse({ max_count: 20 }).max_count).toBe(20);
|
|
expect(() => schema.parse({ max_count: 0 })).toThrow();
|
|
expect(() => schema.parse({ max_count: 201 })).toThrow();
|
|
});
|
|
|
|
it('validates search required keyword', () => {
|
|
const schema = z.object(SearchSchema);
|
|
expect(() => schema.parse({})).toThrow();
|
|
expect(schema.parse({ keyword: 'aaa' }).keyword).toBe('aaa');
|
|
});
|
|
|
|
it('allows feed detail by link_id or url', () => {
|
|
const schema = z.object(GetFeedDetailSchema);
|
|
expect(schema.parse({ link_id: '123' }).link_id).toBe('123');
|
|
expect(schema.parse({ url: 'https://www.xiaoheihe.cn/app/bbs/link/123' }).url).toContain('/app/bbs/link/');
|
|
});
|
|
|
|
it('validates comment payloads', () => {
|
|
const postSchema = z.object(PostCommentSchema);
|
|
const replySchema = z.object(ReplyCommentSchema);
|
|
expect(postSchema.parse({ link_id: '1', content: 'hi' }).content).toBe('hi');
|
|
expect(replySchema.parse({ link_id: '1', comment_id: '2', content: 'ok' }).comment_id).toBe('2');
|
|
expect(() => postSchema.parse({ link_id: '1', content: '' })).toThrow();
|
|
});
|
|
|
|
it('validates set-state tools', () => {
|
|
const likeSchema = z.object(SetLikeStateSchema);
|
|
const favSchema = z.object(SetFavoriteStateSchema);
|
|
expect(likeSchema.parse({ link_id: '1', liked: true }).liked).toBe(true);
|
|
expect(favSchema.parse({ link_id: '1', favorited: false }).favorited).toBe(false);
|
|
});
|
|
});
|
|
|