import { describe, it, expect } from 'vitest'; import { server } from '../msw/server'; import { http, HttpResponse } from 'msw'; import { fetchAndEvaluatePosts } from '@/utils/fetchPosts'; const validPost = (overrides?: Partial) => ({ title: 'Hello', createdAt: 123, postContent: [ { type: 'image', version: 1, id: 'img1', content: { image: 'img-data' } }, { type: 'editor', version: 1, id: 'ed1', content: [{ text: 'Some text' }] }, ], ...overrides, }); describe('fetchAndEvaluatePosts', () => { it('returns isValid=false when structure invalid', async () => { server.use(http.get('/arbitrary/BLOG_POST/:user/:postId', () => HttpResponse.json({}))); const res = await fetchAndEvaluatePosts({ user: 'u', postId: 'p', content: {} }); expect(res.isValid).toBe(false); }); it('maps fields and extracts description when editor present', async () => { server.use( http.get('/arbitrary/BLOG_POST/:user/:postId', () => HttpResponse.json(validPost())), ); const content = { description: '' }; const res = await fetchAndEvaluatePosts({ user: 'alice', postId: 'post1', content }); expect(res.isValid).toBe(true); expect(res.user).toBe('alice'); expect(res.id).toBe('post1'); expect(res.title).toBe('Hello'); expect(res.postImage).toBe('img-data'); expect(res.description).toBe('Some text'); }); it('keeps existing description when provided', async () => { server.use( http.get('/arbitrary/BLOG_POST/:user/:postId', () => HttpResponse.json(validPost())), ); const content = { description: 'keep me' }; const res = await fetchAndEvaluatePosts({ user: 'alice', postId: 'post1', content }); expect(res.description).toBe('keep me'); }); });