feat: layer sqlite e repository posts con test

This commit is contained in:
2026-07-02 00:33:23 +02:00
parent 42d603b55b
commit d3b5e67132
3 changed files with 195 additions and 0 deletions
+74
View File
@@ -0,0 +1,74 @@
import { describe, it, expect, beforeEach } from 'vitest';
import type Database from 'better-sqlite3';
import { createDb } from '../src/lib/db';
import {
createPost, updatePost, deletePost, getPostById, getPublishedBySlug,
listPublished, listAllPosts, listRecent, listArchiveMonths, type PostInput,
} from '../src/lib/posts';
let db: Database.Database;
beforeEach(() => { db = createDb(':memory:'); });
const base: PostInput = {
title: 'Titolo', slug: 'titolo', category: 'Contenuti educativi',
excerpt: 'estratto', body_html: '<p>corpo</p>', cover: null, draft: false,
};
describe('posts repository', () => {
it('crea e rilegge un post pubblicato', () => {
const id = createPost(db, base);
const post = getPostById(db, id)!;
expect(post.title).toBe('Titolo');
expect(post.draft).toBe(0);
expect(post.published_at).not.toBeNull();
});
it('bozza non ha published_at e non appare nelle liste pubbliche', () => {
const id = createPost(db, { ...base, slug: 'bozza', draft: true });
expect(getPostById(db, id)!.published_at).toBeNull();
expect(getPublishedBySlug(db, 'bozza')).toBeUndefined();
expect(listPublished(db).total).toBe(0);
expect(listAllPosts(db)).toHaveLength(1);
});
it('slug duplicato lancia', () => {
createPost(db, base);
expect(() => createPost(db, base)).toThrow();
});
it('update imposta published_at alla prima pubblicazione e lo conserva', () => {
const id = createPost(db, { ...base, draft: true });
updatePost(db, id, { ...base, draft: false });
const first = getPostById(db, id)!.published_at;
expect(first).not.toBeNull();
updatePost(db, id, { ...base, title: 'Nuovo', draft: false });
expect(getPostById(db, id)!.published_at).toBe(first);
expect(getPostById(db, id)!.title).toBe('Nuovo');
});
it('listPublished filtra per categoria e pagina', () => {
for (let i = 0; i < 12; i++) {
createPost(db, { ...base, slug: `post-${i}`, category: i % 2 ? 'Contenuti educativi' : 'Eventi & Presidi' });
}
const all = listPublished(db, { page: 1, perPage: 9 });
expect(all.total).toBe(12);
expect(all.items).toHaveLength(9);
const edu = listPublished(db, { category: 'Contenuti educativi' });
expect(edu.total).toBe(6);
});
it('delete rimuove', () => {
const id = createPost(db, base);
deletePost(db, id);
expect(getPostById(db, id)).toBeUndefined();
});
it('listRecent e archivio mesi', () => {
createPost(db, base);
expect(listRecent(db, 4)).toHaveLength(1);
const months = listArchiveMonths(db);
expect(months).toHaveLength(1);
expect(months[0].count).toBe(1);
expect(months[0].month).toMatch(/^\d{4}-\d{2}$/);
});
});