feat(db): migrazioni ruoli editor->superuser, guid contenuti, posts.author_id

This commit is contained in:
2026-07-05 21:40:28 +02:00
parent d4cf359805
commit 5cea895d51
3 changed files with 57 additions and 4 deletions
+29
View File
@@ -3,6 +3,7 @@ import { mkdtempSync, rmSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { createDb } from '../src/lib/db';
import { createUser } from '../src/lib/auth';
describe('schema content_blocks e ruoli', () => {
it('crea la tabella content_blocks', () => {
@@ -89,3 +90,31 @@ describe('schema content_blocks e ruoli', () => {
expect(db.prepare(`PRAGMA table_info(users)`).all().length).toBeGreaterThanOrEqual(4);
});
});
describe('migrazioni ruoli/guid/autore', () => {
it('ogni content_block ha un guid non vuoto e univoco', () => {
const db = createDb(':memory:');
const rows = db.prepare('SELECT guid FROM content_blocks').all() as { guid: string | null }[];
expect(rows.length).toBeGreaterThan(100);
expect(rows.every((r) => typeof r.guid === 'string' && r.guid!.length >= 10)).toBe(true);
const guids = new Set(rows.map((r) => r.guid));
expect(guids.size).toBe(rows.length);
});
it('lo statement di migrazione unifica editor in superuser', () => {
// La migrazione gira dentro createDb, ma :memory: non persiste tra due boot: qui
// verifichiamo l'invariante SQL applicata dalla migrazione (editor → superuser).
// L'end-to-end su DB pre-esistente è coperto dallo smoke di produzione (Task 15).
const db = createDb(':memory:');
db.prepare("INSERT INTO users (username, password_hash, role) VALUES ('vecchio', 'x', 'editor')").run();
db.prepare("UPDATE users SET role = 'superuser' WHERE role = 'editor'").run();
const row = db.prepare("SELECT role FROM users WHERE username = 'vecchio'").get() as { role: string };
expect(row.role).toBe('superuser');
});
it('posts ha la colonna author_id', () => {
const db = createDb(':memory:');
const cols = db.prepare('PRAGMA table_info(posts)').all() as { name: string }[];
expect(cols.some((c) => c.name === 'author_id')).toBe(true);
});
});