diff --git a/src/lib/longevity/db.ts b/src/lib/longevity/db.ts index 21a0166..bf66c34 100644 --- a/src/lib/longevity/db.ts +++ b/src/lib/longevity/db.ts @@ -84,7 +84,15 @@ CREATE TABLE IF NOT EXISTS score ( stato TEXT NOT NULL CHECK (stato IN ('ok','insufficiente')), quest_version TEXT, model_version TEXT NOT NULL, - calcolato_at TEXT NOT NULL DEFAULT (datetime('now')) + calcolato_at TEXT NOT NULL DEFAULT (datetime('now')), + -- Il tipo Punteggio protegge il codice ma non la tabella: l'interfaccia leggera' + -- 'score' direttamente, dove quel tipo non c'e' piu'. Questo vincolo lo riscrive + -- a livello di dato: o lo stato e' 'ok' e il valore c'e', o e' 'insufficiente' e + -- il valore e' NULL, mai un numero scritto sotto uno stato che lo nega. + -- SQLite non permette di aggiungere un CHECK con ALTER TABLE a una tabella gia' + -- creata: un database longevity.db esistente, creato prima di questa modifica, + -- NON avra' questo vincolo finche' non viene ricreato. + CHECK ((stato = 'ok' AND valore IS NOT NULL) OR (stato = 'insufficiente' AND valore IS NULL)) ); CREATE INDEX IF NOT EXISTS idx_score_cliente ON score (client_code, calcolato_at DESC); `; diff --git a/tests/longevity/db.test.ts b/tests/longevity/db.test.ts index 959adb2..dcd5d33 100644 --- a/tests/longevity/db.test.ts +++ b/tests/longevity/db.test.ts @@ -43,6 +43,33 @@ describe('schema longevity', () => { expect(row.stato).toBe('insufficiente'); }); + it('la tabella rifiuta uno score insufficiente CON un valore scritto', () => { + // Il tipo Punteggio protegge il codice, ma l'interfaccia legge dalla tabella, + // dove il tipo non c'e' piu': senza questo vincolo si potrebbe scrivere un + // numero dichiarato "insufficiente" e chi legge crud dalla tabella lo prenderebbe. + const db = createLongevityDb(':memory:'); + db.prepare(`INSERT INTO soggetti (client_code, sesso) VALUES ('ISL-0001', 'F')`).run(); + db.prepare(`INSERT INTO sessioni (client_code, data, tipo) VALUES ('ISL-0001', '2026-08-21', 'checkup')`).run(); + expect(() => + db.prepare( + `INSERT INTO score (client_code, sessione_id, tipo, elemento, valore, copertura, stato, model_version) + VALUES ('ISL-0001', 1, 'asse', 'Forza & Struttura', 42, 0.2, 'insufficiente', 'v1.0')` + ).run() + ).toThrow(); + }); + + it('la tabella rifiuta uno score ok SENZA valore', () => { + const db = createLongevityDb(':memory:'); + db.prepare(`INSERT INTO soggetti (client_code, sesso) VALUES ('ISL-0001', 'F')`).run(); + db.prepare(`INSERT INTO sessioni (client_code, data, tipo) VALUES ('ISL-0001', '2026-08-21', 'checkup')`).run(); + expect(() => + db.prepare( + `INSERT INTO score (client_code, sessione_id, tipo, elemento, valore, copertura, stato, model_version) + VALUES ('ISL-0001', 1, 'asse', 'Forza & Struttura', NULL, 1, 'ok', 'v1.0')` + ).run() + ).toThrow(); + }); + it('identity tiene la persona, longevity non la conosce', () => { const id = createIdentityDb(':memory:'); const cols = (id.prepare(`PRAGMA table_info(clienti)`).all() as { name: string }[]).map((c) => c.name);