import { describe, it, expect, beforeAll, afterAll, vi } from 'vitest' // Il modulo db.js apre il DB a livello di modulo (top-level). // Per isolarlo usiamo un DB in memoria: vi.stubEnv + vi.resetModules + import dinamico. let salvaPartita, getPartite, getPartita beforeAll(async () => { vi.stubEnv('DB_PATH', ':memory:') vi.resetModules() const mod = await import('../../src/db.js') salvaPartita = mod.salvaPartita getPartite = mod.getPartite getPartita = mod.getPartita }) afterAll(() => { vi.unstubAllEnvs() }) // Stato minimo valido da passare a salvaPartita function makeState({ vincitore = 'home', setHome = 3, setGuest = 1, strisce = [] } = {}) { return { modalitaPartita: '3/5', sp: { nomi: { home: 'Antoniana', guest: 'Ospiti' }, set: { home: setHome, guest: setGuest }, partitaFinita: vincitore ? { vincitore } : null, strisce, }, } } describe('db.js', () => { // ============================================= // salvaPartita // ============================================= describe('salvaPartita', () => { it('ritorna un ID numerico intero positivo', () => { const id = salvaPartita(makeState()) expect(typeof id).toBe('number') expect(id).toBeGreaterThan(0) expect(Number.isInteger(id)).toBe(true) }) it('IDs sono incrementali su inserimenti multipli', () => { const id1 = salvaPartita(makeState()) const id2 = salvaPartita(makeState()) expect(id2).toBeGreaterThan(id1) }) it('il record ha i campi corretti: modalita, nomi, set, vincitore', () => { const state = makeState({ vincitore: 'guest', setHome: 1, setGuest: 3 }) const id = salvaPartita(state) const row = getPartita(Number(id)) expect(row.modalita).toBe('3/5') expect(row.nome_home).toBe('Antoniana') expect(row.nome_guest).toBe('Ospiti') expect(row.set_home).toBe(1) expect(row.set_guest).toBe(3) expect(row.vincitore).toBe('guest') }) it('il campo json è una stringa JSON parsabile', () => { const id = salvaPartita(makeState()) const row = getPartita(Number(id)) expect(() => JSON.parse(row.json)).not.toThrow() }) it('il JSON contiene nomi, set, strisce, vincitore e data', () => { const strisce = [{ set: 1, vincitore: 'home', punt: { home: 25, guest: 20 } }] const state = makeState({ strisce }) const id = salvaPartita(state) const row = getPartita(Number(id)) const json = JSON.parse(row.json) expect(json.nomi).toEqual({ home: 'Antoniana', guest: 'Ospiti' }) expect(json.set).toEqual({ home: 3, guest: 1 }) expect(json.vincitore).toBe('home') expect(json.strisce).toEqual(strisce) expect(typeof json.data).toBe('string') expect(() => new Date(json.data)).not.toThrow() }) it('vincitore nel DB è null se partitaFinita è null', () => { const state = makeState({ vincitore: null }) const id = salvaPartita(state) const row = getPartita(Number(id)) expect(row.vincitore).toBeNull() }) }) // ============================================= // getPartite // ============================================= describe('getPartite', () => { it('ritorna tutte le partite inserite', () => { const prima = getPartite().length salvaPartita(makeState()) salvaPartita(makeState()) const dopo = getPartite() expect(dopo.length).toBe(prima + 2) }) it('ordina per ID discendente (più recente prima)', () => { const id1 = Number(salvaPartita(makeState())) const id2 = Number(salvaPartita(makeState())) const partite = getPartite() const ids = partite.map(p => p.id) const idx1 = ids.indexOf(id1) const idx2 = ids.indexOf(id2) // id2 (inserito dopo) deve apparire prima (indice minore) expect(idx2).toBeLessThan(idx1) }) }) // ============================================= // getPartita // ============================================= describe('getPartita', () => { it('ritorna undefined per ID inesistente', () => { const result = getPartita(999999) expect(result).toBeUndefined() }) it('ritorna il record corretto per ID valido', () => { const id = Number(salvaPartita(makeState({ vincitore: 'home', setHome: 3, setGuest: 0 }))) const row = getPartita(id) expect(row).toBeDefined() expect(row.id).toBe(id) expect(row.set_home).toBe(3) expect(row.set_guest).toBe(0) }) it('il record ha tutti i campi attesi', () => { const id = Number(salvaPartita(makeState())) const row = getPartita(id) const campi = ['id', 'data', 'modalita', 'nome_home', 'nome_guest', 'set_home', 'set_guest', 'vincitore', 'json'] for (const campo of campi) { expect(row).toHaveProperty(campo) } }) }) })