feat: 新增 news 内容集合与 Zod schema(含单测)

This commit is contained in:
lzy
2026-07-23 19:14:38 +08:00
parent 3ecac5b757
commit eab95953bf
6 changed files with 402 additions and 1250 deletions
+350 -1250
View File
File diff suppressed because it is too large Load Diff
+1 -1
View File
@@ -14,6 +14,6 @@
"zod": "^3.25.76" "zod": "^3.25.76"
}, },
"devDependencies": { "devDependencies": {
"vitest": "^4.1.10" "vitest": "^3.2.7"
} }
} }
+10
View File
@@ -0,0 +1,10 @@
import { defineCollection } from 'astro:content';
import { glob } from 'astro/loaders';
import { newsSchema } from './content/schemas';
const news = defineCollection({
loader: glob({ pattern: '**/*.md', base: './src/content/news' }),
schema: newsSchema,
});
export const collections = { news };
+7
View File
@@ -0,0 +1,7 @@
---
title: 官网基座启动
date: 2026-07-23
category: 公告
---
这是 Phase 0 技术验证的第一条新闻。内容编辑链路跑通后,客户可在 Sveltia CMS 自助新增。
+7
View File
@@ -0,0 +1,7 @@
import { z } from 'zod';
export const newsSchema = z.object({
title: z.string(),
date: z.coerce.date(),
category: z.string().default('未分类'),
});
+28
View File
@@ -0,0 +1,28 @@
import { describe, it, expect } from 'vitest';
import { newsSchema } from '../src/content/schemas';
describe('newsSchema', () => {
it('accepts valid frontmatter and coerces date', () => {
const parsed = newsSchema.parse({
title: '官网基座启动',
date: '2026-07-23',
category: '公告',
});
expect(parsed.title).toBe('官网基座启动');
expect(parsed.date).toEqual(new Date('2026-07-23'));
expect(parsed.category).toBe('公告');
});
it('defaults category to 未分类', () => {
const parsed = newsSchema.parse({ title: '测试', date: '2026-07-23' });
expect(parsed.category).toBe('未分类');
});
it('rejects missing title', () => {
expect(() => newsSchema.parse({ date: '2026-07-23' })).toThrow();
});
it('rejects missing date', () => {
expect(() => newsSchema.parse({ title: '测试' })).toThrow();
});
});